Skip to content

Commit 3ec06c4

Browse files
committed
Get work item ids
Fixes #356
1 parent f5a5503 commit 3ec06c4

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

src/Cake.AzureDevOps/AzureDevOpsAliases.Pipelines.cs

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace Cake.AzureDevOps
22
{
3-
using System.Collections.Generic;
43
using Cake.AzureDevOps.Pipelines;
54
using Cake.Core;
65
using Cake.Core.Annotations;
6+
using System.Collections.Generic;
77

88
/// <content>
99
/// Contains functionality related to Azure Pipeline builds.
@@ -331,6 +331,54 @@ public static IEnumerable<AzureDevOpsChange> AzureDevOpsBuildChanges(
331331
.GetChanges();
332332
}
333333

334+
/// <summary>
335+
/// Gets the work item ids associated with an Azure Pipelines build.
336+
/// </summary>
337+
/// <param name="context">The context.</param>
338+
/// <param name="settings">Settings for getting the build.</param>
339+
/// <example>
340+
/// <para>Get work item ids associated with an Azure Pipelines build:</para>
341+
/// <code>
342+
/// <![CDATA[
343+
/// var buildSettings =
344+
/// new AzureDevOpsBuildSettings(
345+
/// new Uri("http://myserver:8080/defaultcollection"),
346+
/// "MyProject",
347+
/// 42,
348+
/// AzureDevOpsAuthenticationNtlm());
349+
///
350+
/// var workItemIds =
351+
/// AzureDevOpsBuildWorkItemIds(
352+
/// buildSettings);
353+
///
354+
/// Information("Work item ids:");
355+
/// foreach (var id in workItemIds)
356+
/// {
357+
/// Information(" {0}", id);
358+
/// }
359+
/// ]]>
360+
/// </code>
361+
/// </example>
362+
/// <returns>The work item ids associated with the build.
363+
/// Returns an empty list if build could not be found and
364+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.</returns>
365+
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
366+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
367+
[CakeMethodAlias]
368+
[CakeAliasCategory("Azure Pipelines")]
369+
[CakeNamespaceImport("Cake.AzureDevOps.Pipelines")]
370+
public static IEnumerable<int> AzureDevOpsBuildWorkItemIds(
371+
this ICakeContext context,
372+
AzureDevOpsBuildSettings settings)
373+
{
374+
context.NotNull(nameof(context));
375+
settings.NotNull(nameof(settings));
376+
377+
return
378+
new AzureDevOpsBuild(context.Log, settings, new BuildClientFactory(), new TestManagementClientFactory())
379+
.GetWorkItemIds();
380+
}
381+
334382
/// <summary>
335383
/// Gets the timeline entries for an Azure Pipelines build.
336384
/// </summary>

src/Cake.AzureDevOps/Pipelines/AzureDevOpsBuild.cs

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
namespace Cake.AzureDevOps.Pipelines
22
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Threading.Tasks;
73
using Cake.AzureDevOps.Authentication;
84
using Cake.Core.Diagnostics;
95
using Microsoft.TeamFoundation.Build.WebApi;
106
using Microsoft.TeamFoundation.TestManagement.WebApi;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Threading.Tasks;
1111

1212
/// <summary>
1313
/// Class for writing issues to Azure DevOps pull requests.
@@ -369,6 +369,32 @@ public IEnumerable<AzureDevOpsChange> GetChanges()
369369
}
370370
}
371371

372+
/// <summary>
373+
/// Gets the work item ids associated with a build.
374+
/// </summary>
375+
/// <returns>The work item ids associated with a build or an empty list if no build could be found and
376+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>false</c>.</returns>
377+
/// <exception cref="AzureDevOpsBuildNotFoundException">If build could not be found and
378+
/// <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/> is set to <c>true</c>.</exception>
379+
public IEnumerable<int> GetWorkItemIds()
380+
{
381+
if (!this.ValidateBuild())
382+
{
383+
return new List<int>();
384+
}
385+
386+
using (var buildClient = this.buildClientFactory.CreateBuildClient(this.CollectionUrl, this.credentials))
387+
{
388+
return
389+
buildClient
390+
.GetBuildWorkItemsRefsAsync(this.ProjectId, this.BuildId)
391+
.ConfigureAwait(false)
392+
.GetAwaiter()
393+
.GetResult()
394+
.Select(r => int.Parse(r.Id));
395+
}
396+
}
397+
372398
/// <summary>
373399
/// Checks if the build is failing.
374400
/// </summary>

0 commit comments

Comments
 (0)