Warm tip: This article is reproduced from serverfault.com, please click

powershell-Azure DevOps使用REST API调用GetItem

(powershell - Azure DevOps calling GetItem using REST API)

发布于 2020-11-27 22:48:30

我正在尝试获取Azure Devops中文件(sonar-project.properties)的文件内容,但似乎无法获取实际内容。这是我尝试调用REST API时的powershell代码。我想获取位于回购根目录中的sonar-project.properties文件的内容。

$uri = "https://dev.azure.com/$organization/$projectId/_apis/git/repositories/$repoId/Items?
                        path=sonar-project.properties&
                        recursionLevel=0&
                        versionDescriptor.version=$branchName&
                        versionDescriptor.versionOptions=0&
                        versionDescriptor.versionType=Branch&
                        includeContent=true&
                        resolveLfs=true&
                        api-version=6.1-preview.1"

$item = (Invoke-WebRequest -Uri $uri -Proxy http:\\webproxy.sweetcompany.ca:1080 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ProxyUseDefaultCredentials -Method Get).Content | ConvertFrom-Json

这是我的输出:

Repo: newmicroservice-repo
Branch: refs/heads/master
objectId      : 9804b758e84cac41a6acc4d011f57310a1f63102
gitObjectType : tree
commitId      : e911bc994bb3dad69580baa0b44e6dc029e3adad
path          : /
isFolder      : True
url           : https://dev.azure.com/BLAHBLAH/GUIDSGUIDS/_apis/git/repositories/GUIDSGUIDS/items?path=%2F&versionType=Branch&versionOptions=None...

现在我知道isFolder设置为true,但是sonar-project.properties是一个实际文件。所以我现在很茫然..有什么想法吗?

Questioner
Nerd in Training
Viewed
0
Kevin Lu-MSFT 2020-11-30 10:04:39

你可以尝试以下PowerShell Rest API示例:我也使用此rest api:Items-获取以从Git Repo类型获取文件内容

$token = "PAT"

$url=" https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/git/repositories/{RepoId}/items?recursionLevel=0&versionDescriptor.version=master&versionDescriptor.versionOptions=0&versionDescriptor.versionType=Branch&includeContent=true&resolveLfs=true&path=sonar-project.properties&6.1-preview.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json

Write-Host "$response"

回购结构:

在此处输入图片说明

结果:

在此处输入图片说明