refactor: Gitee Webhook 回调 AppVeyor 支持多个节点
This commit is contained in:
parent
db5b324eec
commit
e9dc4404ca
|
@ -23,11 +23,7 @@ namespace Bootstrap.Client.Controllers.Api
|
|||
/// <param name="payload"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> 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<ActionResult> Webhook([FromServices] AppVeyorHttpClient client, [FromQuery] GiteeQueryBody query, [FromBody] WebhookPostBody payload) => new StatusCodeResult((int)await client.Post(query, payload));
|
||||
|
||||
/// <summary>
|
||||
/// 跨域握手协议
|
||||
|
|
|
@ -37,16 +37,10 @@ namespace Microsoft.AspNetCore.Builder
|
|||
client.BaseAddress = new Uri(url);
|
||||
});
|
||||
|
||||
services.AddHttpClient<GiteeHttpClient>((provider, client) =>
|
||||
services.AddHttpClient<AppVeyorHttpClient>((provider, client) =>
|
||||
{
|
||||
var config = provider.GetRequiredService<IConfiguration>();
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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
|
|||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class GiteeHttpClient
|
||||
public class AppVeyorHttpClient
|
||||
{
|
||||
HttpClient Client { get; set; }
|
||||
|
||||
|
@ -22,7 +23,7 @@ namespace Bootstrap.Client.Tasks
|
|||
/// </summary>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="client"></param>
|
||||
public GiteeHttpClient(IConfiguration configuration, HttpClient client)
|
||||
public AppVeyorHttpClient(IConfiguration configuration, HttpClient client)
|
||||
{
|
||||
Configuration = configuration;
|
||||
Client = client;
|
||||
|
@ -31,23 +32,34 @@ namespace Bootstrap.Client.Tasks
|
|||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public async Task<bool> Post(GiteeQueryBody query, GiteePushBody payload)
|
||||
public async Task<HttpStatusCode> 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;
|
|
@ -4,15 +4,21 @@ using System.Collections.Generic;
|
|||
namespace Bootstrap.Client.Tasks
|
||||
{
|
||||
/// <summary>
|
||||
/// Gitee 提交事件参数实体类
|
||||
///
|
||||
/// </summary>
|
||||
public class GiteePushBody
|
||||
public class WebhookPostBody
|
||||
{
|
||||
/// <summary>
|
||||
/// 获得/设置 提交分支信息
|
||||
/// </summary>
|
||||
public string Ref { get; set; } = "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gitee 提交事件参数实体类
|
||||
/// </summary>
|
||||
public class GiteePushBody : WebhookPostBody
|
||||
{
|
||||
/// <summary>
|
||||
/// 获得/设置 提交信息集合
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue