From 878b975a39877f85a76264634fe375f16a7ed25f Mon Sep 17 00:00:00 2001 From: Antonio Landolfi Date: Sun, 19 Apr 2020 18:02:13 +0200 Subject: [PATCH] gestione webhook AzDevOps (#7) --- .../CovidDataCollectorController.cs | 57 ++----------------- 1 file changed, 5 insertions(+), 52 deletions(-) diff --git a/src/Covid19Report.Ita.Api/Controllers/CovidDataCollectorController.cs b/src/Covid19Report.Ita.Api/Controllers/CovidDataCollectorController.cs index c615701..7df0e47 100644 --- a/src/Covid19Report.Ita.Api/Controllers/CovidDataCollectorController.cs +++ b/src/Covid19Report.Ita.Api/Controllers/CovidDataCollectorController.cs @@ -26,7 +26,6 @@ namespace Covid19Report.Ita.Api.Controllers { [ApiController] - //[AllowAnonymous] [Route("api/[controller]")] public class CovidDataCollectorController : ControllerBase { @@ -48,48 +47,9 @@ public CovidDataCollectorController(ICosmosRepository cosmosRepository, IGitHubC covid19Services = cosmosRepository.CosmosServices["covid19-ita"].Value; } - [HttpPost("test")] - [Authorize] - public async Task TestAsync(string? saluto, [FromBody] JsonElement? jsonBody) - { - string lastcheck = "2020-02-24T18:00:00"; - using var command = dbConnection.CreateCommand(); - - command.CommandText = "select * from [lastcommit] where [id] = @id"; - command.Parameters.AddWithValue("@id", 0); - - dbConnection.Open(); - using var sqlReader = await command.ExecuteReaderAsync(); - - if (sqlReader.HasRows) - { - sqlReader.Read(); - lastcheck = sqlReader.GetString(1); - } - - //var commitRequest = new CommitRequest() - //{ - // Since = DateTimeOffset.Parse("2020-04-13T15:55:35Z") // DateTimeOffset.Parse(lastcheck, CultureInfo.InvariantCulture) - //}; - // - //var allCommits = await gitHubRepo.Commit.GetAll("pcm-dpc", "COVID-19", commitRequest); - //var changedFiles = new List(); - // - //foreach (var item in allCommits) - //{ - // var commit = await gitHubRepo.Commit.Get("pcm-dpc", "COVID-19", item.Sha); - // changedFiles.AddRange(commit.Files.Select(f => f.Filename)); - //} - // - //var filesDistict = changedFiles.Distinct(); - //var files = filesDistict.Where(f => Regex.IsMatch(f, "((dati-(regioni|province)|note)/.*(\\d|it).csv|README.md)")); - - return Ok(new[] { lastcheck }); - } - [HttpPost("sync")] [Authorize] - public async Task SyncronizeAsync(string? resource, [FromBody]JsonElement? json) + public async Task SyncronizeAsync(string? resource, [FromBody] JsonElement? json) { switch (resource) { @@ -193,23 +153,16 @@ private async Task GetDataListAsync() private async Task SyncronizeCommitAsync(JsonElement? jsonBody = null) { - var deploymnet = jsonBody?.GetProperty("resource").GetProperty("deployment"); + JsonElement? resource = null; if (Request.Headers["User-Agent"] is StringValues userAgent && userAgent.Any(x => x.Contains("VSServices", StringComparison.InvariantCultureIgnoreCase))) { - string? environment = deploymnet?.GetProperty("releaseEnvironment").GetProperty("name").GetString(); - - if (environment != "Production") + resource = jsonBody?.GetProperty("resource"); + if (resource?.GetProperty("stage").GetProperty("name").GetString() != "__default") { return BadRequest(); } } - DateTimeOffset? until = null; - if (jsonBody is JsonElement) - { - until = deploymnet?.GetProperty("completedOn").GetString() is string finishTime ? DateTimeOffset.Parse(finishTime, CultureInfo.InvariantCulture) : DateTimeOffset.UtcNow; - } - var commitService = covid19Services["commits"]; var option = new ApiOptions { @@ -219,7 +172,7 @@ private async Task SyncronizeCommitAsync(JsonElement? jsonBody = var request = new CommitRequest() { - Until = until + Until = resource?.GetProperty("run").GetProperty("finishedDate").GetString() is string finishTime ? DateTimeOffset.Parse(finishTime, CultureInfo.InvariantCulture) : DateTimeOffset.UtcNow }; var commits = await gitHubRepo.Commit.GetAll("leonidev", "covid-19-report-ita", request, option);