Add pull command

This commit is contained in:
Miguel de la Cruz 2020-04-30 08:13:31 +02:00
parent 7829bbca13
commit 2f6d27d831
5 changed files with 76 additions and 3 deletions

View file

@ -87,9 +87,6 @@ func (a *App) PublishNextInGithub(dryRun bool) (bool, error) {
} }
ticket.GithubLink = issue.GetNumber() ticket.GithubLink = issue.GetNumber()
if user := issue.GetUser(); user != nil {
ticket.GithubAssignee = user.GetLogin()
}
ticket.GithubStatus = issue.GetState() ticket.GithubStatus = issue.GetState()
if err := a.Save(); err != nil { if err := a.Save(); err != nil {
return false, err return false, err

View file

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

31
cmd/pull.go Normal file
View file

@ -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))
}

View file

@ -45,6 +45,7 @@ func RootCmd() *cobra.Command {
InitCmd(), InitCmd(),
StatusCmd(), StatusCmd(),
PublishCmd(), PublishCmd(),
PullCmd(),
SyncCmd(), SyncCmd(),
) )

View file

@ -76,10 +76,17 @@ func (c *Campaign) AddTickets(tickets []*Ticket, fileOnly bool) {
} }
func (c *Campaign) RemoveDuplicateTickets(fileOnly bool) { func (c *Campaign) RemoveDuplicateTickets(fileOnly bool) {
datalessTickets := []*Ticket{}
ticketMap := map[string]*Ticket{} ticketMap := map[string]*Ticket{}
for _, t := range c.Tickets { for _, t := range c.Tickets {
filename, _ := t.Data["filename"].(string) filename, _ := t.Data["filename"].(string)
lineNo, _ := t.Data["lineNo"].(int) lineNo, _ := t.Data["lineNo"].(int)
if filename == "" {
datalessTickets = append(datalessTickets, t)
continue
}
if fileOnly { if fileOnly {
ticketMap[filename] = t ticketMap[filename] = t
} else { } else {
@ -88,6 +95,9 @@ func (c *Campaign) RemoveDuplicateTickets(fileOnly bool) {
} }
cleanTickets := []*Ticket{} cleanTickets := []*Ticket{}
// dataless tickets are added first as they come from already
// existing tickets in Jira
cleanTickets = append(cleanTickets, datalessTickets...)
for _, t := range ticketMap { for _, t := range ticketMap {
cleanTickets = append(cleanTickets, t) cleanTickets = append(cleanTickets, t)
} }