-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
128 lines (110 loc) · 4.79 KB
/
main.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
/* (c) 2017 James Renwick */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
namespace cci
{
class Program
{
static Dictionary<string, ProjectDefinition> projects
= new Dictionary<string, ProjectDefinition>();
static List<Tuple<ProjectDefinition, RunResults>> results
= new List<Tuple<ProjectDefinition, RunResults>>();
class RunStatus
{
//public TestResults Results { get; private set; }
public string RepoLink { get; set; }
public bool Completed { get; set; }
}
class RunResponse
{
public string Action { get; private set; } = "run";
public bool Success { get; private set; } = true;
public long BuildReference { get; private set; }
public RunResponse(long runID)
{
BuildReference = runID;
}
}
static void HandleRequest(object sender, RequestReceivedEventArgs e)
{
if (e.Request.Action == "run")
{
var project = projects[e.Request.TaskName];
var runner = new ProjectRunner(project);
int runID;
// Set initial null results & send response
lock (Program.results) {
runID = Program.results.Count;
Program.results.Add(new Tuple<ProjectDefinition, RunResults>(project, null));
}
e.RespondWith(new RunResponse(runID));
// Perform run
Console.WriteLine("[INFO ] Starting Run {0}", runID);
RunResults res = runner.RunAll();
Console.WriteLine("[INFO ] Completed Run {0}", runID);
try { runner.Dispose(); }
catch { }
// Parse test results
var testResults = new List<TestResults>(res.TestResults.Results.Length);
for (int i = 0; i < res.TestResults.Results.Length; i++)
{
var cmdResult = res.TestResults.Results[i];
Console.WriteLine("[INFO ] Writing results for run {0}, test command {1}",
runID, cmdResult.Command.Name ?? i.ToString());
try {
testResults.Add(new OSTestParser().ParseOutput(res.TestResults.Results[i]));
}
catch (Exception x) {
Console.WriteLine("[ERROR] Exception when generating results for "
+ "run {0} test command {1}: {2}", runID, cmdResult.Command.Name ??
i.ToString(), x.ToString());
}
}
// Upload results
try {
new GitResultSaver("results", "[email protected]:jsren/test-results", "master")
.SaveResults(project, res, testResults);
}
catch (Exception x) {
Console.WriteLine("[ERROR] Exception uploading results for run {0}: {1}", runID, x);
}
// Update results
Program.results[runID] = new Tuple<ProjectDefinition, RunResults>(project, res);
Console.WriteLine("[INFO ] Run {0} completed.", runID);
}
else if (e.Request.Action == "status")
{
var buildEntry = Program.results[e.Request.BuildReference];
var taskName = buildEntry.Item1.Title;
var url = Uri.EscapeUriString($"https://github.com/jsren/test-results/blob/master/{taskName}");
e.RespondWith(new RunStatus() {
Completed = buildEntry.Item2 != null,
RepoLink = url
});
}
}
static void Main(string[] args)
{
var project = ProjectDefinition.FromJSON(
System.IO.File.ReadAllText("build.json"));
projects.Add(project.Title, project);
var server = new BuildServer();
server.RequestReceived += HandleRequest;
server.Start(new IPEndPoint(IPAddress.Any, 5274), 5, 0);
Console.WriteLine("[INFO ] Server running.");
bool alive = true;
Console.CancelKeyPress += (s, e) =>
{
Console.WriteLine("[INFO ] Stopping server...");
server.Stop();
Console.WriteLine("[INFO ] Server stopped.");
alive = false;
Environment.Exit(0);
};
while (alive) { Thread.Sleep(1000); }
}
}
}