How do I get the code reviewer(s) of a build in Azure DevOps pipeline? - build

Given a build id, how do I get the code reviewer(s) name in azure DevOps pipeline?
Assume, the build was off of a master branch - which devs merge their feature branch after code has been reviewed in a pull request. This is the policy and no one directly commit their change into master. So that means, every build has a code reviewer behind it. How do i get that?
Thanks!

You can use below Rest api to get the PR reviewers.
1, First call below build rest api with the buildId. And in the response you will get the commit id from the build's sourceVersion and the repository id.
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1
2, After you get the commit id and repository id. You can call commit rest api to get the associated PR id from the comments in the response.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=5.1
3, Then Call pull request reviewer rest api to get the Reviewers.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/reviewers?api-version=5.1
Below is the example scripts in powershell. See this link to get a Personal access token
$buildId= " "
$burl =" https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"
$PAT="personel access token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$buildInfo = Invoke-RestMethod -Uri $curl -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
#get CommitId and repoId
$commitId = $buildInfo.sourceVersion
$repoId=$buildInfo.repository.id
#commit rest api
$curl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/commits/$($commitId)?api-version=5.1"
$commitInfo = Invoke-RestMethod -Uri $curl -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
#get PR id
$prId = $commitInfo.comment.split(" ")[2].TrimEnd(":")
$prurl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/pullRequests/$($prId)/reviewers?api-version=5.1"
Invoke-RestMethod -Uri $prurl -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
If you can find the build from the pipeline runs history in the UI page with a give buildId. It will be much easier. You can get the PR id from the title directly. See below pic.
You can also click on the commit id shown on above screenshot, to see the details of the commit, where you will get the associated PR.

Here is what I have finally working. Took Levi's code snippet above and just fixed a line to get pull request id working in various scenarios. Kudos to Levi's for helping! Hope it helps someone.
$PAT="personel access token"
$base64EncodedPAT = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$basicAuth = #{Authorization = "Basic $base64EncodedPAT" }
$buildId= "..."
function GetCodeReviewers() {
#Get build info
$buildUrl = "https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"
$buildInfo = Invoke-RestMethod -Method Get -Uri $buildUrl -Headers $basicAuth
# Get Commit Info
$commitUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/commits/$($buildInfo.sourceVersion)?api-version=5.1"
$commitInfo = Invoke-RestMethod -Uri $commitUrl -Method Get -Headers $basicAuth
#Get Code Reviewers
$comment = $commitInfo.comment
#$pullRequestId = $comment.split(" ")[2].TrimEnd(":") # it turns out, the 3rd item may not always be the PullRequestID so the next line may not work for all scenarios
#note that, a comment could come as follows:
# case 1: Merge PR 1234: some other text here including story or bug numbers
# case 2: Merge pull request 1234 some additional text goes here including story or bug numbers
# The following will pick the first number - which I assume will always be the PullRequestID
$pullRequestId = $null
$pullRequestId = $comment.Replace(':', '').Split(" ").Trim() | Where-Object {[int]::TryParse($_, $pullRequestId)} | Select-Object -First 1
$pullRequestUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/pullRequests/$($pullRequestId)/reviewers?api-version=5.1"
$reviewers = Invoke-RestMethod -Uri $pullRequestUrl -Method Get -Headers $basicAuth
return $reviewers.value
}

Related

how to take latest backupid to JSON for restore data to an instance in a different project GCP

I am working on restore the latest backup restore data to an instance into a different project GCP in Windows power shell.
Using below queries using power shell getting backup id's list
$cred = gcloud auth print-access-token
$headers = #{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/source-project/instances/source-instance/backupRuns" | Select-Object -Expand Content
Restore using below command
$cred = gcloud auth print-access-token
$headers = #{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/target-project/instances/target-instance/restoreBackup" | Select-Object -Expand Content
In the request.json file updating latest backupid to restore latest backup up.
How to take latest backup id from the list automatically using power shell
Please help any one.

How to creatte an index pattern in Opensearch use the API?

I want to create an index pattern using the Opensearch API. The issue i'm having is with the message body type. I'm getting the following message when running my script
I'm running opensearch 1.3
Invoke-RestMethod: {"statusCode":400,"error":"Bad Request","message":"[request body.attributes]: expected value of type [object] but got [undefined]"}
$baseuri = "https://<clusterendpoint>.<region>.es.amazonaws.com"
#obtain auth cookie
$authbody = #"
{"username":"$apiUserName", "password":"$apiPassword"}
"#
$authUri = "$baseuri/_dashboards/auth/login"
Invoke-RestMethod -Method Post -Uri $authUri -Headers #{"osd-xsrf"="true"} -ContentType "application/json" -Body $authbody -SessionVariable S1
#### create index pattern
$indexid = "indexname"
$body=#"
{
"index_pattern": {
"title": "indexname-*",
"timeFieldName": "#timestamp"
}
}
"#
$uri = "$baseuri/_dashboards/api/saved_objects/index-pattern/$indexid"
Invoke-RestMethod -Method Post -Uri $uri -Headers #{"osd-xsrf"="true"} -ContentType "application/json" -WebSession $S1 -Body $body -Verbose
i've attempted various ways of defining an object with no luck. when I run the invoke with ($body | convertfrom-json) i get a different error message.
Invoke-RestMethod: {"statusCode":400,"error":"Bad Request","message":"[request body]: expected a plain object value, but found [null] instead."}
UPDATE: I did originally attempt to use the create index pattern API for elastic, but this results in a new index being created, not an opensearch index_pattern.
$body=#{
"index_pattern"= #{
"title"= "ai-summary"
}
}
$uri = "$baseuri/api/index_patterns/$indexid"
Invoke-RestMethod -Method Post -Uri $uri -Headers #{"osd-xsrf"="true"} -ContentType "application/json" -WebSession $S1 -Body ($body | ConvertTo-Json) -Verbose -Credential $credObject

Getting Tests Run on Build with Azure DevOps REST APIs

Is there a REST API for getting the number of tests run/failed for a build?
I see there's one for getting Code Coverage but unless I am missing something obvious I can't find an API for just getting the number of tests run for a build.
Looks like that all APIs available to get Test Results require a test runid, but I only have a buildid.
You should try to use the Runs - Query API. Pass the optional build id.
GET https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate={minLastUpdatedDate}&maxLastUpdatedDate={maxLastUpdatedDate}&buildIds={buildIds}&api-version=6.0
It's worth noting that you can customize the title of the run (with the build number included) by setting that on the task step of the pipeline.
I'm not sure if there is endpoint dedicated for tests but you can get tests from logs
$AzureDevOpsAuthenicationHeader = #{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(System.AccessToken)")) }
$uri = "https://dev.azure.com/$(devopsAccount)/$(projectName)/_apis/build/builds/$(Build.BuildId)/logs/$(logId)?api-version=5.1"
Write-Host $uri
# Invoke the REST call
$result = Invoke-RestMethod -Uri $uri -Method Get -Headers $AzureDevOpsAuthenicationHeader
Write-Host $result
$lines = $result.Split([Environment]::NewLine)
$passed = 0;
$failed = 0;
foreach($line in $lines) {
if ($line -match "Passed:.(\d+)") {
$passed = $matches[1]
}
if ($line -match "Failed:.(\d+)") {
$failed = $matches[1]
}
}
echo $passed
echo $failed
You need to pass your build id and log id. To get log id please get all logs and go trough them and find the one with your task with tests.
In terms of buildId vs runId, they are the same. I mean buildId = runId. With newer syntax there were a change in nomenclature.
You can use the Runs Query Api as Matt mentioned. However i found the buildId query parameter didnot work probably. You may need to filter the api results by the buildId. See below example:
$connectionToken="Personal Access Token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate=2020-10-20&maxLastUpdatedDate=2020-10-22&api-version=6.0"
$results= Invoke-RestMethod -Uri $trurl -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
$run = $results.value | Where-Object{$_.buildConfiguration.id -eq $buildId}
$runId = $run.id
You can also check out Runs List Api. I found the buildId was always appended to the run title. You can filter api results by the run name. See below:
$connectionToken="Personal Access Token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?api-version=6.0"
$results= Invoke-RestMethod -Uri $trurl -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
$run = $results.value | Where-Object{$_.name -match $buildId}
$runId = $run.id

List of Google API endpoints call by the SDK

We have previously generated a list of Google's API end-points utilised by the SDK by grepping the source repo. Now that that doesn't seem to be available, has anyone else found a way of obtaining such a list? We need to be able to whitelist these end-points on our corporate firewall/proxy.
Thanks!
PART 1
If your objective is to whitelist URLs for your firewall, the URL *.googleapis.com will cover 99% of everything you need. There are only a few endpoints left:
bookstore.endpoints.endpoints-portal-demo.cloud.goog
cloudvolumesgcp-api.netapp.com
echo-api.endpoints.endpoints-portal-demo.cloud.goog
elasticsearch-service.gcpmarketplace.elastic.co
gcp.redisenterprise.com
payg-prod.gcpmarketplace.confluent.cloud
prod.cloud.datastax.com
PART 2
List the Google API endpoints that are available for your project with this command:
gcloud services list --available --format json | jq -r ".[].config.name"
https://cloud.google.com/sdk/gcloud/reference/services/list
Refer to PART 5 for a PowerShell script that produces a similar list.
PART 3
Process the Discovery Document which provides machine readable information:
Google API Discovery Service
curl https://www.googleapis.com/discovery/v1/apis | jq -r ".items[].discoveryRestUrl"
Once you have a list of discovery documents, process each document and extract the rootUrl key.
curl https://youtubereporting.googleapis.com/$discovery/rest?version=v1 | jq -r ".rootUrl"
PART 4
PowerShell script to process the Discovery Document and generate an API endpoint list:
Copy this code to a file named list_google_apis.ps1. Run the command as follows:
powershell ".\list_google_apis.ps1 | Sort-Object -Unique | Out-File -Encoding ASCII -FilePath apilist.txt"
There will be some errors displayed as some of the discovery document URLs result in 404 (NOT FOUND) errors.
$url_discovery = "https://www.googleapis.com/discovery/v1/apis"
$params = #{
Uri = $url_discovery
ContentType = 'application/json'
}
$r = Invoke-RestMethod #params
foreach($item in $r.items) {
$url = $item.discoveryRestUrl
try {
$p = #{
Uri = $url
ContentType = 'application/json'
}
$doc = Invoke-RestMethod #p
$doc.rootUrl
} catch {
Write-Host "Failed:" $url -ForegroundColor Red
}
}
PART 5
PowerShell script that I wrote a while back that produces similar output to gcloud services list.
Documentation for the API:
https://cloud.google.com/service-usage/docs/reference/rest/v1/services/list
<#
.SYNOPSIS
This program displays a list of Google Cloud services
.DESCRIPTION
Google Service Management allows service producers to publish their services on
Google Cloud Platform so that they can be discovered and used by service consumers.
.NOTES
This program requires the Google Cloud SDK CLI is installed and set up.
https://cloud.google.com/sdk/docs/quickstarts
.LINK
PowerShell Invoke-RestMethod
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-5.1
Google Cloud CLI print-access-token Documentation
https://cloud.google.com/sdk/gcloud/reference/auth/print-access-token
Google Cloud API Documentation
https://cloud.google.com/service-infrastructure/docs/service-management/reference/rest
https://cloud.google.com/service-usage/docs/reference/rest/v1/services
https://cloud.google.com/service-infrastructure/docs/service-management/reference/rest/v1/services/list
#>
function Get-AccessToken {
# Get an OAuth Access Token
$accessToken=gcloud auth print-access-token
return $accessToken
}
function Display-ServiceTable {
Param([array][Parameter(Position = 0, Mandatory = $true)] $serviceList)
if ($serviceList.Count -lt 1) {
Write-Output "No services were found"
return
}
# Display as a table
$serviceList.serviceConfig | Select name, title | Format-Table -Wrap | more
}
function Get-ServiceList {
Param([string][Parameter(Position = 0, Mandatory = $true)] $accessToken)
# Build the url
# https://cloud.google.com/service-infrastructure/docs/service-management/reference/rest/v1/services/list
$url="https://servicemanagement.googleapis.com/v1/services"
# Build the Invoke-RestMethod parameters
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-5.1
$params = #{
Headers = #{
Authorization = "Bearer " + $accessToken
}
Method = 'Get'
ContentType = "application/json"
}
# Create an array to store the API output which is an array of services
$services = #()
# Google APIs page the output
$nextPageToken = $null
do {
if ($nextPageToken -eq $null)
{
$uri = $url
} else {
$uri = $url + "?pageToken=$nextPageToken"
}
try {
# Get the list of services
$output = Invoke-RestMethod #params -Uri $uri
} catch {
Write-Host "Error: REST API failed." -ForegroundColor Red
Write-Host "URL: $url" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
return $services
}
# Debug: Display as JSON
# $output | ConvertTo-Json
# Append services to list
$services += $output.services
$nextPageToken = $output.nextPageToken
} while ($nextPageToken -ne $null)
return $services
}
############################################################
# Main Program
############################################################
$accessToken = Get-AccessToken
$serviceList = Get-ServiceList $accessToken
Display-ServiceTable $serviceList
Command-line tool JQ

Using powershell to download an embedded video

I need to download a monthly broadcast automatically (will set a scheduled task) using powershell.
Here is the embedded URL: https://www.jw.org/download/?fileformat=MP4&output=html&pub=jwb&issue=201601&option=TRGCHlZRQVNYVrXF&txtCMSLang=E
The only thing that changes each month is the 201602, 201603, etc. Once I have able to pull the 720p video file, I will work on programmatically adding that part of the URL, based on the current system clock (I can manage this)
I have tried these without success:
Attempt 1:
$source = "https://www.jw.org/download/?fileformat=MP4&output=html&pub=jwb&issue=201601&option=TRGCHlZRQVNYVrXF&txtCMSLang=E"
$destination = "c:\broadcasts\test.mp4"
Invoke-WebRequest $source -OutFile $destination
Attempt 2:
$source = "https://www.jw.org/download/?fileformat=MP4&output=html&pub=jwb&issue=201601&option=TRGCHlZRQVNYVrXF&txtCMSLang=E"
$dest = "c:\broadcasts\test.mp4"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)
Attempt 3:
Import-Module BitsTransfer
$url = "https://www.jw.org/download/?fileformat=MP4&output=html&pub=jwb&issue=201601&option=TRGCHlZRQVNYVrXF&txtCMSLang=E"
$output = "c:\broadcasts\test.mp4"
Start-BitsTransfer -Source $url -Destination $output
Both of these end up with a test.mp4 that is basically just an empty file.
Then I found the another page that holds the video (and the download links for different qualities) and tried to pull these links using the following (I know I could have used $webpage.links):
Attempt 4:
$webpage=Invoke-webrequest "http://tv.jw.org/#en/video/VODStudio/pub-
jwb_201601_1_VIDEO"
$webpage.RawContent | Out-File "c:\scripts\webpage.txt" ASCII -Width 9999
And found that the raw content doesn't have the mp4 visible. My idea was to pull the raw content, parse it with regex and grab the 720p URL, save it in a variable and then send that to a BitsTransfer bit of code.
Please help?