|
<# |
|
|
|
.SYNOPSIS |
|
|
|
Gets tags for a TFS/VSTS build. |
|
|
|
.DESCRIPTION |
|
|
|
This script gets the tags associated with a specific build in TFS/VSTS. Authentication can be performed using the current users credentials, a personal access token, or system OAUTH. |
|
|
|
.INPUTS |
|
|
|
No objects can be piped to this script. |
|
|
|
.OUTPUTS |
|
|
|
A string representing a list of tags, separated by a comma (,). |
|
|
|
.PARAMETER TeamFoundationCollectionUri |
|
|
|
The URI of the team foundation collection. For example: https://fabrikamfiber.visualstudio.com/. This will default to the current server. |
|
|
|
.PARAMETER TeamProject |
|
|
|
The name of the team project that contains the build to retrieve tags from. This will default to the current project. |
|
|
|
.PARAMETER BuildId |
|
|
|
The ID of the build to retreive tags from, this will default to primary artifact build id. |
|
|
|
.PARAMETER PersonalAccessToken |
|
|
|
The personal access token used to authenticate with TFS/VSTS. If omitted, the script will attempt to use the system OAUTH token. |
|
|
|
.PARAMETER UseDefaultCredentials |
|
|
|
Uses the credentials of the current user to make the request. |
|
|
|
.EXAMPLE |
|
|
|
# Get tags for build 988. |
|
|
|
.\Get-TfsBuildTags.ps1 -BuildId 988 |
|
|
|
# Returns: |
|
# Test1,Test2 |
|
|
|
.NOTES |
|
|
|
Author: Jeff Block <jeff.block@nosnitor.net> |
|
About: http://www.rcsit.com/2017/07/17/getting-tags-from-a-vsts-tfs-build/ |
|
Location: https://gist.github.com/jsblock78/907db5c0c315273b47887eb52f77c895 |
|
|
|
#> |
|
|
|
[CmdletBinding()] |
|
Param ( |
|
[string] $TeamFoundationCollectionUri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI, |
|
[string] $TeamProject = $env:SYSTEM_TEAMPROJECT, |
|
[int] $BuildId = $env:BUILD_BUILDID, |
|
[string] $PersonalAccessToken = "", |
|
[switch] $UseDefaultCredentials |
|
) |
|
|
|
# Validate parameters |
|
If ([string]::IsNullOrEmpty($TeamFoundationCollectionUri)) { |
|
Throw "TeamFoundationCollectionUri parameter cannot be null or empty." |
|
} |
|
|
|
If ([string]::IsNullOrEmpty($TeamProject)) { |
|
Throw "TeamProject parameter cannot be null or empty." |
|
} |
|
|
|
if ($BuildId -eq 0) { |
|
Throw "BuildId parameter must be supplied." |
|
} |
|
|
|
If (!([string]::IsNullOrEmpty($PersonalAccessToken)) -and ($UseDefaultCredentials -eq $true)) { |
|
Throw "PersonalAccessToken and UseDefaultCredentials parameters can not both be supplied." |
|
} |
|
|
|
Write-Verbose "TeamFoundationCollectionUri : $TeamFoundationCollectionUri" |
|
Write-Verbose "TeamProject : $TeamProject" |
|
Write-Verbose "BuildId : $BuildId" |
|
$baseUrl = "$TeamFoundationCollectionUri/$TeamProject/_apis" |
|
|
|
# Auth |
|
$token = "" |
|
$headers = $null |
|
If (!($UseDefaultCredentials)) { |
|
If (!([string]::IsNullOrEmpty($PersonalAccessToken))) { |
|
Write-Verbose "Using personal access token." |
|
$creds = ":$($PersonalAccessToken)" |
|
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($creds)) |
|
$token = "Basic $encodedCreds" |
|
$headers = @{Authorization = $token} |
|
} |
|
ElseIf (!([string]::IsNullOrEmpty($env:System_AccessToken))) { |
|
Write-Verbose "Using system access token." |
|
$token = "Bearer $($env:System_AccessToken)" |
|
$headers = @{Authorization = $token} |
|
} |
|
Else { |
|
$UseDefaultCredentials = $true |
|
} |
|
} |
|
Write-Verbose "BaseUrl: [$baseUrl]" |
|
Write-Verbose "Token: [$token]" |
|
|
|
$buildUrl = "$baseUrl/build/builds/$BuildId`?api-version=2.0" |
|
$response = Invoke-RestMethod -Uri $buildUrl -Method Get -Headers:$headers -UseDefaultCredentials:$useDefaultCredentials |
|
if ($response.GetType().Name -eq "String") { |
|
if ($response.Contains("Visual Studio Team Services | Sign In")) { |
|
Throw "Unable to authenticate using supplied authorization." |
|
} |
|
} |
|
if ($response.Tags.Count -gt 0) { |
|
$tags = $response.Tags -join "," |
|
return $tags |
|
} |