campaigner/jira/jira.go

110 lines
2.4 KiB
Go
Raw Normal View History

package jira
import (
2020-03-05 20:42:46 +01:00
"bytes"
"fmt"
"text/template"
"git.ctrlz.es/mgdelacroix/campaigner/model"
jira "gopkg.in/andygrunwald/go-jira.v1"
)
type JiraClient struct {
*jira.Client
}
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,
}
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)
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) {
tp := jira.BasicAuthTransport{
Username: username,
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 {
return nil, err
2020-03-01 12:30:49 +01:00
}
return &JiraClient{client}, nil
2020-03-01 13:02:52 +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-01 12:30:49 +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
}