campaigner/app/jira.go

168 lines
3.5 KiB
Go
Raw Normal View History

2020-04-29 19:52:15 +02:00
package app
import (
2020-03-05 20:42:46 +01:00
"bytes"
"encoding/json"
"fmt"
"text/template"
"git.ctrlz.es/mgdelacroix/campaigner/model"
jira "gopkg.in/andygrunwald/go-jira.v1"
)
2020-04-29 19:52:15 +02:00
func (a *App) InitJiraClient() error {
2020-03-05 22:37:01 +01:00
tp := jira.BasicAuthTransport{
2020-04-29 19:52:15 +02:00
Username: a.Campaign.Jira.Username,
Password: a.Campaign.Jira.Token,
2020-03-05 22:37:01 +01:00
}
2020-04-29 19:52:15 +02:00
client, err := jira.NewClient(tp.Client(), a.Campaign.Jira.Url)
2020-03-05 22:37:01 +01:00
if err != nil {
2020-04-29 19:52:15 +02:00
return err
2020-03-05 22:37:01 +01:00
}
2020-04-29 19:52:15 +02:00
a.jiraClient = client
return nil
2020-03-05 22:37:01 +01:00
}
2020-04-29 19:52:15 +02:00
func (a *App) GetJiraIssueFromTicket(ticket *model.Ticket) (*jira.Issue, error) {
summaryTmpl, err := template.New("").Parse(a.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()
2020-04-29 19:52:15 +02:00
descriptionTemplate, err := template.ParseFiles(a.Campaign.IssueTemplate)
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-04-29 19:52:15 +02:00
"Project": a.Campaign.Jira.Project,
"Issue Type": a.Campaign.Jira.IssueType,
"Epic Link": a.Campaign.Jira.Epic,
}
2020-04-29 19:52:15 +02:00
createMetaInfo, _, err := a.jiraClient.Issue.GetCreateMeta(a.Campaign.Jira.Project)
if err != nil {
return nil, err
}
2020-04-29 19:52:15 +02:00
project := createMetaInfo.GetProjectWithKey(a.Campaign.Jira.Project)
if project == nil {
2020-04-29 19:52:15 +02:00
return nil, fmt.Errorf("Error retrieving project with key %s", a.Campaign.Jira.Project)
}
2020-04-29 19:52:15 +02:00
issueType := project.GetIssueTypeWithName(a.Campaign.Jira.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-04-29 19:52:15 +02:00
func (a *App) PublishInJira(ticket *model.Ticket, dryRun bool) (*jira.Issue, error) {
issue, err := a.GetJiraIssueFromTicket(ticket)
if err != nil {
return nil, err
}
if dryRun {
b, _ := json.MarshalIndent(issue, "", " ")
fmt.Println(string(b))
return issue, nil
}
2020-04-29 19:52:15 +02:00
newIssue, _, err := a.jiraClient.Issue.Create(issue)
if err != nil {
return nil, err
}
return newIssue, nil
}
2020-03-01 12:30:49 +01:00
2020-04-29 19:52:15 +02:00
func (a *App) GetIssue(issueNo string) (*jira.Issue, error) {
issue, _, err := a.jiraClient.Issue.Get(issueNo, nil)
2020-03-01 13:02:52 +01:00
if err != nil {
return nil, err
}
return issue, nil
}
2020-03-05 22:37:01 +01:00
2020-04-29 19:52:15 +02:00
func (a *App) PublishNextInJira(dryRun bool) (bool, error) {
ticket := a.Campaign.NextJiraUnpublishedTicket()
2020-03-05 22:37:01 +01:00
if ticket == nil {
return false, nil
}
2020-04-29 19:52:15 +02:00
issue, err := a.PublishInJira(ticket, dryRun)
2020-03-05 22:37:01 +01:00
if err != nil {
return false, err
}
if dryRun {
return true, nil
}
2020-04-29 19:52:15 +02:00
issue, _, err = a.jiraClient.Issue.Get(issue.Key, nil)
2020-03-06 19:54:52 +01:00
if err != nil {
return false, err
}
2020-04-29 19:52:15 +02:00
ticket.JiraLink = fmt.Sprintf("%s/browse/%s", a.Campaign.Jira.Url, issue.Key)
2020-03-06 19:54:52 +01:00
ticket.Summary = issue.Fields.Summary
ticket.Description = issue.Fields.Description
2020-04-28 15:18:34 +02:00
ticket.JiraStatus = issue.Fields.Status.Name
2020-04-29 19:52:15 +02:00
if err := a.Save(); err != nil {
2020-03-05 22:37:01 +01:00
return false, err
}
return true, nil
}
2020-04-29 19:52:15 +02:00
func (a *App) PublishAllInJira(dryRun bool) (int, error) {
2020-03-05 22:37:01 +01:00
count := 0
for {
2020-04-29 19:52:15 +02:00
next, err := a.PublishNextInJira(dryRun)
2020-03-05 22:37:01 +01:00
if err != nil {
return count, err
}
if !next {
break
}
count++
}
return count, nil
}
2020-04-29 19:52:15 +02:00
func (a *App) PublishBatchInJira(batch int, dryRun bool) error {
for i := 1; i <= batch; i++ {
2020-04-29 19:52:15 +02:00
next, err := a.PublishNextInJira(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
}