campaigner/cmd/sync.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-03-04 22:26:04 +01:00
package cmd
import (
2020-04-29 16:37:52 +01:00
"fmt"
"encoding/json"
2020-03-04 22:26:04 +01:00
"github.com/spf13/cobra"
2020-04-29 16:37:52 +01:00
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/jira"
"git.ctrlz.es/mgdelacroix/campaigner/model"
2020-03-04 22:26:04 +01:00
)
func SyncCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sync",
2020-04-27 11:22:15 +01:00
Short: "Syncs the tickets",
Long: "Synchronizes the status of the published tickets with remote providers",
2020-03-04 22:26:04 +01:00
Args: cobra.NoArgs,
Run: syncCmdF,
}
2020-04-27 11:22:15 +01:00
cmd.Flags().BoolP("all", "a", false, "syncs all the published tickets")
cmd.Flags().StringP("jira-issue", "j", "", "syncs a ticket by Jira issue number")
2020-04-29 16:37:52 +01:00
cmd.Flags().IntP("github-issue", "g", 0, "syncs a ticket by GitHub issue number")
2020-04-27 11:22:15 +01:00
2020-03-04 22:26:04 +01:00
return cmd
}
2020-04-29 16:37:52 +01:00
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))
}