2020-03-01 11:42:36 +01:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2020-03-01 12:30:49 +01:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
type JiraIssue struct {
|
|
|
|
Key string `json:"key"`
|
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,
|
|
|
|
Url: "https://mattermost.atlassian.net/rest/api/2",
|
2020-03-01 11:42:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 12:30:49 +01:00
|
|
|
func (c *JiraClient) CreateTicket(epicId, team, summary, description string) (string, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := http.DoPost(c.Username, c.Token, c.Url+"/issue/", body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
respBody, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var issue JiraIssue
|
|
|
|
if err := json.Unmarshal(respBody, &issue); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return issue.Key, nil
|
2020-03-01 11:42:36 +01:00
|
|
|
}
|