diff --git a/src/client/Bootstrap.Client/Controllers/Api/GitController.cs b/src/client/Bootstrap.Client/Controllers/Api/GitController.cs index c4a89190..e0babaa7 100644 --- a/src/client/Bootstrap.Client/Controllers/Api/GitController.cs +++ b/src/client/Bootstrap.Client/Controllers/Api/GitController.cs @@ -23,11 +23,7 @@ namespace Bootstrap.Client.Controllers.Api /// /// [HttpPost] - public async Task Webhook([FromServices] GiteeHttpClient client, [FromQuery] GiteeQueryBody query, [FromBody] GiteePushBody payload) - { - var ret = await client.Post(query, payload); - return ret ? (ActionResult)new OkResult() : new BadRequestResult(); - } + public async Task Webhook([FromServices] AppVeyorHttpClient client, [FromQuery] GiteeQueryBody query, [FromBody] WebhookPostBody payload) => new StatusCodeResult((int)await client.Post(query, payload)); /// /// 跨域握手协议 diff --git a/src/client/Bootstrap.Client/HttpClientExtensions.cs b/src/client/Bootstrap.Client/HttpClientExtensions.cs index 8aa053ae..0c80014d 100644 --- a/src/client/Bootstrap.Client/HttpClientExtensions.cs +++ b/src/client/Bootstrap.Client/HttpClientExtensions.cs @@ -37,16 +37,10 @@ namespace Microsoft.AspNetCore.Builder client.BaseAddress = new Uri(url); }); - services.AddHttpClient((provider, client) => + services.AddHttpClient((provider, client) => { - var config = provider.GetRequiredService(); - var url = config["B4BIM:Api"]; - var token = config["B4BIM:Token"]; - - client.BaseAddress = new Uri(url); client.Timeout = TimeSpan.FromSeconds(5); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); }); return services; } diff --git a/src/client/Bootstrap.Client/Tasks/GiteeHttpClient.cs b/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs similarity index 51% rename from src/client/Bootstrap.Client/Tasks/GiteeHttpClient.cs rename to src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs index 6ccc6dd4..bb649e7e 100644 --- a/src/client/Bootstrap.Client/Tasks/GiteeHttpClient.cs +++ b/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; @@ -11,7 +12,7 @@ namespace Bootstrap.Client.Tasks /// /// /// - public class GiteeHttpClient + public class AppVeyorHttpClient { HttpClient Client { get; set; } @@ -22,7 +23,7 @@ namespace Bootstrap.Client.Tasks /// /// /// - public GiteeHttpClient(IConfiguration configuration, HttpClient client) + public AppVeyorHttpClient(IConfiguration configuration, HttpClient client) { Configuration = configuration; Client = client; @@ -31,23 +32,34 @@ namespace Bootstrap.Client.Tasks /// /// /// - public async Task Post(GiteeQueryBody query, GiteePushBody payload) + public async Task Post(GiteeQueryBody query, WebhookPostBody payload) { - var ret = false; - if (query.Id == "melhgtr0awltdhrh") + var ret = HttpStatusCode.NoContent; + var section = Configuration.GetSection($"Appveyor:{query.Id}"); + if (section != null) { + var token = section["Token"]; + var url = section["Api"]; + Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + Client.BaseAddress = new Uri(url); + var allowBranchs = query.AllowBranchs.SpanSplit("|"); var branch = payload.Ref.SpanSplit("/").LastOrDefault(); if (!string.IsNullOrEmpty(branch) && allowBranchs.Any(b => b.Equals(branch, StringComparison.OrdinalIgnoreCase))) { - var accountName = Configuration["B4BIM:AccountName"]; - var projectSlug = Configuration["B4BIM:ProjectSlug"]; + var accountName = section["AccountName"]; + var projectSlug = section["ProjectSlug"]; // 调用 webhook 接口 - // http://localhost:50852/api/Traces + // http://nugetp.b4bim.cn:8050/api/builds - var resp = await Client.PostAsJsonAsync("", new AppveyorBuildPostBody() { AccountName = accountName, ProjectSlug = projectSlug, Branch = branch }); - ret = resp.IsSuccessStatusCode; + var resp = await Client.PostAsJsonAsync("", new AppveyorBuildPostBody() + { + AccountName = accountName, + ProjectSlug = projectSlug, + Branch = branch + }); + ret = resp.IsSuccessStatusCode ? HttpStatusCode.OK : resp.StatusCode; } } return ret; diff --git a/src/client/Bootstrap.Client/Tasks/GiteePushBody.cs b/src/client/Bootstrap.Client/Tasks/GiteePushBody.cs index 87c1bb92..e170ece3 100644 --- a/src/client/Bootstrap.Client/Tasks/GiteePushBody.cs +++ b/src/client/Bootstrap.Client/Tasks/GiteePushBody.cs @@ -4,15 +4,21 @@ using System.Collections.Generic; namespace Bootstrap.Client.Tasks { /// - /// Gitee 提交事件参数实体类 + /// /// - public class GiteePushBody + public class WebhookPostBody { /// /// 获得/设置 提交分支信息 /// public string Ref { get; set; } = ""; + } + /// + /// Gitee 提交事件参数实体类 + /// + public class GiteePushBody : WebhookPostBody + { /// /// 获得/设置 提交信息集合 ///