2020-04-29 18:52:15 +01:00
|
|
|
package app
|
2020-03-01 11:42:36 +01:00
|
|
|
|
|
|
|
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"
|
2020-10-03 13:40:09 +01:00
|
|
|
"io"
|
|
|
|
"strings"
|
2020-03-05 20:42:25 +01:00
|
|
|
"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-04-29 21:54:49 +01:00
|
|
|
const GithubIssueJiraField = "customfield_11106"
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
func (a *App) InitJiraClient() error {
|
2020-03-05 22:37:01 +01:00
|
|
|
tp := jira.BasicAuthTransport{
|
2020-04-29 18:52:15 +01:00
|
|
|
Username: a.Campaign.Jira.Username,
|
|
|
|
Password: a.Campaign.Jira.Token,
|
2020-03-05 22:37:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01: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 18:52:15 +01:00
|
|
|
return err
|
2020-03-05 22:37:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 21:54:49 +01:00
|
|
|
a.JiraClient = client
|
2020-04-29 18:52:15 +01:00
|
|
|
return nil
|
2020-03-05 22:37:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 21:54:49 +01:00
|
|
|
func (a *App) UpdateJiraAfterGithub(ticket *model.Ticket) error {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"fields": map[string]interface{}{
|
|
|
|
GithubIssueJiraField: a.Campaign.GetGithubUrl(ticket),
|
|
|
|
"fixVersions": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"name": "Help Wanted",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := a.JiraClient.Issue.UpdateIssue(ticket.JiraLink, data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
func (a *App) GetJiraIssueFromTicket(ticket *model.Ticket) (*jira.Issue, error) {
|
|
|
|
summaryTmpl, err := template.New("").Parse(a.Campaign.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-04-29 18:52:15 +01:00
|
|
|
descriptionTemplate, err := template.ParseFiles(a.Campaign.IssueTemplate)
|
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-04-29 18:52:15 +01:00
|
|
|
"Project": a.Campaign.Jira.Project,
|
|
|
|
"Issue Type": a.Campaign.Jira.IssueType,
|
|
|
|
"Epic Link": a.Campaign.Jira.Epic,
|
2020-03-05 20:42:25 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 21:54:49 +01:00
|
|
|
createMetaInfo, _, err := a.JiraClient.Issue.GetCreateMeta(a.Campaign.Jira.Project)
|
2020-03-05 20:42:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
project := createMetaInfo.GetProjectWithKey(a.Campaign.Jira.Project)
|
2020-03-05 20:42:25 +01:00
|
|
|
if project == nil {
|
2020-04-29 18:52:15 +01:00
|
|
|
return nil, fmt.Errorf("Error retrieving project with key %s", a.Campaign.Jira.Project)
|
2020-03-05 20:42:25 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
issueType := project.GetIssueTypeWithName(a.Campaign.Jira.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-04-29 18:52:15 +01:00
|
|
|
func (a *App) PublishInJira(ticket *model.Ticket, dryRun bool) (*jira.Issue, error) {
|
|
|
|
issue, err := a.GetJiraIssueFromTicket(ticket)
|
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-04-29 21:54:49 +01:00
|
|
|
newIssue, _, err := a.JiraClient.Issue.Create(issue)
|
2020-03-05 20:42:25 +01:00
|
|
|
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-04-29 18:52:15 +01:00
|
|
|
func (a *App) GetIssue(issueNo string) (*jira.Issue, error) {
|
2020-04-29 21:54:49 +01:00
|
|
|
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-01 11:42:36 +01:00
|
|
|
}
|
2020-03-05 22:37:01 +01:00
|
|
|
|
2020-10-03 13:40:09 +01:00
|
|
|
func (a *App) PublishNextInJira(w io.Writer, dryRun bool) (bool, error) {
|
2020-04-29 18:52:15 +01:00
|
|
|
ticket := a.Campaign.NextJiraUnpublishedTicket()
|
2020-03-05 22:37:01 +01:00
|
|
|
if ticket == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
issue, err := a.PublishInJira(ticket, 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-04-29 21:54:49 +01: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 21:54:49 +01:00
|
|
|
ticket.JiraLink = issue.Key
|
2020-03-06 19:54:52 +01:00
|
|
|
ticket.Summary = issue.Fields.Summary
|
2020-04-27 10:42:29 +01:00
|
|
|
ticket.Description = issue.Fields.Description
|
2020-04-28 14:18:34 +01:00
|
|
|
ticket.JiraStatus = issue.Fields.Status.Name
|
2020-04-29 18:52:15 +01:00
|
|
|
if err := a.Save(); err != nil {
|
2020-03-05 22:37:01 +01:00
|
|
|
return false, err
|
|
|
|
}
|
2020-09-24 13:02:36 +01:00
|
|
|
|
2020-10-03 13:40:09 +01:00
|
|
|
fmt.Fprintf(w, "Ticket published: %s/browse/%s\n", strings.TrimSuffix(a.Campaign.Jira.Url, "/"), ticket.JiraLink)
|
2020-09-24 13:02:36 +01:00
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:40:09 +01:00
|
|
|
func (a *App) PublishAllInJira(w io.Writer, dryRun bool) (int, error) {
|
2020-03-05 22:37:01 +01:00
|
|
|
count := 0
|
|
|
|
for {
|
2020-10-03 13:40:09 +01:00
|
|
|
next, err := a.PublishNextInJira(w, dryRun)
|
2020-03-05 22:37:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
if !next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:40:09 +01:00
|
|
|
func (a *App) PublishBatchInJira(w io.Writer, batch int, dryRun bool) error {
|
2020-04-27 10:42:29 +01:00
|
|
|
for i := 1; i <= batch; i++ {
|
2020-10-03 13:40:09 +01:00
|
|
|
next, err := a.PublishNextInJira(w, 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
|
|
|
|
}
|
2020-04-30 07:13:31 +01:00
|
|
|
|
|
|
|
func (a *App) GetTicketsFromJiraEpic() ([]*model.Ticket, error) {
|
|
|
|
jql := fmt.Sprintf("project = %s AND type = %s AND \"Epic Link\" = %s", a.Campaign.Jira.Project, a.Campaign.Jira.IssueType, a.Campaign.Jira.Epic)
|
|
|
|
|
|
|
|
page := 0
|
|
|
|
maxPerPage := 50
|
|
|
|
issues := []jira.Issue{}
|
|
|
|
for {
|
|
|
|
opts := &jira.SearchOptions{StartAt: maxPerPage * page, MaxResults: maxPerPage}
|
|
|
|
pageIssues, _, err := a.JiraClient.Issue.Search(jql, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
issues = append(issues, pageIssues...)
|
|
|
|
if len(pageIssues) < maxPerPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
page++
|
|
|
|
}
|
|
|
|
|
|
|
|
tickets := []*model.Ticket{}
|
|
|
|
for _, issue := range issues {
|
|
|
|
// ToDo: if they have github link, fill and fetch github data
|
|
|
|
ticket := &model.Ticket{
|
|
|
|
JiraLink: issue.Key,
|
|
|
|
JiraStatus: issue.Fields.Status.Name,
|
|
|
|
Summary: issue.Fields.Summary,
|
|
|
|
Description: issue.Fields.Description,
|
|
|
|
}
|
|
|
|
tickets = append(tickets, ticket)
|
|
|
|
}
|
|
|
|
return tickets, nil
|
|
|
|
}
|