2020-04-29 19:52:15 +02:00
|
|
|
package app
|
2020-03-07 13:08:03 +01:00
|
|
|
|
|
|
|
import (
|
2020-04-28 11:21:31 +02:00
|
|
|
"bytes"
|
2020-03-07 13:08:03 +01:00
|
|
|
"context"
|
2020-04-27 11:42:29 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-10-03 14:40:09 +02:00
|
|
|
"io"
|
2020-04-29 22:54:49 +02:00
|
|
|
"os"
|
2020-04-28 11:21:31 +02:00
|
|
|
"text/template"
|
2020-03-07 13:08:03 +01:00
|
|
|
|
2020-03-07 13:27:11 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
|
|
|
|
2020-04-27 11:42:29 +02:00
|
|
|
"github.com/StevenACoffman/j2m"
|
2020-09-24 11:13:52 +02:00
|
|
|
"github.com/google/go-github/v32/github"
|
2020-03-07 13:27:11 +01:00
|
|
|
"golang.org/x/oauth2"
|
2020-03-07 13:08:03 +01:00
|
|
|
)
|
|
|
|
|
2020-04-29 19:52:15 +02:00
|
|
|
func (a *App) InitGithubClient() error {
|
2020-03-07 13:08:03 +01:00
|
|
|
ctx := context.Background()
|
2020-04-29 19:52:15 +02:00
|
|
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: a.Campaign.Github.Token})
|
2020-03-07 13:08:03 +01:00
|
|
|
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
|
2020-03-07 13:27:11 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 11:21:31 +02: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-04-29 19:52:15 +02:00
|
|
|
func (a *App) PublishInGithub(ticket *model.Ticket, dryRun bool) (*github.Issue, error) {
|
2020-04-27 11:42:29 +02:00
|
|
|
mdDescription := j2m.JiraToMD(ticket.Description)
|
2020-04-29 19:52:15 +02:00
|
|
|
if a.Campaign.FooterTemplate != "" {
|
|
|
|
footer, err := getFooterTemplate(ticket, a.Campaign.FooterTemplate)
|
2020-04-28 11:21:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mdDescription += "\n" + footer
|
|
|
|
}
|
|
|
|
|
2020-04-27 11:42:29 +02:00
|
|
|
issueRequest := &github.IssueRequest{
|
|
|
|
Title: &ticket.Summary,
|
|
|
|
Body: &mdDescription,
|
2020-04-29 19:52:15 +02:00
|
|
|
Labels: &a.Campaign.Github.Labels,
|
2020-04-27 11:42:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-04-27 11:42:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newIssue, nil
|
2020-03-07 13:27:11 +01:00
|
|
|
}
|
|
|
|
|
2020-10-03 14:40:09 +02:00
|
|
|
func (a *App) PublishNextInGithub(w io.Writer, dryRun bool) (bool, error) {
|
2020-04-29 19:52:15 +02:00
|
|
|
ticket := a.Campaign.NextGithubUnpublishedTicket()
|
2020-03-07 13:27:11 +01:00
|
|
|
if ticket == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:52:15 +02:00
|
|
|
issue, err := a.PublishInGithub(ticket, dryRun)
|
2020-03-07 13:27:11 +01:00
|
|
|
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 {
|
2020-03-07 13:27:11 +01:00
|
|
|
return false, err
|
|
|
|
}
|
2020-04-29 22:54:49 +02:00
|
|
|
|
2020-10-03 14:40:09 +02:00
|
|
|
fmt.Fprintf(w, "Issue published: https://github.com/%s/issues/%d\n", a.Campaign.Github.Repo, ticket.GithubLink)
|
2020-09-24 11:13:52 +02:00
|
|
|
|
2020-10-03 14:40:09 +02:00
|
|
|
if err := a.UpdateJiraAfterGithub(ticket); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error updating Jira info for %s after publishing in Github\n", ticket.JiraLink)
|
2020-04-29 22:54:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 13:27:11 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-10-03 14:40:09 +02:00
|
|
|
func (a *App) PublishAllInGithub(w io.Writer, dryRun bool) (int, error) {
|
2020-03-07 13:27:11 +01:00
|
|
|
count := 0
|
|
|
|
for {
|
2020-10-03 14:40:09 +02:00
|
|
|
next, err := a.PublishNextInGithub(w, dryRun)
|
2020-03-07 13:27:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
if !next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2020-10-03 14:40:09 +02:00
|
|
|
func (a *App) PublishBatchInGithub(w io.Writer, batch int, dryRun bool) error {
|
2020-04-27 11:42:29 +02:00
|
|
|
for i := 1; i <= batch; i++ {
|
2020-10-03 14:40:09 +02:00
|
|
|
next, err := a.PublishNextInGithub(w, dryRun)
|
2020-03-07 13:27:11 +01:00
|
|
|
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
|
|
|
}
|
2020-09-21 09:27:50 +02:00
|
|
|
|
|
|
|
func (a *App) GithubSync() error {
|
|
|
|
tickets := a.Campaign.GetPublishedGithubTickets()
|
|
|
|
total := len(tickets)
|
|
|
|
owner, repo := a.Campaign.RepoComponents()
|
|
|
|
|
|
|
|
for i, ticket := range tickets {
|
|
|
|
fmt.Printf("\rUpdating ticket %d of %d", i+1, total)
|
|
|
|
|
2020-10-16 09:11:01 +02:00
|
|
|
// ticket is on its final stage, continue
|
|
|
|
if ticket.IsAssigned() && ticket.IsClosed() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:27:50 +02:00
|
|
|
issue, _, err := a.GithubClient.Issues.Get(context.Background(), owner, repo, ticket.GithubLink)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
assignee := issue.GetAssignee()
|
|
|
|
if assignee != nil {
|
|
|
|
ticket.GithubAssignee = assignee.GetLogin()
|
|
|
|
}
|
|
|
|
ticket.GithubStatus = issue.GetState()
|
|
|
|
}
|
|
|
|
fmt.Print("\n")
|
|
|
|
|
|
|
|
return a.Save()
|
|
|
|
}
|
2020-10-03 14:07:05 +02:00
|
|
|
|
|
|
|
func (a *App) ListLabels() ([]string, error) {
|
|
|
|
owner, repo := a.Campaign.RepoComponents()
|
|
|
|
opts := &github.ListOptions{Page: 0, PerPage: 100}
|
|
|
|
labels, _, err := a.GithubClient.Issues.ListLabels(context.Background(), owner, repo, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
strLabels := make([]string, len(labels))
|
|
|
|
for i, label := range labels {
|
|
|
|
strLabels[i] = *label.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return strLabels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) CheckLabels(labels []string) (bool, []string, error) {
|
|
|
|
ghLabels, err := a.ListLabels()
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
badLabels := []string{}
|
|
|
|
for _, label := range labels {
|
|
|
|
exists := false
|
|
|
|
for _, ghLabel := range ghLabels {
|
|
|
|
if label == ghLabel {
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
badLabels = append(badLabels, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(badLabels) == 0 {
|
|
|
|
return true, nil, nil
|
|
|
|
}
|
|
|
|
return false, badLabels, nil
|
|
|
|
}
|