forked from StudioWhyNot/AgileBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtlassianClient.cs
235 lines (212 loc) · 7.89 KB
/
AtlassianClient.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
using Atlassian.Jira;
using Discord;
using Discord.WebSocket;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AgileBot.Models;
namespace AgileBot
{
public static class JiraStatus
{
public const string ToDo = "To Do";
public const string OpenBug = "Open Bug";
public const string InProgress = "In Progress";
public const string InReview = "In Review";
public const string Done = "Done";
}
public static class JiraIssueType
{
public const string Task = "Task";
public const string Bug = "Bug";
public const string Epic = "Epic";
}
public class AtlassianClient
{
public static Color JiraBlue => new Color(0, 82, 204);
public AtlassianSettings Settings { get; private set; }
public Jira JiraClient { get; private set; }
public AtlassianClient(AtlassianSettings settings)
{
Settings = settings;
JiraRestClientSettings set = new JiraRestClientSettings();
set.EnableRequestTrace = true;
JiraClient = Jira.CreateOAuthRestClient(Settings.JiraURL, Settings.ConsumerKey, Settings.ConsumerSecret, Settings.OAuthAccessToken, Settings.OAuthAccessSecret, settings: set);
}
public async Task<bool> TryExecuteJiraCommand(string[] command, SocketUserMessage e)
{
var key = command.FirstOrDefault();
if(key.Contains("-"))
{
return await PostJiraTask(key, e);
}
else
{
return await GetJiraProject(key, e);
}
}
public async Task<bool> GetJiraProject(string key, SocketUserMessage e)
{
key = key.ToUpper();
Project proj;
try
{
proj = await JiraClient.Projects.GetProjectAsync(key);
}
catch
{
return false;
}
await e.Channel.TriggerTypingAsync();
//Need custom REST request to get project description.
var projectInfo = await GetProjectInfo(key);
var boards = await GetAllBoards();
var board = boards?.values.FirstOrDefault(b => b.name.StartsWith(proj.Name));
string url;
if(board != null)
{
url = $"{JiraClient.Url}secure/RapidBoard.jspa?rapidView={board.id}&projectKey={key}";
}
else
{
url = $"{JiraClient.Url}projects/{key}";
}
var file = JiraClient.RestClient.DownloadData(proj.AvatarUrls.Large);
using(MemoryStream memStream = new MemoryStream())
{
memStream.Write(file, 0, file.Length);
memStream.Seek(0, SeekOrigin.Begin);
var avatarFileName = "avatar.png";
var builder = new EmbedBuilder();
builder.WithAuthor(proj.Key, url:url);
builder.WithThumbnailUrl($"attachment://{avatarFileName}");
builder.WithTitle(proj.Name);
builder.WithUrl(url);
builder.WithDescription(projectInfo?.description);
builder.WithColor(JiraBlue);
await e.Channel.SendFileAsync(memStream, avatarFileName, embed: builder.Build());
}
return true;
}
public async Task<bool> PostJiraTask(string key, SocketUserMessage e)
{
var issue = await GetJiraTask(key);
if(issue == null) return false;
await e.Channel.TriggerTypingAsync();
await e.Channel.SendMessageAsync(embed: GetEmbedForJiraIssue(issue));
return true;
}
public async Task<Issue?> GetJiraTask(string key)
{
Issue? issue = null;
try
{
issue = await JiraClient.Issues.GetIssueAsync(key);
}
catch
{
}
return issue;
}
public string GetURLForIssue(ComparableString key)
{
return $"{AgileBot.Atlassian.JiraClient.Url}browse/{key}";
}
public async Task<List<Issue>> GetAllJiraIssuesForUser(string jql, int maxIssues = 10)
{
var issues = await JiraClient.Issues.GetIssuesFromJqlAsync(jql, maxIssues);
return issues.ToList();
}
public EmbedBuilder GetEmbedBuilderForJiraIssue(Issue issue)
{
var builder = new EmbedBuilder();
var user = issue.AssigneeUser;
var assignee = string.Empty;
string? assigneeAvatarURL = null;
if(user != null)
{
assignee = user.DisplayName;
assigneeAvatarURL = user.AvatarUrls.Large;
}
string? reporterAvatarURL = null;
user = issue.ReporterUser;
var reporter = string.Empty;
if(user != null)
{
reporter = user.DisplayName;
reporterAvatarURL = user.AvatarUrls.Large;
}
///TODO: Fetch image urls/attachments and embed them
//var attachments = await issue.GetAttachmentsAsync();
//foreach(var attach in attachments)
//{
// //if(attach.MimeType == )
//}
var url = $"{JiraClient.Url}browse/{issue.Key}";
builder.WithAuthor(assignee, assigneeAvatarURL, url);
builder.WithTitle($"{issue.Key} - {issue.Status}");
builder.AddField(issue.Summary, string.IsNullOrEmpty(issue.Description?.Trim()) ? "\u200b" : issue.Description);
builder.WithFooter(reporter, reporterAvatarURL);
builder.WithUrl(url);
if(issue.Created != null) builder.WithTimestamp(new DateTimeOffset(issue.Created.Value));
builder.Color = JiraBlue;
return builder;
}
public Embed GetEmbedForJiraIssue(Issue issue)
{
return GetEmbedBuilderForJiraIssue(issue).Build();
}
public async Task<ProjectInfo?> GetProjectInfo(string projectKey)
{
var url = $"{JiraClient.Url}rest/api/2/project/{projectKey}";
var restRequest = new RestRequest(url, Method.GET, DataFormat.Json);
try
{
var resp = await JiraClient.RestClient.ExecuteRequestAsync(restRequest);
var respData = JsonConvert.DeserializeObject<ProjectInfo>(resp.Content);
return respData;
}
catch(Exception except)
{
Console.WriteLine($"Error: {except.Message}");
}
return null;
}
public async Task<IssueStatus?> GetStatus(string status)
{
try
{
return await JiraClient.Statuses.GetStatusAsync(status);
}
catch
{
return null;
}
}
public async Task<ListBoardsResponseInfo?> GetAllBoards()
{
ListBoardsResponseInfo? respData = null;
var url = $"{JiraClient.Url}rest/agile/latest/board/";
var restRequest = new RestRequest(url, Method.GET, DataFormat.Json);
try
{
var resp = await JiraClient.RestClient.ExecuteRequestAsync(restRequest);
respData = JsonConvert.DeserializeObject<ListBoardsResponseInfo>(resp.Content);
return respData;
}
catch(Exception except)
{
Console.WriteLine($"Error: {except.Message}");
}
return respData;
}
}
}