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; + } + /// /// ///