Created
September 13, 2018 19:22
-
-
Save jsblock78/5994bef0266c9f2c867eca807bb2ca0e to your computer and use it in GitHub Desktop.
New-TestCaseAssociatedAutomation.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Updates the test association details for a test case in TFS/VSTS. | |
.DESCRIPTION | |
This script will update the details for automated test association in a TFS/VSTS test case work item. | |
.INPUTS | |
No objects can be piped to this script. | |
.OUTPUTS | |
No objects can be piped to this script. | |
.PARAMETER TeamFoundationCollectionUri | |
The URI of the team foundation collection. For example: https://fabrikamfiber.visualstudio.com/. This defaults to the current team foundation collection. | |
.PARAMETER TeamProject | |
The name of the team project that contains the build to add tags to. This defaults to the current project. | |
.PARAMETER PersonalAccessToken | |
The personal access token used to authenticate with TFS/VSTS. If omitted, the script will attempt to use the system OAUTH token or default credentials. | |
.PARAMETER UseDefaultCredentials | |
Uses the credentials of the current user to make the request. | |
.PARAMETER WorkItemId | |
Required. The ID of the work item representing the test case to associate automation with. | |
.PARAMETER AutomatedTestName | |
Required. The namespace.class.methodname name of the test. | |
.PARAMETER AutomatedTestStorage | |
Required. The container in which the test exists. The test assembly name (e.g. unittestproject1.dll) | |
.PARAMETER AutomatedTestId | |
The ID of the automated test. If not supplied, a GUID will be generated. | |
.PARAMETER AutomatedTestType | |
The type of the automated test. If not supplied, Unit Test will be used. | |
.EXAMPLE | |
# Set test case ID to run UnitTest1. | |
.\Add-TestCaseAssociatedAutomation.ps1 -WorkItemId 818231 -AutomatedTestName MyNamespace.MyProject.MyClass.UnitTest1 -AutomatedTestStorage UnitTestProject1.dll | |
.NOTES | |
Author: Jeff Block <jsblock78@outlook.com> | |
#> | |
[CmdletBinding()] | |
Param ( | |
[string] $TeamFoundationCollectionUri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI, | |
[string] $TeamProject = $env:SYSTEM_TEAMPROJECT, | |
[string] $PersonalAccessToken = "", | |
[switch] $UseDefaultCredentials, | |
[int] $WorkItemId, | |
[string] $AutomatedTestName = "", | |
[string] $AutomatedTestStorage = "", | |
[string] $AutomatedTestId = "$(New-Guid)", | |
[string] $AutomatedTestType = "Unit Test" | |
) | |
# 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 "WorkItemId : $WorkItemId" | |
$baseUrl = "$TeamFoundationCollectionUri/$TeamProject/_apis" | |
If ([string]::IsNullOrEmpty($AutomatedTestName)) { | |
Throw "AutomatedTestName parameter cannot be null or empty." | |
} | |
If ([string]::IsNullOrEmpty($AutomatedTestStorage)) { | |
Throw "AutomatedTestStorage parameter cannot be null or empty." | |
} | |
Write-Verbose "AutomatedTestName : $AutomatedTestName" | |
Write-Verbose "AutomatedTestStorage : $AutomatedTestStorage" | |
Write-Verbose "AutomatedTestId : $AutomatedTestId" | |
Write-Verbose "AutomatedTestType : $AutomatedTestStorage" | |
# 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]" | |
$tagUrl = "$baseUrl/wit/workitems/$WorkItemId`?api-version=1.0" | |
$body = @" | |
[ | |
{ | |
"op": "add", | |
"path": "/fields/Microsoft.VSTS.TCM.AutomatedTestName", | |
"value": "$AutomatedTestName" | |
}, | |
{ | |
"op": "add", | |
"path": "/fields/Microsoft.VSTS.TCM.AutomatedTestStorage", | |
"value": "$AutomatedTestStorage" | |
}, | |
{ | |
"op": "add", | |
"path": "/fields/Microsoft.VSTS.TCM.AutomatedTestId", | |
"value": "$AutomatedTestId" | |
}, | |
{ | |
"op": "add", | |
"path": "/fields/Microsoft.VSTS.TCM.AutomatedTestType", | |
"value": "$AutomatedTestType" | |
}, | |
{ | |
"op": "add", | |
"path": "/fields/Microsoft.VSTS.TCM.AutomationStatus", | |
"value": "Automated" | |
} | |
] | |
"@ | |
$response = Invoke-RestMethod -Uri $tagUrl -Method Patch -Headers:$headers -UseDefaultCredentials:$useDefaultCredentials -ContentType application/json-patch+json -Body $body | |
Write-Verbose "Response:`r`n$response" | |
If ($response.GetType().Name -eq "String") { | |
If ($response.Contains("Visual Studio Team Services | Sign In")) { | |
Throw "Unable to authenticate using supplied authorization." | |
} | |
} | |
If ($tagsArray.Count -gt 0) { | |
ForEach($tag in $tagsArray) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment