Initial approach to github issue creation

This commit is contained in:
Miguel de la Cruz 2020-03-07 13:27:11 +01:00
parent fdaf72aac4
commit dcee28eca8
5 changed files with 101 additions and 19 deletions

View file

@ -2,12 +2,11 @@ package cmd
import (
"fmt"
"context"
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/config"
"git.ctrlz.es/mgdelacroix/campaigner/jira"
"git.ctrlz.es/mgdelacroix/campaigner/github"
"git.ctrlz.es/mgdelacroix/campaigner/jira"
"github.com/spf13/cobra"
)
@ -32,7 +31,7 @@ func GithubPublishCmd() *cobra.Command {
Use: "github",
Short: "Publishes the campaign tickets in github",
Args: cobra.NoArgs,
Run: githubPublishCmdF,
RunE: githubPublishCmdF,
}
cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign")
@ -96,25 +95,39 @@ func jiraPublishCmdF(cmd *cobra.Command, _ []string) error {
return nil
}
func githubPublishCmdF(cmd *cobra.Command, _ []string) {
func githubPublishCmdF(cmd *cobra.Command, _ []string) error {
all, _ := cmd.Flags().GetBool("all")
batch, _ := cmd.Flags().GetInt("batch")
dryRun, _ := cmd.Flags().GetBool("dry-run")
if !all && batch == 0 {
return fmt.Errorf("One of --all or --batch flags is required")
}
cfg, err := config.ReadConfig()
if err != nil {
ErrorAndExit(cmd, err)
}
// cmp, err := campaign.Read()
// if err != nil {
// ErrorAndExit(cmd, err)
// }
githubClient := github.NewClient("my/repo", cfg.GithubToken)
repos, _, err := githubClient.Repositories.List(context.Background(), "", nil)
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
for _, repo := range repos {
cmd.Println(*repo.Name)
githubClient := github.NewClient("my/repo", cfg.GithubToken)
if all {
count, err := githubClient.PublishAll(cmp, dryRun)
if err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("All %d tickets successfully published in github\n", count)
} else {
if err := githubClient.PublishBatch(cmp, batch, dryRun); err != nil {
ErrorAndExit(cmd, err)
}
cmd.Printf("Batch of %d tickets successfully published in github\n", batch)
}
return nil
}

View file

@ -3,8 +3,11 @@ package github
import (
"context"
"golang.org/x/oauth2"
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/model"
"github.com/google/go-github/v29/github"
"golang.org/x/oauth2"
)
type GithubClient struct {
@ -23,3 +26,60 @@ func NewClient(repo, token string) *GithubClient {
Repo: repo,
}
}
func (c *GithubClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign, dryRun bool) (*github.Issue, error) {
return nil, 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
}
ticket.GithubLink = *issue.ID
// move this to a publish service that can do both github and
// jira, as we need to update a jira issue field with the github
// link
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 := 0; i <= batch; i++ {
next, err := c.PublishNextTicket(cmp, dryRun)
if err != nil {
return err
}
if !next {
return nil
}
}
return nil
}

View file

@ -113,7 +113,7 @@ func (c *JiraClient) GetIssue(issueNo string) (*jira.Issue, error) {
}
func (c *JiraClient) PublishNextTicket(cmp *model.Campaign, dryRun bool) (bool, error) {
ticket := cmp.NextUnpublishedTicket()
ticket := cmp.NextJiraUnpublishedTicket()
if ticket == nil {
return false, nil
}

View file

@ -16,7 +16,7 @@ type Campaign struct {
Tickets []*Ticket `json:"tickets,omitempty"`
}
func (c *Campaign) NextUnpublishedTicket() *Ticket {
func (c *Campaign) NextJiraUnpublishedTicket() *Ticket {
for _, ticket := range c.Tickets {
if ticket.JiraLink == "" {
return ticket
@ -25,6 +25,15 @@ func (c *Campaign) NextUnpublishedTicket() *Ticket {
return nil
}
func (c *Campaign) NextGithubUnpublishedTicket() *Ticket {
for _, ticket := range c.Tickets {
if ticket.JiraLink != "" && ticket.GithubLink != 0 {
return ticket
}
}
return nil
}
func (c *Campaign) PrintStatus(w io.Writer) {
fmt.Fprintf(w, "Url: %s\n", c.Url)
fmt.Fprintf(w, "Project: %s\n", c.Project)

View file

@ -6,7 +6,7 @@ import (
)
type Ticket struct {
GithubLink string `json:"githubLink,omitempty"`
GithubLink int64 `json:"githubLink,omitempty"`
JiraLink string `json:"jiraLink,omitempty"`
Summary string `json:"summary,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`