diff --git a/cmd/pull.go b/cmd/pull.go new file mode 100644 index 0000000..29ff477 --- /dev/null +++ b/cmd/pull.go @@ -0,0 +1,12 @@ +package cmd + +func PullCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "pull", + Short: "Imports tickets from Jira", + Long: "Imports all tickets from a Jira epic issue", + RunE: pullCmdF, + } + + cmd.Flags().BoolP("epic") +} diff --git a/cmd/sync.go b/cmd/sync.go index 4196357..e5aea97 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -1,7 +1,14 @@ package cmd import ( + "fmt" + "encoding/json" + "github.com/spf13/cobra" + + "git.ctrlz.es/mgdelacroix/campaigner/campaign" + "git.ctrlz.es/mgdelacroix/campaigner/jira" + "git.ctrlz.es/mgdelacroix/campaigner/model" ) func SyncCmd() *cobra.Command { @@ -15,9 +22,40 @@ func SyncCmd() *cobra.Command { 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") + cmd.Flags().IntP("github-issue", "g", 0, "syncs a ticket by GitHub issue number") return cmd } -func syncCmdF(_ *cobra.Command, _ []string) {} +func syncCmdF(cmd *cobra.Command, _ []string) { + jiraIssue, _ := cmd.Flags().GetString("jira-issue") + // githubIssue, _ := cmd.Flags().GetInt() + + // check that one is defined, or all + + cmp, err := campaign.Read() + if err != nil { + ErrorAndExit(cmd, err) + } + + var ticket *model.Ticket + if jiraIssue != "" { + ticket = cmp.GetByJiraIssue(jiraIssue) + if ticket == nil { + ErrorAndExit(cmd, fmt.Errorf("Could not find jira issue %s", jiraIssue)) + } + } + + jiraClient, err := jira.NewClient(cmp.Jira.Url, cmp.Jira.Username, cmp.Jira.Token) + if err != nil { + ErrorAndExit(cmd, err) + } + + i, _, err := jiraClient.Issue.Get(ticket.JiraIssue(), nil) + if err != nil { + ErrorAndExit(cmd, err) + } + b, _ := json.MarshalIndent(i.Fields, "", " ") + + fmt.Printf(string(b)) +} diff --git a/model/campaign.go b/model/campaign.go index 571620a..c7a7bd7 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -102,3 +102,13 @@ func (c *Campaign) RepoComponents() (string, string) { } return "", "" } + +func (c *Campaign) GetByJiraIssue(num string) (ticket *Ticket) { + for _, t := range c.Tickets { + if t.JiraIssue() == num { + ticket = t + return + } + } + return +} diff --git a/model/ticket.go b/model/ticket.go index 94de336..fa61b6d 100644 --- a/model/ticket.go +++ b/model/ticket.go @@ -2,6 +2,7 @@ package model import ( "fmt" + "strings" ) type Ticket struct { @@ -48,3 +49,11 @@ func (t *Ticket) PrintStatus() { fmt.Printf("[%s] %s\n", t.JiraLink, t.Summary) } } + +func (t *Ticket) JiraIssue() string { + parts := strings.Split(t.JiraLink, "/") + if len(parts) < 2 { + return "" + } + return parts[len(parts)-1] +}