Search code examples
azureazure-devopsbranchazure-repos

Cross Search in all repositories and branches in Azure Devops Repos


Azure has very nice function to facilitate cross search through all repositories:

enter image description here

I have noticed that after I did search for a keyword which was in one of the branches it was not found. How can I cross search also through all the branches from all repositories?


Solution

  • You can run the following PowerShell scripts to update the searchable branches via REST API.

    $token = "{PAT}"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $url="https://dev.azure.com/{Org name}/{Project name}/_apis/policy/Configurations/{Configuration ID}?api-version=7.1-preview.1"
    $body = @'
    {
        "type": {
            "id": "0517f88d-4ec5-4343-9d26-9930ebd53069"
        },
        "isBlocking": false,
        "isEnabled": false,
        "settings": {
            "scope": [
                {
                    "repositoryId": "{Your repo ID}"
                }
            ],
            "searchBranches": [
                "refs/heads/Branch1",
                "refs/heads/branch2",
                "refs/heads/Branch3"
            ]
        }
    }
    '@
    $head = @{ Authorization =" Basic $token" }
    Invoke-RestMethod -Uri $url -Method PUT -Headers $head -Body $body -ContentType application/json
    
    
    • PAT: Personal access token
    • Org name: Your organization name
    • Project name: Your project name
    • Configuration ID: The configuration ID. You can get it by running REST API Configurations - List or check the network trace when editing the searchable branch from UI.
    • repositoryId: The ID of your repo. You can get it when you run REST API Configurations - List. Or you can see it from the URL when you edit searchable branches from UI.
    • searchBranches: The branches you want to search. Set it as [ ] if you want to remove all branches except the default beanch.