127 lines
5.5 KiB
C#
127 lines
5.5 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using ln.http;
|
|
using ln.json;
|
|
using ln.logging;
|
|
|
|
namespace ln.build.repositories
|
|
{
|
|
|
|
public class GiteaRepositoryInterface : GitRepositoryInterface
|
|
{
|
|
public string BaseURL { get; }
|
|
|
|
public string AuthorizationToken { get; set; }
|
|
|
|
public GiteaRepositoryInterface(string baseURL) :base("gitea")
|
|
{
|
|
BaseURL = baseURL;
|
|
}
|
|
|
|
HttpClient CreateHttpClient()
|
|
{
|
|
HttpClient httpClient = new HttpClient();
|
|
httpClient.DefaultRequestHeaders.Add("Authorization",String.Format("token {0}", AuthorizationToken));
|
|
return httpClient;
|
|
}
|
|
|
|
public override bool DetectValidity(string cloneUrl) => cloneUrl.StartsWith(BaseURL);
|
|
public override void UpdateBuildState(CIJob job)
|
|
{
|
|
string buildStateURL = string.Format("{3}/api/v1/repos/{0}/{1}/statuses/{2}",
|
|
job.GetVariable("REPO_OWNER"),
|
|
job.GetVariable("REPO_NAME"),
|
|
job.GetVariable("COMMIT_ID"),
|
|
BaseURL
|
|
);
|
|
|
|
if (buildStateURL != null)
|
|
{
|
|
JSONObject stateObject = new JSONObject();
|
|
stateObject.Add("context", string.Format("ln.build - {0}", job.CIService.HostName));
|
|
stateObject.Add("description", "build job pending");
|
|
stateObject.Add("state", job.BuildState.ToString().ToLower());
|
|
stateObject.Add("target_url", job.CIService.GetJobURL(job));
|
|
|
|
using (HttpClient httpClient = CreateHttpClient())
|
|
{
|
|
HttpResponseMessage response = httpClient.PostAsync(buildStateURL, new StringContent(stateObject.ToString(),Encoding.UTF8,"application/json")).Result;
|
|
job.Logger.Log(LogLevel.DEBUG, "UpdateBuildState({0}): {1}", job.BuildState, buildStateURL);
|
|
job.Logger.Log(LogLevel.DEBUG, "Request: {0}", stateObject.ToString());
|
|
job.Logger.Log(LogLevel.DEBUG, "Response: {0}", response );
|
|
}
|
|
}
|
|
}
|
|
|
|
public override HttpResponse WebHookHandler(CIService ciService, 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(ciService, this, 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())
|
|
.SetVariable("NUGET_APIKEY", "3yAJPMxcaEhb_HP62dxK")
|
|
.SetVariable("NUGET_SOURCE", "http://nuget.l--n.de/nuget/l--n/v3/index.json")
|
|
;
|
|
|
|
using (HttpClient httpClient = CreateHttpClient())
|
|
{
|
|
string triggerFile = string.Format("{3}/api/v1/repos/{0}/{1}/contents/build.ln?ref={2}",
|
|
job.GetVariable("REPO_OWNER"),
|
|
job.GetVariable("REPO_NAME"),
|
|
job.GetVariable("COMMIT_ID"),
|
|
BaseURL
|
|
);
|
|
|
|
HttpResponseMessage triggerResponse = httpClient.GetAsync(triggerFile).Result;
|
|
if (triggerResponse.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
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;
|
|
|
|
}
|
|
}
|
|
|
|
} |