diff --git a/cmd/publish.go b/cmd/publish.go index a394092..790e48d 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -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 } diff --git a/github/github.go b/github/github.go index 14da664..8e177bc 100644 --- a/github/github.go +++ b/github/github.go @@ -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 { @@ -20,6 +23,63 @@ func NewClient(repo, token string) *GithubClient { client := github.NewClient(tc) return &GithubClient{ Client: client, - Repo: repo, + 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 +} diff --git a/jira/jira.go b/jira/jira.go index 9fbec84..9b0b4df 100644 --- a/jira/jira.go +++ b/jira/jira.go @@ -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 } diff --git a/model/campaign.go b/model/campaign.go index 2d93a6a..5813025 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -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) diff --git a/model/ticket.go b/model/ticket.go index 912c2ca..9c53386 100644 --- a/model/ticket.go +++ b/model/ticket.go @@ -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"`