-
Notifications
You must be signed in to change notification settings - Fork 1
/
jira_issue.go
104 lines (90 loc) · 3.42 KB
/
jira_issue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package message
import "fmt"
type jiraMessage struct {
issue *JiraIssue
Base
}
// JiraIssue requires project and summary to create a real jira issue.
// Other fields depend on permissions given to the specific project, and
// all fields must be legitimate custom fields defined for the project.
// To see whether you have the right permissions to create an issue with certain
// fields, check your JIRA interface on the web.
type JiraIssue struct {
IssueKey string `bson:"issue_key" json:"issue_key" yaml:"issue_key"`
Project string `bson:"project" json:"project" yaml:"project"`
Summary string `bson:"summary" json:"summary" yaml:"summary"`
Description string `bson:"description" json:"description" yaml:"description"`
Reporter string `bson:"reporter" json:"reporter" yaml:"reporter"`
Assignee string `bson:"assignee" json:"assignee" yaml:"assignee"`
Type string `bson:"type" json:"type" yaml:"type"`
Components []string `bson:"components" json:"components" yaml:"components"`
Labels []string `bson:"labels" json:"labels" yaml:"labels"`
FixVersions []string `bson:"versions" json:"versions" yaml:"versions"`
// ... other fields
Fields map[string]interface{} `bson:"fields" json:"fields" yaml:"fields"`
Callback func(string) `bson:"-" json:"-" yaml:"-"`
}
// JiraField is a struct composed of a key-value pair.
type JiraField struct {
Key string
Value interface{}
}
// MakeJiraMessage creates a jiraMessage instance with the given JiraIssue.
func MakeJiraMessage(issue *JiraIssue) Composer {
return &jiraMessage{
issue: issue,
}
}
// NewJiraMessage creates and returns a fully formed jiraMessage, which implements
// message.Composer. project string and summary string are required, and any
// number of additional fields may be included. Fields with keys Reporter, Assignee,
// Type, and Labels will be specifically assigned to respective fields in the new
// jiraIssue included in the jiraMessage, (e.g. JiraIssue.Reporter, etc), and
// all other fields will be included in jiraIssue.Fields.
func NewJiraMessage(project, summary string, fields ...JiraField) Composer {
issue := JiraIssue{
Project: project,
Summary: summary,
Fields: map[string]interface{}{},
}
// Assign given fields to jira issue fields
for _, f := range fields {
switch f.Key {
case "reporter", "Reporter":
issue.Reporter = f.Value.(string)
case "assignee", "Assignee":
issue.Assignee = f.Value.(string)
case "type", "Type":
issue.Type = f.Value.(string)
case "labels", "Labels":
issue.Labels = f.Value.([]string)
case "component", "Component":
issue.Components = f.Value.([]string)
default:
issue.Fields[f.Key] = f.Value
}
}
// Setting "Task" as the default value for IssueType
if issue.Type == "" {
issue.Type = "Task"
}
return MakeJiraMessage(&issue)
}
func (m *jiraMessage) String() string { return m.issue.Summary }
func (m *jiraMessage) Raw() interface{} { return m.issue }
func (m *jiraMessage) Loggable() bool { return m.issue.Summary != "" && m.issue.Type != "" }
func (m *jiraMessage) Annotate(k string, v interface{}) error {
if m.issue.Fields == nil {
m.issue.Fields = map[string]interface{}{}
}
value, ok := v.(string)
if !ok {
return fmt.Errorf("value %+v for key %s is not a string, which is required for jira fields",
k, v)
}
if _, ok := m.issue.Fields[k]; ok {
return fmt.Errorf("value %s already exists", k)
}
m.issue.Fields[k] = value
return nil
}