2020-02-29 21:56:43 +01:00
|
|
|
package cmd
|
|
|
|
|
2020-03-01 01:17:41 +01:00
|
|
|
import (
|
2020-03-01 02:47:50 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-03-04 22:40:42 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/config"
|
2020-03-01 02:47:50 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/jira"
|
2020-03-05 20:42:25 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
2020-03-01 02:47:50 +01:00
|
|
|
|
2020-03-01 01:17:41 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2020-02-29 21:56:43 +01:00
|
|
|
func StandaloneCmd() *cobra.Command {
|
2020-03-01 01:17:41 +01:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "standalone",
|
|
|
|
Short: "Standalone fire-and-forget commands",
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.AddCommand(
|
|
|
|
CreateJiraTicketStandaloneCmd(),
|
2020-03-01 13:02:52 +01:00
|
|
|
GetJiraTicketStandaloneCmd(),
|
2020-03-01 01:17:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return cmd
|
2020-02-29 21:56:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 01:17:41 +01:00
|
|
|
func CreateJiraTicketStandaloneCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create-jira-ticket",
|
|
|
|
Short: "Creates a jira ticket from a template",
|
|
|
|
Args: cobra.NoArgs,
|
2020-03-01 02:42:05 +01:00
|
|
|
RunE: createJiraTicketStandaloneCmdF,
|
2020-03-01 01:17:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().String("url", "", "The jira server URL")
|
2020-03-05 17:38:53 +01:00
|
|
|
_ = cmd.MarkFlagRequired("url")
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().String("epic", "", "The jira epic id to associate the ticket with")
|
2020-03-01 12:30:49 +01:00
|
|
|
_ = cmd.MarkFlagRequired("epic")
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().StringP("project", "p", "", "The jira project key to associate the tickets with")
|
|
|
|
_ = cmd.MarkFlagRequired("project")
|
|
|
|
cmd.Flags().String("summary", "", "The summary of the ticket")
|
2020-03-01 01:17:41 +01:00
|
|
|
_ = cmd.MarkFlagRequired("summary")
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().String("template", "", "The template to render the description of the ticket")
|
2020-03-01 01:17:41 +01:00
|
|
|
_ = cmd.MarkFlagRequired("template")
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().String("username", "", "The jira username")
|
|
|
|
cmd.Flags().String("token", "", "The jira token")
|
|
|
|
cmd.Flags().StringSliceP("vars", "v", []string{}, "The variables to use in the template")
|
2020-03-05 22:57:01 +01:00
|
|
|
cmd.Flags().Bool("dry-run", false, "Print the ticket information instead of creating it")
|
2020-03-01 01:17:41 +01:00
|
|
|
|
|
|
|
return cmd
|
2020-02-29 21:56:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 13:02:52 +01:00
|
|
|
func GetJiraTicketStandaloneCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "get-jira-ticket",
|
|
|
|
Short: "Gets the ticket from jira",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Run: getJiraTicketStandaloneCmdF,
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().String("url", "", "The jira server URL")
|
2020-03-05 17:38:53 +01:00
|
|
|
_ = cmd.MarkFlagRequired("url")
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().String("username", "", "The jira username")
|
|
|
|
cmd.Flags().String("token", "", "The jira token")
|
2020-03-01 13:02:52 +01:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
func getVarMap(vars []string) (map[string]interface{}, error) {
|
|
|
|
varMap := map[string]interface{}{}
|
2020-03-01 02:43:54 +01:00
|
|
|
for _, v := range vars {
|
|
|
|
parts := strings.Split(v, "=")
|
2020-03-01 02:42:05 +01:00
|
|
|
if len(parts) < 2 {
|
2020-03-01 02:43:54 +01:00
|
|
|
return nil, fmt.Errorf("cannot parse var %s", v)
|
2020-03-01 02:42:05 +01:00
|
|
|
}
|
2020-03-01 11:42:36 +01:00
|
|
|
varMap[parts[0]] = strings.Join(parts[1:], "=")
|
2020-03-01 02:42:05 +01:00
|
|
|
}
|
|
|
|
return varMap, nil
|
|
|
|
}
|
2020-03-01 01:17:41 +01:00
|
|
|
|
2020-03-01 02:42:05 +01:00
|
|
|
func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error {
|
2020-03-05 17:38:53 +01:00
|
|
|
url, _ := cmd.Flags().GetString("url")
|
2020-03-05 20:42:25 +01:00
|
|
|
epic, _ := cmd.Flags().GetString("epic")
|
|
|
|
project, _ := cmd.Flags().GetString("project")
|
2020-03-01 02:47:50 +01:00
|
|
|
username, _ := cmd.Flags().GetString("username")
|
|
|
|
token, _ := cmd.Flags().GetString("token")
|
2020-03-05 20:42:25 +01:00
|
|
|
summary, _ := cmd.Flags().GetString("summary")
|
|
|
|
template, _ := cmd.Flags().GetString("template")
|
2020-03-01 02:47:50 +01:00
|
|
|
vars, _ := cmd.Flags().GetStringSlice("vars")
|
2020-03-05 22:57:01 +01:00
|
|
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-04 22:40:42 +01:00
|
|
|
if username == "" || token == "" {
|
|
|
|
cfg, err := config.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if username == "" {
|
|
|
|
username = cfg.JiraUsername
|
|
|
|
}
|
|
|
|
if token == "" {
|
|
|
|
token = cfg.JiraToken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:47:50 +01:00
|
|
|
varMap, err := getVarMap(vars)
|
2020-03-01 02:42:05 +01:00
|
|
|
if err != nil {
|
2020-03-01 13:03:47 +01:00
|
|
|
return fmt.Errorf("error processing vars: %w", err)
|
2020-03-01 02:42:05 +01:00
|
|
|
}
|
2020-03-01 11:42:36 +01:00
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
jiraClient, err := jira.NewClient(url, username, token)
|
2020-03-01 02:42:05 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
campaign := &model.Campaign{
|
|
|
|
Epic: epic,
|
|
|
|
Project: project,
|
|
|
|
Summary: summary,
|
|
|
|
Template: template,
|
2020-03-04 22:13:55 +01:00
|
|
|
}
|
2020-03-05 20:42:25 +01:00
|
|
|
ticket := &model.Ticket{Data: varMap}
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-05 22:57:01 +01:00
|
|
|
issue, err := jiraClient.PublishTicket(ticket, campaign, dryRun)
|
2020-03-01 02:42:05 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Printf("Ticket %s successfully created in JIRA", issue.Key)
|
2020-03-01 02:47:50 +01:00
|
|
|
return nil
|
2020-03-01 01:17:41 +01:00
|
|
|
}
|
2020-03-01 13:02:52 +01:00
|
|
|
|
|
|
|
func getJiraTicketStandaloneCmdF(cmd *cobra.Command, args []string) {
|
2020-03-05 17:38:53 +01:00
|
|
|
url, _ := cmd.Flags().GetString("url")
|
2020-03-01 13:02:52 +01:00
|
|
|
username, _ := cmd.Flags().GetString("username")
|
|
|
|
token, _ := cmd.Flags().GetString("token")
|
|
|
|
|
2020-03-04 22:40:42 +01:00
|
|
|
if username == "" || token == "" {
|
|
|
|
cfg, err := config.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if username == "" {
|
|
|
|
username = cfg.JiraUsername
|
|
|
|
}
|
|
|
|
if token == "" {
|
|
|
|
token = cfg.JiraToken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:38:53 +01:00
|
|
|
jiraClient, err := jira.NewClient(url, username, token)
|
2020-03-04 22:13:55 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-01 13:02:52 +01:00
|
|
|
|
|
|
|
issue, err := jiraClient.GetIssue(args[0])
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:13:55 +01:00
|
|
|
fmt.Printf("Summary: %s\nKey: %s\nStatus: %s\nAsignee: %s\n", issue.Fields.Summary, issue.Key, issue.Fields.Status.Name, issue.Fields.Assignee.DisplayName)
|
2020-03-01 13:02:52 +01:00
|
|
|
}
|