To work with releases, you need to add the Microsoft.VisualStudio.Services.Release.Client package that contains ReleaseHttpClient. We consider the following operations here:
- View release definitions of a team project
- View releases of a team project
- View work items and commits are referenced to a release
We can use the GetReleaseDefinitionsAsync method to get a release definitions list. The method may use only team project name as a parameter. If you want to limit a count of returning release definitions, you can use the searchText (to search by template) or path (to search by path). By default, the method returns the list only with basic information such as:
- ID
- Name
- Description
- Path
- Etc.
If you want to see more details (such as environments and artifacts), you can specify that in the expand parameter. Example to get a release definitions list and configured environments:
var reldefList = ReleaseClient.GetReleaseDefinitionsAsync(teamProjectName, expand: ReleaseDefinitionExpands.Environments).Result; foreach (var reldef in reldefList) { Console.WriteLine(“===========RELEASE DEFINITION==================================”); Console.WriteLine(“ID: {0} PATH: {1} NAME: {2}”, reldef.Id, reldef.Path, reldef.Name); Console.WriteLine(“—————————————————————“); Console.Write(“STAGES:”);
Console.WriteLine(); } |
To get a list of created releases of a team project, we can use the GetReleasesAsync method with a team project name. The method returns all team project releases. If we want to find releases of a specific release definition, we can add a release definition id to the parameters. The resulting list also contains only a basic information. If you want see additional data (environments, artifacts, etc), you can specify that in the expand parameter. Example to get last 10 releases and state of their environments:
var rels = ReleaseClient.GetReleasesAsync(teamProjectName, reldefId, expand: ReleaseExpands.Environments, top: 10).Result; foreach (var rel in rels) { Console.WriteLine(“———–RELEASE———————————————“); Console.WriteLine(“ID: {0} REASON: {1} NAME: {2}”, rel.Id, rel.Reason, rel.Name); Console.WriteLine(“—————————————————————“); Console.WriteLine(“STAGES:”);
Console.WriteLine(); } |
Additionally, we can find changes and items that were passed with a release. Here we should use BuildHttpClient with the GetBuildChangesAsync and GetBuildWorkItemsRefsAsync methods. These methods use a team project name and build id as parameters. If a release is linked to a build, that build will be in the Artifacts property and the Type member of the Artifact property should be equal to “Build”. In this case, we can find a build id in the DefinitionReference dictionary with “version” key. Example to view work items and commits of a release:
var relDetails = ReleaseClient.GetReleaseAsync(teamProjectName, relId).Result; if (relDetails.Artifacts != null) {
{
{
{
Console.WriteLine(“BUILDID : {0}”, buildId); Console.WriteLine(“COMMITS : {0}”, String.Join(“; “, from x in commits select x.Id.Substring(0, 8))); Console.WriteLine(“WORK ITEMS: {0}”, String.Join(“; “, from x in workItems select x.Id)); } } } } |
The sample application:
https://github.com/ashamrai/TFRestApi/tree/master/24.TFRestApiAppExploreReleases