fix(Gitee): 增加Gitee接口保护

This commit is contained in:
Argo Zhang 2019-05-29 16:48:49 +08:00
parent 8d38819976
commit 1cfbf639a2
1 changed files with 26 additions and 7 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -28,10 +29,10 @@ namespace Bootstrap.Admin.Controllers.Api
public async Task<ActionResult> Issues([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") public async Task<ActionResult> Issues([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{ {
var client = httpClientFactory.CreateClient(); var client = httpClientFactory.CreateClient();
var content = await client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"); var content = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"));
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase); var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
var labels = new string[] { "open", "closed", "rejected" }; var labels = new string[] { "open", "closed", "rejected" };
var result = regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}");
return new JsonResult(new { schemaVersion = 1, label, message = string.Join(" ", result), color }); return new JsonResult(new { schemaVersion = 1, label, message = string.Join(" ", result), color });
} }
@ -48,10 +49,10 @@ namespace Bootstrap.Admin.Controllers.Api
public async Task<ActionResult> Pulls([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") public async Task<ActionResult> Pulls([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{ {
var client = httpClientFactory.CreateClient(); var client = httpClientFactory.CreateClient();
var content = await client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"); var content = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"));
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase); var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
var labels = new string[] { "open", "merged", "closed" }; var labels = new string[] { "open", "merged", "closed" };
var result = regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}");
return new JsonResult(new { schemaVersion = 1, label, message = string.Join(" ", result), color }); return new JsonResult(new { schemaVersion = 1, label, message = string.Join(" ", result), color });
} }
@ -68,9 +69,9 @@ namespace Bootstrap.Admin.Controllers.Api
public async Task<ActionResult> Releases([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") public async Task<ActionResult> Releases([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{ {
var client = httpClientFactory.CreateClient(); var client = httpClientFactory.CreateClient();
var content = await client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"); var content = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"));
var regex = Regex.Match(content, $"<a href=\"/{userName}/{repoName}/releases/([^\\s]+)\" target=\"_blank\">", RegexOptions.IgnoreCase); var regex = Regex.Match(content, $"<a href=\"/{userName}/{repoName}/releases/([^\\s]+)\" target=\"_blank\">", RegexOptions.IgnoreCase);
var result = regex.Groups[1].Value; var result = string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value;
return new JsonResult(new { schemaVersion = 1, label, message = result, color }); return new JsonResult(new { schemaVersion = 1, label, message = result, color });
} }
@ -88,10 +89,28 @@ namespace Bootstrap.Admin.Controllers.Api
public async Task<ActionResult> Builds([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") public async Task<ActionResult> Builds([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
{ {
var client = httpClientFactory.CreateClient(); var client = httpClientFactory.CreateClient();
var content = await client.GetAsJsonAsync<AppveyorBuildResult>($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}"); var content = await GetJsonAsync(() => client.GetAsJsonAsync<AppveyorBuildResult>($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}"));
return new JsonResult(new { schemaVersion = 1, label, message = content.Build.Version, color }); return new JsonResult(new { schemaVersion = 1, label, message = content.Build.Version, color });
} }
private async static Task<T> GetJsonAsync<T>(Func<Task<T>> callback)
{
var ret = default(T);
try
{
ret = await callback();
}
catch (TaskCanceledException)
{
}
catch (Exception ex)
{
ex.Log();
}
return ret;
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>