繁体   English   中英

如何使用Azure DevOps REST Api从Git仓库下载分支中的文件?

[英]How to download a file in a branch from a Git repo using Azure DevOps REST Api?

对于master分支中的文件,我可以使用以下URL - http://tfsserver:8080/tfs/DefaultCollection/TFSProject/_apis/git/repositories/GitRepo/items?path=FilePath&api-version=4.1

但是如果我需要从分支机构下载文件呢?

PS

我完全清楚我可以克隆一个git存储库。 我特别需要REST API。

编辑1

所以,我尝试了以下代码:

$Url = "http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/itemsbatch?api-version=4.1"
$Body = @"
{
    "itemDescriptors": [
    {
        "path": "/Bootstrap.ps1",
        "version": "shelve/vsts",
        "versionType": "branch"
    }]
}
"@
Invoke-RestMethod -UseDefaultCredentials -Uri $Url -OutFile $PSScriptRoot\AutomationBootstrapImpl.ps1 -Method Post -ContentType "application/json" -Body $Body

它成功了,但生成的文件并不完全符合我的预期:

{"count":1,"value":[[{"objectId":"ceb9d83e971abdd3326d67e25b20c2cb1b4943e2","gitObjectType":"blob","commitId":"d4a039914002613e775f6274aee6489b244a42a7","path":"/bootstrap.ps1","url":"http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/items/bootstrap.ps1?versionType=Branch&version=shelve%2Fvsts&versionOptions=None"}]]}

但是,它提供了我可用于从分支获取文件的URL - http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/items/bootstrap.ps1?versionType=Branch&version=shelve%2Fvsts&versionOptions=None

那么,我们走了:

$Url = "http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/items/bootstrap.ps1?versionType=Branch&version=shelve/vsts"
Invoke-RestMethod -UseDefaultCredentials -Uri $Url -OutFile $PSScriptRoot\AutomationBootstrapImpl.ps1

它按预期工作。 但我仍然不知道这个API方法的名称是什么?

GetItems Batch API包含versionType类型的GitVersionType

版本类型(分支,标记或提交)。 确定如何解释Id

因此,如果您向REST API URL添加属性:

?versionType=Branch&version=myBranch

这应该足以从特定分支获取项目

正如OP所提到的,它提供了一个中间URL,指向:

http://tfsserver:8080/tfs/{organization}/{project}/_apis/git/repositories/{repositoryId}/items/{path}?versionType=Branch&version=myBranch

这意味着:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM