Skip to content

Commit

Permalink
Add Rest Method checks to Prepare-Release (Azure#15290)
Browse files Browse the repository at this point in the history
Co-authored-by: Wes Haggard <Wes.Haggard@microsoft.com>
  • Loading branch information
2 people authored and vindicatesociety committed Sep 18, 2021
1 parent 2042c90 commit fea5997
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
62 changes: 35 additions & 27 deletions eng/common/scripts/Helpers/DevOps-WorkItem-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@ $ReleaseDevOpsOrgParameters = @("--organization", "https://dev.azure.com/azure-
$ReleaseDevOpsCommonParameters = $ReleaseDevOpsOrgParameters + @("--output", "json")
$ReleaseDevOpsCommonParametersWithProject = $ReleaseDevOpsCommonParameters + @("--project", "Release")

function Get-DevOpsRestHeaders()
{
$headers = $null
if (Get-Variable -Name "devops_pat" -ValueOnly -ErrorAction "Ignore")
{
$encodedToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([string]::Format("{0}:{1}", "", $devops_pat)))
$headers = @{ Authorization = "Basic $encodedToken" }
}
else
{
# Get a temp access token from the logged in az cli user for azure devops resource
$jwt_accessToken = (az account get-access-token --resource "499b84ac-1321-427f-aa17-267ca6975798" --query "accessToken" --output tsv)
$headers = @{ Authorization = "Bearer $jwt_accessToken" }
}

return $headers
}

function CheckDevOpsAccess()
{
# Dummy test query to validate permissions
$query = "SELECT [System.ID] FROM WorkItems WHERE [Work Item Type] = 'Package' AND [Package] = 'azure-sdk-template'"

$response = Invoke-RestMethod -Method POST `
-Uri "https://dev.azure.com/azure-sdk/Release/_apis/wit/wiql/?api-version=6.0" `
-Headers (Get-DevOpsRestHeaders) -Body "{ ""query"": ""$query"" }" -ContentType "application/json" | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashTable

if ($response -isnot [HashTable] -or !$response.ContainsKey("workItems")) {
throw "Failed to run test query against Azure DevOps. Please ensure you are logged into the public azure cloud. Consider running 'az logout' and then 'az login'."
}
}

function Invoke-AzBoardsCmd($subCmd, $parameters, $output = $true)
{
$azCmdStr = "az boards ${subCmd} $($parameters -join ' ')"
Expand All @@ -26,23 +58,11 @@ function Invoke-Query($fields, $wiql, $output = $true)
Write-Host "Executing query $wiql"
}

$headers = $null
if (Get-Variable -Name "devops_pat" -ValueOnly -ErrorAction "Ignore")
{
$encodedToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([string]::Format("{0}:{1}", "", $devops_pat)))
$headers = @{ Authorization = "Basic $encodedToken" }
}
else
{
# Get a temp access token from the logged in az cli user for azure devops resource
$jwt_accessToken = (az account get-access-token --resource "499b84ac-1321-427f-aa17-267ca6975798" --query "accessToken" --output tsv)
$headers = @{ Authorization = "Bearer $jwt_accessToken" }
}
$response = Invoke-RestMethod -Method POST `
-Uri "https://dev.azure.com/azure-sdk/Release/_apis/wit/wiql/?`$top=10000&api-version=6.0" `
-Headers $headers -Body $body -ContentType "application/json" | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashTable
-Headers (Get-DevOpsRestHeaders) -Body $body -ContentType "application/json" | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashTable

if (!$response.workItems) {
if ($response -isnot [HashTable] -or !$response.ContainsKey("workItems")) {
Write-Verbose "Query returned no items. $wiql"
return ,@()
}
Expand Down Expand Up @@ -891,20 +911,8 @@ function UpdatePackageVersions($pkgWorkItem, $plannedVersions, $shippedVersions)

$body = "[" + ($fieldUpdates -join ',') + "]"

$headers = $null
if (Get-Variable -Name "devops_pat" -ValueOnly -ErrorAction "Ignore")
{
$encodedToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([string]::Format("{0}:{1}", "", $devops_pat)))
$headers = @{ Authorization = "Basic $encodedToken" }
}
else
{
# Get a temp access token from the logged in az cli user for azure devops resource
$jwt_accessToken = (az account get-access-token --resource "499b84ac-1321-427f-aa17-267ca6975798" --query "accessToken" --output tsv)
$headers = @{ Authorization = "Bearer $jwt_accessToken" }
}
$response = Invoke-RestMethod -Method PATCH `
-Uri "https://dev.azure.com/azure-sdk/_apis/wit/workitems/${id}?api-version=6.0" `
-Headers $headers -Body $body -ContentType "application/json-patch+json" | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashTable
-Headers (Get-DevOpsRestHeaders) -Body $body -ContentType "application/json-patch+json" | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashTable
return $response
}
2 changes: 2 additions & 0 deletions eng/common/scripts/Update-DevOps-Release-WorkItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ if (!$?){
. (Join-Path $PSScriptRoot SemVer.ps1)
. (Join-Path $PSScriptRoot Helpers DevOps-WorkItem-Helpers.ps1)

CheckDevOpsAccess

$parsedNewVersion = [AzureEngSemanticVersion]::new($version)
$state = "In Release"
$releaseType = $parsedNewVersion.VersionType
Expand Down

0 comments on commit fea5997

Please sign in to comment.