This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathViewLane.aspx.cs
executable file
·434 lines (366 loc) · 15.4 KB
/
ViewLane.aspx.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* ViewLane.aspx.cs
*
* Authors:
* Rolf Bjarne Kvinge ([email protected])
*
* Copyright 2009 Novell, Inc. (http://www.novell.com)
*
* See the LICENSE file included with the distribution for details.
*
*/
using System;
using System.Collections.Generic;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MonkeyWrench.DataClasses;
using MonkeyWrench.DataClasses.Logic;
using MonkeyWrench.Web.WebServices;
public partial class ViewLane : System.Web.UI.Page
{
GetViewLaneDataResponse response;
static Dictionary<string, string> protectedBranches = new Dictionary<string, string>();
private new Master Master
{
get { return base.Master as Master; }
}
string ParseRepo(string repo)
{
var regex = new Regex(@"github\.com[:/](.*)$");
var result = regex.Match(repo).Groups[1].Value;
return result.Replace(".git", "");
}
string ParseBranch(string branch)
{
return branch.Split('/').Last();
}
// TODO: kill this
bool IsBranchProtected(DBLane lane)
{
if (!lane.repository.Contains("github")) return false;
var repo = ParseRepo(lane.repository);
var branch = ParseBranch(lane.max_revision);
var key = repo + ":" + branch;
var token = Session["github_token"];
var url = "https://api.github.com/repos/" + repo + "/branches/" + branch + "/protection";
var client = WebRequest.Create(url) as HttpWebRequest;
client.Accept = "application/vnd.github.loki-preview+json";
client.ContentType = "application/json";
client.Method = WebRequestMethods.Http.Get;
client.PreAuthenticate = true;
client.UserAgent = "app";
client.Headers.Add("Authorization", "token " + token);
if (protectedBranches.ContainsKey(key)) client.Headers.Add("If-None-Match", protectedBranches[key]);
try {
var resp = client.GetResponse() as HttpWebResponse;
if (resp.Headers.AllKeys.Contains("Etag"))
protectedBranches.Add(key, resp.Headers["Etag"]);
return resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.NotModified;
} catch (WebException) {
return false;
}
}
protected override void OnLoad (EventArgs e)
{
base.OnLoad (e);
string action = null;
int id;
if (Request ["commit"] == null) {
response = Utils.LocalWebService.GetViewLaneData2 (Master.WebServiceLogin,
Utils.TryParseInt32 (Request ["lane_id"]), Request ["lane"],
Utils.TryParseInt32 (Request ["host_id"]), Request ["host"],
Utils.TryParseInt32 (Request ["revision_id"]), Request ["revision"], false);
} else {
response = Utils.LocalWebService.GetViewLaneData3 (Master.WebServiceLogin,
Request ["lane"],
Request ["commit"]);
}
if (response.Exception != null) {
if (response.Exception.HttpCode == 403) {
Master.RequestLogin ();
return;
}
lblMessage.Text = response.Exception.Message;
return;
}
if (Authentication.IsInRole (response, MonkeyWrench.DataClasses.Logic.Roles.Administrator))
action = Request ["action"];
DBHost host = response.Host;
DBLane lane = response.Lane;
DBRevision revision = response.Revision;
if (lane == null || host == null || revision == null) {
Response.Redirect ("index.aspx", false);
return;
}
if (!string.IsNullOrEmpty (action)) {
switch (action) {
case "clearrevision":
Utils.LocalWebService.ClearRevision (Master.WebServiceLogin, lane.id, host.id, revision.id);
break;
case "deleterevision":
Utils.LocalWebService.RescheduleRevision (Master.WebServiceLogin, lane.id, host.id, revision.id);
break;
case "ignorerevision":
Utils.LocalWebService.IgnoreRevision (Master.WebServiceLogin, lane.id, host.id, revision.id);
break;
case "abortrevision":
Utils.LocalWebService.AbortRevision (Master.WebServiceLogin, lane.id, host.id, revision.id);
break;
case "clearstep":
if (int.TryParse (Request ["work_id"], out id))
Utils.LocalWebService.ClearWork (Master.WebServiceLogin, id);
break;
case "abortstep":
if (int.TryParse (Request ["work_id"], out id))
Utils.LocalWebService.AbortWork (Master.WebServiceLogin, id);
break;
case "pausestep":
if (int.TryParse (Request ["work_id"], out id))
Utils.LocalWebService.PauseWork (Master.WebServiceLogin, id);
break;
case "resumestep":
if (int.TryParse (Request ["work_id"], out id))
Utils.LocalWebService.ResumeWork (Master.WebServiceLogin, id);
break;
}
Response.Redirect (string.Format ("ViewLane.aspx?lane_id={0}&host_id={1}&revision_id={2}", lane.id, host.id, revision.id), false);
Page.Visible = false;
return;
}
header.InnerHtml = GenerateHeader (response, lane, host, revision, "Build of");
buildtable.InnerHtml = GenerateLane (response);
}
public static string GenerateHeader (GetViewLaneDataResponse response, DBLane lane, DBHost host, DBRevision revision, string description)
{
if (!Authentication.IsInRole (response, MonkeyWrench.DataClasses.Logic.Roles.Administrator)) {
return string.Format (@"
<h2>{4} revision <a href='ViewLane.aspx?lane_id={0}&host_id={1}&revision_id={6}'>{5}</a> on lane '{2}' on '<a href='ViewHostHistory.aspx?host_id={1}'>{3}</a>'
(<a href='ViewTable.aspx?lane_id={0}&host_id={1}'>table</a>)</h2><br/>", lane.id, host.id, lane.lane, host.host, description, revision.revision, revision.id);
} else {
return string.Format (@"
<h2>{4} revision <a href='ViewLane.aspx?lane_id={0}&host_id={1}&revision_id={6}'>{5}</a> on lane '<a href='EditLane.aspx?lane_id={0}'>{2}</a>' on '<a href='ViewHostHistory.aspx?host_id={1}'>{3}</a>'
(<a href='ViewTable.aspx?lane_id={0}&host_id={1}'>table</a>)</h2><br/>", lane.id, host.id, lane.lane, host.host, description, revision.revision, revision.id);
}
}
string confirmViewLaneAction(DBLane lane, DBHost host, DBRevision dbr, string action, string command)
{
return String.Format("javascript:confirmViewLaneAction (\"ViewLane.aspx?lane_id={0}&host_id={2}&revision_id={1}&action={3}\", \"{4}\");", lane.id, dbr.id, host.id, action, command);
}
void GenerateActionLink(StringBuilder header, DBLane lane, DBHost host, DBRevision dbr, string cmd, string short_label, string long_label, bool hidden = false)
{
var href = confirmViewLaneAction(lane, host, dbr, cmd, short_label);
if (hidden)
header.AppendFormat("- <a style='{0}' href='{1}'>{2}</a>", "display:none", href, long_label);
else
header.AppendFormat("- <a href='{0}'>{1}</a>", href, long_label);
}
public static string CalculateDiffLink (string repository, string revision)
{
if (string.IsNullOrEmpty (repository))
return revision;
string path, hostname;
if (repository.StartsWith ("git@", StringComparison.Ordinal)) {
var colon = repository.IndexOf (':');
if (colon < 4)
return revision;
path = repository.Substring (colon + 1);
hostname = repository.Substring (4, colon - 4);
} else {
Uri uri;
if (!Uri.TryCreate (repository, UriKind.Absolute, out uri))
return revision;
hostname = uri.Host;
path = uri.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
}
if (hostname != "github.com")
return revision;
if (string.IsNullOrEmpty (path))
return revision;
if (string.IsNullOrEmpty (path))
return revision;
var paths = path.Split (new char [] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (paths.Length < 2)
return revision;
// Strip off any trailing '.git'
if (paths [1].EndsWith (".git", StringComparison.Ordinal))
paths [1] = paths [1].Substring (0, paths [1].Length - 4);
return $"<a href='https://github.com/{paths [0]}/{paths [1]}/commit/{revision}'>diff</a>";
}
public string GenerateLane (GetViewLaneDataResponse response)
{
StringBuilder matrix = new StringBuilder ();
List<DBWorkView2> steps = response.WorkViews;
DBRevision dbr = response.Revision;
DBRevisionWork revisionwork = response.RevisionWork;
DBLane lane = response.Lane;
DBHost host = response.Host;
DBRevision revision = response.Revision;
StringBuilder header = new StringBuilder ();
header.AppendFormat ("Revision: {0}", CalculateDiffLink (lane.repository, dbr.revision));
header.AppendFormat (" - Status: {0}", revisionwork.State);
header.AppendFormat (" - Author: {0}", dbr.author);
header.AppendFormat (" - Commit date: {0}", dbr.date.ToString ("yyyy/MM/dd HH:mm:ss UTC"));
if (Authentication.IsInRole (response, Roles.Administrator)) {
bool hidden = IsBranchProtected(lane);
bool isExecuting = revisionwork.State == DBState.Executing || (revisionwork.State == DBState.Issues && !revisionwork.completed) || revisionwork.State == DBState.Aborted;
if (isExecuting) {
GenerateActionLink(header, lane, host, dbr, "clearrevision", "clear", "reset work");
GenerateActionLink(header, lane, host, dbr, "deleterevision", "delete", "delete work");
GenerateActionLink(header, lane, host, dbr, "abortrevision", "abort", "abort work");
} else if (response.RevisionWork.State == DBState.Ignore) {
header.AppendFormat (" - <a href='ViewLane.aspx?lane_id={0}&host_id={2}&revision_id={1}&action=clearrevision'>build this revision</a>", lane.id, dbr.id, host.id);
} else {
if (response.RevisionWork.State == DBState.NotDone)
header.AppendFormat (" - <a href='ViewLane.aspx?lane_id={0}&host_id={2}&revision_id={1}&action=ignorerevision'>don't build</a>", lane.id, dbr.id, host.id);
if (response.RevisionWork.State != DBState.NoWorkYet) {
if (revisionwork.is_protected && (response.RevisionWork.State == DBState.Success || response.RevisionWork.State == DBState.Issues)) {
header.AppendFormat (" - {0}", MonkeyWrench.Configuration.ProtectedMessage);
} else {
GenerateActionLink (header, lane, host, dbr, "clearrevision", "clear", "reset work", hidden);
GenerateActionLink (header, lane, host, dbr, "deleterevision", "delete", "delete work", hidden);
}
}
}
}
if (response.WorkHost != null) {
header.AppendFormat (" - Assigned to <a href='ViewHostHistory.aspx?host_id={1}'>{0}</a>", response.WorkHost.host, response.WorkHost.id);
} else {
header.AppendFormat (" - Unassigned.");
}
if (!revisionwork.completed && revisionwork.State != DBState.NotDone && revisionwork.State != DBState.Paused && revisionwork.State != DBState.Ignore) {
header.Insert (0, "<center><table class='executing'><td>");
header.Append ("</td></table></center>");
}
// matrix.AppendFormat ("<div class='buildstatus {0}'>Status: {0}</div>", revisionwork.State.ToString ().ToLowerInvariant ());
matrix.AppendLine ("<table class='buildstatus'>");
matrix.AppendFormat ("<tr class='{0}'>", revisionwork.State.ToString ().ToLowerInvariant ());
matrix.Append ("<th colspan='9'>");
matrix.Append (header.ToString ());
matrix.Append ("</th>");
matrix.AppendLine ("</tr>");
matrix.AppendLine ("<tr>");
matrix.AppendLine ("\t<th>Step</th>");
matrix.AppendLine ("\t<th>Result</th>");
matrix.AppendLine ("\t<th>Start Time</th>");
matrix.AppendLine ("\t<th>Duration</th>");
matrix.AppendLine ("\t<th>Html report</th>");
matrix.AppendLine ("\t<th>Summary</th>");
matrix.AppendLine ("\t<th>Files</th>");
matrix.AppendLine ("\t<th>Host</th>");
matrix.AppendLine ("\t<th>Misc</th>");
matrix.AppendLine ("</tr>");
bool failed = false;
for (int s = 0; s < steps.Count; s++) {
DBWorkView2 step = steps [s];
DBState state = (DBState) step.state;
bool nonfatal = step.nonfatal;
string command = step.command;
List<DBWorkFileView> files = response.WorkFileViews [s];
IEnumerable<DBFileLink> links = response.Links.Where<DBFileLink> ((DBFileLink link) => link.work_id == step.id);
if (state == DBState.Failed && !nonfatal)
failed = true;
matrix.AppendLine ("<tr>");
// step
DBWorkFileView step_log = files.Find ((v) => v.filename == command + ".log");
matrix.Append ("\t<td>");
if (step_log != null) {
matrix.AppendFormat ("<a href='GetFile.aspx?id={0}'>{1}</a> ", step_log.id, command);
} else {
matrix.Append (command);
}
matrix.Append ("</td>");
// result
string result;
switch (state) {
case DBState.NotDone:
result = failed ? "skipped" : "queued"; break;
case DBState.Executing:
result = "running"; break;
case DBState.Failed:
result = nonfatal ? "issues" : "failure"; break;
case DBState.Success:
case DBState.Aborted:
case DBState.Timeout:
case DBState.Paused:
default:
result = state.ToString ().ToLowerInvariant ();
break;
}
// result
matrix.AppendFormat ("\t<td class='{0}'>{0}</td>", result);
if (state > DBState.NotDone && state != DBState.Paused && state != DBState.Ignore && state != DBState.DependencyNotFulfilled) {
matrix.AppendFormat ("<td>{0}</td>", step.starttime.ToString ("yyyy/MM/dd HH:mm:ss UTC"));
} else {
matrix.AppendLine ("<td>-</td>");
}
// duration
matrix.Append ("\t<td>");
if (state >= DBState.Executing && state != DBState.Paused && state != DBState.Ignore && state != DBState.DependencyNotFulfilled && state != DBState.Aborted) {
matrix.Append ("[");
matrix.Append (MonkeyWrench.Utilities.GetDurationFromWorkView (step).ToString ());
matrix.Append ("]");
} else {
matrix.Append ("-");
}
matrix.AppendLine ("</td>");
// html report
matrix.AppendLine ("<td>");
DBWorkFileView index_html = null;
foreach (DBWorkFileView file in files) {
if (file.filename == "index.html") {
index_html = file;
break;
}
}
if (index_html != null) {
matrix.AppendFormat ("<a href='ViewHtmlReportEmbedded.aspx?workfile_id={0}&lane_id={1}&host_id={2}&revision_id={3}'>View html report</a>", index_html.id, lane.id, host.id, revision.id);
} else {
matrix.AppendLine ("-");
}
matrix.AppendLine ("</td>");
// summary
matrix.AppendLine ("<td>");
matrix.AppendLine (step.summary);
matrix.AppendLine ("</td>");
// files
matrix.AppendLine ("<td style='text-align: left;'>");
var sb = new StringBuilder ();
var file_count = 0;
foreach (DBWorkFileView file in files.Where ((v) => !v.hidden).OrderBy ((v) => v.filename)) {
if (file.hidden)
continue;
file_count++;
sb.AppendFormat ("<a href='GetFile.aspx?id={0}'>{1}</a> <br /> ", file.id, file.filename);
}
foreach (var link in links.OrderBy ((v) => v.link)) {
file_count++;
sb.Append (link.link).Append ("<br />");
}
if (file_count > 3) {
matrix.AppendFormat (
"<a href='#' id='showFiles_{0}' onclick='javascript:document.getElementById (\"files_{0}\").style.display = \"block\"; document.getElementById (\"showFiles_{0}\").style.display = \"none\"; document.getElementById (\"hideFiles_{0}\").style.display = \"block\";'>Show {2} files</a>" +
"<a href='#' id='hideFiles_{0}' onclick='javascript:document.getElementById (\"files_{0}\").style.display = \"none\"; document.getElementById (\"showFiles_{0}\").style.display = \"block\"; document.getElementById (\"hideFiles_{0}\").style.display = \"none\";' style='display: none'>Hide {2} files</a>" +
"<span id='files_{0}' style='display: none'>{1}</span>",
step.id, sb.ToString (), file_count);
} else {
matrix.Append (sb.ToString ());
}
matrix.AppendLine ("</td>");
// host
matrix.AppendFormat ("<td>{0}</td>", step.workhost);
// misc
matrix.AppendFormat ("\t<td><a href='ViewWorkTable.aspx?lane_id={1}&host_id={2}&command_id={3}'>History for '{4}'</a></td>", result, lane.id, host.id, step.command_id, command);
matrix.AppendLine ("</tr>");
}
matrix.AppendLine ("</table>");
return matrix.ToString ();
}
}