campaigner/app/github.go

131 lines
2.7 KiB
Go
Raw Normal View History

2020-04-29 19:52:15 +02:00
package app
import (
"bytes"
"context"
"encoding/json"
"fmt"
2020-04-29 22:54:49 +02:00
"os"
"text/template"
"git.ctrlz.es/mgdelacroix/campaigner/model"
"github.com/StevenACoffman/j2m"
"github.com/google/go-github/v29/github"
"golang.org/x/oauth2"
)
2020-04-29 19:52:15 +02:00
func (a *App) InitGithubClient() error {
ctx := context.Background()
2020-04-29 19:52:15 +02:00
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: a.Campaign.Github.Token})
tc := oauth2.NewClient(ctx, ts)
2020-04-29 22:54:49 +02:00
a.GithubClient = github.NewClient(tc)
2020-04-29 19:52:15 +02:00
return nil
}
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
}
2020-04-29 19:52:15 +02:00
func (a *App) PublishInGithub(ticket *model.Ticket, dryRun bool) (*github.Issue, error) {
mdDescription := j2m.JiraToMD(ticket.Description)
2020-04-29 19:52:15 +02:00
if a.Campaign.FooterTemplate != "" {
footer, err := getFooterTemplate(ticket, a.Campaign.FooterTemplate)
if err != nil {
return nil, err
}
mdDescription += "\n" + footer
}
issueRequest := &github.IssueRequest{
Title: &ticket.Summary,
Body: &mdDescription,
2020-04-29 19:52:15 +02:00
Labels: &a.Campaign.Github.Labels,
}
if dryRun {
b, _ := json.MarshalIndent(issueRequest, "", " ")
fmt.Println(string(b))
return &github.Issue{
Title: issueRequest.Title,
Body: issueRequest.Body,
}, nil
}
2020-04-29 19:52:15 +02:00
owner, repo := a.Campaign.RepoComponents()
2020-04-29 22:54:49 +02:00
newIssue, _, err := a.GithubClient.Issues.Create(context.Background(), owner, repo, issueRequest)
if err != nil {
return nil, err
}
return newIssue, nil
}
2020-04-29 19:52:15 +02:00
func (a *App) PublishNextInGithub(dryRun bool) (bool, error) {
ticket := a.Campaign.NextGithubUnpublishedTicket()
if ticket == nil {
return false, nil
}
2020-04-29 19:52:15 +02:00
issue, err := a.PublishInGithub(ticket, dryRun)
if err != nil {
return false, err
}
if dryRun {
return true, nil
}
2020-04-27 12:22:15 +02:00
ticket.GithubLink = issue.GetNumber()
2020-04-27 17:47:13 +02:00
ticket.GithubStatus = issue.GetState()
2020-04-29 19:52:15 +02:00
if err := a.Save(); err != nil {
return false, err
}
2020-04-29 22:54:49 +02:00
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
}
2020-04-29 19:52:15 +02:00
func (a *App) PublishAllInGithub(dryRun bool) (int, error) {
count := 0
for {
2020-04-29 19:52:15 +02:00
next, err := a.PublishNextInGithub(dryRun)
if err != nil {
return count, err
}
if !next {
break
}
count++
}
return count, nil
}
2020-04-29 19:52:15 +02:00
func (a *App) PublishBatchInGithub(batch int, dryRun bool) error {
for i := 1; i <= batch; i++ {
2020-04-29 19:52:15 +02:00
next, err := a.PublishNextInGithub(dryRun)
if err != nil {
return err
}
if !next {
return nil
}
}
return nil
}