12

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?

Pradeep
  • 5,101
  • 14
  • 68
  • 140

4 Answers4

15

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
}
Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • 1
    Is there any PowerShell script code to do all the above operations in single execution? – Pradeep Sep 15 '20 at 04:21
  • Hi @Pradeep, I have updated the answer, please check it. – Vito Liu Sep 15 '20 at 07:35
  • Thanks @Vito, once I will test the above script, then I will accept your answer. – Pradeep Sep 15 '20 at 10:37
  • Hi @Pradeep, Feel free to let me know the result. If you still have any questions, I will still be here to help you. – Vito Liu Sep 16 '20 at 02:54
  • 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. – Pradeep Sep 16 '20 at 06:03
  • Hi @Pradeep, Please check the doc: [Format-Table](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-table?view=powershell-7) and [Export-Csv](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7) – Vito Liu Sep 16 '20 at 10:47
  • Thanks, my URL was structured differently but this still works: `https://{organization}.visualstudio.com/{project}/_apis/git/repositories?api-version=6.1-preview.1` – mukesh.kumar Mar 11 '22 at 08:01
6

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
    
Henrik Høyer
  • 1,225
  • 1
  • 19
  • 27
Jesus Sanz Losa
  • 159
  • 2
  • 6
2
Install-Module -Name VSTeam 
$pat="YYYY"
Set-VSTeamAccount https://dev.azure.com/dXXXX/ -PersonalAccessToken $pat
Get-VSTeamGitRepository
Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
0

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
        }
    }