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

3
我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我对Azure cli还比较陌生。我有一个案例,希望在Azure devops中搜索我的每个代码仓库,并检查pom.xml文件中依赖项的版本。

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

enter image description here

什么是使用 Azure CLI 复制此行为并生成报告的最佳方法?
1个回答

3
在 az cli 中,有 az repos 来管理 Azure Repos。您可以运行 az repos show 来获取 Git 存储库的详细信息,但没有代码搜索命令。
而不是使用 az cli,您可以在脚本中使用 Code Search Results - Fetch Code Search Results REST API 来获取搜索文本的结果。
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)}

非常感谢您的帮助。我已经让它工作了。虽然在 REST API 中有一个错误,阻止我获得我想要的结果(https://github.com/MicrosoftDocs/vsts-rest-api-specs/issues/465?_pjax=%23js-repo-pjax-container),但它像魔法一样运行。这不是我们可以在此线程中解决的问题。 - Richie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接