-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYouTrackService.cs
301 lines (254 loc) · 10 KB
/
YouTrackService.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
namespace Hbo.Sheepish
{
using Standard;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml.Linq;
// General documentation for the YouTrack REST APIs: http://confluence.jetbrains.com/display/YTD5/YouTrack+REST+API+Reference
public class YouTrackService
{
public class User
{
public string Login { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
}
public class Project
{
public string Name { get; set; }
public string ShortName { get; set; }
public string Description { get; set; }
}
public class IssueSummary
{
public string Id { get; set; }
public string Summary { get; set; }
public override string ToString()
{
return string.Format("({0}) {1}", Id, Summary);
}
}
public class SavedSearch : IEquatable<SavedSearch>
{
public string Name { get; set; }
public string Query { get; set; }
public Project ProjectScope { get; set; }
public string GetAugmentedQuery(string query)
{
var queryBuilder = new StringBuilder();
if (ProjectScope != null)
{
queryBuilder.AppendFormat("project: {0} ", ProjectScope.ShortName);
}
queryBuilder.AppendFormat("{0} {1}", this.Query ?? "", query ?? "");
return queryBuilder.ToString().Trim();
}
public static string GetAugmentedQuery(SavedSearch scope, string query)
{
if (scope == null)
{
return query ?? "";
}
return scope.GetAugmentedQuery(query);
}
#region IEquatable<SavedSearch> implementation
public bool Equals(SavedSearch other)
{
if (other == null)
{
return false;
}
if (other.Name != this.Name || other.Query != this.Query)
{
return false;
}
if (ProjectScope == null)
{
return other.ProjectScope == null;
}
return ProjectScope.Equals(other.ProjectScope);
}
#endregion
public override bool Equals(object obj)
{
return this.Equals(obj as SavedSearch);
}
public override int GetHashCode()
{
return Name.GetHashCode() ^ ((Query ?? "").GetHashCode() << 4) ^ ((ProjectScope == null ? 0 : ProjectScope.GetHashCode()) >> 13);
}
public override string ToString()
{
return Name;
}
}
public static class SavedSearches
{
public static readonly SavedSearch Everything = new SavedSearch { Name = "Everything" };
}
private static readonly string _UserAgentString = "HBO Sheepish Client";
private readonly string _BaseUrl;
private readonly CookieContainer _CookieJar = new CookieContainer();
private const int _MaxRetryCount = 8;
private static readonly TimeSpan _RetryDelay = new TimeSpan(1000);
#region Http Verb Implementations
private static XDocument _Get(string path, CookieContainer jar)
{
var webRequest = (HttpWebRequest)HttpWebRequest.Create(path);
webRequest.Method = "GET";
webRequest.Accept = "application/xml";
webRequest.UserAgent = _UserAgentString;
webRequest.CookieContainer = jar;
WebResponse response = webRequest.GetResponse();
try
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
return XDocument.Parse(reader.ReadToEnd());
}
}
finally
{
response.Close();
}
}
private static XDocument _Post(string path, string postData, CookieContainer jar)
{
var webRequest = (HttpWebRequest)HttpWebRequest.Create(path);
webRequest.Method = "POST";
webRequest.UserAgent = _UserAgentString;
webRequest.Accept = "application/xml";
webRequest.ContentType = "text/plain; charset=utf-8";
webRequest.CookieContainer = jar;
if (!string.IsNullOrEmpty(postData))
{
byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
webRequest.ContentLength = postData.Length;
using (var dataStream = webRequest.GetRequestStream())
{
dataStream.Write(dataBytes, 0, dataBytes.Length);
}
}
else
{
webRequest.ContentLength = 0;
}
var response = webRequest.GetResponse();
try
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
return XDocument.Parse(reader.ReadToEnd());
}
}
finally
{
Utility.SafeDispose(ref response);
}
}
#endregion
public YouTrackService(string baseUri, CookieContainer jar)
{
_BaseUrl = baseUri;
_CookieJar = jar;
}
public void Login(string username, string password)
{
var path = string.Format("{0}/rest/user/login?login={1}&password={2}", _BaseUrl, username, password);
_Post(path, null, _CookieJar);
}
public User GetCurrentUser()
{
var path = string.Format("{0}/rest/user/current", _BaseUrl);
var response = _Get(path, _CookieJar);
return new User
{
Login = response.Element("user").Attribute("login").Value,
Email = response.Element("user").Attribute("email").Value,
FullName = response.Element("user").Attribute("fullName").Value,
};
}
public List<Project> GetProjects()
{
var path = string.Format("{0}/rest/project/all", _BaseUrl);
var response = _Get(path, _CookieJar);
var retList = new List<Project>(
from project in response.Element("projects").Elements() select new Project
{
Name = project.Attribute("name").Value,
ShortName = project.Attribute("shortName").Value,
Description = project.Attribute("description") != null ? project.Attribute("description").Value : "",
});
return retList;
}
public List<SavedSearch> GetSavedSearches()
{
var retList = new List<SavedSearch> { SavedSearches.Everything };
retList.AddRange(from proj in GetProjects() select new SavedSearch
{
Name = proj.Name,
ProjectScope = proj
});
var savedSearchResponse = _Get(string.Format("{0}/rest/user/search", _BaseUrl), _CookieJar);
retList.AddRange(
from savedSearch in savedSearchResponse.Element("savedSearches").Elements()
select new SavedSearch
{
Name = savedSearch.Attribute("name").Value,
Query = savedSearch.Value,
ProjectScope = null,
});
return retList;
}
public int GetIssueCount(SavedSearch scope, string query)
{
for (int i = 0; i < _MaxRetryCount; ++i)
{
var path = string.Format("{0}/rest/issue/count?filter={1}", _BaseUrl, Utility.UrlEncode(SavedSearch.GetAugmentedQuery(scope, query)));
var response = _Get(path, _CookieJar);
int count = int.Parse(response.Root.Value);
if (count != -1)
{
return count;
}
Thread.Sleep(_RetryDelay);
}
throw new Exception("Unable to determine number of issues from server.");
}
public List<IssueSummary> GetRecentlyUpdatedIssues(SavedSearch scope, string queryFilter) {
const int MaxCount = 5;
var path = string.Format("{0}/rest/issue?filter={1}+sort+by%3A+updated&with=summary&max={2}", _BaseUrl, Utility.UrlEncode(SavedSearch.GetAugmentedQuery(scope, queryFilter)), MaxCount);
var response = _Get(path, _CookieJar);
var retList = new List<IssueSummary>(
from issueNode in response.Element("issueCompacts").Elements() select new IssueSummary
{
Id = issueNode.Attribute("id").Value,
Summary = issueNode.Element("field").Element("value").Value
});
return retList;
}
public Uri GetQueryUri(SavedSearch scope, string queryFilter)
{
if (scope == null)
{
return new Uri(string.Format("{0}/issues?q={1}", _BaseUrl, Utility.UrlEncode(queryFilter)));
}
string prefix = _BaseUrl + "/issues";
if (scope.ProjectScope != null)
{
prefix += "/" + scope.ProjectScope.ShortName;
}
string queryPart = (scope.Query ?? "") + " " + queryFilter;
return new Uri(string.Format("{0}?q={1}", prefix, Utility.UrlEncode(queryPart.Trim())));
}
public Uri GetIssueUri(IssueSummary summary)
{
return new Uri(string.Format("{0}/issue/{1}", _BaseUrl, summary.Id));
}
}
}