如何使用 Azure DevOps REST Api 从 Git 存储库下载分支中的文件? [英] How to download a file in a branch from a Git repo using Azure DevOps REST Api?
问题描述
对于主分支中的文件,我可以使用以下 URL - http://tfsserver:8080/tfs/DefaultCollection/TFSProject/_apis/git/repositories/GitRepo/items?path=FilePath&api-版本=4.1
For a file in the master branch I can use the following URL - http://tfsserver:8080/tfs/DefaultCollection/TFSProject/_apis/git/repositories/GitRepo/items?path=FilePath&api-version=4.1
但是如果我需要从分支下载文件怎么办?
But what if I need to download a file from the branch?
附言
我非常清楚我可以克隆一个 git 存储库.我特别需要 REST API.
I know perfectly well that I can clone a git repository. I need REST API specifically.
编辑 1
所以,我尝试了以下代码:
So, I tried the following code:
$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 $PSScriptRootAutomationBootstrapImpl.ps1 -Method Post -ContentType "application/json" -Body $Body
它成功了,但生成的文件并不完全符合我的预期:
It succeeds, but the generated file is not exactly what I expected:
{"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
However, it gives the URL I can use to get the file from branch - 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 $PSScriptRootAutomationBootstrapImpl.ps1
它按预期工作.但是我还是不知道这个API方法叫什么名字?
And it works as expected. But I still do not know what is the name of this API method?
推荐答案
GetItems Batch API 确实包含一个 versionType
类型 >GitVersionType
:
The GetItems Batch API does include a versionType
of type GitVersionType
:
版本类型(分支、标签或提交).确定如何解释 Id
Version type (branch, tag, or commit). Determines how Id is interpreted
因此,如果您将属性添加到 REST API URL:
So if you add to your REST API URL the attributes:
?versionType=Branch&version=myBranch
这应该足以从特定分支获取项目
That should be enough to get the items from a specific branch
正如 OP 所提到的,它提供了一个指向的中间 URL:
As the OP mentions, it gives an intermediate URL which points to:
http://tfsserver:8080/tfs/{organization}/{project}/_apis/git/repositories/{repositoryId}/items/{path}?versionType=Branch&version=myBranch
这意味着:
- 它按预期使用
?versionType=Branch&version=myBranch
- 但它使用
_apis/git/repositories/{repositoryId}/items
Items Get API,而不是_apis/git/repositories/{repositoryId}/itemsbatch
- it uses as expected
?versionType=Branch&version=myBranch
- But it uses
_apis/git/repositories/{repositoryId}/items
Items Get API, not_apis/git/repositories/{repositoryId}/itemsbatch
这篇关于如何使用 Azure DevOps REST Api 从 Git 存储库下载分支中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!