Allow standalone command to create issues
This commit is contained in:
parent
ab2e28e29e
commit
1b66405f39
6 changed files with 148 additions and 93 deletions
106
jira/jira.go
106
jira/jira.go
|
@ -1,6 +1,13 @@
|
|||
package jira
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
||||
|
||||
jira "gopkg.in/andygrunwald/go-jira.v1"
|
||||
)
|
||||
|
||||
|
@ -8,6 +15,64 @@ type JiraClient struct {
|
|||
*jira.Client
|
||||
}
|
||||
|
||||
func (c *JiraClient) GetIssueFromTicket(ticket *model.Ticket, campaign *model.Campaign) (*jira.Issue, error) {
|
||||
summaryTmpl, err := template.New("").Parse(campaign.Summary)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var summaryBytes bytes.Buffer
|
||||
if err := summaryTmpl.Execute(&summaryBytes, ticket.Data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
summary := summaryBytes.String()
|
||||
|
||||
descriptionTemplate, err := template.ParseFiles(campaign.Template)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var descriptionBytes bytes.Buffer
|
||||
if err := descriptionTemplate.Execute(&descriptionBytes, ticket.Data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
description := descriptionBytes.String()
|
||||
|
||||
data := map[string]string{
|
||||
"Description": description,
|
||||
"Summary": summary,
|
||||
"Project": campaign.Project,
|
||||
"Issue Type": "Story",
|
||||
"Epic Link": campaign.Epic,
|
||||
}
|
||||
|
||||
if team, ok := ticket.Data["team"]; ok {
|
||||
data["team"] = team.(string)
|
||||
}
|
||||
|
||||
createMetaInfo, _, err := c.Issue.GetCreateMeta(campaign.Project)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
project := createMetaInfo.GetProjectWithKey(campaign.Project)
|
||||
if project == nil {
|
||||
return nil, fmt.Errorf("Error retrieving project with key %s", campaign.Project)
|
||||
}
|
||||
|
||||
issueType := project.GetIssueTypeWithName("Story")
|
||||
if issueType == nil {
|
||||
return nil, fmt.Errorf("Error retrieving issue type with name Story")
|
||||
}
|
||||
|
||||
issue, err := jira.InitIssueWithMetaAndFields(project, issueType, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return issue, nil
|
||||
}
|
||||
|
||||
func NewClient(url, username, token string) (*JiraClient, error) {
|
||||
tp := jira.BasicAuthTransport{
|
||||
Username: username,
|
||||
|
@ -22,38 +87,21 @@ func NewClient(url, username, token string) (*JiraClient, error) {
|
|||
return &JiraClient{client}, nil
|
||||
}
|
||||
|
||||
func (c *JiraClient) CreateIssue(epicId, team, summary, description string) (string, error) {
|
||||
/*
|
||||
data := map[string]interface{}{
|
||||
"fields": map[string]interface{}{
|
||||
"project": map[string]interface{}{"key": "MM"},
|
||||
"summary": summary,
|
||||
"description": description,
|
||||
"issuetype": map[string]interface{}{"name": "Story"},
|
||||
"customfield_10007": epicId,
|
||||
"customfield_11101": map[string]interface{}{"value": team},
|
||||
},
|
||||
}
|
||||
func (c *JiraClient) CreateTicket(ticket *model.Ticket, campaign *model.Campaign) (*jira.Issue, error) {
|
||||
issue, err := c.GetIssueFromTicket(ticket, campaign)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b, _ := json.MarshalIndent(issue, "", " ")
|
||||
fmt.Println(string(b))
|
||||
|
||||
res, err := http.DoPost(c.Username, c.Token, c.Url+"issue/", body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
newIssue, _, err := c.Issue.Create(issue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
issue, err := IssueFromJson(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return issue.Key, nil
|
||||
*/
|
||||
return "", nil
|
||||
return newIssue, nil
|
||||
}
|
||||
|
||||
func (c *JiraClient) GetIssue(issueNo string) (*jira.Issue, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue