Skip to content

Commit ce4ae1c

Browse files
committed
Add method to check if build is failing
1 parent eb78a65 commit ce4ae1c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Cake.AzureDevOps/AzureDevOpsAliases.Pipelines.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,53 @@ public static AzureDevOpsBuild AzureDevOpsBuildUsingAzurePipelinesOAuthToken(
8989
return AzureDevOpsBuild(context, settings);
9090
}
9191

92+
/// <summary>
93+
/// Returns if the Azure DevOps build is failing.
94+
/// </summary>
95+
/// <param name="context">The context.</param>
96+
/// <param name="settings">Settings for getting the build.</param>
97+
/// <example>
98+
/// <para>Get changes associated with an Azure Pipelines build:</para>
99+
/// <code>
100+
/// <![CDATA[
101+
/// var buildSettings =
102+
/// new AzureDevOpsBuildSettings(
103+
/// new Uri("http://myserver:8080/defaultcollection"),
104+
/// "MyProject",
105+
/// 42,
106+
/// AzureDevOpsAuthenticationNtlm());
107+
///
108+
/// var isFailing =
109+
/// AzureDevOpsBuildIsFailing(
110+
/// buildSettings);
111+
///
112+
/// if (isFailing)
113+
/// {
114+
/// Information("Build is failing");
115+
/// }
116+
/// ]]>
117+
/// </code>
118+
/// </example>
119+
/// <returns>The changes associated with the build.
120+
/// Returns an empty list if build could not be found and
121+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.</returns>
122+
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
123+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
124+
[CakeMethodAlias]
125+
[CakeAliasCategory("Azure Pipelines")]
126+
[CakeNamespaceImport("Cake.AzureDevOps.Pipelines")]
127+
public static bool AzureDevOpsBuildIsFailing(
128+
this ICakeContext context,
129+
AzureDevOpsBuildSettings settings)
130+
{
131+
context.NotNull(nameof(context));
132+
settings.NotNull(nameof(settings));
133+
134+
return
135+
new AzureDevOpsBuild(context.Log, settings, new BuildClientFactory())
136+
.IsBuildFailing();
137+
}
138+
92139
/// <summary>
93140
/// Gets the changes associated with an Azure Pipelines build.
94141
/// </summary>

src/Cake.AzureDevOps/Pipelines/AzureDevOpsBuild.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ public AzureDevOpsBuildStatus? Status
243243
/// Returns 0 if no build could be found and
244244
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.
245245
/// </summary>
246+
/// <remarks>
247+
/// Result is only available after a build has been finished.
248+
/// The check if a running build is failing you can call <see cref="IsBuildFailing"/>.
249+
/// </remarks>
246250
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
247251
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
248252
public AzureDevOpsBuildResult? Result
@@ -312,6 +316,19 @@ public IEnumerable<AzureDevOpsChange> GetChanges()
312316
}
313317
}
314318

319+
/// <summary>
320+
/// Checks if the build is failing.
321+
/// </summary>
322+
/// <returns><c>true</c> if build is failing.</returns>
323+
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
324+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
325+
public bool IsBuildFailing()
326+
{
327+
return
328+
this.ValidateBuild() &&
329+
this.GetTimelineRecords().Any(x => x.Result.HasValue && x.Result.Value == AzureDevOpsTaskResult.Failed);
330+
}
331+
315332
/// <summary>
316333
/// Gets the timeline entries for a build.
317334
/// </summary>

0 commit comments

Comments
 (0)