From c5ccfef7d0038db80eac34abec6aa0a6c7608e74 Mon Sep 17 00:00:00 2001 From: Argo Window10 Date: Sat, 4 Jul 2020 06:45:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=A7=81=E6=9C=89=20?= =?UTF-8?q?Appveyor=20Webhook=20=E8=B0=83=E7=94=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/GitController.cs | 42 ++++++++++++++ .../Controllers/Api/InterfaceController.cs | 4 +- .../Bootstrap.Client/HttpClientExtensions.cs | 14 +++++ .../Bootstrap.Client/Tasks/GiteeHttpClient.cs | 56 +++++++++++++++++++ .../Tasks/GiteePushEventArgs.cs | 37 ++++++++++++ src/client/Bootstrap.Client/appsettings.json | 6 ++ 6 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/client/Bootstrap.Client/Controllers/Api/GitController.cs create mode 100644 src/client/Bootstrap.Client/Tasks/GiteeHttpClient.cs diff --git a/src/client/Bootstrap.Client/Controllers/Api/GitController.cs b/src/client/Bootstrap.Client/Controllers/Api/GitController.cs new file mode 100644 index 00000000..07672967 --- /dev/null +++ b/src/client/Bootstrap.Client/Controllers/Api/GitController.cs @@ -0,0 +1,42 @@ +using Bootstrap.Client.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; + +namespace Bootstrap.Client.Controllers.Api +{ + /// + /// + /// + [Route("api/[controller]/[action]")] + [ApiController] + [AllowAnonymous] + public class GitController : ControllerBase + { + /// + /// Appveyor 私有服务器 Webhook + /// + /// + /// + /// + /// + [HttpPost] + public async Task Webhook([FromServices] GiteeHttpClient client, [FromQuery] GiteeQueryBody query, [FromBody] GiteePushEventArgs payload) + { + var ret = await client.Post(query, payload); + return ret ? (ActionResult)new OkResult() : new BadRequestResult(); + } + + /// + /// 跨域握手协议 + /// + /// + [HttpOptions] + public string Options() + { + return string.Empty; + } + } +} diff --git a/src/client/Bootstrap.Client/Controllers/Api/InterfaceController.cs b/src/client/Bootstrap.Client/Controllers/Api/InterfaceController.cs index c8ecd5a3..9377f35b 100644 --- a/src/client/Bootstrap.Client/Controllers/Api/InterfaceController.cs +++ b/src/client/Bootstrap.Client/Controllers/Api/InterfaceController.cs @@ -21,7 +21,7 @@ namespace Bootstrap.Client.Controllers.Api /// /// [HttpPost] - public async Task Log([FromServices]ISendMail sendMail, [FromBody]string message) + public async Task Log([FromServices] ISendMail sendMail, [FromBody] string message) { return await sendMail.SendMailAsync(MessageFormat.Exception, message.Replace("\r\n", "
").Replace("\n", "
")); } @@ -34,7 +34,7 @@ namespace Bootstrap.Client.Controllers.Api /// /// [HttpPost] - public async Task Healths([FromServices]ISendMail sendMail, [FromServices]IWebHostEnvironment env, [FromBody]string message) + public async Task Healths([FromServices] ISendMail sendMail, [FromServices] IWebHostEnvironment env, [FromBody] string message) { return await sendMail.SendMailAsync(MessageFormat.Healths, message.FormatHealths(env.WebRootPath)); } diff --git a/src/client/Bootstrap.Client/HttpClientExtensions.cs b/src/client/Bootstrap.Client/HttpClientExtensions.cs index 7e3924d5..8aa053ae 100644 --- a/src/client/Bootstrap.Client/HttpClientExtensions.cs +++ b/src/client/Bootstrap.Client/HttpClientExtensions.cs @@ -1,9 +1,11 @@ using Bootstrap.Client.DataAccess; +using Bootstrap.Client.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; +using System.Net.Http.Headers; namespace Microsoft.AspNetCore.Builder { @@ -34,6 +36,18 @@ namespace Microsoft.AspNetCore.Builder var url = $"{authHost}/api/Traces"; client.BaseAddress = new Uri(url); }); + + 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/GiteeHttpClient.cs new file mode 100644 index 00000000..b16e0022 --- /dev/null +++ b/src/client/Bootstrap.Client/Tasks/GiteeHttpClient.cs @@ -0,0 +1,56 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; + +namespace Bootstrap.Client.Tasks +{ + /// + /// + /// + public class GiteeHttpClient + { + HttpClient Client { get; set; } + + IConfiguration Configuration { get; set; } + + /// + /// 构造函数 + /// + /// + /// + public GiteeHttpClient(IConfiguration configuration, HttpClient client) + { + Configuration = configuration; + Client = client; + } + + /// + /// + /// + public async Task Post(GiteeQueryBody query, GiteePushEventArgs payload) + { + var ret = false; + if (query.Id == "melhgtr0awltdhrh") + { + 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"]; + + // 调用 webhook 接口 + // http://localhost:50852/api/Traces + + var resp = await Client.PostAsJsonAsync("", new AppveyorBuildPostBody() { AccountName = accountName, ProjectSlug = projectSlug, Branch = branch }); + ret = resp.IsSuccessStatusCode; + } + } + return ret; + } + } +} diff --git a/src/client/Bootstrap.Client/Tasks/GiteePushEventArgs.cs b/src/client/Bootstrap.Client/Tasks/GiteePushEventArgs.cs index 71f8d55f..951ba67d 100644 --- a/src/client/Bootstrap.Client/Tasks/GiteePushEventArgs.cs +++ b/src/client/Bootstrap.Client/Tasks/GiteePushEventArgs.cs @@ -85,4 +85,41 @@ namespace Bootstrap.Client.Tasks /// public string Url { get; set; } = ""; } + + /// + /// + /// + public class GiteeQueryBody + { + /// + /// + /// + public string Id { get; set; } = ""; + + /// + /// + /// + public string AllowBranchs { get; set; } = "master|dev"; + } + + /// + /// + /// + public class AppveyorBuildPostBody + { + /// + /// + /// + public string AccountName { get; set; } = ""; + + /// + /// + /// + public string ProjectSlug { get; set; } = ""; + + /// + /// + /// + public string Branch { get; set; } = ""; + } } diff --git a/src/client/Bootstrap.Client/appsettings.json b/src/client/Bootstrap.Client/appsettings.json index 9848e16a..967e526a 100644 --- a/src/client/Bootstrap.Client/appsettings.json +++ b/src/client/Bootstrap.Client/appsettings.json @@ -60,6 +60,12 @@ "BlackList": [ "" ] }, "AllowOrigins": "http://localhost,http://argo.zylweb.cn", + "B4BIM": { + "Token": "", + "AccountName": "", + "ProjectSlug": "", + "Api": "" + }, "LongbowCache": { "Enabled": true, "CacheItems": [