How to script an Azure repository search using az cli

Viewed 375

I'm fairly new to Azure cli. I've got a case where I'd like to perform a search on each of my code repos in Azure devops and check for the version of a dependency in the pom.xml file.

The front end search via the web console provides something very close to what i need but i can't easily export the results. In the front end I enter in a search like so and it kind of gives me what i need.

enter image description here

Whats the best way to replicate this behaviour to produce a kind of report using the azure cli?

1 Answers

In az cli, there is az repos to manage Azure Repos. You could run az repos show to get the details of a Git repository, but there is no Code search commands.

Instead of az cli, you could use Code Search Results - Fetch Code Search Results REST api in your script to get results of the search text.

POST https://almsearch.dev.azure.com/{organization}/{project}/_apis/search/codesearchresults?api-version=6.0-preview.1

The script uses the api looks like:

Param( 
[string]$organisation = "org", 
[string]$project = "projectname", 
[string]$keepForever = "true", 
[string]$user = " ", 
[string]$token = "PAT" ) 

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$postresults = "https://almsearch.dev.azure.com/$organisation/$project/_apis/search/codesearchresults?api-version=6.0-preview.1" 

 $body = '{

}'

$result = Invoke-RestMethod -Uri $postresults -Method Post -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Related