2020-03-01 11:42:36 +01:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2020-03-05 20:42:46 +01:00
|
|
|
"bytes"
|
2020-03-05 22:57:01 +01:00
|
|
|
"encoding/json"
|
2020-03-05 20:42:25 +01:00
|
|
|
"fmt"
|
|
|
|
"text/template"
|
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
|
2020-03-05 20:42:25 +01:00
|
|
|
"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 22:37:01 +01:00
|
|
|
func NewClient(url, username, token string) (*JiraClient, error) {
|
|
|
|
tp := jira.BasicAuthTransport{
|
|
|
|
Username: username,
|
|
|
|
Password: token,
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := jira.NewClient(tp.Client(), url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &JiraClient{client}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JiraClient) GetIssueFromTicket(ticket *model.Ticket, cmp *model.Campaign) (*jira.Issue, error) {
|
|
|
|
summaryTmpl, err := template.New("").Parse(cmp.Summary)
|
2020-03-05 20:42:25 +01:00
|
|
|
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()
|
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
descriptionTemplate, err := template.ParseFiles(cmp.Template)
|
2020-03-05 20:42:25 +01:00
|
|
|
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,
|
2020-03-05 22:37:01 +01:00
|
|
|
"Project": cmp.Project,
|
|
|
|
"Issue Type": cmp.IssueType,
|
|
|
|
"Epic Link": cmp.Epic,
|
2020-03-05 20:42:25 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
createMetaInfo, _, err := c.Issue.GetCreateMeta(cmp.Project)
|
2020-03-05 20:42:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
project := createMetaInfo.GetProjectWithKey(cmp.Project)
|
2020-03-05 20:42:25 +01:00
|
|
|
if project == nil {
|
2020-03-05 22:37:01 +01:00
|
|
|
return nil, fmt.Errorf("Error retrieving project with key %s", cmp.Project)
|
2020-03-05 20:42:25 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
issueType := project.GetIssueTypeWithName(cmp.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 22:57:01 +01:00
|
|
|
func (c *JiraClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign, dryRun bool) (*jira.Issue, error) {
|
2020-03-05 22:37:01 +01:00
|
|
|
issue, err := c.GetIssueFromTicket(ticket, cmp)
|
2020-03-05 20:42:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:57:01 +01:00
|
|
|
if dryRun {
|
|
|
|
b, _ := json.MarshalIndent(issue, "", " ")
|
|
|
|
fmt.Println(string(b))
|
|
|
|
return issue, nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
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
|
|
|
}
|
2020-03-05 22:37:01 +01:00
|
|
|
|
2020-03-05 22:57:01 +01:00
|
|
|
func (c *JiraClient) PublishNextTicket(cmp *model.Campaign, dryRun bool) (bool, error) {
|
2020-03-05 22:37:01 +01:00
|
|
|
ticket := cmp.NextUnpublishedTicket()
|
|
|
|
if ticket == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:57:01 +01:00
|
|
|
issue, err := c.PublishTicket(ticket, cmp, dryRun)
|
2020-03-05 22:37:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:57:01 +01:00
|
|
|
if dryRun {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-03-06 19:54:52 +01:00
|
|
|
issue, _, err = c.Issue.Get(issue.Key, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
ticket.JiraLink = issue.Key
|
2020-03-06 19:54:52 +01:00
|
|
|
ticket.Summary = issue.Fields.Summary
|
2020-03-05 22:37:01 +01:00
|
|
|
if err := campaign.Save(cmp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:57:01 +01:00
|
|
|
func (c *JiraClient) PublishAll(cmp *model.Campaign, dryRun bool) (int, error) {
|
2020-03-05 22:37:01 +01:00
|
|
|
count := 0
|
|
|
|
for {
|
2020-03-05 22:57:01 +01:00
|
|
|
next, err := c.PublishNextTicket(cmp, dryRun)
|
2020-03-05 22:37:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
if !next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:57:01 +01:00
|
|
|
func (c *JiraClient) PublishBatch(cmp *model.Campaign, batch int, dryRun bool) error {
|
2020-03-05 22:42:49 +01:00
|
|
|
for i := 0; i <= batch; i++ {
|
2020-03-05 22:57:01 +01:00
|
|
|
next, err := c.PublishNextTicket(cmp, dryRun)
|
2020-03-05 22:42:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !next {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 22:37:01 +01:00
|
|
|
return nil
|
|
|
|
}
|