campaigner/github/github.go

111 lines
2.3 KiB
Go
Raw Normal View History

package github
import (
"context"
"encoding/json"
"fmt"
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/model"
"github.com/StevenACoffman/j2m"
"github.com/google/go-github/v29/github"
"golang.org/x/oauth2"
)
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,
Repo: repo,
}
}
func (c *GithubClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign, dryRun bool) (*github.Issue, error) {
mdDescription := j2m.JiraToMD(ticket.Description)
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
}
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 12:22:15 +02:00
ticket.GithubLink = issue.GetNumber()
2020-04-27 17:47:13 +02:00
if user := issue.GetUser(); user != nil {
ticket.GithubAssignee = user.GetLogin()
}
ticket.GithubStatus = issue.GetState()
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 {
for i := 1; i <= batch; i++ {
next, err := c.PublishNextTicket(cmp, dryRun)
if err != nil {
return err
}
if !next {
return nil
}
}
return nil
}