From 1c05744e688ba64efe02dd081d105ee945f81e79 Mon Sep 17 00:00:00 2001 From: "Matthew Parker [SSW]" <61717342+MattParkerDev@users.noreply.github.com> Date: Mon, 8 Jul 2024 02:10:14 +1000 Subject: [PATCH] asdf --- .../Application/PipelineSummaryDto.cs | 26 +++++++++++++++ .../Application/StepContainerDto.cs | 26 +++++++++++++++ .../Host/PipelineApplication.cs | 32 +++++++++++++++++-- .../Host/Services/PostStepService.cs | 5 +++ 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/ParallelPipelines/Application/PipelineSummaryDto.cs create mode 100644 src/ParallelPipelines/Application/StepContainerDto.cs diff --git a/src/ParallelPipelines/Application/PipelineSummaryDto.cs b/src/ParallelPipelines/Application/PipelineSummaryDto.cs new file mode 100644 index 0000000..8e1a85f --- /dev/null +++ b/src/ParallelPipelines/Application/PipelineSummaryDto.cs @@ -0,0 +1,26 @@ +using ParallelPipelines.Domain.Entities; +using ParallelPipelines.Domain.Enums; + +namespace ParallelPipelines.Application; + +public class PipelineSummaryDto +{ + public CompletionType? OverallCompletionType { get; set; } + public DateTimeOffset? DeploymentStartTime; + public DateTimeOffset? DeploymentEndTime; + public List StepContainers { get; set; } = []; +} + +public static class PipelineSummaryMapper +{ + public static PipelineSummaryDto ToDto(PipelineSummary pipelineSummary) + { + return new PipelineSummaryDto + { + OverallCompletionType = pipelineSummary.OverallCompletionType, + DeploymentStartTime = pipelineSummary.DeploymentStartTime, + DeploymentEndTime = pipelineSummary.DeploymentEndTime, + StepContainers = pipelineSummary.StepContainers.Select(StepContainerMapper.ToDto).ToList() + }; + } +} diff --git a/src/ParallelPipelines/Application/StepContainerDto.cs b/src/ParallelPipelines/Application/StepContainerDto.cs new file mode 100644 index 0000000..2365dcc --- /dev/null +++ b/src/ParallelPipelines/Application/StepContainerDto.cs @@ -0,0 +1,26 @@ +using ParallelPipelines.Domain.Entities; +using ParallelPipelines.Domain.Enums; + +namespace ParallelPipelines.Application; + +public class StepContainerDto +{ + public string? StepName { get; set; } + public CompletionType? CompletionType { get; set; } + public DateTimeOffset? StartTime { get; set; } + public DateTimeOffset? EndTime { get; set; } +} + +public static class StepContainerMapper +{ + public static StepContainerDto ToDto(StepContainer stepContainer) + { + return new StepContainerDto + { + StepName = stepContainer.GetStepName(), + CompletionType = stepContainer.CompletionType, + StartTime = stepContainer.StartTime, + EndTime = stepContainer.EndTime + }; + } +} diff --git a/src/ParallelPipelines/Host/PipelineApplication.cs b/src/ParallelPipelines/Host/PipelineApplication.cs index 3499e69..4f46969 100644 --- a/src/ParallelPipelines/Host/PipelineApplication.cs +++ b/src/ParallelPipelines/Host/PipelineApplication.cs @@ -1,10 +1,13 @@ using System.Diagnostics; +using System.IO.Compression; +using System.Text.Json; using Actions.Octokit; using GitHub; using GitHub.Models; using Microsoft.Extensions.Hosting; using Octokit; using Octokit.Internal; +using ParallelPipelines.Domain.Entities; using ParallelPipelines.Host.Helpers; using ParallelPipelines.Host.Services; using Spectre.Console; @@ -33,11 +36,34 @@ public async Task StartAsync(CancellationToken cancellationToken) var owner = Context.Current.Repo.Owner; var repo = Context.Current.Repo.Repo; var githubToken = _pipelineContext.Configuration["WorkflowGithubToken"]; - //var githubClient = new GitHubClient(new ProductHeaderValue("ParallelPipelines"), new InMemoryCredentialStore(new Credentials(githubToken, AuthenticationType.Bearer))); - var githubClient = new GitHubClient(new ProductHeaderValue("ParallelPipelines")); + var githubClient = new GitHubClient(new ProductHeaderValue("ParallelPipelines"), new InMemoryCredentialStore(new Credentials(githubToken, AuthenticationType.Bearer))); + //var githubClient = new GitHubClient(new ProductHeaderValue("ParallelPipelines"), new InMemoryCredentialStore(new Credentials("", AuthenticationType.Bearer))); owner = "MattParkerDev"; repo = "ParallelPipelines"; - var runs = await githubClient.Actions.Workflows.Runs.ListByWorkflow(owner, repo, "example-prod-deploy.yml"); + runId = 9828456326; + var run = await githubClient.Actions.Workflows.Runs.Get(owner, repo, runId); + var attemptNumber = run.RunAttempt + 1; + if (attemptNumber > 1) + { + AnsiConsole.WriteLine($"ParallelPipelines is running on attempt number {attemptNumber}"); + var result = await githubClient.Actions.Artifacts.ListWorkflowArtifacts(owner, repo, runId); + var artifacts = result.Artifacts; + var failedRunArtifact = artifacts.FirstOrDefault(s => s.Name == "parallel-pipelines-artifact"); + if (failedRunArtifact is not null) + { + await using var stream = await githubClient.Actions.Artifacts.DownloadArtifact(owner, repo, failedRunArtifact.Id, "zip"); + // unzip stream and get json string + var zipArchive = new ZipArchive(stream); + var entry = zipArchive.Entries.FirstOrDefault(); + if (entry is not null) + { + await using var entryStream = entry.Open(); + var json = JsonSerializer.DeserializeAsync(entryStream, cancellationToken: cancellationToken); + + } + } + } + var pipelineSummary = await _orchestratorService.RunPipeline(cancellationToken); await _postStepService.RunPostSteps(pipelineSummary, cancellationToken); diff --git a/src/ParallelPipelines/Host/Services/PostStepService.cs b/src/ParallelPipelines/Host/Services/PostStepService.cs index 3021f13..2d2ede6 100644 --- a/src/ParallelPipelines/Host/Services/PostStepService.cs +++ b/src/ParallelPipelines/Host/Services/PostStepService.cs @@ -1,5 +1,7 @@ using System.Diagnostics; +using System.Text.Json; using Microsoft.Extensions.Options; +using ParallelPipelines.Application; using ParallelPipelines.Domain.Entities; using ParallelPipelines.Host.Helpers; using ParallelPipelines.Host.InternalHelpers; @@ -19,6 +21,9 @@ public async Task RunPostSteps(PipelineSummary pipelineSummary, CancellationToke { var text = GetGithubSummary(pipelineSummary, cancellationToken); await WriteGithubSummary(text, cancellationToken); + var pipelineSummaryDto = PipelineSummaryMapper.ToDto(pipelineSummary); + var summaryFile = await PipelineFileHelper.GitRootDirectory.CreateFileIfMissingAndGetFile("pipeline-summary-dto.json"); + await File.WriteAllTextAsync(summaryFile.FullName, JsonSerializer.Serialize(pipelineSummaryDto, new JsonSerializerOptions { WriteIndented = true }), cancellationToken); } private string GetGithubSummary(PipelineSummary pipelineSummary, CancellationToken cancellationToken)