2020-03-01 11:42:36 +01:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2020-03-01 12:30:49 +01:00
|
|
|
"encoding/json"
|
2020-03-01 13:02:52 +01:00
|
|
|
"io"
|
2020-03-01 12:30:49 +01:00
|
|
|
|
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/http"
|
2020-03-01 11:42:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type JiraClient struct {
|
|
|
|
Username string
|
2020-03-01 12:30:49 +01:00
|
|
|
Token string
|
|
|
|
Url string
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:02:52 +01:00
|
|
|
type JiraIssueFieldsStatus struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type JiraIssueFields struct {
|
|
|
|
Status JiraIssueFieldsStatus `json:"status"`
|
|
|
|
}
|
|
|
|
|
2020-03-01 12:30:49 +01:00
|
|
|
type JiraIssue struct {
|
2020-03-01 13:03:47 +01:00
|
|
|
Key string `json:"key"`
|
2020-03-01 13:02:52 +01:00
|
|
|
Fields JiraIssueFields `json:"fields"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func IssueFromJson(body io.Reader) (*JiraIssue, error) {
|
|
|
|
var issue JiraIssue
|
|
|
|
if err := json.NewDecoder(body).Decode(&issue); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &issue, nil
|
2020-03-01 11:42:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(username, token string) *JiraClient {
|
|
|
|
return &JiraClient{
|
|
|
|
Username: username,
|
2020-03-01 12:30:49 +01:00
|
|
|
Token: token,
|
2020-03-01 13:02:52 +01:00
|
|
|
Url: "https://mattermost.atlassian.net/rest/api/2/",
|
2020-03-01 11:42:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:02:52 +01:00
|
|
|
func (c *JiraClient) CreateIssue(epicId, team, summary, description string) (string, error) {
|
2020-03-01 12:30:49 +01:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"fields": map[string]interface{}{
|
|
|
|
"project": map[string]interface{}{"key": "MM"},
|
|
|
|
"summary": summary,
|
|
|
|
"description": description,
|
|
|
|
"issuetype": map[string]interface{}{"name": "Story"},
|
|
|
|
"customfield_10007": epicId,
|
|
|
|
"customfield_11101": map[string]interface{}{"value": team},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:02:52 +01:00
|
|
|
res, err := http.DoPost(c.Username, c.Token, c.Url+"issue/", body)
|
2020-03-01 12:30:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2020-03-01 13:02:52 +01:00
|
|
|
issue, err := IssueFromJson(res.Body)
|
2020-03-01 12:30:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:02:52 +01:00
|
|
|
return issue.Key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JiraClient) GetIssue(issueNo string) (*JiraIssue, error) {
|
|
|
|
res, err := http.DoGet(c.Username, c.Token, c.Url+"issue/"+issueNo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-03-01 12:30:49 +01:00
|
|
|
}
|
2020-03-01 13:02:52 +01:00
|
|
|
defer res.Body.Close()
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-01 13:02:52 +01:00
|
|
|
issue, err := IssueFromJson(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return issue, nil
|
2020-03-01 11:42:36 +01:00
|
|
|
}
|