-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
341 lines (304 loc) · 12.9 KB
/
Form1.cs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JiraDemo
{
public partial class Form1 : Form
{
private readonly string baseUrl = "https://YOURJIRADOMAIN.atlassian.net/rest/api/3";
string email = string.Empty;
string apikey = string.Empty;
string basicAuth = string.Empty;
List<IssueModel> EpicList = new List<IssueModel>();
List<IssueModel> StoryList = new List<IssueModel>();
List<IssueModel> TaskList = new List<IssueModel>();
List<IssueModel> SubtaskList = new List<IssueModel>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//example project name
textBox1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
//you can get these parameters from app ui
email = "YOUR_EMAIL";
apikey = "YOUR_TOKEN";
SetToken();
GetIssueListByProject(textBox1.Text);
}
public void GetIssueListByProject(string project)
{
var maxCount = 0;
var totalCount = 10;
var issueList = new List<Issue>();
//Jira allows you max 100 record per request so we need this while section to get all record from
while (maxCount < totalCount)
{
var client = new RestClient(baseUrl + "/search?jql=project=" + project + "&maxResults=10&startAt=" + maxCount);
var request = new RestRequest
{
Method = Method.GET,
RequestFormat = DataFormat.Json
};
request.AddHeader("Authorization", "Basic " + basicAuth);
var response = client.Execute(request);
var result = response.Content;
var issueResponse = JsonConvert.DeserializeObject<IssueResponse>(result);
issueList.AddRange(issueResponse.issues);
totalCount = issueResponse.total;
maxCount = maxCount + 10;
}
#region epic
//epic is the 1. level of jira hierarchy
var epicList = issueList.Where(x => x.fields.issuetype.name == "Epic").ToList();
foreach (var epic in epicList)
{
var newModel = new IssueModel();
newModel.Id = epic.id;
newModel.Key = epic.key;
newModel.Self = epic.self;
newModel.Description = epic.fields.issuetype.description;
newModel.ProjectName = project;
newModel.Release = epic.fields.fixVersions[0].name;
newModel.IssueType = epic.fields.issuetype.name;
newModel.EpicId = 0;
newModel.EpicTitle = "-";
newModel.StoryId = 0;
newModel.StoryTitle = "-";
newModel.TaskId = 0;
newModel.TaskTitle = "-";
newModel.SubtaskId = 0;
newModel.SubtaskTitle = "-";
newModel.AssignedTo = "-";
newModel.OriginalEstimate = 0;
newModel.TimeLogged = 0;
EpicList.Add(newModel);
}
#endregion
#region story
//story is 2. hierarchy, below epic.
var storyList = issueList.Where(x => x.fields.issuetype.name == "Story").ToList();
foreach (var story in storyList)
{
var newModel = new IssueModel();
newModel.Id = story.id;
newModel.Key = story.key;
newModel.Self = story.self;
newModel.Description = story.fields.issuetype.description;
newModel.ProjectName = project;
newModel.Release = SetRelease(story);
newModel.IssueType = story.fields.issuetype.name;
newModel.EpicId = SetEpic(story).Id;
newModel.EpicTitle = EpicList.FirstOrDefault(x => x.Id == newModel.EpicId).Description;
newModel.StoryId = 0;
newModel.StoryTitle = "-";
newModel.TaskId = 0;
newModel.TaskTitle = "-";
newModel.SubtaskId = 0;
newModel.SubtaskTitle = "-";
newModel.AssignedTo = "-";
newModel.OriginalEstimate = 0;
newModel.TimeLogged = 0;
StoryList.Add(newModel);
}
#endregion
#region task
//task is 3. hierarchy but not always belongs to story, sometimes belongs to epic so that we create SetEpic and SetStory methods below
var taskList = issueList.Where(x => x.fields.issuetype.name == "Task").ToList();
foreach (var task in taskList)
{
var newModel = new IssueModel();
newModel.Id = task.id;
newModel.Key = task.key;
newModel.Self = task.self;
newModel.Description = task.fields.issuetype.description;
newModel.ProjectName = project;
newModel.Release = SetRelease(task);
newModel.IssueType = task.fields.issuetype.name;
newModel.EpicId = SetEpic(task)?.Id;
newModel.EpicTitle = EpicList.FirstOrDefault(x => x.Id == newModel.EpicId)?.Description;
newModel.StoryId = SetStory(task)?.Id;
newModel.StoryTitle = StoryList.FirstOrDefault(x => x.Id == SetStory(task).Id)?.Description;
newModel.TaskId = task.id;
newModel.TaskTitle = task.fields.summary;
newModel.SubtaskId = 0;
newModel.SubtaskTitle = "-";
newModel.AssignedTo = SetAssignee(task);
newModel.OriginalEstimate = task.fields.timeoriginalestimate ?? 0;
newModel.TimeLogged = Convert.ToInt32(task.fields.aggregatetimespent) / 60;
TaskList.Add(newModel);
}
#endregion
#region subtask
//lowest level of hierarchy
var subtaskList = issueList.Where(x => x.fields.issuetype.name == "Subtask").ToList();
foreach (var subtask in subtaskList)
{
var newModel = new IssueModel();
newModel.Id = subtask.id;
newModel.Key = subtask.key;
newModel.Self = subtask.self;
newModel.Description = subtask.fields.issuetype.description;
newModel.ProjectName = project;
newModel.Release = SetRelease(subtask);
newModel.IssueType = subtask.fields.issuetype.name;
newModel.EpicId = SetEpic(subtask).Id;
newModel.EpicTitle = EpicList.FirstOrDefault(x => x.Id == newModel.EpicId).Description;
newModel.StoryId = SetStory(subtask).Id;
newModel.StoryTitle = StoryList.FirstOrDefault(x => x.Id == SetStory(subtask).Id)?.Description;
newModel.TaskId = subtask.id;
newModel.TaskTitle = subtask.fields.summary;
newModel.SubtaskId = 0;
newModel.SubtaskTitle = subtask.fields.summary;
newModel.AssignedTo = SetAssignee(subtask);
newModel.OriginalEstimate = subtask.fields.timeoriginalestimate ?? 0;
newModel.TimeLogged = Convert.ToInt32(subtask.fields.aggregatetimespent) / 60;
SubtaskList.Add(newModel);
}
#endregion
var resultList = new List<IssueModel>();
resultList.AddRange(EpicList);
resultList.AddRange(StoryList);
resultList.AddRange(TaskList);
resultList.AddRange(SubtaskList);
var jsonresult = JsonConvert.SerializeObject(resultList);
}
public string SetAssignee(Issue issue)
{
var assisgnee = string.Empty;
if (issue.fields.assignee != null && !string.IsNullOrEmpty(issue.fields.assignee.displayName))
{
assisgnee = issue.fields.assignee.displayName;
}
return assisgnee;
}
public IssueModel SetStory(Issue issue)
{
var story = new IssueModel();
var type = issue.fields.issuetype.name;
if (type == "Task")
{
var parentType = issue.fields.parent.fields.issuetype.name;
if (parentType == "Story")
{
story = StoryList.FirstOrDefault(y => y.Id == issue.fields.parent.id);
}
}
else if (type == "Subtask")
{
var parentType = issue.fields.parent.fields.issuetype.name;
if (parentType == "Story")
{
story = StoryList.FirstOrDefault(y => y.Id == issue.fields.parent.id);
}
else if (parentType == "Task")
{
story = StoryList.FirstOrDefault(x => x.Id == TaskList.FirstOrDefault(y => y.Id == issue.fields.parent.id)?.StoryId) ?? new IssueModel();
}
}
return story;
}
public IssueModel SetEpic(Issue issue)
{
var epic = new IssueModel();
var type = issue.fields.issuetype.name;
if (type == "Story")
{
epic = EpicList.FirstOrDefault(x => x.Id == issue.fields.parent.id);
}
else if (type == "Task")
{
var parentType = issue.fields.parent.fields.issuetype.name;
if (parentType == "Epic")
{
epic = EpicList.FirstOrDefault(x => x.Id == issue.fields.parent.id);
}
else if (parentType == "Story")
{
epic = EpicList.FirstOrDefault(x => x.Id == StoryList.FirstOrDefault(y => y.Id == issue.fields.parent.id).Id);
}
}
else if (type == "Subtask")
{
var parentType = issue.fields.parent.fields.issuetype.name;
if (parentType == "Epic")
{
epic = EpicList.FirstOrDefault(x => x.Id == issue.fields.parent.id);
}
else if (parentType == "Story")
{
epic = EpicList.FirstOrDefault(x => x.Id == StoryList.FirstOrDefault(y => y.Id == issue.fields.parent.id).EpicId);
}
else if (parentType == "Task")
{
epic = EpicList.FirstOrDefault(x => x.Id == TaskList.FirstOrDefault(y => y.Id == issue.fields.parent.id).EpicId);
}
}
return epic;
}
public string SetRelease(Issue issue)
{
var release = string.Empty;
var type = string.Empty;
if (issue.fields.parent == null && issue.fields.issuetype.name == "Epic")
{
type = "Epic";
}
else
{
type = issue.fields.issuetype.name;
}
if (type == "Epic")
{
release = issue.fields.fixVersions[0].name;
}
else if (type == "Story")
{
release = EpicList.FirstOrDefault(x => x.Id == issue.fields.parent.id).Release;
}
else if (type == "Task")
{
var parentType = issue.fields.parent.fields.issuetype.name;
if (parentType == "Epic")
{
release = EpicList.FirstOrDefault(x => x.Id == issue.fields.parent.id).Release;
}
else if (parentType == "Story")
{
release = StoryList.FirstOrDefault(x => x.Id == issue.fields.parent.id).Release;
}
}
else if (type == "Subtask")
{
var parentType = issue.fields.parent.fields.issuetype.name;
if (parentType == "Epic")
{
release = EpicList.FirstOrDefault(x => x.Id == issue.fields.parent.id).Release;
}
else if (parentType == "Story")
{
release = StoryList.FirstOrDefault(x => x.Id == issue.fields.parent.id).Release;
}
else if (parentType == "Task")
{
release = TaskList.FirstOrDefault(x => x.Id == issue.fields.parent.id).Release;
}
}
return release;
}
//this is token that you need to connect to Jira
public void SetToken()
{
basicAuth = Convert.ToBase64String(Encoding.ASCII.GetBytes(email + ":" + apikey));
}
}
}