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

94 lines
3.2 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 Pool pool = new Pool(2);
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 repoName = message["repository"]["name"].ToNative().ToString();
string cloneUrl = message["repository"]["clone_url"].ToNative().ToString();
string notifyEmail = message["pusher"]["email"].ToNative().ToString();
foreach (JSONValue commit in (message["commits"] as JSONArray).Children)
{
string commitID = commit["id"].ToNative().ToString();
Logging.Log("Received CI request for repository {2} [{0}] for the commit {1}", cloneUrl, commitID, repoName);
CIJob job = new CIJob(repoName, cloneUrl, commitID, notifyEmail);
job.SetVariable("REPO_OWNER", message["repository"]["owner"]["username"].ToNative().ToString());
job.SetVariable("REPO_NAME", message["repository"]["name"].ToNative().ToString());
job.SetVariable("COMMIT_ID", commitID);
job.UpdateBuildState(BuildState.PENDING);
pool.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)
{
CommandRunner.DefaultThrow = CRThrow.NEGATIVE;
SimpleRouter genericRouter = new SimpleRouter();
genericRouter.AddSimpleRoute("/", WebHookRequest);
HTTPServer httpServer = new HTTPServer(new Endpoint(IPv6.ANY, 1888), new LoggingRouter(genericRouter));
pool.Start();
Logging.Log("Starting http listener...");
httpServer.Start();
}
}
}