From 2f6d27d831f919d177e3d09a70796d7f07bcbb89 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Thu, 30 Apr 2020 08:13:31 +0200 Subject: [PATCH] Add pull command --- app/github.go | 3 --- app/jira.go | 34 ++++++++++++++++++++++++++++++++++ cmd/pull.go | 31 +++++++++++++++++++++++++++++++ cmd/root.go | 1 + model/campaign.go | 10 ++++++++++ 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 cmd/pull.go diff --git a/app/github.go b/app/github.go index f7aef72..6937bcc 100644 --- a/app/github.go +++ b/app/github.go @@ -87,9 +87,6 @@ func (a *App) PublishNextInGithub(dryRun bool) (bool, error) { } ticket.GithubLink = issue.GetNumber() - if user := issue.GetUser(); user != nil { - ticket.GithubAssignee = user.GetLogin() - } ticket.GithubStatus = issue.GetState() if err := a.Save(); err != nil { return false, err diff --git a/app/jira.go b/app/jira.go index db1bfa8..f8c3aa2 100644 --- a/app/jira.go +++ b/app/jira.go @@ -183,3 +183,37 @@ func (a *App) PublishBatchInJira(batch int, dryRun bool) error { } return nil } + +func (a *App) GetTicketsFromJiraEpic() ([]*model.Ticket, error) { + jql := fmt.Sprintf("project = %s AND type = %s AND \"Epic Link\" = %s", a.Campaign.Jira.Project, a.Campaign.Jira.IssueType, a.Campaign.Jira.Epic) + + page := 0 + maxPerPage := 50 + issues := []jira.Issue{} + for { + opts := &jira.SearchOptions{StartAt: maxPerPage * page, MaxResults: maxPerPage} + pageIssues, _, err := a.JiraClient.Issue.Search(jql, opts) + if err != nil { + return nil, err + } + + issues = append(issues, pageIssues...) + if len(pageIssues) < maxPerPage { + break + } + page++ + } + + tickets := []*model.Ticket{} + for _, issue := range issues { + // ToDo: if they have github link, fill and fetch github data + ticket := &model.Ticket{ + JiraLink: issue.Key, + JiraStatus: issue.Fields.Status.Name, + Summary: issue.Fields.Summary, + Description: issue.Fields.Description, + } + tickets = append(tickets, ticket) + } + return tickets, nil +} diff --git a/cmd/pull.go b/cmd/pull.go new file mode 100644 index 0000000..7d41683 --- /dev/null +++ b/cmd/pull.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "git.ctrlz.es/mgdelacroix/campaigner/app" +) + +func PullCmd() *cobra.Command { + return &cobra.Command{ + Use: "pull", + Short: "Imports tickets from Jira", + Long: "Imports all tickets from a Jira epic issue. This command is only intended to use when the Jira tickets are already created and we want to use campaigner to import and manage them", + Args: cobra.NoArgs, + Run: withApp(pullCmdF), + } +} + +func pullCmdF(a *app.App, cmd *cobra.Command, _ []string) { + tickets, err := a.GetTicketsFromJiraEpic() + if err != nil { + ErrorAndExit(cmd, err) + } + + a.Campaign.AddTickets(tickets, false) + + if err := a.Save(); err != nil { + ErrorAndExit(cmd, err) + } + cmd.Printf("%d tickets have been added\n", len(tickets)) +} diff --git a/cmd/root.go b/cmd/root.go index 2f266c1..81f9010 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,6 +45,7 @@ func RootCmd() *cobra.Command { InitCmd(), StatusCmd(), PublishCmd(), + PullCmd(), SyncCmd(), ) diff --git a/model/campaign.go b/model/campaign.go index 9d8f844..b994552 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -76,10 +76,17 @@ func (c *Campaign) AddTickets(tickets []*Ticket, fileOnly bool) { } func (c *Campaign) RemoveDuplicateTickets(fileOnly bool) { + datalessTickets := []*Ticket{} ticketMap := map[string]*Ticket{} for _, t := range c.Tickets { filename, _ := t.Data["filename"].(string) lineNo, _ := t.Data["lineNo"].(int) + + if filename == "" { + datalessTickets = append(datalessTickets, t) + continue + } + if fileOnly { ticketMap[filename] = t } else { @@ -88,6 +95,9 @@ func (c *Campaign) RemoveDuplicateTickets(fileOnly bool) { } cleanTickets := []*Ticket{} + // dataless tickets are added first as they come from already + // existing tickets in Jira + cleanTickets = append(cleanTickets, datalessTickets...) for _, t := range ticketMap { cleanTickets = append(cleanTickets, t) }