2020-03-04 22:22:16 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-03-05 22:37:01 +01:00
|
|
|
"fmt"
|
2020-03-07 13:08:03 +01:00
|
|
|
"context"
|
2020-03-05 22:37:01 +01:00
|
|
|
|
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
|
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/config"
|
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/jira"
|
2020-03-07 13:08:03 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/github"
|
2020-03-05 22:37:01 +01:00
|
|
|
|
2020-03-04 22:22:16 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func JiraPublishCmd() *cobra.Command {
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd := &cobra.Command{
|
2020-03-04 22:22:16 +01:00
|
|
|
Use: "jira",
|
2020-03-07 13:08:03 +01:00
|
|
|
Short: "Publishes the campaign tickets in jira",
|
2020-03-04 22:22:16 +01:00
|
|
|
Args: cobra.NoArgs,
|
2020-03-05 20:42:25 +01:00
|
|
|
RunE: jiraPublishCmdF,
|
2020-03-04 22:22:16 +01:00
|
|
|
}
|
2020-03-05 20:42:25 +01:00
|
|
|
|
|
|
|
cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign")
|
|
|
|
cmd.Flags().IntP("batch", "b", 0, "Number of tickets to publish")
|
2020-03-05 22:57:01 +01:00
|
|
|
cmd.Flags().Bool("dry-run", false, "Print the tickets information instead of publishing them")
|
2020-03-05 20:42:25 +01:00
|
|
|
|
|
|
|
return cmd
|
2020-03-04 22:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 13:08:03 +01:00
|
|
|
func GithubPublishCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "github",
|
|
|
|
Short: "Publishes the campaign tickets in github",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: githubPublishCmdF,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign")
|
|
|
|
cmd.Flags().IntP("batch", "b", 0, "Number of tickets to publish")
|
|
|
|
cmd.Flags().Bool("dry-run", false, "Print the tickets information instead of publishing them")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:22:16 +01:00
|
|
|
func PublishCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "publish",
|
|
|
|
Short: "Publishes the campaign tickets in different providers",
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.AddCommand(
|
2020-03-07 13:08:03 +01:00
|
|
|
GithubPublishCmd(),
|
2020-03-04 22:22:16 +01:00
|
|
|
JiraPublishCmd(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
func jiraPublishCmdF(cmd *cobra.Command, _ []string) error {
|
2020-03-05 22:37:01 +01:00
|
|
|
all, _ := cmd.Flags().GetBool("all")
|
|
|
|
batch, _ := cmd.Flags().GetInt("batch")
|
2020-03-05 22:57:01 +01:00
|
|
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
2020-03-05 22:37:01 +01:00
|
|
|
|
|
|
|
if !all && batch == 0 {
|
|
|
|
return fmt.Errorf("One of --all or --batch flags is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := config.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-05 20:42:25 +01:00
|
|
|
|
2020-03-05 22:37:01 +01:00
|
|
|
cmp, err := campaign.Read()
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
jiraClient, err := jira.NewClient(cmp.Url, cfg.JiraUsername, cfg.JiraToken)
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if all {
|
2020-03-05 22:57:01 +01:00
|
|
|
count, err := jiraClient.PublishAll(cmp, dryRun)
|
2020-03-05 22:37:01 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
cmd.Printf("All %d tickets successfully published in jira\n", count)
|
|
|
|
} else {
|
2020-03-05 22:57:01 +01:00
|
|
|
if err := jiraClient.PublishBatch(cmp, batch, dryRun); err != nil {
|
2020-03-05 22:37:01 +01:00
|
|
|
ErrorAndExit(cmd, err)
|
2020-03-05 20:42:25 +01:00
|
|
|
}
|
2020-03-05 22:37:01 +01:00
|
|
|
cmd.Printf("Batch of %d tickets successfully published in jira\n", batch)
|
|
|
|
}
|
2020-03-05 20:42:25 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-07 13:08:03 +01:00
|
|
|
|
|
|
|
func githubPublishCmdF(cmd *cobra.Command, _ []string) {
|
|
|
|
cfg, err := config.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmp, err := campaign.Read()
|
|
|
|
// if err != nil {
|
|
|
|
// ErrorAndExit(cmd, err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
githubClient := github.NewClient("my/repo", cfg.GithubToken)
|
|
|
|
|
|
|
|
repos, _, err := githubClient.Repositories.List(context.Background(), "", nil)
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
|
|
|
cmd.Println(*repo.Name)
|
|
|
|
}
|
|
|
|
}
|