From a13a0ec949a681b5724fa9cf0317a109518580d9 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 21 Sep 2020 09:27:50 +0200 Subject: [PATCH] Add initial version of the sync command --- app/github.go | 24 ++++++++++++++++++++++++ cmd/sync.go | 19 ++++++++----------- model/campaign.go | 10 ++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/github.go b/app/github.go index 6937bcc..dc136db 100644 --- a/app/github.go +++ b/app/github.go @@ -128,3 +128,27 @@ func (a *App) PublishBatchInGithub(batch int, dryRun bool) error { } return nil } + +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) + + 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() +} diff --git a/cmd/sync.go b/cmd/sync.go index df56363..0cbfd12 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -1,27 +1,24 @@ package cmd import ( - "fmt" + "git.ctrlz.es/mgdelacroix/campaigner/app" "github.com/spf13/cobra" ) func SyncCmd() *cobra.Command { - cmd := &cobra.Command{ + return &cobra.Command{ Use: "sync", Short: "Syncs the tickets", Long: "Synchronizes the status of the published tickets with remote providers", Args: cobra.NoArgs, - Run: syncCmdF, + Run: withApp(syncCmdF), } - - cmd.Flags().BoolP("all", "a", false, "syncs all the published tickets") - cmd.Flags().StringP("jira-issue", "j", "", "syncs a ticket by Jira issue number") - cmd.Flags().StringP("github-issue", "g", "", "syncs a ticket by GitHub issue number") - - return cmd } -func syncCmdF(cmd *cobra.Command, _ []string) { - ErrorAndExit(cmd, fmt.Errorf("Not implemented yet")) +func syncCmdF(a *app.App, cmd *cobra.Command, _ []string) { + if err := a.GithubSync(); err != nil { + ErrorAndExit(cmd, err) + } + cmd.Println("Synchronization completed") } diff --git a/model/campaign.go b/model/campaign.go index b994552..2b46609 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -105,6 +105,16 @@ func (c *Campaign) RemoveDuplicateTickets(fileOnly bool) { c.Tickets = cleanTickets } +func (c *Campaign) GetPublishedGithubTickets() []*Ticket { + publishedTickets := []*Ticket{} + for _, ticket := range c.Tickets { + if ticket.IsPublishedGithub() { + publishedTickets = append(publishedTickets, ticket) + } + } + return publishedTickets +} + func (c *Campaign) FillTicket(t *Ticket) error { summaryTmpl, err := template.New("").Parse(c.Summary) if err != nil {