-
Notifications
You must be signed in to change notification settings - Fork 1
/
jiraGetIssues.go
264 lines (229 loc) · 9.07 KB
/
jiraGetIssues.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
// JiraObject is the first layer of jql search
type JiraObject struct { //xxxx
Expend string `json:"expend"`
StartAt int `json:"startAt"`
MaxResults int `json:"maxResults"`
Total int `json:"total"`
Issues []JiraIssues `json:"issues"`
}
// JiraIssues is for JiraObject Issues
type JiraIssues struct {
Expend string `json:"expend"`
ID string `json:"id"`
Self string `json:"self"`
Key string `json:"key"`
Fields JiraFields `json:"fields"`
}
// JiraIssuesChangeLog is the feedback json of specific jira ticket
type JiraIssuesChangeLog struct {
Expend string `json:"expend"`
ID string `json:"id"`
Self string `json:"self"`
Key string `json:"key"`
Fields JiraFields `json:"fields"`
ChangeLog JiraIssuesChangeLogData `json:"changelog"`
}
// JiraFields is the field structure for JIRA issues
type JiraFields struct {
Summary string `json:"summary"`
Progress interface{} `json:"progress"`
IssueType JiraFieldsIssueType `json:"issuetype"`
Votes interface{} `json:"votes"`
Resolution JiraFieldsResolution `json:"resolution"`
FixVersion []JiraFieldsFixVersions `json:"fixVersions"`
ResoluationDate string `json:"resolutiondate"`
TimeSpent int `json:"timespent"`
Reporter JiraFieldsReporter `json:"reporter"`
AggregateTimeOriginalEstimate int `json:"aggregatetimeoriginalestimate"`
Updated string `json:"updated"`
Created string `json:"created"`
Description string `json:"description"`
Priority JiraFieldsPriority `json:"priority"`
DueDate string `json:"duedate"`
Status JiraFieldsStatus `json:"status"`
Labels []string `json:"labels"`
Assignee JiraFieldsReporter `json:"assignee"`
Project JiraFieldsProject `json:"project"`
Version []JiraFieldsVersions `json:"versions"`
Components []JiraFieldsResolution `json:"components"`
}
// JiraFieldsVersions is the fields of jira versions
type JiraFieldsVersions struct {
Self string `json:"self"`
ID string `json:"id"`
Name string `json:"name"`
Archived bool `json:"archived"`
Released bool `json:"released"`
ReleaseDate string `json:"releaseDate"`
}
// JiraFieldsProject is the filds of Jira Project
type JiraFieldsProject struct {
Self string `json:"self"`
ID string `json:"id"`
Key string `json:"key"`
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
AvatarUrls interface{} `json:"avatarUrls"`
}
// JiraFieldsStatus is the fiedls of Jira Status
type JiraFieldsStatus struct {
Self string `json:"self"`
Description string `json:"description"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
ID string `json:"id"`
}
// JiraFieldsPriority is the fields of priority
type JiraFieldsPriority struct {
Self string `json:"self"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
ID string `json:"id"`
}
//JiraFieldsReporter is the fields of Jira Reporter
type JiraFieldsReporter struct {
Self string `json:"self"`
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
AvatarUrls interface{} `json:"avatarUrls"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
}
// JiraFieldsResolution is the fields of Jira Resolution
type JiraFieldsResolution struct {
Self string `json:"self"`
ID string `json:"id"`
Description string `json:"description"`
Name string `json:"name"`
}
// JiraFieldsIssueType is the fields of Issue Type
type JiraFieldsIssueType struct {
Self string `json:"self"`
ID string `json:"id"`
Description string `json:"description"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
SubTask bool `json:"subtask"`
}
// JiraFieldsFixVersions is the fields of Fix Versions
type JiraFieldsFixVersions struct {
Self string `json:"self"`
ID string `json:"id"`
Name string `json:"name"`
Archievd bool `json:"archievd"`
Released bool `json:"released"`
ResolutionDate string `json:"resolutiondate"`
TimeSpent string `json:"timespent"`
}
// JiraIssuesChangeLogData is the fiedls of change log data
type JiraIssuesChangeLogData struct {
StartAt int `json:"startAt"`
MaxResults int `json:"maxResult"`
Total int `json:"total"`
Histories []JiraIssuesChangeLogDataHistories `json:"histories"`
}
// JiraIssuesChangeLogDataHistories is the fields of data history
type JiraIssuesChangeLogDataHistories struct {
ID string `json:"id"`
Author JiraIssuesChangeLogDataHistoriesAuthor `json:"author"`
Created string `json:"created"`
Items []JiraIssuesChangeLogDataHistoriesItems `json:"items"`
}
// JiraIssuesChangeLogDataHistoriesAuthor is the fields of Author
type JiraIssuesChangeLogDataHistoriesAuthor struct {
Self string `json:"self"`
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
AvatarUrls interface{} `json:"avatarUrls"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
}
// JiraIssuesChangeLogDataHistoriesItems is the fields of Items
type JiraIssuesChangeLogDataHistoriesItems struct {
Field string `json:"field"`
FieldType string `json:"fieldtype"`
From string `json:"from"`
FromString string `json:"fromString"`
To string `json:"to"`
ToString string `json:"toString"`
}
type objectErrorMessage struct {
ErrorMessages []string `json:"errorMessages"`
Errors interface{} `json:"errors"`
}
func main() {
jiraweb := flag.String("jiraweb", "", "Jira Web Site Address, example : -jiraweb=https://inhouse.htcstudio.com/jira")
projectname := flag.String("projectname", "", "Project Name, example : -projectname=TYGH")
username := flag.String("username", "", "User Name")
password := flag.String("password", "", "Password")
flag.Parse()
if flag.NFlag() == 4 {
client := &http.Client{}
getProjectIssueList := fmt.Sprintf("%s/rest/api/2/search?jql=project=%s&maxResults=-1", *jiraweb, *projectname)
req, err := http.NewRequest("GET", getProjectIssueList, nil)
req.SetBasicAuth(*username, *password)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
// Clone http response
var bodyBytes []byte
if resp.Body != nil {
bodyBytes, _ = ioutil.ReadAll(resp.Body)
}
respError := ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
resp.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
var jiraObject JiraObject
errorMessage := json.NewDecoder(resp.Body).Decode(&jiraObject)
if errorMessage != nil {
log.Fatal(errorMessage)
}
// If value of Total it seems Project name is error.
if jiraObject.Total == 0 {
var errorMessageObject objectErrorMessage
json.NewDecoder(respError).Decode(&errorMessageObject)
for x := range errorMessageObject.ErrorMessages {
fmt.Println(errorMessageObject.ErrorMessages[x])
}
log.Fatal("Issue of Project is empty")
}
fmt.Println("Total : ", jiraObject.Total)
fmt.Println("MaxResult : ", jiraObject.MaxResults)
for x := range jiraObject.Issues {
Key := jiraObject.Issues[x].Key
fmt.Println("Issue TYPE", jiraObject.Issues[x].Fields.IssueType.Name)
fmt.Println(Key)
changeLogCommand := fmt.Sprintf("%s/rest/api/2/issue/%s?expand=changelog", *jiraweb, Key)
reqChangeLog, err := http.NewRequest("GET", changeLogCommand, nil)
reqChangeLog.SetBasicAuth(*username, *password)
reqChangeLog.Header.Add("Content-Type", "application/json")
respChangeLog, err := client.Do(reqChangeLog)
if err != nil {
log.Fatal(err)
}
var jiraChangeLog JiraIssuesChangeLog
json.NewDecoder(respChangeLog.Body).Decode(&jiraChangeLog)
for y := range jiraChangeLog.ChangeLog.Histories {
ChangeLogID := jiraChangeLog.ChangeLog.Histories[y].ID
ChangeLogCreated := jiraChangeLog.ChangeLog.Histories[y].Created
fmt.Printf(" ==> ID -> %s , Created -> %s \n", ChangeLogID, ChangeLogCreated)
}
}
} else {
CommandName := strings.Split(os.Args[0], "/")
fmt.Printf("Please chekc help file by \" %s -h \" \n", CommandName[len(CommandName)-1])
}
}