Skip to content

Commit 86b9a16

Browse files
authored
Merge pull request #237 from christianbumann/feature/gh-234
(GH-234) Add an alias to get builds by specific settings
2 parents 87d20db + f2b33ee commit 86b9a16

12 files changed

+1268
-28
lines changed

src/Cake.AzureDevOps.Tests/Pipelines/AzureDevOpsSettingsTests.cs

Lines changed: 684 additions & 0 deletions
Large diffs are not rendered by default.

src/Cake.AzureDevOps/AzureDevOpsAliases.Pipelines.cs

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

92+
/// <summary>
93+
/// Gets Azure Pipelines builds using the specified settings.
94+
/// </summary>
95+
/// <param name="context">The context.</param>
96+
/// <param name="settings">Settings for getting the build.</param>
97+
/// <example>
98+
/// <para>Get builds running on Azure DevOps Server:</para>
99+
/// <code>
100+
/// <![CDATA[
101+
/// var buildSettings =
102+
/// new AzureDevOpsBuildsSettings(
103+
/// new Uri("http://myserver:8080/defaultcollection"),
104+
/// "MyProject",
105+
/// AzureDevOpsAuthenticationNtlm());
106+
///
107+
/// var builds =
108+
/// AzureDevOpsBuilds(
109+
/// buildSettings);
110+
/// ]]>
111+
/// </code>
112+
/// </example>
113+
/// <returns>Description of the builds or an empty list of builds.</returns>
114+
[CakeMethodAlias]
115+
[CakeAliasCategory("Azure Pipelines")]
116+
[CakeNamespaceImport("Cake.AzureDevOps.Pipelines")]
117+
public static IEnumerable<AzureDevOpsBuild> AzureDevOpsBuilds(
118+
this ICakeContext context,
119+
AzureDevOpsBuildsSettings settings)
120+
{
121+
context.NotNull(nameof(context));
122+
settings.NotNull(nameof(settings));
123+
124+
return AzureDevOpsBuildsHelper.GetAzureDevOpsBuilds(
125+
context.Log,
126+
settings);
127+
}
128+
92129
/// <summary>
93130
/// Returns if the Azure DevOps build is failing.
94131
/// </summary>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace Cake.AzureDevOps
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Class for reading environment variables.
7+
/// </summary>
8+
internal static class EnvironmentVariableHelper
9+
{
10+
/// <summary>
11+
/// Gets the value for the environment variable 'SYSTEM_ACCESSTOKEN'.
12+
/// </summary>
13+
/// <returns>The value for the environment variable 'SYSTEM_ACCESSTOKEN'.</returns>
14+
/// <exception cref="InvalidOperationException">If the environment variable was not found or the value is whitespace.</exception>
15+
public static string GetSystemAccessToken()
16+
{
17+
var accessToken = Environment.GetEnvironmentVariable("SYSTEM_ACCESSTOKEN", EnvironmentVariableTarget.Process);
18+
if (string.IsNullOrWhiteSpace(accessToken))
19+
{
20+
throw new InvalidOperationException(
21+
"Failed to read the SYSTEM_ACCESSTOKEN environment variable. Make sure you are running in an Azure Pipelines build and that the 'Allow Scripts to access OAuth token' option is enabled.");
22+
}
23+
24+
return accessToken;
25+
}
26+
27+
/// <summary>
28+
/// Gets the value for the environment variable 'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'.
29+
/// </summary>
30+
/// <returns>The value for the environment variable 'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'.</returns>
31+
/// <exception cref="InvalidOperationException">If the environment variable was not found or the value is whitespace.</exception>
32+
public static Uri GetSystemTeamFoundationCollectionUri()
33+
{
34+
var collectionUrl = Environment.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", EnvironmentVariableTarget.Process);
35+
if (string.IsNullOrWhiteSpace(collectionUrl))
36+
{
37+
throw new InvalidOperationException(
38+
"Failed to read the SYSTEM_TEAMFOUNDATIONCOLLECTIONURI environment variable. Make sure you are running in an Azure Pipelines build.");
39+
}
40+
41+
return new Uri(collectionUrl);
42+
}
43+
44+
/// <summary>
45+
/// Gets the value for the environment variable 'SYSTEM_TEAMPROJECT'.
46+
/// </summary>
47+
/// <returns>The value for the environment variable 'SYSTEM_TEAMPROJECT'.</returns>
48+
/// <exception cref="InvalidOperationException">If the environment variable was not found or the value is whitespace.</exception>
49+
public static string GetSystemTeamProject()
50+
{
51+
var projectName = Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT", EnvironmentVariableTarget.Process);
52+
if (string.IsNullOrWhiteSpace(projectName))
53+
{
54+
throw new InvalidOperationException(
55+
"Failed to read the SYSTEM_TEAMPROJECT environment variable. Make sure you are running in an Azure Pipelines build.");
56+
}
57+
58+
return projectName;
59+
}
60+
}
61+
}

src/Cake.AzureDevOps/Pipelines/AzureDevOpsBuild.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ public AzureDevOpsBuild(ICakeLog log, AzureDevOpsBuildSettings settings)
3030
{
3131
}
3232

33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="AzureDevOpsBuild"/> class.
35+
/// </summary>
36+
/// <param name="log">The Cake log context.</param>
37+
/// <param name="settings">Settings for accessing AzureDevOps.</param>
38+
/// <param name="build">The build.</param>
39+
internal AzureDevOpsBuild(ICakeLog log, AzureDevOpsBuildsSettings settings, Build build)
40+
{
41+
log.NotNull(nameof(log));
42+
settings.NotNull(nameof(settings));
43+
build.NotNull(nameof(build));
44+
45+
this.log = log;
46+
this.build = build;
47+
this.buildClientFactory = new BuildClientFactory();
48+
this.credentials = settings.Credentials;
49+
this.CollectionUrl = settings.CollectionUrl;
50+
}
51+
3352
/// <summary>
3453
/// Initializes a new instance of the <see cref="AzureDevOpsBuild"/> class.
3554
/// </summary>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Cake.AzureDevOps.Pipelines
2+
{
3+
/// <summary>
4+
/// Possible order of a build query.
5+
/// </summary>
6+
public enum AzureDevOpsBuildQueryOrder : byte
7+
{
8+
/// <summary>
9+
/// Order by finish time ascending.
10+
/// </summary>
11+
FinishTimeAscending = 2,
12+
13+
/// <summary>
14+
/// Order by finish time descending.
15+
/// </summary>
16+
FinishTimeDescending = 3,
17+
18+
/// <summary>
19+
/// Order by queue time descending.
20+
/// </summary>
21+
QueueTimeDescending = 4,
22+
23+
/// <summary>
24+
/// Order by queue time ascending.
25+
/// </summary>
26+
QueueTimeAscending = 5,
27+
28+
/// <summary>
29+
/// Order by start time descending.
30+
/// </summary>
31+
StartTimeDescending = 6,
32+
33+
/// <summary>
34+
/// Order by start time ascending.
35+
/// </summary>
36+
StartTimeAscending = 7,
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Cake.AzureDevOps.Pipelines
2+
{
3+
using Microsoft.TeamFoundation.Build.WebApi;
4+
5+
/// <summary>
6+
/// Extensions for the <see cref="AzureDevOpsBuildQueryOrder"/> class.
7+
/// </summary>
8+
internal static class AzureDevOpsBuildQueryOrderExtensions
9+
{
10+
/// <summary>
11+
/// Converts a <see cref="AzureDevOpsBuildQueryOrderExtensions"/> to an <see cref="BuildQueryOrder"/>.
12+
/// </summary>
13+
/// <param name="queryOrder">Query order to convert.</param>
14+
/// <returns>Converted query order.</returns>
15+
public static BuildQueryOrder ToBuildQueryOrder(this AzureDevOpsBuildQueryOrder queryOrder)
16+
{
17+
switch (queryOrder)
18+
{
19+
case AzureDevOpsBuildQueryOrder.FinishTimeAscending:
20+
return BuildQueryOrder.FinishTimeAscending;
21+
case AzureDevOpsBuildQueryOrder.FinishTimeDescending:
22+
return BuildQueryOrder.FinishTimeDescending;
23+
case AzureDevOpsBuildQueryOrder.QueueTimeDescending:
24+
return BuildQueryOrder.QueueTimeDescending;
25+
case AzureDevOpsBuildQueryOrder.QueueTimeAscending:
26+
return BuildQueryOrder.QueueTimeAscending;
27+
case AzureDevOpsBuildQueryOrder.StartTimeDescending:
28+
return BuildQueryOrder.StartTimeDescending;
29+
case AzureDevOpsBuildQueryOrder.StartTimeAscending:
30+
return BuildQueryOrder.StartTimeAscending;
31+
default:
32+
throw new System.Exception("Unknown value");
33+
}
34+
}
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Cake.AzureDevOps.Pipelines
2+
{
3+
using Microsoft.TeamFoundation.Build.WebApi;
4+
5+
/// <summary>
6+
/// Extensions for the <see cref="AzureDevOpsBuildResult"/> class.
7+
/// </summary>
8+
internal static class AzureDevOpsBuildResultExtensions
9+
{
10+
/// <summary>
11+
/// Converts a <see cref="AzureDevOpsBuildResult"/> to an <see cref="BuildResult"/>.
12+
/// </summary>
13+
/// <param name="result">Build result to convert.</param>
14+
/// <returns>Converted build result.</returns>
15+
public static BuildResult ToBuildResult(this AzureDevOpsBuildResult result)
16+
{
17+
switch (result)
18+
{
19+
case AzureDevOpsBuildResult.None:
20+
return BuildResult.None;
21+
case AzureDevOpsBuildResult.Succeeded:
22+
return BuildResult.Succeeded;
23+
case AzureDevOpsBuildResult.PartiallySucceeded:
24+
return BuildResult.PartiallySucceeded;
25+
case AzureDevOpsBuildResult.Failed:
26+
return BuildResult.Failed;
27+
case AzureDevOpsBuildResult.Canceled:
28+
return BuildResult.Canceled;
29+
default:
30+
throw new System.Exception("Unknown value");
31+
}
32+
}
33+
}
34+
}

src/Cake.AzureDevOps/Pipelines/AzureDevOpsBuildSettings.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,16 @@ public AzureDevOpsBuildSettings(AzureDevOpsBuildSettings settings)
7373

7474
/// <summary>
7575
/// Initializes a new instance of the <see cref="AzureDevOpsBuildSettings"/> class using environment variables
76-
/// as set by a Azure Pipelines build.
76+
/// as set by an Azure Pipelines build.
7777
/// </summary>
7878
/// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
7979
public AzureDevOpsBuildSettings(IAzureDevOpsCredentials credentials)
8080
{
8181
credentials.NotNull(nameof(credentials));
8282

8383
this.Credentials = credentials;
84-
85-
var collectionUrl = Environment.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", EnvironmentVariableTarget.Process);
86-
if (string.IsNullOrWhiteSpace(collectionUrl))
87-
{
88-
throw new InvalidOperationException(
89-
"Failed to read the SYSTEM_TEAMFOUNDATIONCOLLECTIONURI environment variable. Make sure you are running in an Azure Pipelines build.");
90-
}
91-
92-
this.CollectionUrl = new Uri(collectionUrl);
93-
94-
var projectName = Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT", EnvironmentVariableTarget.Process);
95-
if (string.IsNullOrWhiteSpace(projectName))
96-
{
97-
throw new InvalidOperationException(
98-
"Failed to read the SYSTEM_TEAMPROJECT environment variable. Make sure you are running in an Azure Pipelines build.");
99-
}
100-
101-
this.ProjectName = projectName;
84+
this.CollectionUrl = EnvironmentVariableHelper.GetSystemTeamFoundationCollectionUri();
85+
this.ProjectName = EnvironmentVariableHelper.GetSystemTeamProject();
10286

10387
var buildId = Environment.GetEnvironmentVariable("BUILD_BUILDID", EnvironmentVariableTarget.Process);
10488
if (string.IsNullOrWhiteSpace(buildId))
@@ -159,14 +143,7 @@ public AzureDevOpsBuildSettings(IAzureDevOpsCredentials credentials)
159143
/// <returns>The instance of <see cref="AzureDevOpsBuildSettings"/> class.</returns>
160144
public static AzureDevOpsBuildSettings UsingAzurePipelinesOAuthToken()
161145
{
162-
var accessToken = Environment.GetEnvironmentVariable("SYSTEM_ACCESSTOKEN", EnvironmentVariableTarget.Process);
163-
164-
if (string.IsNullOrWhiteSpace(accessToken))
165-
{
166-
throw new InvalidOperationException(
167-
"Failed to read the SYSTEM_ACCESSTOKEN environment variable. Make sure you are running in an Azure Pipelines build and that the 'Allow Scripts to access OAuth token' option is enabled.");
168-
}
169-
146+
var accessToken = EnvironmentVariableHelper.GetSystemAccessToken();
170147
return new AzureDevOpsBuildSettings(new AzureDevOpsOAuthCredentials(accessToken));
171148
}
172149
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Cake.AzureDevOps.Pipelines
2+
{
3+
using Microsoft.TeamFoundation.Build.WebApi;
4+
5+
/// <summary>
6+
/// Extensions for the <see cref="AzureDevOpsBuildStatus"/> class.
7+
/// </summary>
8+
internal static class AzureDevOpsBuildStatusExtensions
9+
{
10+
/// <summary>
11+
/// Converts a <see cref="AzureDevOpsBuildStatus"/> to an <see cref="BuildStatus"/>.
12+
/// </summary>
13+
/// <param name="status">State to convert.</param>
14+
/// <returns>Converted state.</returns>
15+
public static BuildStatus ToBuildStatus(this AzureDevOpsBuildStatus status)
16+
{
17+
switch (status)
18+
{
19+
case AzureDevOpsBuildStatus.None:
20+
return BuildStatus.None;
21+
case AzureDevOpsBuildStatus.InProgress:
22+
return BuildStatus.InProgress;
23+
case AzureDevOpsBuildStatus.Completed:
24+
return BuildStatus.Completed;
25+
case AzureDevOpsBuildStatus.Cancelling:
26+
return BuildStatus.Cancelling;
27+
case AzureDevOpsBuildStatus.Postponed:
28+
return BuildStatus.Postponed;
29+
case AzureDevOpsBuildStatus.NotStarted:
30+
return BuildStatus.NotStarted;
31+
case AzureDevOpsBuildStatus.All:
32+
return BuildStatus.All;
33+
default:
34+
throw new System.Exception("Unknown value");
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)