Adds template execution and correctly calls create ticket method

This commit is contained in:
Miguel de la Cruz 2020-03-01 11:42:36 +01:00
parent 41baff8743
commit c9cfb83e32
3 changed files with 46 additions and 9 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
campaigner campaigner
campaign.json campaign.json
template.tmpl

View file

@ -1,8 +1,10 @@
package cmd package cmd
import ( import (
"bytes"
"fmt" "fmt"
"strings" "strings"
"text/template"
"git.ctrlz.es/mgdelacroix/campaigner/jira" "git.ctrlz.es/mgdelacroix/campaigner/jira"
@ -50,7 +52,7 @@ func getVarMap(vars []string) (map[string]string, error) {
if len(parts) < 2 { if len(parts) < 2 {
return nil, fmt.Errorf("cannot parse var %s", v) 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 return varMap, nil
} }
@ -58,32 +60,44 @@ func getVarMap(vars []string) (map[string]string, error) {
func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error { func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error {
username, _ := cmd.Flags().GetString("username") username, _ := cmd.Flags().GetString("username")
token, _ := cmd.Flags().GetString("token") token, _ := cmd.Flags().GetString("token")
summary, _ := cmd.Flags().GetString("summary") summaryTmplStr, _ := cmd.Flags().GetString("summary")
template, _ := cmd.Flags().GetString("template") templatePath, _ := cmd.Flags().GetString("template")
vars, _ := cmd.Flags().GetStringSlice("vars") vars, _ := cmd.Flags().GetStringSlice("vars")
varMap, err := getVarMap(vars) varMap, err := getVarMap(vars)
if err != nil { if err != nil {
return fmt.Errorf("error processing vars: %w") return fmt.Errorf("error processing vars: %w")
} }
// process template sumTmpl, err := template.New("").Parse(summaryTmplStr)
tmpl, err := template.ParseFiles(template)
if err != nil { if err != nil {
ErrorAndExit(cmd, err) 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 { if err != nil {
ErrorAndExit(cmd, err) 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) ticketKey, err := jiraClient.CreateTicket(summary, description)
if err != nil { if err != nil {
ErrorAndExit(cmd, err) ErrorAndExit(cmd, err)
} }
cmd.Printf("Ticket %s successfully created in JIRA") cmd.Printf("Ticket %s successfully created in JIRA", ticketKey)
return nil return nil
} }

22
jira/jira.go Normal file
View file

@ -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
}