Add initial version of the sync command

This commit is contained in:
Miguel de la Cruz 2020-09-21 09:27:50 +02:00
parent f0d4dfb962
commit a13a0ec949
3 changed files with 42 additions and 11 deletions

View file

@ -128,3 +128,27 @@ func (a *App) PublishBatchInGithub(batch int, dryRun bool) error {
} }
return nil 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()
}

View file

@ -1,27 +1,24 @@
package cmd package cmd
import ( import (
"fmt" "git.ctrlz.es/mgdelacroix/campaigner/app"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func SyncCmd() *cobra.Command { func SyncCmd() *cobra.Command {
cmd := &cobra.Command{ return &cobra.Command{
Use: "sync", Use: "sync",
Short: "Syncs the tickets", Short: "Syncs the tickets",
Long: "Synchronizes the status of the published tickets with remote providers", Long: "Synchronizes the status of the published tickets with remote providers",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: syncCmdF, Run: withApp(syncCmdF),
}
} }
cmd.Flags().BoolP("all", "a", false, "syncs all the published tickets") func syncCmdF(a *app.App, cmd *cobra.Command, _ []string) {
cmd.Flags().StringP("jira-issue", "j", "", "syncs a ticket by Jira issue number") if err := a.GithubSync(); err != nil {
cmd.Flags().StringP("github-issue", "g", "", "syncs a ticket by GitHub issue number") ErrorAndExit(cmd, err)
return cmd
} }
cmd.Println("Synchronization completed")
func syncCmdF(cmd *cobra.Command, _ []string) {
ErrorAndExit(cmd, fmt.Errorf("Not implemented yet"))
} }

View file

@ -105,6 +105,16 @@ func (c *Campaign) RemoveDuplicateTickets(fileOnly bool) {
c.Tickets = cleanTickets 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 { func (c *Campaign) FillTicket(t *Ticket) error {
summaryTmpl, err := template.New("").Parse(c.Summary) summaryTmpl, err := template.New("").Parse(c.Summary)
if err != nil { if err != nil {