From 1cfbf639a27c623204b9517d9d5da7a7ae601d93 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 29 May 2019 16:48:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(Gitee):=20=E5=A2=9E=E5=8A=A0Gitee=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/GiteeController.cs | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/GiteeController.cs b/Bootstrap.Admin/Controllers/Api/GiteeController.cs index 59ba5858..56b4854e 100644 --- a/Bootstrap.Admin/Controllers/Api/GiteeController.cs +++ b/Bootstrap.Admin/Controllers/Api/GiteeController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System; using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; @@ -28,10 +29,10 @@ namespace Bootstrap.Admin.Controllers.Api public async Task 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 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, "
([\\d]+)
", RegexOptions.IgnoreCase); 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 }); } @@ -48,10 +49,10 @@ namespace Bootstrap.Admin.Controllers.Api public async Task 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 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, "
([\\d]+)
", RegexOptions.IgnoreCase); 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 }); } @@ -68,9 +69,9 @@ namespace Bootstrap.Admin.Controllers.Api public async Task 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 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, $"", 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 }); } @@ -88,10 +89,28 @@ namespace Bootstrap.Admin.Controllers.Api public async Task 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 content = await client.GetAsJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}"); + var content = await GetJsonAsync(() => client.GetAsJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}")); return new JsonResult(new { schemaVersion = 1, label, message = content.Build.Version, color }); } + private async static Task GetJsonAsync(Func> callback) + { + var ret = default(T); + try + { + ret = await callback(); + } + catch (TaskCanceledException) + { + + } + catch (Exception ex) + { + ex.Log(); + } + return ret; + } + /// /// ///