簡體   English   中英

Azure DevOps 使用 REST API 調用 GetItem

[英]Azure DevOps calling GetItem using REST API

我正在嘗試在 Azure Devops 中獲取文件(sonar-project.properties)的文件內容,但我似乎無法獲得實際內容。 這是我在嘗試調用 REST api 時的 powershell 代碼。 我想獲取位於 repo 根目錄中的 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

這是我的 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...

現在我知道 isFolder 設置為 true,但 sonar-project.properties 是一個實際文件。 所以我現在不知所措..有什么想法嗎?

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"

回購結構:

在此處輸入圖像描述

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM