From 1f2a56f999b4f310aef326a3fbbabadb7cc13c4f Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 14 Jun 2019 15:19:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(#IXXPS):=20GiteeController=20=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Comment comment #IXXPS #Issue close https://gitee.com/LongbowEnterprise/dashboard/issues?id=IXXPS --- .../Controllers/Api/GiteeController.cs | 50 ++++++++++++------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/GiteeController.cs b/Bootstrap.Admin/Controllers/Api/GiteeController.cs index c8f471a5..0c8ed856 100644 --- a/Bootstrap.Admin/Controllers/Api/GiteeController.cs +++ b/Bootstrap.Admin/Controllers/Api/GiteeController.cs @@ -29,11 +29,14 @@ 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 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 = 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 }); + var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content => + { + var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); + var labels = new string[] { "open", "closed", "rejected" }; + var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); + return string.Join(" ", result); + }); + return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); } /// @@ -49,11 +52,14 @@ 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 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 = 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 }); + var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content => + { + var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); + var labels = new string[] { "open", "merged", "closed" }; + var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); + return string.Join(" ", result); + }); + return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); } /// @@ -69,10 +75,12 @@ 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 GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases")); - var regex = Regex.Match(content, $"", RegexOptions.IgnoreCase); - var result = string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value; - return new JsonResult(new { schemaVersion = 1, label, message = result, color }); + var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content => + { + var regex = Regex.Match(content, $"", RegexOptions.IgnoreCase); + return string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value; + }); + return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); } /// @@ -89,16 +97,20 @@ 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 GetJsonAsync(() => client.GetAsJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}")); - return new JsonResult(new { schemaVersion = 1, label, message = content == null ? "unknown" : content.Build.Version, color }); + var ret = await GetJsonAsync(() => client.GetAsJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}"), content => + { + return content == null ? "unknown" : content.Build.Version; + }); + return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); } - private async static Task GetJsonAsync(Func> callback) + private async static Task GetJsonAsync(Func> requestUrl, Func callback) { - var ret = default(T); + var ret = "unreachable"; try { - ret = await callback(); + var resq = await requestUrl(); + ret = callback(resq); } catch (TaskCanceledException) {