DEV Community

absterellio
absterellio

Posted on

Using Azure Devops CLI to generate repository reports

I needed to see some reports on my codebase, such as information about merges to the main branch of code. To do this, I made use of Microsoft’s Azure DevOps CLI and its corresponding extensions to write a PowerShell script that generates a CSV file with a report of completed pull requests.


Resources

  1. Azure DevOps CLI documentation

  2. The completed initial version of this script can be found on my GitHub:

    GitHub logo absterellio / azure-devops-cli-tool-v1

    Basic Powershell script that uses the Azure Devops CLI to generate repository reports


Setup

To begin, we need to set up a few things. The Azure DevOps CLI documentation linked above has more details on this. The CLI installation is a very quick download process.

1. Install Azure DevOps CLI
2. Add the Azure DevOps extension


The Code

Like stated above, my goal was to generate reports on specific repositories in my codebase. Below, I noted some key ways I made use of the CLI in my script.

Authentication:

To begin using the CLI, we need to authenticate. Following the instructions on this page, Azure DevOps PAT documentation, I created my own personal access token (PAT). The following line takes in the token and authenticates against your organization.

echo $pat | az devops login --organization ex-projectLink

"ex-projectLink" should be replaced with the applicable link to the project, and the $pat variable should be the created access token.

Listing pull requests made into a repository:

The CLI specifies a reference for working with repositories (az repos). I added flags to specify completed PR’s that went into the repo’s 'main' branch. "$projectName" should be the name of the project that you are querying.

az repos pr list --project $projectName --repository $repo --status completed --target-branch ex-main --include-links | ConvertFrom-Json

The ConvertFrom-Json is a built in cmdlet that I used to convert the json return value into an object so I can grab properties from it later.

Retrieving work items attached to a pull request:

az repos pr work-item list --id $pr.pullRequestId | ConvertFrom-Json

Replace {$pr.pullRequestId} with the corresponding Id of the pull request you’d like to grab work items for and this will return a list of items that were attached to this PR.

Outputting the results to a csv file:

First, I set the headers of the CSV file that I’ll output my results to.
Set-Content -Path $location -Value '"PR Number","Date Closed","Repo Name","Created By","Reviewers","Work Item Id"'

With the returned PR, grab the id, names of creators/reviewers, closed date, etc. from the pull request and any other applicable values.

$workItemId = $workItem.id
$createdBy = $pr.createdBy.displayName
$reviewedBy = $pr.reviewers.displayName
$date = $pr.closedDate

Then, output the results to the file.

Add-Content -Path $location -Value "$prNumber,$date,$repo,$createdBy,$reviewedByString,$workItemId"

I hope this serves as a quick introduction to the Azure DevOps CLI and its basic uses for querying repository and pull request history. Next, I plan to add a GUI and more usability features to this tool for everyday usage.

Thanks for reading! Feel free to have a look at the script in GitHub and leave thoughts or suggestions for improvement.

Top comments (0)