Capture commit count per pull request in Azure DevOps - azure-devops

I would like to get info on the number of commits per pull request. For now, semi-automated would be fine. Just looking for a simple approach, say, a PowerShell script. I'll likely need to create similar types of reports with different data in the future.
Does any approach lend itself to such a quick and dirty approach with Azure DevOps data?

There is a special Rest API for this: Pull Request Commits - Get Pull Request Commits:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/commits?api-version=5.1
So, simple PowerShell script:
$pat = "YOUR-PERSONAL-ACCESS-TOKEN"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,"$pat")))
$headers = #{Authorization=("Basic {0}" -f $base64AuthInfo)}
$url = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/commits?api-version=5.1"
$commitsCount = (Invoke-RestMethod -Method Get -Uri $url -Headers $headers -Body $jsonBody -ContentType 'application/json').count

Related

Managing branches with Azure DevOps Classic Build Pipelines and TFVC

How do you manage building from branches when using Azure DevOps Classic Build Pipelines and TFVC?
I believe that the only viable option is to copy the build pipeline with a new name and update the source code mapping to point to the new TFVC branch.
I see the ADO web UI provides the option to clone an individual build definition, yet as I have over 200+ build pipelines to clone whenever I branch is there a more efficient way to do this? Or is the only option to write a custom tool to leverage the ADO REST Api?
Since you need to clone pipelines in batches, using scripts to run the Rest API will be a reasonable method. As far as I know, there is no easy way out of the box other than this.
You could try the following PowerShell Script Sample:
$DefinitionIds = "PipelineIDs" #InPut All Pipelineids(e.g. "324,323,xxx" )
$DefinitionId = $DefinitionIds.split(",");
$token = "PAT Token"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
foreach ($i in $DefinitionId)
{
echo $i
$url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/definitions/$($i)?api-version=6.0"
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Get -ContentType application/json
Write-Host "$($response | ConvertTo-Json -Depth 100)"
$response.repository.properties.tfvcMapping= '{"mappings":[{"serverPath":"$/TFVCBranchName","mappingType":"map","localPath":"\\"}]}' # ServerPath is the Branch name
$response.repository.name = "TFVCRepoName" #Repo Source Name
$response.name = "Pipeline $i Clone" # Cloned PipelineName
echo $response.name
$url1= "https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/definitions?api-version=6.0"
$json = #($response) | ConvertTo-Json -Depth 100
$response1 = Invoke-RestMethod -Uri $url1 -Headers #{Authorization = "Basic $token"} -Method Post -Body $json -ContentType application/json
}
Here are the Two Rest APIs used in the Script:
Definitions - Get
Definitions - Create
Result:
The cloned Pipeline will be set to the new TFVC branch and Build definition name.

How to retrieve list of projects within an organisation by running a query in Azure DevOps?

I want all the projects within a collection (organization) listed by running a query.
I searched so many resources but I hadn't got any relevant results. anybody
You can get that list if you visit Organization Page. And you cannot get that list using queries, you should REST APIs.
There is an extension in Azure DevOps Marketplace which lists projects and properties like process template etc. you can take a look at it here
You can't do ith with query.
You can do it with Rest API Projects - List, for example in PowerShell:
$pat = "YOUR-PERSONAL-ACCESS-TOKEN"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,"$pat")))
$headers = #{Authorization=("Basic {0}" -f $base64AuthInfo)}
$azureDevOpsUrl = "https://dev.azure.com/{your-organization-name}"
$projects = Invoke-RestMethod -Uri "$azureDevOpsUrl/_apis/projects" -Method Get -Headers $headers -ContentType application/json

How to access Workitem Fields in azure devops (Like Development ,deployment,link fields)

Can anybody tell me how to GET the fields associated to workitem
Like Development field in azure devops.
OR
How to fetch the links associated to workitems in azure devops.
You can call Work item rest api to get the links associated to workitems by adding the `$expand=relations to the api string..
GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?`$expand=relations&api-version=5.0
You can check below example in powershell script.
$url = "https://dev.azure.com/<Org>/<Proj>/_apis/wit/workitems/5?`$expand=All&api-version=5.0"
$connectionToken = "<PAT>"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$result = Invoke-RestMethod -Uri $url -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method get
Then you can get the associated linked in the relations property.

Azure DevOps API - Set Board Options

I'm trying to do few things with Azure DevOps projects through Rest API, but I really quickly got stuck.
For example, I want to use the method "Set Board Options", documentation is here https://learn.microsoft.com/en-us/rest/api/azure/devops/work/boards/set%20board%20options?view=azure-devops-rest-5.1
Does anyone have an idea of how to find out which options (and how exactly) I can use? Is there any way to lookup these values in the Azure DevOps portal or somewhere else?
I was trying to use the .Net library (https://github.com/microsoft/azure-devops-dotnet-samples), but the situation there is the same. Method SetBoardOptions exists but takes Dictionary of strings as the first argument and there is no documentation on how to actually fill up this dictionary, which values are possible to use etc.
This API is in preview so I guess this is the reason why the docs so poor:
After a big search and tries, I think you can change 2 options in the board settings:
1) statusBadgeIsPublic - True or False.
2) cardReordering - 0 or 1.
I success to do it with PowerShell:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,"MY-PAT")))
$headers = #{Authorization=("Basic {0}" -f $base64AuthInfo)}
$URI = "https://dev.azure.com/{org}/{project}/{team}/_apis/work/boards/{board}?api-version=5.1"
$params = #{
"cardReordering"="1";
} | ConvertTo-Json
Invoke-RestMethod -Method Put -Uri $URI -Headers $headers -Body $params -ContentType 'application/json'

How would you access attributes of work items (Features, User Stories, Bugs, or Tasks) associated with a build in the release pipeline?

My team and I are trying to find a way to automatically build release notes and generate them in markdown to be put into a Wiki for users to access. I found a video from Microsoft where their team uses a process where they have release notes for each feature in their Feature objects, but they have manually query those objects and build a markdown file for each release manually. I'm sure by now they must have figured this out, but can't find anything from them yet on that.
Now, I have seen a couple of market place tools (Bravo Notes for one) that do this, but I figured there must be a way we can make a task ourselves that does this for us and automatically place the markdown file in our Wiki (after an approval process of course).
If anyone has any ideas on how I can accomplish this, please let me know. I'm not afraid of getting my hands dirty with some Powershell scripts.
You can use Azure DevOps Rest API to get the work item that associated to the build, then get work item details. create from the details a Markdown file and add it to the wiki.
Example to PowerShell script that do it (get work items asscoited with the build and print the AssignedTo field):
$user = ""
$token = "MY-PAT"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$url = "https://dev.azure.com/{org}/{project}/_apis/build/builds/{buildId}/workitems?api-version=5.1"
$workItems = Invoke-RestMethod -Uri $url -Method Get -ContentType application/json -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}
$json = $workItems | ConvertTo-Json
$workItems.value.ForEach({
$workItem = Invoke-RestMethod -Uri $_.url -Method Get -ContentType application/json -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host $workItem.fields.'System.AssignedTo'.displayName
})