From 9dd34b5a1bc2be8ed936a0dfc54f62008c7ca2c5 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Tue, 28 Apr 2020 11:21:31 +0200 Subject: [PATCH] Allow to add a footer to the github issues --- cmd/init.go | 15 ++++++++++----- github/github.go | 24 ++++++++++++++++++++++++ jira/jira.go | 4 ++-- model/campaign.go | 13 +++++++------ 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 08a92a8..02e9121 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -27,7 +27,9 @@ func InitCmd() *cobra.Command { --repository johndoe/awesomeproject \ -l 'Area/API' -l 'Tech/Go' \ --summary 'Refactor {{.function}} to inject the configuration service' \ - --template ./refactor-config.tmpl`, + --issue-template ./refactor-config.tmpl \ + --footer-template ./github-footer.tmpl +`, Args: cobra.NoArgs, Run: initCmdF, } @@ -40,7 +42,8 @@ func InitCmd() *cobra.Command { cmd.Flags().StringP("repository", "r", "", "the github repository") cmd.Flags().StringSliceP("label", "l", []string{}, "the labels to add to the Github issues") cmd.Flags().StringP("summary", "s", "", "the summary of the tickets") - cmd.Flags().StringP("template", "t", "", "the template path for the description of the tickets") + cmd.Flags().StringP("issue-template", "t", "", "the template path for the description of the tickets") + cmd.Flags().StringP("footer-template", "f", "", "the template path to append to the github issues as a footer") cmd.Flags().StringP("issue-type", "i", "Story", "the issue type to create the tickets as") return cmd @@ -68,7 +71,8 @@ func initCmdF(cmd *cobra.Command, _ []string) { epic := getStringFlagOrAskIfEmpty("epic", "JIRA epic:") repo := getStringFlagOrAskIfEmpty("repository", "GitHub repository:") summary := getStringFlagOrAskIfEmpty("summary", "Ticket summary template:") - template := getStringFlagOrAskIfEmpty("template", "Ticket description template path:") + issueTemplate := getStringFlagOrAskIfEmpty("issue-template", "Ticket description template path:") + footerTemplate := getStringFlagOrAskIfEmpty("footer-template", "Github issue footer template path:") issueType, _ := cmd.Flags().GetString("issue-type") labels, _ := cmd.Flags().GetStringSlice("label") @@ -88,8 +92,9 @@ func initCmdF(cmd *cobra.Command, _ []string) { Repo: repo, Labels: labels, }, - Summary: summary, - Template: template, + Summary: summary, + IssueTemplate: issueTemplate, + FooterTemplate: footerTemplate, } if err := campaign.Save(cmp); err != nil { ErrorAndExit(cmd, err) diff --git a/github/github.go b/github/github.go index 4686fb0..7dceedd 100644 --- a/github/github.go +++ b/github/github.go @@ -1,9 +1,11 @@ package github import ( + "bytes" "context" "encoding/json" "fmt" + "text/template" "git.ctrlz.es/mgdelacroix/campaigner/campaign" "git.ctrlz.es/mgdelacroix/campaigner/model" @@ -30,8 +32,30 @@ func NewClient(repo, token string) *GithubClient { } } +func getFooterTemplate(ticket *model.Ticket, templatePath string) (string, error) { + footerTmpl, err := template.ParseFiles(templatePath) + if err != nil { + return "", err + } + + var footerBytes bytes.Buffer + if err := footerTmpl.Execute(&footerBytes, ticket); err != nil { + return "", err + } + return footerBytes.String(), nil +} + func (c *GithubClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign, dryRun bool) (*github.Issue, error) { mdDescription := j2m.JiraToMD(ticket.Description) + if cmp.FooterTemplate != "" { + footer, err := getFooterTemplate(ticket, cmp.FooterTemplate) + if err != nil { + return nil, err + } + + mdDescription += "\n" + footer + } + issueRequest := &github.IssueRequest{ Title: &ticket.Summary, Body: &mdDescription, diff --git a/jira/jira.go b/jira/jira.go index 56ec71a..2ec9135 100644 --- a/jira/jira.go +++ b/jira/jira.go @@ -42,7 +42,7 @@ func (c *JiraClient) GetIssueFromTicket(ticket *model.Ticket, cmp *model.Campaig } summary := summaryBytes.String() - descriptionTemplate, err := template.ParseFiles(cmp.Template) + descriptionTemplate, err := template.ParseFiles(cmp.IssueTemplate) if err != nil { return nil, err } @@ -132,7 +132,7 @@ func (c *JiraClient) PublishNextTicket(cmp *model.Campaign, dryRun bool) (bool, return false, err } - ticket.JiraLink = issue.Key + ticket.JiraLink = fmt.Sprintf("%s/browse/%s", cmp.Jira.Url, issue.Key) ticket.Summary = issue.Fields.Summary ticket.Description = issue.Fields.Description // ToDo: sync JiraStatus diff --git a/model/campaign.go b/model/campaign.go index a4b1758..571620a 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -26,11 +26,12 @@ type ConfigGithub struct { // ToDo: add key-value extra params as a map to allow for customfield_whatever = team type Campaign struct { - Jira ConfigJira `json:"jira"` - Github ConfigGithub `json:"github"` - Summary string `json:"summary"` - Template string `json:"template"` - Tickets []*Ticket `json:"tickets,omitempty"` + Jira ConfigJira `json:"jira"` + Github ConfigGithub `json:"github"` + Summary string `json:"summary"` + IssueTemplate string `json:"issue_template"` + FooterTemplate string `json:"footer_template"` + Tickets []*Ticket `json:"tickets,omitempty"` } func (c *Campaign) NextJiraUnpublishedTicket() *Ticket { @@ -81,7 +82,7 @@ func (c *Campaign) FillTicket(t *Ticket) error { } t.Summary = summaryBytes.String() - descriptionTemplate, err := template.ParseFiles(c.Template) + descriptionTemplate, err := template.ParseFiles(c.IssueTemplate) if err != nil { return err }