Created
March 6, 2017 19:17
-
-
Save BenLubar/fac1717326eda59cf1676d8ff2248e8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Inedo.BuildMaster.Data; | |
using Inedo.BuildMaster.Web; | |
using Inedo.BuildMaster.Web.Controls; | |
using Inedo.Documentation; | |
using Inedo.Serialization; | |
using System; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Inedo.BuildMaster.Extensibility.PromotionRequirements | |
{ | |
[DisplayName("Pipeline Stage Complete")] | |
[Description("Ensures a pipeline stage of another application has been completed.")] | |
public sealed class PipelineStagePromotionRequirement : PromotionRequirement | |
{ | |
[Required] | |
[Persistent] | |
[DisplayName("Application")] | |
[SuggestibleValue(typeof(ApplicationNameSuggestionProvider))] | |
public string TargetApplicationName { get; set; } | |
[Required] | |
[Persistent] | |
[DisplayName("Pipeline")] | |
[SuggestibleValue(typeof(PipelineNameSuggestionProvider))] | |
public string PipelineName { get; set; } | |
[Required] | |
[Persistent] | |
[DisplayName("Pipeline Stage")] | |
public string PipelineStageName { get; set; } | |
public override async Task<PromotionRequirementStatus> GetStatusAsync(PromotionContext context) | |
{ | |
using (var db = new DB.Context()) | |
{ | |
var applicationId = (await db.Applications_GetApplicationsAsync(null).ConfigureAwait(false)) | |
.FirstOrDefault(a => a.Application_Name.Equals(this.TargetApplicationName, StringComparison.OrdinalIgnoreCase)) | |
?.Application_Id; | |
if (!applicationId.HasValue) | |
{ | |
return new PromotionRequirementStatus(PromotionRequirementState.NotMet, new RichDescription("No application with the name ", new Hilite(this.TargetApplicationName), " was found.")); | |
} | |
var pipeline = (await db.Pipelines_GetPipelinesAsync(applicationId).ConfigureAwait(false)) | |
.FirstOrDefault(p => p.Pipeline_Name.Equals(this.PipelineName, StringComparison.OrdinalIgnoreCase)); | |
if (pipeline == null) | |
{ | |
return new PromotionRequirementStatus(PromotionRequirementState.NotMet, new RichDescription("No pipeline with the name ", new Hilite(this.PipelineName), " was found in the application ", new Hilite(this.TargetApplicationName), ".")); | |
} | |
var latestRelease = (await db.Releases_GetReleasesSlimAsync(applicationId).ConfigureAwait(false)) | |
.OrderByDescending(r => r.Release_Sequence) | |
.FirstOrDefault(r => r.ReleaseStatus_Name != Domains.ReleaseStatus.Cancelled); | |
if (latestRelease == null) | |
{ | |
return new PromotionRequirementStatus(PromotionRequirementState.NotMet, new RichDescription("There is no latest release for the application ", new Hilite(this.TargetApplicationName), ".")); | |
} | |
var promotion = (await db.Builds_GetPromotionsSlimAsync(applicationId, latestRelease.Release_Number, latestRelease.Furthest_Build_Number).ConfigureAwait(false)) | |
.FirstOrDefault(p => p.Pipeline_Id == pipeline.Pipeline_Id && p.PipelineStage_Name.Equals(this.PipelineStageName, StringComparison.OrdinalIgnoreCase)); | |
if (promotion == null) | |
{ | |
return new PromotionRequirementStatus(PromotionRequirementState.NotMet, new RichDescription( | |
new Hilite(this.TargetApplicationName), " release ", new Hilite(latestRelease.Release_Number), | |
" build ", new Hilite('#' + latestRelease.Furthest_Build_Number), | |
" has not yet reached ", new Hilite(this.PipelineStageName), ".")); | |
} | |
if (promotion.PromotionStatus_Name != Domains.PromotionStatus.Completed) | |
{ | |
return new PromotionRequirementStatus(PromotionRequirementState.NotMet, new RichDescription( | |
new Hilite(this.TargetApplicationName), " release ", new Hilite(latestRelease.Release_Number), | |
" build ", new Hilite('#' + latestRelease.Furthest_Build_Number), | |
" is ", new Hilite(promotion.PromotionStatus_Name), " for ", | |
new Hilite(this.PipelineStageName), ".")); | |
} | |
return new PromotionRequirementStatus(PromotionRequirementState.Met, new RichDescription( | |
new Hilite(this.TargetApplicationName), " release ", new Hilite(latestRelease.Release_Number), | |
" build ", new Hilite('#' + latestRelease.Furthest_Build_Number), | |
" completed ", new Hilite(this.PipelineStageName), " on ", | |
new Hilite(WUtil.FormatUtcDateTime(promotion.Promoted_Date)), ".")); | |
} | |
} | |
public override RichDescription GetDescription() | |
{ | |
return new RichDescription( | |
"The latest release of ", new Hilite(this.TargetApplicationName), | |
" has completed the ", new Hilite(this.PipelineStageName), | |
" stage of the ", new Hilite(this.PipelineName), " pipeline." | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment