【问题标题】:How to download a file in a branch from a Git repo using Azure DevOps REST Api?如何使用 Azure DevOps REST Api 从 Git 存储库下载分支中的文件?
【发布时间】:2019-01-17 02:53:27
【问题描述】:

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

但是如果我需要从分支下载文件怎么办?

附言

我非常清楚我可以克隆一个 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方法叫什么名字?

【问题讨论】:

    标签: git azure-devops


    【解决方案1】:

    GetItems Batch API 确实包含versionType of type 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
    

    这意味着:

    【讨论】:

    • 请看我的编辑。虽然不是真的,但我会接受它,因为它确实会导致答案。不过,如果您可以修改它并提及执行此操作的 API 方法,那就太棒了。
    • @mark 我已经相应地编辑了答案,并添加了相关 API 的链接。
    • 谢谢。我希望没有人会因为您提到特定于我的特定环境的具体值而感到困惑,例如 codexyzbootstrap.ps1shelve/vsts
    • @mark 好点。我已将问题编辑为仅使用占位符,而不是特定值。
    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2021-02-18
    • 2021-02-28
    • 2020-11-25
    • 2021-04-21
    • 2020-12-23
    相关资源
    最近更新 更多