Add update jira issue with github link

This commit is contained in:
Miguel de la Cruz 2020-04-29 22:54:49 +02:00
parent 1ccd21f59c
commit 7829bbca13
4 changed files with 44 additions and 10 deletions

View file

@ -15,8 +15,8 @@ import (
type App struct {
Path string
jiraClient *jira.Client
githubClient *github.Client
JiraClient *jira.Client
GithubClient *github.Client
Campaign *model.Campaign
}

View file

@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"text/template"
"git.ctrlz.es/mgdelacroix/campaigner/model"
@ -19,7 +20,7 @@ func (a *App) InitGithubClient() error {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: a.Campaign.Github.Token})
tc := oauth2.NewClient(ctx, ts)
a.githubClient = github.NewClient(tc)
a.GithubClient = github.NewClient(tc)
return nil
}
@ -63,7 +64,7 @@ func (a *App) PublishInGithub(ticket *model.Ticket, dryRun bool) (*github.Issue,
}
owner, repo := a.Campaign.RepoComponents()
newIssue, _, err := a.githubClient.Issues.Create(context.Background(), owner, repo, issueRequest)
newIssue, _, err := a.GithubClient.Issues.Create(context.Background(), owner, repo, issueRequest)
if err != nil {
return nil, err
}
@ -93,6 +94,13 @@ func (a *App) PublishNextInGithub(dryRun bool) (bool, error) {
if err := a.Save(); err != nil {
return false, err
}
if !dryRun {
if err := a.UpdateJiraAfterGithub(ticket); err != nil {
fmt.Fprintf(os.Stderr, "error updating Jira info for %s after publishing in Github\n", ticket.JiraLink)
}
}
return true, nil
}

View file

@ -11,6 +11,8 @@ import (
jira "gopkg.in/andygrunwald/go-jira.v1"
)
const GithubIssueJiraField = "customfield_11106"
func (a *App) InitJiraClient() error {
tp := jira.BasicAuthTransport{
Username: a.Campaign.Jira.Username,
@ -22,10 +24,26 @@ func (a *App) InitJiraClient() error {
return err
}
a.jiraClient = client
a.JiraClient = client
return nil
}
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
}
func (a *App) GetJiraIssueFromTicket(ticket *model.Ticket) (*jira.Issue, error) {
summaryTmpl, err := template.New("").Parse(a.Campaign.Summary)
if err != nil {
@ -57,7 +75,7 @@ func (a *App) GetJiraIssueFromTicket(ticket *model.Ticket) (*jira.Issue, error)
"Epic Link": a.Campaign.Jira.Epic,
}
createMetaInfo, _, err := a.jiraClient.Issue.GetCreateMeta(a.Campaign.Jira.Project)
createMetaInfo, _, err := a.JiraClient.Issue.GetCreateMeta(a.Campaign.Jira.Project)
if err != nil {
return nil, err
}
@ -92,7 +110,7 @@ func (a *App) PublishInJira(ticket *model.Ticket, dryRun bool) (*jira.Issue, err
return issue, nil
}
newIssue, _, err := a.jiraClient.Issue.Create(issue)
newIssue, _, err := a.JiraClient.Issue.Create(issue)
if err != nil {
return nil, err
}
@ -101,7 +119,7 @@ func (a *App) PublishInJira(ticket *model.Ticket, dryRun bool) (*jira.Issue, err
}
func (a *App) GetIssue(issueNo string) (*jira.Issue, error) {
issue, _, err := a.jiraClient.Issue.Get(issueNo, nil)
issue, _, err := a.JiraClient.Issue.Get(issueNo, nil)
if err != nil {
return nil, err
}
@ -123,12 +141,12 @@ func (a *App) PublishNextInJira(dryRun bool) (bool, error) {
return true, nil
}
issue, _, err = a.jiraClient.Issue.Get(issue.Key, nil)
issue, _, err = a.JiraClient.Issue.Get(issue.Key, nil)
if err != nil {
return false, err
}
ticket.JiraLink = fmt.Sprintf("%s/browse/%s", a.Campaign.Jira.Url, issue.Key)
ticket.JiraLink = issue.Key
ticket.Summary = issue.Fields.Summary
ticket.Description = issue.Fields.Description
ticket.JiraStatus = issue.Fields.Status.Name

View file

@ -127,3 +127,11 @@ func (c *Campaign) RepoComponents() (string, string) {
}
return "", ""
}
func (c *Campaign) GetJiraUrl(ticket *Ticket) string {
return fmt.Sprintf("%s/browse/%s", c.Jira.Url, ticket.JiraLink)
}
func (c *Campaign) GetGithubUrl(ticket *Ticket) string {
return fmt.Sprintf("https://github.com/%s/issues/%d", c.Github.Repo, ticket.GithubLink)
}