如何使用 az cli 编写 Azure 存储库搜索脚本

时间:2021-04-21 08:19:43

标签: azure azure-devops

我对 Azure cli 相当陌生。我有一个案例,我想在 Azure devops 中对我的每个代码存储库执行搜索,并检查 pom.xml 文件中的依赖项版本。

通过 Web 控制台进行的前端搜索提供了非常接近我需要的东西,但我无法轻松导出结果。在前端,我输入这样的搜索,它给了我我需要的东西。

enter image description here

复制此行为以使用 azure cli 生成某种报告的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

在 az cli 中,有 az repos 用于管理 Azure Repos。您可以运行 az repos show 来获取 Git 存储库的详细信息,但没有代码搜索命令。

您可以在脚本中使用 Code Search Results - Fetch Code Search Results REST api 来获取搜索文本的结果,而不是 az cli。

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

脚本使用的api看起来像:

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