2020-02-29 21:56:43 +01:00
|
|
|
package cmd
|
|
|
|
|
2020-03-01 01:17:41 +01:00
|
|
|
import (
|
2020-03-01 11:42:36 +01:00
|
|
|
"bytes"
|
2020-03-01 02:47:50 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-03-01 11:42:36 +01:00
|
|
|
"text/template"
|
2020-03-01 02:47:50 +01:00
|
|
|
|
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/jira"
|
|
|
|
|
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(),
|
|
|
|
)
|
|
|
|
|
|
|
|
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-01 12:30:49 +01:00
|
|
|
cmd.Flags().String("epic", "", "the jira epic id to associate the ticket with")
|
|
|
|
_ = cmd.MarkFlagRequired("epic")
|
|
|
|
cmd.Flags().String("team", "", "the team for the new ticket")
|
|
|
|
_ = cmd.MarkFlagRequired("epic")
|
|
|
|
cmd.Flags().String("username", "", "the jira username")
|
2020-03-01 01:17:41 +01:00
|
|
|
_ = cmd.MarkFlagRequired("username")
|
2020-03-01 12:30:49 +01:00
|
|
|
cmd.Flags().String("token", "", "the jira token")
|
2020-03-01 01:17:41 +01:00
|
|
|
_ = cmd.MarkFlagRequired("token")
|
2020-03-01 12:30:49 +01:00
|
|
|
cmd.Flags().String("summary", "", "the summary of the ticket")
|
2020-03-01 01:17:41 +01:00
|
|
|
_ = cmd.MarkFlagRequired("summary")
|
2020-03-01 12:30:49 +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")
|
|
|
|
cmd.Flags().StringSliceP("vars", "v", []string{}, "the variables to use in the template")
|
|
|
|
|
|
|
|
return cmd
|
2020-02-29 21:56:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:42:05 +01:00
|
|
|
func getVarMap(vars []string) (map[string]string, error) {
|
|
|
|
varMap := map[string]string{}
|
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-01 12:30:49 +01:00
|
|
|
epicId, _ := cmd.Flags().GetString("epic")
|
|
|
|
team, _ := cmd.Flags().GetString("team")
|
2020-03-01 02:47:50 +01:00
|
|
|
username, _ := cmd.Flags().GetString("username")
|
|
|
|
token, _ := cmd.Flags().GetString("token")
|
2020-03-01 11:42:36 +01:00
|
|
|
summaryTmplStr, _ := cmd.Flags().GetString("summary")
|
|
|
|
templatePath, _ := cmd.Flags().GetString("template")
|
2020-03-01 02:47:50 +01:00
|
|
|
vars, _ := cmd.Flags().GetStringSlice("vars")
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-01 02:47:50 +01:00
|
|
|
varMap, err := getVarMap(vars)
|
2020-03-01 02:42:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error processing vars: %w")
|
|
|
|
}
|
2020-03-01 11:42:36 +01:00
|
|
|
|
|
|
|
sumTmpl, err := template.New("").Parse(summaryTmplStr)
|
2020-03-01 02:54:58 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-01 11:42:36 +01:00
|
|
|
|
|
|
|
var summaryBytes bytes.Buffer
|
|
|
|
if err := sumTmpl.Execute(&summaryBytes, varMap); err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
summary := summaryBytes.String()
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-01 11:42:36 +01:00
|
|
|
descTmpl, err := template.ParseFiles(templatePath)
|
2020-03-01 02:42:05 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-01 12:30:49 +01:00
|
|
|
|
|
|
|
var descriptionBytes bytes.Buffer
|
2020-03-01 11:42:36 +01:00
|
|
|
if err := descTmpl.Execute(&descriptionBytes, varMap); err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
description := descriptionBytes.String()
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-01 11:42:36 +01:00
|
|
|
jiraClient := jira.NewClient(username, token)
|
2020-03-01 12:30:49 +01:00
|
|
|
|
|
|
|
ticketKey, err := jiraClient.CreateTicket(epicId, team, summary, description)
|
2020-03-01 02:42:05 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-01 12:30:49 +01:00
|
|
|
|
2020-03-01 11:42:36 +01:00
|
|
|
cmd.Printf("Ticket %s successfully created in JIRA", ticketKey)
|
2020-03-01 02:47:50 +01:00
|
|
|
return nil
|
2020-03-01 01:17:41 +01:00
|
|
|
}
|