From c9cfb83e326c6cb1270f77126221736dd3d21031 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sun, 1 Mar 2020 11:42:36 +0100 Subject: [PATCH] Adds template execution and correctly calls create ticket method --- .gitignore | 1 + cmd/standalone.go | 32 +++++++++++++++++++++++--------- jira/jira.go | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 jira/jira.go diff --git a/.gitignore b/.gitignore index 72d5a56..a4029d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ campaigner campaign.json +template.tmpl diff --git a/cmd/standalone.go b/cmd/standalone.go index 0f1b738..4531cf0 100644 --- a/cmd/standalone.go +++ b/cmd/standalone.go @@ -1,8 +1,10 @@ package cmd import ( + "bytes" "fmt" "strings" + "text/template" "git.ctrlz.es/mgdelacroix/campaigner/jira" @@ -50,7 +52,7 @@ func getVarMap(vars []string) (map[string]string, error) { if len(parts) < 2 { return nil, fmt.Errorf("cannot parse var %s", v) } - varMap[parts[0]] = strings.Join(parts[1:], "") + varMap[parts[0]] = strings.Join(parts[1:], "=") } return varMap, nil } @@ -58,32 +60,44 @@ func getVarMap(vars []string) (map[string]string, error) { func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error { username, _ := cmd.Flags().GetString("username") token, _ := cmd.Flags().GetString("token") - summary, _ := cmd.Flags().GetString("summary") - template, _ := cmd.Flags().GetString("template") + summaryTmplStr, _ := cmd.Flags().GetString("summary") + templatePath, _ := cmd.Flags().GetString("template") vars, _ := cmd.Flags().GetStringSlice("vars") varMap, err := getVarMap(vars) if err != nil { return fmt.Errorf("error processing vars: %w") } - - // process template - tmpl, err := template.ParseFiles(template) + + sumTmpl, err := template.New("").Parse(summaryTmplStr) if err != nil { ErrorAndExit(cmd, err) } - tmpl.Execute() // ToDo: write in a description var + + var summaryBytes bytes.Buffer + if err := sumTmpl.Execute(&summaryBytes, varMap); err != nil { + ErrorAndExit(cmd, err) + } + summary := summaryBytes.String() - jiraClient, err := jira.NewClient(username, token) + descTmpl, err := template.ParseFiles(templatePath) if err != nil { ErrorAndExit(cmd, err) } + var descriptionBytes bytes.Buffer + if err := descTmpl.Execute(&descriptionBytes, varMap); err != nil { + ErrorAndExit(cmd, err) + } + description := descriptionBytes.String() + + jiraClient := jira.NewClient(username, token) + ticketKey, err := jiraClient.CreateTicket(summary, description) if err != nil { ErrorAndExit(cmd, err) } - cmd.Printf("Ticket %s successfully created in JIRA") + cmd.Printf("Ticket %s successfully created in JIRA", ticketKey) return nil } diff --git a/jira/jira.go b/jira/jira.go new file mode 100644 index 0000000..a8d0040 --- /dev/null +++ b/jira/jira.go @@ -0,0 +1,22 @@ +package jira + +import ( + "fmt" +) + +type JiraClient struct { + Username string + Token string +} + +func NewClient(username, token string) *JiraClient { + return &JiraClient{ + Username: username, + Token: token, + } +} + +func (c *JiraClient) CreateTicket(summary, description string) (string, error) { + fmt.Printf("Summary: %s\nDescription: %s\n", summary, description) + return "", nil +}