feat: 增加私有 Appveyor Webhook 调用接口

This commit is contained in:
Argo Window10 2020-07-04 06:45:37 +08:00
parent fde8231177
commit c5ccfef7d0
6 changed files with 157 additions and 2 deletions

View File

@ -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
{
/// <summary>
///
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[AllowAnonymous]
public class GitController : ControllerBase
{
/// <summary>
/// Appveyor 私有服务器 Webhook
/// </summary>
/// <param name="client"></param>
/// <param name="query"></param>
/// <param name="payload"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult> Webhook([FromServices] GiteeHttpClient client, [FromQuery] GiteeQueryBody query, [FromBody] GiteePushEventArgs payload)
{
var ret = await client.Post(query, payload);
return ret ? (ActionResult)new OkResult() : new BadRequestResult();
}
/// <summary>
/// 跨域握手协议
/// </summary>
/// <returns></returns>
[HttpOptions]
public string Options()
{
return string.Empty;
}
}
}

View File

@ -21,7 +21,7 @@ namespace Bootstrap.Client.Controllers.Api
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
public async Task<bool> Log([FromServices]ISendMail sendMail, [FromBody]string message)
public async Task<bool> Log([FromServices] ISendMail sendMail, [FromBody] string message)
{
return await sendMail.SendMailAsync(MessageFormat.Exception, message.Replace("\r\n", "<br>").Replace("\n", "<br>"));
}
@ -34,7 +34,7 @@ namespace Bootstrap.Client.Controllers.Api
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
public async Task<bool> Healths([FromServices]ISendMail sendMail, [FromServices]IWebHostEnvironment env, [FromBody]string message)
public async Task<bool> Healths([FromServices] ISendMail sendMail, [FromServices] IWebHostEnvironment env, [FromBody] string message)
{
return await sendMail.SendMailAsync(MessageFormat.Healths, message.FormatHealths(env.WebRootPath));
}

View File

@ -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<GiteeHttpClient>((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;
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
public class GiteeHttpClient
{
HttpClient Client { get; set; }
IConfiguration Configuration { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="configuration"></param>
/// <param name="client"></param>
public GiteeHttpClient(IConfiguration configuration, HttpClient client)
{
Configuration = configuration;
Client = client;
}
/// <summary>
///
/// </summary>
public async Task<bool> 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;
}
}
}

View File

@ -85,4 +85,41 @@ namespace Bootstrap.Client.Tasks
/// </summary>
public string Url { get; set; } = "";
}
/// <summary>
///
/// </summary>
public class GiteeQueryBody
{
/// <summary>
///
/// </summary>
public string Id { get; set; } = "";
/// <summary>
///
/// </summary>
public string AllowBranchs { get; set; } = "master|dev";
}
/// <summary>
///
/// </summary>
public class AppveyorBuildPostBody
{
/// <summary>
///
/// </summary>
public string AccountName { get; set; } = "";
/// <summary>
///
/// </summary>
public string ProjectSlug { get; set; } = "";
/// <summary>
///
/// </summary>
public string Branch { get; set; } = "";
}
}

View File

@ -60,6 +60,12 @@
"BlackList": [ "" ]
},
"AllowOrigins": "http://localhost,http://argo.zylweb.cn",
"B4BIM": {
"Token": "",
"AccountName": "",
"ProjectSlug": "",
"Api": ""
},
"LongbowCache": {
"Enabled": true,
"CacheItems": [