2018-08-06 19:39:31 +01:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2018-08-07 19:15:25 +01:00
|
|
|
"bytes"
|
2018-09-11 14:15:39 +01:00
|
|
|
"crypto/tls"
|
2018-08-06 19:39:31 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-08-07 19:15:25 +01:00
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
2018-08-06 19:39:31 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/documize/community/core/env"
|
|
|
|
"github.com/documize/community/domain/section/provider"
|
2018-09-27 15:14:48 +01:00
|
|
|
"github.com/documize/community/domain/store"
|
2018-08-06 19:39:31 +01:00
|
|
|
jira "gopkg.in/andygrunwald/go-jira.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
const (
|
|
|
|
logID = "jira"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provider represents Gemini
|
|
|
|
type Provider struct {
|
|
|
|
Runtime *env.Runtime
|
2018-09-27 15:14:48 +01:00
|
|
|
Store *store.Store
|
2018-08-06 19:39:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Meta describes us.
|
|
|
|
func (*Provider) Meta() provider.TypeMeta {
|
|
|
|
section := provider.TypeMeta{}
|
|
|
|
section.ID = "dca48000-8a60-438c-b6d1-e4160f3ac8e3"
|
|
|
|
section.Title = "Jira"
|
|
|
|
section.Description = "Issue tracking"
|
|
|
|
section.ContentType = "jira"
|
|
|
|
section.PageType = "tab"
|
|
|
|
|
|
|
|
return section
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render converts Jira data into HTML suitable for browser rendering.
|
2018-08-07 19:43:25 +01:00
|
|
|
func (p *Provider) Render(ctx *provider.Context, config, data string) string {
|
|
|
|
var c = jiraConfig{}
|
|
|
|
err := json.Unmarshal([]byte(config), &c)
|
|
|
|
if err != nil {
|
|
|
|
p.Runtime.Log.Error("Unable to read Jira config", err)
|
|
|
|
return ""
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
creds, err := getCredentials(ctx, p.Store)
|
|
|
|
if err != nil {
|
|
|
|
p.Runtime.Log.Error("unable to fetch Jira connector configuration", err)
|
|
|
|
return ""
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
client, _, err := authenticate(creds)
|
|
|
|
if err != nil {
|
|
|
|
p.Runtime.Log.Error("unable to authenticate with Jira", err)
|
|
|
|
return ""
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
issues, err := getIssues(c, client)
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-08 12:18:04 +01:00
|
|
|
return generateGrid(creds.URL, issues)
|
2018-08-07 19:43:25 +01:00
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
// Refresh fetches latest issues list.
|
|
|
|
func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData string) {
|
|
|
|
var c = jiraConfig{}
|
|
|
|
err := json.Unmarshal([]byte(config), &c)
|
|
|
|
if err != nil {
|
|
|
|
p.Runtime.Log.Error("Unable to read Jira config", err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
creds, err := getCredentials(ctx, p.Store)
|
|
|
|
if err != nil {
|
|
|
|
p.Runtime.Log.Error("unable to fetch Jira connector configuration", err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
client, _, err := authenticate(creds)
|
|
|
|
if err != nil {
|
|
|
|
p.Runtime.Log.Error("unable to authenticate with Jira", err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
issues, err := getIssues(c, client)
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
j, err := json.Marshal(issues)
|
|
|
|
if err != nil {
|
|
|
|
p.Runtime.Log.Error("unable to marshal Jira items", err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
newData = string(j)
|
2018-08-06 19:39:31 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command handles authentication and issues list preview.
|
|
|
|
func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
|
|
|
method := query.Get("method")
|
|
|
|
|
|
|
|
if len(method) == 0 {
|
|
|
|
provider.WriteMessage(w, logID, "missing method name")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch method {
|
2018-08-07 19:43:25 +01:00
|
|
|
case "previewIssues":
|
|
|
|
previewIssues(ctx, p.Store, w, r)
|
|
|
|
case "previewGrid":
|
|
|
|
previewGrid(ctx, p.Store, w, r)
|
2018-08-06 19:39:31 +01:00
|
|
|
case "auth":
|
|
|
|
auth(ctx, p.Store, w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:14:48 +01:00
|
|
|
func auth(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) {
|
2018-08-07 19:15:25 +01:00
|
|
|
creds, err := getCredentials(ctx, store)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
|
|
|
provider.WriteForbidden(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
// Authenticate
|
|
|
|
_, _, err = authenticate(creds)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
2018-08-07 19:15:25 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
provider.WriteError(w, logID, err)
|
2018-08-06 19:39:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
provider.WriteJSON(w, "OK")
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-09-27 15:14:48 +01:00
|
|
|
func previewIssues(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) {
|
2018-08-07 19:43:25 +01:00
|
|
|
creds, err := getCredentials(ctx, store)
|
|
|
|
if err != nil {
|
|
|
|
provider.WriteForbidden(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client, _, err := authenticate(creds)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
provider.WriteError(w, logID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := readConfig(ctx, store, w, r)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
provider.WriteError(w, logID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
issues, err := getIssues(config, client)
|
|
|
|
|
|
|
|
provider.WriteJSON(w, issues)
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:14:48 +01:00
|
|
|
func previewGrid(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) {
|
2018-08-07 19:15:25 +01:00
|
|
|
creds, err := getCredentials(ctx, store)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
2018-08-07 19:15:25 +01:00
|
|
|
provider.WriteForbidden(w)
|
2018-08-06 19:39:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
client, _, err := authenticate(creds)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
provider.WriteError(w, logID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
config, err := readConfig(ctx, store, w, r)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
2018-08-07 19:15:25 +01:00
|
|
|
fmt.Println(err)
|
2018-08-06 19:39:31 +01:00
|
|
|
provider.WriteError(w, logID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
issues, err := getIssues(config, client)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "html; charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-08-08 12:18:04 +01:00
|
|
|
w.Write([]byte(generateGrid(creds.URL, issues)))
|
2018-08-06 19:39:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
// Pull config from HTTP request.
|
2018-09-27 15:14:48 +01:00
|
|
|
func readConfig(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) (config jiraConfig, err error) {
|
2018-08-06 19:39:31 +01:00
|
|
|
defer r.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &config)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
// Get Jira connector configuration.
|
2018-09-27 15:14:48 +01:00
|
|
|
func getCredentials(ctx *provider.Context, store *store.Store) (login jiraLogin, err error) {
|
2018-08-07 19:15:25 +01:00
|
|
|
creds, err := store.Setting.GetUser(ctx.OrgID, "", "jira", "")
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
err = json.Unmarshal([]byte(creds), &login)
|
|
|
|
if err != nil {
|
|
|
|
return login, err
|
2018-08-06 19:39:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
// Perform Jira login.
|
|
|
|
func authenticate(login jiraLogin) (c *jira.Client, u *jira.User, err error) {
|
2018-09-11 14:15:39 +01:00
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
// client := &http.Client{Transport: tr}
|
|
|
|
|
|
|
|
tp := jira.BasicAuthTransport{Username: login.Username, Password: login.Secret, Transport: tr}
|
2018-08-07 19:15:25 +01:00
|
|
|
c, err = jira.NewClient(tp.Client(), login.URL)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
2018-09-24 18:53:01 +01:00
|
|
|
fmt.Println("Cannot authenticate with Jira:", err)
|
2018-08-06 19:39:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
u, _, err = c.User.Get(login.Username)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
2018-09-24 18:53:01 +01:00
|
|
|
fmt.Println("Cannot get authenticated Jira user:", err)
|
2018-08-06 19:39:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch Jira issues using configuration criteria.
|
|
|
|
func getIssues(config jiraConfig, client *jira.Client) (issues []jira.Issue, err error) {
|
|
|
|
opts := &jira.SearchOptions{Expand: "", MaxResults: 500, StartAt: 0}
|
|
|
|
issues, _, err = client.Issue.Search(config.JQL, opts)
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
// Generate issues grid
|
2018-08-08 12:18:04 +01:00
|
|
|
func generateGrid(jiraURL string, issues []jira.Issue) string {
|
2018-08-07 19:15:25 +01:00
|
|
|
t := template.New("issues")
|
|
|
|
t, _ = t.Parse(renderTemplate)
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
payload := jiraGrid{}
|
|
|
|
payload.ItemCount = len(issues)
|
|
|
|
payload.Issues = issues
|
2018-08-08 12:18:04 +01:00
|
|
|
payload.JiraURL = jiraURL
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
buffer := new(bytes.Buffer)
|
2018-08-07 19:43:25 +01:00
|
|
|
err := t.Execute(buffer, payload)
|
2018-08-07 19:15:25 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Jira render error", err)
|
|
|
|
}
|
2018-08-06 19:39:31 +01:00
|
|
|
|
2018-08-07 19:43:25 +01:00
|
|
|
return buffer.String()
|
2018-08-06 19:39:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type jiraConfig struct {
|
2018-08-07 19:43:25 +01:00
|
|
|
JQL string `json:"jql"`
|
|
|
|
ItemCount int `json:"itemCount"`
|
2018-08-06 19:39:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type jiraLogin struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
}
|
2018-08-07 19:15:25 +01:00
|
|
|
|
|
|
|
type jiraGrid struct {
|
2018-08-08 12:18:04 +01:00
|
|
|
JiraURL string `json:"url"`
|
2018-08-07 19:15:25 +01:00
|
|
|
Issues []jira.Issue `json:"issues"`
|
|
|
|
ItemCount int `json:"itemCount"`
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:18:04 +01:00
|
|
|
type jiraFilter struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
JQL string `json:"jql"`
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:25 +01:00
|
|
|
// the HTML that is rendered by this section.
|
|
|
|
const renderTemplate = `
|
2018-08-07 19:43:25 +01:00
|
|
|
<p>{{.ItemCount}} items</p>
|
2018-08-07 19:15:25 +01:00
|
|
|
<table class="basic-table section-jira-table">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th class="bordered no-width">Key</th>
|
|
|
|
<th class="bordered no-width">T</th>
|
|
|
|
<th class="bordered no-width">Status</th>
|
|
|
|
<th class="bordered no-width">P</th>
|
2018-08-08 12:18:04 +01:00
|
|
|
<th class="bordered no-width">Component/s</th>
|
2018-08-07 19:15:25 +01:00
|
|
|
<th class="bordered">Summary</th>
|
|
|
|
<th class="bordered no-width">Assignee</th>
|
|
|
|
<th class="bordered no-width">Fix Version/s</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2018-08-08 12:18:04 +01:00
|
|
|
{{$app := .JiraURL}}
|
|
|
|
{{range $item := .Issues}}
|
2018-08-07 19:15:25 +01:00
|
|
|
<tr>
|
2018-09-24 18:53:01 +01:00
|
|
|
<td class="bordered no-width"><a href="{{ $app }}/browse/{{ $item.Key }}">{{ $item.Key }} </a></td>
|
2018-08-07 19:15:25 +01:00
|
|
|
<td class="bordered no-width"><img class="section-jira-icon" src='{{ $item.Fields.Type.IconURL }}' /></td>
|
2018-09-24 18:53:01 +01:00
|
|
|
<td class="bordered no-width"><span class="badge badge-warning">{{ $item.Fields.Status.Name }}</span> </td>
|
2018-08-07 19:15:25 +01:00
|
|
|
<td class="bordered no-width"><img class="section-jira-icon" src='{{ $item.Fields.Priority.IconURL }}' /></td>
|
2018-08-08 12:18:04 +01:00
|
|
|
<td class="bordered no-width">
|
|
|
|
{{range $comp := $item.Fields.Components}}
|
|
|
|
{{ $comp.Name }}
|
|
|
|
{{end}}
|
2018-09-24 18:53:01 +01:00
|
|
|
|
|
|
|
</td>
|
|
|
|
<td class="bordered no-width">{{ $item.Fields.Summary }} </td>
|
|
|
|
<td class="bordered no-width">
|
|
|
|
{{if $item.Fields.Assignee}}
|
|
|
|
{{$item.Fields.Assignee.DisplayName}}
|
|
|
|
{{end}}
|
|
|
|
|
2018-08-08 12:18:04 +01:00
|
|
|
</td>
|
|
|
|
<td class="bordered no-width">
|
|
|
|
{{range $ver := $item.Fields.FixVersions}}
|
|
|
|
{{ $ver.Name }}
|
|
|
|
{{end}}
|
2018-09-24 18:53:01 +01:00
|
|
|
|
2018-08-08 12:18:04 +01:00
|
|
|
</td>
|
2018-08-07 19:15:25 +01:00
|
|
|
</tr>
|
|
|
|
{{end}}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
`
|