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

Azure DevOps calling GetItem using REST API

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

I'm trying to get the file contents of a file (sonar-project.properties) in Azure Devops but I can't seem to get the actual content. Here is my powershell code when trying to call the REST api. I want to get the content of the sonar-project.properties file that sits in the root directory of the repo.

$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

And here is my output:

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...

Now I know the isFolder is set to true, but the sonar-project.properties is an actual file. So I am at a loss right now.. Any ideas?

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

You could try the following PowerShell Rest API Sample: I also use this rest api: Items - Get to get the file content from Git Repo type.

$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"

Repo structure:

enter image description here

Result:

enter image description here