azure-devopscommitazure-repos

Find commits filtered by comment in Azure Devops Repos


As we know, every commit in a repository has a comment:

enter image description here

Is there a way in Azure Repos to find all commits filtering by comments? For instance:

Find all commits with comments containing keyword 'sonar'


Solution

  • The function is not available in Azure DevOps web page currently.

    Workaround:

    The following is a PowerShell script that uses Azure DevOps REST API to find the comments containing the keyword you need. I filtered the top 1000 records. You can also refer to the examples here to filter the records you need.

        $Orgname = "Orgname" 
        $projectname="projectname"
        $reponame="reponame"
        $keyword="sonar"
        $PAT = "xxxxx"
        
        $PATGetBytes = [System.Text.Encoding]::ASCII.GetBytes(":$PAT")
        $Authentication = [System.Convert]::ToBase64String($PATGetBytes)
        $Headers = @{Authorization = ("Basic {0}" -f $Authentication) }
        
        $commits = Invoke-RestMethod  "https://dev.azure.com/$Orgname/$projectname/_apis/git/repositories/$reponame/commits?`$top=1000&api-version=6.0"  -Method 'GET' -Headers $Headers  -ContentType 'application/json'
    
        foreach($commit in $commits.value)
        {
           if ($commit.comment -match $keyword)
           {
              Write-Output "$($commit.commitId)  $($commit.remoteUrl)  $($commit.comment)"
           }  
        }
    
    

    My test result:

    enter image description here

    Update:

    This is the sample to cross search all projects and repos for given organization. It also filtered the top 1000 records for each repo.

    $organization = "xxxx"
    $PAT = "xxxx"
    $keyword="sonar"
    
    $PATGetBytes = [System.Text.Encoding]::ASCII.GetBytes(":$PAT")
    $Authentication = [System.Convert]::ToBase64String($PATGetBytes)
    $Headers = @{Authorization = ("Basic {0}" -f $Authentication) }
    
    
    # get all projects of the organization
    $uri = "https://dev.azure.com/$($organization)/_apis/projects?api-version=6.0"
    $projects = Invoke-RestMethod -Uri $uri -Method 'GET' -Headers $Headers  -ContentType 'application/json'
    
    foreach($project in $projects.value)
    {
        # get all repos of the project
        $uri = "https://dev.azure.com/$($organization)/$($project.id)/_apis/git/repositories?api-version=6.0"
        $repos = Invoke-RestMethod -Uri $uri -Method 'GET' -Headers $Headers  -ContentType 'application/json'
    
        foreach($repo in $repos.value)
        {
            Write-Output "Project: $($project.name), Repo: $($repo.name)"
            # get the commits of the repo
            $uri = "https://dev.azure.com/$($organization)/$($project.id)/_apis/git/repositories/$($repo.id)/commits?`$top=1000&api-version=6.0"
            $commits = Invoke-RestMethod -Uri $uri -Method 'GET' -Headers $Headers  -ContentType 'application/json'
    
    
            foreach($commit in $commits.value)
            {
                 if ($commit.comment -match $keyword)
                   {
                      Write-Output "Commit: $($commit.commitId), url: $($commit.remoteUrl), comment: $($commit.comment)"
                   }  
            }
            Write-Output "-----------------------------------------------------------------------------"
    
        }
    }