ln.build/ln.build.server/Program.cs

91 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using ln.http;
using ln.http.router;
using ln.json;
using ln.logging;
using ln.type;
using ln.threading;
using ln.build;
namespace ln.build.server
{
class Program
{
static CIService CIService;
static HttpResponse WebHookRequest(HttpRoutingContext context, HttpRequest request)
{
HttpResponse response = new HttpResponse(request);
if (!request.Method.Equals("POST"))
{
response.StatusCode = 405;
} else if (!request.GetRequestHeader("content-type").Equals("application/json"))
{
response.StatusCode = 415;
response.ContentWriter.WriteLine("Unsupported Media Type, should be application/json");
} else {
JSONValue jsonRequest = JSONParser.Parse(request.ContentReader.ReadToEnd());
if (jsonRequest is JSONObject message)
{
try
{
string cloneUrl = message["repository"]["clone_url"].ToNative().ToString();
foreach (JSONValue commit in (message["commits"] as JSONArray).Children)
{
string commitID = commit["id"].ToNative().ToString();
Logging.Log("Received CI request of repository {0} for commit {1}", cloneUrl, commitID);
CIJob job = new CIJob(cloneUrl)
.SetVariable("REPO_OWNER", message["repository"]["owner"]["username"].ToNative().ToString())
.SetVariable("REPO_NAME", message["repository"]["name"].ToNative().ToString())
.SetVariable("COMMIT_ID", commitID)
.SetVariable("NOTIFY", message["pusher"]["email"].ToNative().ToString());
job.UpdateBuildState(BuildState.PENDING);
CIService.Enqueue(job);
}
} catch (Exception e)
{
response.StatusCode = 500;
response.StatusMessage = "An exception occured";
response.ContentWriter.WriteLine("{0}", e.ToString());
Logging.Log(e);
}
} else {
response.StatusCode = 400;
}
}
return response;
}
static void Main(string[] args)
{
CIService = new CIService();
CIService.Start();
CIService.AddPipeline(new DotNetPipeLine());
SimpleRouter genericRouter = new SimpleRouter();
genericRouter.AddSimpleRoute("/", WebHookRequest);
HTTPServer httpServer = new HTTPServer(new Endpoint(IPv6.ANY, 1888), new LoggingRouter(genericRouter));
Logging.Log("Starting http listener...");
httpServer.Start();
}
}
}