0

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?

Nerd in Training
  • 2,098
  • 1
  • 22
  • 44
  • Firstly I'd highly recommend you use `Invoke-RestMethod` when talking to REST APIs as it automatically parses the JSON returned from endpoints.. Secondly, what do you get if you call the url that is returned? I ask because in the documentation example for downloading a file from the same endpoint, the response isn't a file but JSON that contains a URL which I assume is a direct download link, maybe it's similar here? – PMental Nov 28 '20 at 17:47
  • I get the same output as above. I actually just want to retrieve the file contents :( – Nerd in Training Nov 29 '20 at 01:34
  • Does this answer your question? [Get file from azure devops repository using rest api](https://stackoverflow.com/questions/62464734/get-file-from-azure-devops-repository-using-rest-api) – Guenther Schmitz Nov 29 '20 at 17:26

1 Answers1

1

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

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • It's insane that this worked. I still don't see what I did different besides having the parameters aligned differently – Nerd in Training Nov 30 '20 at 13:59
  • 1
    Nevermind. It was because I had $uri or $url set with multiple lines. My naive self thought strings were the same as in c#. DOH! Thank you so much!! – Nerd in Training Nov 30 '20 at 14:03