2020-03-07 13:08:03 +01:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2020-04-28 10:21:31 +01:00
|
|
|
"bytes"
|
2020-03-07 13:08:03 +01:00
|
|
|
"context"
|
2020-04-27 10:42:29 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-04-28 10:21:31 +01:00
|
|
|
"text/template"
|
2020-03-07 13:08:03 +01:00
|
|
|
|
2020-03-07 13:27:11 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
|
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
|
|
|
|
2020-04-27 10:42:29 +01:00
|
|
|
"github.com/StevenACoffman/j2m"
|
2020-03-07 13:08:03 +01:00
|
|
|
"github.com/google/go-github/v29/github"
|
2020-03-07 13:27:11 +01:00
|
|
|
"golang.org/x/oauth2"
|
2020-03-07 13:08:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type GithubClient struct {
|
|
|
|
*github.Client
|
|
|
|
Repo string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(repo, token string) *GithubClient {
|
|
|
|
ctx := context.Background()
|
|
|
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
|
|
|
|
tc := oauth2.NewClient(ctx, ts)
|
|
|
|
|
|
|
|
client := github.NewClient(tc)
|
|
|
|
return &GithubClient{
|
|
|
|
Client: client,
|
2020-03-07 13:27:11 +01:00
|
|
|
Repo: repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:21:31 +01:00
|
|
|
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-03-07 13:27:11 +01:00
|
|
|
func (c *GithubClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign, dryRun bool) (*github.Issue, error) {
|
2020-04-27 10:42:29 +01:00
|
|
|
mdDescription := j2m.JiraToMD(ticket.Description)
|
2020-04-28 10:21:31 +01:00
|
|
|
if cmp.FooterTemplate != "" {
|
|
|
|
footer, err := getFooterTemplate(ticket, cmp.FooterTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mdDescription += "\n" + footer
|
|
|
|
}
|
|
|
|
|
2020-04-27 10:42:29 +01:00
|
|
|
issueRequest := &github.IssueRequest{
|
|
|
|
Title: &ticket.Summary,
|
|
|
|
Body: &mdDescription,
|
|
|
|
Labels: &cmp.Github.Labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
if dryRun {
|
|
|
|
b, _ := json.MarshalIndent(issueRequest, "", " ")
|
|
|
|
fmt.Println(string(b))
|
|
|
|
return &github.Issue{
|
|
|
|
Title: issueRequest.Title,
|
|
|
|
Body: issueRequest.Body,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, repo := cmp.RepoComponents()
|
|
|
|
newIssue, _, err := c.Issues.Create(context.Background(), owner, repo, issueRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newIssue, nil
|
2020-03-07 13:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GithubClient) PublishNextTicket(cmp *model.Campaign, dryRun bool) (bool, error) {
|
|
|
|
ticket := cmp.NextGithubUnpublishedTicket()
|
|
|
|
if ticket == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
issue, err := c.PublishTicket(ticket, cmp, dryRun)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dryRun {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 11:22:15 +01:00
|
|
|
ticket.GithubLink = issue.GetNumber()
|
2020-04-27 16:47:13 +01:00
|
|
|
if user := issue.GetUser(); user != nil {
|
|
|
|
ticket.GithubAssignee = user.GetLogin()
|
|
|
|
}
|
|
|
|
ticket.GithubStatus = issue.GetState()
|
2020-03-07 13:27:11 +01:00
|
|
|
if err := campaign.Save(cmp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GithubClient) PublishAll(cmp *model.Campaign, dryRun bool) (int, error) {
|
|
|
|
count := 0
|
|
|
|
for {
|
|
|
|
next, err := c.PublishNextTicket(cmp, dryRun)
|
|
|
|
if err != nil {
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
if !next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GithubClient) PublishBatch(cmp *model.Campaign, batch int, dryRun bool) error {
|
2020-04-27 10:42:29 +01:00
|
|
|
for i := 1; i <= batch; i++ {
|
2020-03-07 13:27:11 +01:00
|
|
|
next, err := c.PublishNextTicket(cmp, dryRun)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !next {
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-07 13:08:03 +01:00
|
|
|
}
|
2020-03-07 13:27:11 +01:00
|
|
|
return nil
|
2020-03-07 13:08:03 +01:00
|
|
|
}
|