Last active
February 25, 2019 20:22
-
-
Save TomasBouda/ab6b5136be29d21aa625c448c6393e53 to your computer and use it in GitHub Desktop.
Extending Azure Build Pipeline with PowerShell
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 | |
Creates common headers for Azure Devops API | |
.DESCRIPTION | |
Creates common headers for Azure Devops API with prepared Authorization token and Content-Type application/json. | |
Must be executed from Azure Devops Pipeline to have the $env:SYSTEM_ACCESSTOKEN variable available. | |
.INPUTS | |
None. You cannot pipe objects to Get-CommonHeaders. | |
.OUTPUTS | |
PSObject | |
.EXAMPLE | |
C:\PS> Get-CommonHeaders | |
#> | |
function Get-CommonHeaders(){ | |
return @{ | |
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" # Agent Job - Aditional options - Allow scripts to access the OAuth token | |
'Content-Type' = 'application/json' | |
} | |
} | |
<# | |
.SYNOPSIS | |
Gets RepositoryId from $env:BUILD_REPOSITORY_NAME | |
.DESCRIPTION | |
Gets RepositoryId from $env:BUILD_REPOSITORY_NAME. | |
Must be executed from Azure Devops Pipeline to have the $env:BUILD_REPOSITORY_NAME variable available. | |
.INPUTS | |
None. You cannot pipe objects to Get-CommonHeaders. | |
.OUTPUTS | |
System.String, RepositoryId | |
.EXAMPLE | |
C:\PS> Get-RepositoryId | |
.LINK | |
https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/list?view=azure-devops-rest-5.0 | |
#> | |
function Get-RepositoryId(){ | |
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories?api-version=5.0" | |
$response = Invoke-RestMethod -Method 'Get' -Uri $url -Headers (Get-CommonHeaders) | |
$x = $response.value | Where-Object {$_.Name -eq $env:BUILD_REPOSITORY_NAME} | |
return $x.Id | |
} | |
# Initialize RepositoryId | |
[string]$REPOSITORY_ID = Get-RepositoryId | |
<# | |
.SYNOPSIS | |
Logs error issue to timeline record of current task. | |
.DESCRIPTION | |
Logs error issue to timeline record of current task. | |
.INPUTS | |
System.String - Message to be logged | |
.OUTPUTS | |
Write-TaskError sends the objects to the host. It does not return any objects. However, the host might display the objects that Write-TaskError sends to it. | |
.EXAMPLE | |
C:\PS> Write-TaskError "Some message" | |
.LINK | |
https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md | |
#> | |
function Write-TaskError(){ | |
Param( | |
[parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
Position=0)] | |
[String] | |
$Message | |
) | |
Write-Host "##vso[task.logissue type=error;] $Message" | |
} | |
<# | |
.SYNOPSIS | |
Logs warning issue to timeline record of current task. | |
.DESCRIPTION | |
Log warning issue to timeline record of current task. | |
.INPUTS | |
System.String - Message to be logged | |
.OUTPUTS | |
Write-TaskError sends the objects to the host. It does not return any objects. However, the host might display the objects that Write-TaskError sends to it. | |
.EXAMPLE | |
C:\PS> Write-TaskWarning "Some message" | |
.LINK | |
https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md | |
#> | |
function Write-TaskWarning(){ | |
Param( | |
[parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
Position=0)] | |
[String] | |
$Message | |
) | |
Write-Host "##vso[task.logissue type=warning;] $Message" | |
} | |
<# | |
.SYNOPSIS | |
Returns raw file from given repository | |
.DESCRIPTION | |
Log warning issue to timeline record of current task. | |
.PARAMETER RepositoryId | |
Id of Repository where the file is located | |
.PARAMETER FilePath | |
Path to a file inside repository | |
.PARAMETER Branch | |
Name of a branch from which file will be returned | |
.OUTPUTS | |
Write-TaskError sends the objects to the host. It does not return any objects. However, the host might display the objects that Write-TaskError sends to it. | |
.EXAMPLE | |
C:\PS> Get-FileFromRepository -RepositoryId "c5357e8b-b9f8-4f71-82a7-5f7512387aa6" -FilePath "Scripts\Fnctions.ps1" -Branch "develop" | |
.LINK | |
https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md | |
#> | |
function Get-FileFromRepository(){ | |
Param( | |
[parameter(Mandatory=$true)] | |
[string]$RepositoryId, | |
[parameter(Mandatory=$true)] | |
[string]$FilePath, | |
[parameter(Mandatory=$false)] | |
[string]$Branch="master" | |
) | |
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/sourceProviders/TfsGit/filecontents?repository=$RepositoryId&path=$FilePath&api-version=5.0-preview.1&commitOrBranch=$Branch" | |
return Invoke-RestMethod -Method 'Get' -Uri $url -Headers (Get-CommonHeaders) | |
} | |
<# | |
.SYNOPSIS | |
Adds comment to related Pull Request | |
.DESCRIPTION | |
Adds comment to related Pull Request | |
.PARAMETER Content | |
Comment message | |
.INPUTS | |
Comment message | |
.OUTPUTS | |
Write-TaskError sends the objects to the host. It does not return any objects. However, the host might display the objects that Write-TaskError sends to it. | |
.EXAMPLE | |
C:\PS> Add-Comment "Some comment" | |
.LINK | |
https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20request%20threads/create?view=azure-devops-rest-5.0 | |
#> | |
function Add-Comment(){ | |
Param( | |
[parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
Position=0)] | |
[string] | |
$Content | |
) | |
if($Env:SYSTEM_PULLREQUEST_PULLREQUESTID){ | |
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$(Get-RepositoryId)/pullRequests/$Env:SYSTEM_PULLREQUEST_PULLREQUESTID/threads?api-version=5.0" | |
$jsonBody = ConvertTo-Json -InputObject @{ | |
comments = @( | |
@{ | |
parentCommentId = 0 | |
content = $Content | |
} | |
) | |
status = 1 | |
threadContext = $null | |
pullRequestThreadContext = $null | |
} | |
Invoke-RestMethod -Method 'Post' -Uri $url -Headers (Get-CommonHeaders) -Body $jsonBody | |
} | |
else{ | |
Write-TaskError "Missing PullRequestId" | |
} | |
} | |
<# | |
.SYNOPSIS | |
Cancels Pull Request Autocomplete | |
.DESCRIPTION | |
Cancels Pull Request Autocomplete. Personal access token: $env:PR_TOKEN variable is required. | |
The variable $env:PR_TOKEN can be setup in Azure Devops Build pipeline, environment variables. | |
.INPUTS | |
None. You cannot pipe objects to Suspend-Autocomplete. | |
.OUTPUTS | |
Suspend-Autocomplete does not return any objects. | |
.EXAMPLE | |
C:\PS> Suspend-Autocomplete | |
.LINK | |
https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/update?view=azure-devops-rest-5.0 | |
#> | |
function Suspend-Autocomplete(){ | |
if($Env:SYSTEM_PULLREQUEST_PULLREQUESTID){ | |
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$(Get-RepositoryId)/pullRequests/$($Env:SYSTEM_PULLREQUEST_PULLREQUESTID)?api-version=5.0" | |
$jsonBody = ConvertTo-Json -InputObject @{ | |
autoCompleteSetBy = @{ | |
id = "00000000-0000-0000-0000-000000000000" | |
} | |
} | |
$prHeaders = @{ | |
Authorization = "Basic $env:PR_TOKEN" | |
'Content-Type' = 'application/json' | |
} | |
Invoke-RestMethod -Method 'Patch' -Uri $url -Headers $prHeaders -Body $jsonBody | |
} | |
else{ | |
Write-TaskError "Missing PullRequestId" | |
} | |
} | |
# Output environment variables | |
Write-Debug "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = $Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" | |
Write-Debug "SYSTEM_TEAMPROJECTID = $env:SYSTEM_TEAMPROJECTID" | |
Write-Debug "BUILD_REPOSITORY_NAME = $Env:BUILD_REPOSITORY_NAME" | |
Write-Debug "SYSTEM_PULLREQUEST_PULLREQUESTID = $Env:SYSTEM_PULLREQUEST_PULLREQUESTID" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment