How to get all repos in Azure DevOps
Asked Answered
B

4

17

I have number of projects in Azure DevOps. I want to able to iterate through all Repos in Azure DevOps and get the name of the Repo, Creator of Repo and Last Updated/commit.

And get a notification when some one created new Repo?

Bonanno answered 14/9, 2020 at 15:40 Comment(0)
F
25

We can list the repo info via REST API

List all repositories and get the name of the repo:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.1-preview.1

Get the Creator:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=6.1-preview.1

Note: we can get the branch creator via this API, I didn't find any APIs to get the repo creator.

Get latest commit:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?searchCriteria.$top=1&api-version=6.1-preview.1

Get a notification when some one created new Repo

We cannot create this notification, we can get a notification when someone updated the repo code. Please refer this link for more details: Supported event types  

Update1

//List project name
$connectionToken="PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$ProjectUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.1-preview.1" 
$Repo = (Invoke-RestMethod -Uri $ProjectUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$RepoName= $Repo.value.name
Write-Host  $RepoName

//get latest commit info and branch creator
$RepoID=$Repo.value.id
Write-Host  $RepoID
ForEach ($Id in $RepoID)
{

//Get latest commit info
$ProjectUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/$Id/commits?api-version=6.1-preview.1" 
$CommitInfo = (Invoke-RestMethod -Uri $ProjectUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$CommitID = $CommitInfo.value.commitId | Select-Object -first 1
Write-Host $CommitID
$CommitUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/$Id/commits/$($CommitID)?api-version=6.0-preview.1"
$LatestCommitInfo = (Invoke-RestMethod -Uri $CommitUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
Write-Host "LatestCommitInfo = $($LatestCommitInfo | ConvertTo-Json -Depth 100)"

//Get branch name and creatot
$BarchCreatorUrl = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/$Id/refs?api-version=6.1-preview.1"
$CreateorInfo = (Invoke-RestMethod -Uri $BarchCreatorUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
Write-Host $CreateorInfo.value.name
Write-Host $CreateorInfo.value.creator.displayName
}
Fillender answered 15/9, 2020 at 2:38 Comment(7)
Is there any PowerShell script code to do all the above operations in single execution?Bonanno
Hi @Pradeep, I have updated the answer, please check it.Fillender
Thanks @Vito, once I will test the above script, then I will accept your answer.Bonanno
Hi @Pradeep, Feel free to let me know the result. If you still have any questions, I will still be here to help you.Fillender
It is working as expected, but I want to get the creator of the Repo. and I want to display the output in table format.Bonanno
Hi @Pradeep, Please check the doc: Format-Table and Export-CsvFillender
Thanks, my URL was structured differently but this still works: https://{organization}.visualstudio.com/{project}/_apis/git/repositories?api-version=6.1-preview.1Silicle
H
9

Another option (easier with the Auth part) is to retrieve your Azure Repositories is through the AZ CLI using following commands:

  1. Log into Azure

    az login -t $tenant
    
  2. First configure default values for organization and project, this will help you to not specify --project and --organization parameters in every az devops/repos command:

    az devops configure -d organization=$organizationUrl project=$project
    
  3. List repositories of your subscription and default project and organization

    az repos list --subscription $subscription
    
Halide answered 11/2, 2022 at 13:37 Comment(1)
how do we know the subscription?Liuka
G
3
Install-Module -Name VSTeam 
$pat="YYYY"
Set-VSTeamAccount https://dev.azure.com/dXXXX/ -PersonalAccessToken $pat
Get-VSTeamGitRepository
Gladden answered 10/11, 2022 at 12:27 Comment(0)
V
2

or you can run this script (if az devops doesn't work, then add the azure-devops extension) and remember to replace URL-of-your-ado-org with your real URL:

    # will be prompted to log in via browser
    az login

    # get all your ADO projects in your org in json format
    $adoprojs = az devops project list

    # convert to powershell object
    $projObjs = $adoprojs | ConvertFrom-Json

    # loop through each ADO project and find it's repos
    foreach ($proj in $projObjs.value.name) {
        write-host "looking in $proj ADO project for repos"
        # set to specific ADO project
        az devops configure --defaults organization=URl-of-your-ado-org project=$proj
        # now get it's repos (in json)
        $jsonRepos = az repos list
        # convert repos from json format to powershell object
        $RepoObjs = $jsonRepos | ConvertFrom-Json
        # now list each repo
        foreach ($repo in $RepoObjs) {
            write-host "  " $repo.name
        }
    }
Virgil answered 11/4, 2023 at 20:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.