This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
forked from coreos/issue-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubfieldmapper.go
267 lines (220 loc) · 7.48 KB
/
githubfieldmapper.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package cfg
import (
"encoding/json"
"errors"
"fmt"
"github.com/andygrunwald/go-jira"
"github.com/indeedeng/issue-sync/lib/models"
"strings"
"time"
)
type FieldMapper interface {
MapFields(issue *models.ExtendedGithubIssue) (jira.IssueFields, error)
GetFieldValue(jIssue *jira.Issue, fieldKey FieldKey) (interface{}, error)
// getFieldIDs requests the metadata of every issue field in the JIRA
// project, and saves the IDs of the custom fields used by issue-sync.
GetFieldIDs(client jira.Client) (map[FieldKey]string, error)
}
type DefaultFieldMapper struct {
Config *Config
}
type JsonFieldMapper struct {
Config *Config
}
type TestFieldMapper struct {
HandleMapFields func(issue *models.ExtendedGithubIssue) (jira.IssueFields, error)
HandleGetFieldValue func(jIssue *jira.Issue, fieldKey FieldKey) (interface{}, error)
HandleGetFieldIDs func(client jira.Client) (map[FieldKey]string, error)
}
// Test Field mapper
func (m TestFieldMapper) MapFields(issue *models.ExtendedGithubIssue) (jira.IssueFields, error) {
return m.HandleMapFields(issue)
}
func (m TestFieldMapper) GetFieldValue(jIssue *jira.Issue, fieldKey FieldKey) (interface{}, error) {
return m.HandleGetFieldValue(jIssue, fieldKey)
}
func (m TestFieldMapper) GetFieldIDs(client jira.Client) (map[FieldKey]string, error) {
return m.HandleGetFieldIDs(client)
}
// Default Field Mapper
func (m DefaultFieldMapper) GetFieldValue(jIssue *jira.Issue, fieldKey FieldKey) (interface{}, error) {
switch fieldKey {
case GitHubID:
fallthrough
case GitHubNumber:
return jIssue.Fields.Unknowns.Int(m.Config.GetCompleteFieldKey(fieldKey))
default:
result, exists := jIssue.Fields.Unknowns.Value(m.Config.GetCompleteFieldKey(fieldKey))
if !exists {
return nil, errors.New("field not found")
}
return result, nil
}
}
func (m DefaultFieldMapper) MapFields(issue *models.ExtendedGithubIssue) (jira.IssueFields, error) {
fields := jira.IssueFields{
Type: jira.IssueType{
Name: "Improvement", // TODO: Decide on issue type
},
Project: m.Config.GetProject(),
Summary: issue.GetTitle(),
Description: issue.GetBody(),
Unknowns: map[string]interface{}{},
}
fields.Unknowns[m.Config.GetCompleteFieldKey(GitHubID)] = issue.GetID()
fields.Unknowns[m.Config.GetCompleteFieldKey(GitHubNumber)] = issue.GetNumber()
fields.Unknowns[m.Config.GetCompleteFieldKey(GitHubStatus)] = issue.GetState()
fields.Unknowns[m.Config.GetCompleteFieldKey(GitHubReporter)] = issue.User.GetLogin()
githubLabels := make([]string, len(issue.Labels))
for i, v := range issue.Labels {
githubLabels[i] = *v.Name
}
fields.Unknowns[m.Config.GetCompleteFieldKey(GitHubLabels)] = strings.Join(githubLabels, ",")
fields.Unknowns[m.Config.GetCompleteFieldKey(LastISUpdate)] = time.Now().Format(DateFormat)
return fields, nil
}
func (m DefaultFieldMapper) GetFieldIDs(client jira.Client) (map[FieldKey]string, error) {
log := m.Config.log
log.Debug("Collecting field IDs.")
req, err := client.NewRequest("GET", "/rest/api/2/field", nil)
if err != nil {
return map[FieldKey]string{}, err
}
jFields := new([]jiraField)
_, err = client.Do(req, jFields)
if err != nil {
return map[FieldKey]string{}, err
}
fieldIDs := map[FieldKey]string{}
for _, field := range *jFields {
switch field.Name {
case "GitHub ID":
fieldIDs[GitHubID] = fmt.Sprint(field.Schema.CustomID)
case "GitHub Number":
fieldIDs[GitHubNumber] = fmt.Sprint(field.Schema.CustomID)
case "GitHub Labels":
fieldIDs[GitHubLabels] = fmt.Sprint(field.Schema.CustomID)
case "GitHub Status":
fieldIDs[GitHubStatus] = fmt.Sprint(field.Schema.CustomID)
case "GitHub Reporter":
fieldIDs[GitHubReporter] = fmt.Sprint(field.Schema.CustomID)
case "Last Issue-Sync Update":
fieldIDs[LastISUpdate] = fmt.Sprint(field.Schema.CustomID)
}
}
_, ok := fieldIDs[GitHubID]
if !ok {
return fieldIDs, errors.New("could not find ID of 'GitHub ID' custom field; check that it is named correctly")
}
_, ok = fieldIDs[GitHubNumber]
if !ok {
return fieldIDs, errors.New("could not find ID of 'GitHub Number' custom field; check that it is named correctly")
}
_, ok = fieldIDs[GitHubLabels]
if !ok {
return fieldIDs, errors.New("could not find ID of 'Github Labels' custom field; check that it is named correctly")
}
_, ok = fieldIDs[GitHubStatus]
if !ok {
return fieldIDs, errors.New("could not find ID of 'Github Status' custom field; check that it is named correctly")
}
_, ok = fieldIDs[GitHubReporter]
if !ok {
return fieldIDs, errors.New("could not find ID of 'Github Reporter' custom field; check that it is named correctly")
}
_, ok = fieldIDs[LastISUpdate]
if !ok {
return fieldIDs, errors.New("could not find ID of 'Last Issue-Sync Update' custom field; check that it is named correctly")
}
log.Debug("All fields have been checked.")
return fieldIDs, nil
}
// Json Field Mapper
func (m JsonFieldMapper) GetFieldValue(jIssue *jira.Issue, fieldKey FieldKey) (interface{}, error) {
var result interface{}
jsonGithubData, err := jIssue.Fields.Unknowns.String(m.Config.GetCompleteFieldKey(GitHubIssueData))
if err != nil {
return nil, err
}
var parsedJson map[string]interface{}
err = json.Unmarshal([]byte(jsonGithubData), &parsedJson)
if err != nil {
return nil, err
}
switch fieldKey {
case GitHubID:
result = int64(parsedJson["githubId"].(float64))
case GitHubNumber:
result = int64(parsedJson["githubNumber"].(float64))
case GitHubLabels:
result = parsedJson["githubLabels"]
case GitHubStatus:
result = parsedJson["githubStatus"]
case GitHubReporter:
result = parsedJson["githubReporter"]
case GitHubCommits:
result = parsedJson["githubCommits"]
if result == nil {
result = make([]interface{}, 0)
}
case LastISUpdate:
result = parsedJson["lastIssueSyncUpdate"]
}
return result, nil
}
func (m JsonFieldMapper) MapFields(issue *models.ExtendedGithubIssue) (jira.IssueFields, error) {
fields := jira.IssueFields{
Type: jira.IssueType{
Name: "Task", // TODO: Determine issue type
},
Project: m.Config.GetProject(),
Summary: issue.GetTitle(),
Description: issue.GetBody(),
Unknowns: map[string]interface{}{},
}
githubLabels := make([]string, len(issue.Labels))
for i, v := range issue.Labels {
githubLabels[i] = *v.Name
}
data := map[string]interface{}{
"githubId": issue.GetID(),
"githubNumber": issue.GetNumber(),
"githubStatus":issue.GetState(),
"githubReporter": issue.User.GetLogin(),
"githubLabels": strings.Join(githubLabels, ","),
"githubCommits": issue.CommitIds,
"lastIssueSyncUpdate": time.Now().Format(DateFormat),
}
j, err := json.Marshal(data)
if err != nil {
return fields, err
}
fields.Unknowns[m.Config.GetCompleteFieldKey(GitHubIssueData)] = string(j)
return fields, nil
}
func (m JsonFieldMapper) GetFieldIDs(client jira.Client) (map[FieldKey]string, error) {
log := m.Config.log
log.Debug("Collecting field IDs.")
req, err := client.NewRequest("GET", "/rest/api/2/field", nil)
if err != nil {
return map[FieldKey]string{}, err
}
jFields := new([]jiraField)
_, err = client.Do(req, jFields)
if err != nil {
return map[FieldKey]string{}, err
}
fieldIDs := map[FieldKey]string{}
for _, field := range *jFields {
switch field.Name {
case "GitHub Issue Data":
fieldIDs[GitHubIssueData] = fmt.Sprint(field.Schema.CustomID)
}
}
_, ok := fieldIDs[GitHubIssueData]
if !ok {
return fieldIDs, errors.New("could not find ID of 'GitHub Issue Data' custom field; check that it is named correctly")
}
log.Debug("All fields have been checked.")
return fieldIDs, nil
}