Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

P.S.

I know perfectly well that I can clone a git repository. I need REST API specifically.

EDIT 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 $PSScriptRoot\AutomationBootstrapImpl.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"}]]}

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

So, here we go:

$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

And it works as expected. But I still do not know what is the name of this API method?

like image 770
mark Avatar asked Jan 17 '19 02:01

mark


People also ask

How do I pull code from Azure DevOps repository?

From the Pull Requests view, select New Pull Request. Select the source and target branches, enter a title and optional description, and select Create. After the PR is created, select Open in browser to open the new PR in the Azure DevOps web portal.

How do I pull a branch from Azure DevOps?

In Team Explorer, select Home and choose Branches. In the Branches view, right-click the target branch and select Checkout. Right-click the source branch, and select Merge From. Verify the merge options and then click Merge.


1 Answers

The GetItems Batch API does include a versionType of type GitVersionType:

Version type (branch, tag, or commit). Determines how Id is interpreted

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

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

That means:

  • 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
like image 130
VonC Avatar answered Sep 28 '22 08:09

VonC