2020-03-01 11:42:36 +01:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2020-03-05 20:42:46 +01:00
|
|
|
"bytes"
|
2020-03-05 20:42:25 +01:00
|
|
|
"fmt"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
|
|
|
|
2020-03-04 22:13:55 +01:00
|
|
|
jira "gopkg.in/andygrunwald/go-jira.v1"
|
2020-03-01 11:42:36 +01:00
|
|
|
)
|
|
|
|
|
2020-03-04 22:13:55 +01:00
|
|
|
type JiraClient struct {
|
|
|
|
*jira.Client
|
2020-03-01 11:42:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
func (c *JiraClient) GetIssueFromTicket(ticket *model.Ticket, campaign *model.Campaign) (*jira.Issue, error) {
|
|
|
|
summaryTmpl, err := template.New("").Parse(campaign.Summary)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var summaryBytes bytes.Buffer
|
|
|
|
if err := summaryTmpl.Execute(&summaryBytes, ticket.Data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
summary := summaryBytes.String()
|
|
|
|
|
|
|
|
descriptionTemplate, err := template.ParseFiles(campaign.Template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var descriptionBytes bytes.Buffer
|
|
|
|
if err := descriptionTemplate.Execute(&descriptionBytes, ticket.Data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
description := descriptionBytes.String()
|
|
|
|
|
|
|
|
data := map[string]string{
|
|
|
|
"Description": description,
|
2020-03-05 20:42:46 +01:00
|
|
|
"Summary": summary,
|
|
|
|
"Project": campaign.Project,
|
2020-03-05 21:31:20 +01:00
|
|
|
"Issue Type": campaign.IssueType,
|
2020-03-05 20:42:46 +01:00
|
|
|
"Epic Link": campaign.Epic,
|
2020-03-05 20:42:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if team, ok := ticket.Data["team"]; ok {
|
|
|
|
data["team"] = team.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
createMetaInfo, _, err := c.Issue.GetCreateMeta(campaign.Project)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
project := createMetaInfo.GetProjectWithKey(campaign.Project)
|
|
|
|
if project == nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving project with key %s", campaign.Project)
|
|
|
|
}
|
|
|
|
|
2020-03-05 21:31:20 +01:00
|
|
|
issueType := project.GetIssueTypeWithName(campaign.IssueType)
|
2020-03-05 20:42:25 +01:00
|
|
|
if issueType == nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving issue type with name Story")
|
|
|
|
}
|
|
|
|
|
|
|
|
issue, err := jira.InitIssueWithMetaAndFields(project, issueType, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return issue, nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:38:53 +01:00
|
|
|
func NewClient(url, username, token string) (*JiraClient, error) {
|
2020-03-04 22:13:55 +01:00
|
|
|
tp := jira.BasicAuthTransport{
|
2020-03-01 11:42:36 +01:00
|
|
|
Username: username,
|
2020-03-04 22:13:55 +01:00
|
|
|
Password: token,
|
2020-03-01 12:30:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:38:53 +01:00
|
|
|
client, err := jira.NewClient(tp.Client(), url)
|
2020-03-01 12:30:49 +01:00
|
|
|
if err != nil {
|
2020-03-04 22:13:55 +01:00
|
|
|
return nil, err
|
2020-03-01 12:30:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 22:13:55 +01:00
|
|
|
return &JiraClient{client}, nil
|
2020-03-01 13:02:52 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
func (c *JiraClient) CreateTicket(ticket *model.Ticket, campaign *model.Campaign) (*jira.Issue, error) {
|
|
|
|
issue, err := c.GetIssueFromTicket(ticket, campaign)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newIssue, _, err := c.Issue.Create(issue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newIssue, nil
|
2020-03-04 22:13:55 +01:00
|
|
|
}
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-04 22:13:55 +01:00
|
|
|
func (c *JiraClient) GetIssue(issueNo string) (*jira.Issue, error) {
|
|
|
|
issue, _, err := c.Issue.Get(issueNo, nil)
|
2020-03-01 13:02:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return issue, nil
|
2020-03-01 11:42:36 +01:00
|
|
|
}
|