How to see commits by user accross multiple repositories in Azure DevOps?
Asked Answered
T

1

5

I want to see the commits by a user across all repositories in my organization in Azure DevOps. I cannot see such options available.

I can see the below option in Azure DevOps, but it shows commits in a single repository.

enter image description here

I know we can use the below git command to see the commits in a single repository.

git log --author<AuthorName> --all ( or --branches)
Termagant answered 29/3, 2021 at 7:33 Comment(3)
Hi, How about the issue? Does the answer below resolved your question? If not, would you please let me know the latest information about this issue?Ogdoad
@VitoLiu-MSFT sorry, I couldn't work on this item, I will verify today and update youTermagant
Hi, You could try it and share the results here. If you have any follow-up questions, you can also share them here and I will still be here to help you. Have a nice day. :)Ogdoad
O
8

Check this REST API sample By author, it contain the filed author, we could enter Alias or display name of the author to list all commit info.

In addition, we could add the filed searchCriteria.itemVersion.version to filter branch.

GET https://dev.azure.com/{Org name}/{project name}/_apis/git/repositories/{repo name}/commits?searchCriteria.author={searchCriteria.author}&searchCriteria.itemVersion.version={branch name}&api-version=6.0

I want to see the commits by a user across all repositories in my organization in Azure DevOps

a. List all projects via org name and get the project name.

GET https://dev.azure.com/{organization}/_apis/projects?api-version=6.0

b. List all repos via org name and project name and get the repo name.

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

c. List all branches via org name, project name and repo name, then get branch name

GET https://dev.azure.com/{Org name}/{project name}/_apis/git/repositories/{repo name}/refs?filter=heads&api-version=6.1-preview.1

d. List commit info via Alias or display name of the author

GET https://dev.azure.com/{Org name}/{project name}/_apis/git/repositories/{repo name}/commits?searchCriteria.author={searchCriteria.author}&searchCriteria.itemVersion.version={branch name}&api-version=6.0

Power shell script:

cls

#List all projects via org name
$ListAllProjectsURL="https://dev.azure.com/{org name}/_apis/projects?api-version=6.0"
$PAT="{pat}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

#get the project name
$ListAllProjects = Invoke-RestMethod -Uri $ListAllProjectsURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

ForEach ($ProjectName in $ListAllProjects.value.name){
    #Write-Host $ProjectName
    #List all repos via org name and project name and get the repo name.
    $ListAllRepoURL = "https://dev.azure.com/{org name}/$($ProjectName)/_apis/git/repositories?api-version=6.0"
    $ListAllRepo = Invoke-RestMethod -Uri $ListAllRepoURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

    ForEach ($RepoName in $ListAllRepo.value.name){
        #Write-Host $RepoName
        $ListAllBranchURL ="https://dev.azure.com/{org name}/$($ProjectName)/_apis/git/repositories/$($RepoName)/refs?filter=heads&api-version=6.1-preview.1"                          
        $ListBranchName = Invoke-RestMethod -Uri $ListAllBranchURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
        #get branch name
        foreach($Branch in $ListBranchName.value){
            $BranchName = $Branch.name.split("/",3)[-1]
            #write-host $BranchName

            #List the commits by a user across all repositories in one organization in Azure DevOps
            $ListCommitInfoViaUserURL = "https://dev.azure.com/{org name}/$($ProjectName)/_apis/git/repositories/$($RepoName)/commits?searchCriteria.author={User display name}&searchCriteria.itemVersion.version=$($BranchName)&api-version=6.0"
            $ListCommitInfo = Invoke-RestMethod -Uri $ListCommitInfoViaUserURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

            if($ListCommitInfo.count -eq 0){

            }else{
                Write-Host "Project name is:"$ProjectName "    repo name is:" $RepoName    "branch name is:" $BranchName    "and commit ID is:" $ListCommitInfo.value.commitId 
            }
        }
    }
}

Result:

enter image description here

Ogdoad answered 29/3, 2021 at 9:8 Comment(4)
Thank you, I tried the APIs it looks good. but I have never worked on Powershell, so I request you to either convert it to C# code, or let me know how I can do it?Termagant
I found this sample, it get a list of team projects in a Azure DevOps Services organization, you could refer to it to convert the power shell script to C# code.Ogdoad
What is PAT? the authentication mechanism? if so, how do I generate it? is it a token that I need to generate on Azure DevOps under my account?Gall
The rest api is the same in every language, just use the same endpoints and api calls and it is language agnosticSikorsky

© 2022 - 2024 — McMap. All rights reserved.