Home > Enterprise >  Downloading DevOps Git repo subfolder content in zip format using REST API
Downloading DevOps Git repo subfolder content in zip format using REST API

Time:08-22

I'm trying to create a collection (webpage) of all our teams sphinx documentation (html, js, and CSS etc.) from multiple DevOps repositories in one internal "portal" - to use the generated documentation as subpages.

I don't want to use GIT, as this as I understand it, includes a whole GIT repo (Been looking into sparse-checkout) - I only want the auto generated files contained in a folder - i.e. same functionality as using DevOps download as zip for one specific folder. Let's say I have a repository: SomeRepo and a subfolder named doc/build/html (SomeRepo/doc/build/html) that I want to download as a zip archive.

I'm able to use Azure REST API to download and store content of one file (from powershell script in any format), but what I want is the entire "sub-folder" or tree (content with all files and subfolders) inside the "Git" repository as zip.

I've been researching this a little bit, and it seems that there is multiple ways to achieve this, but I don't know what the best solution is for this problem - and I want to use REST API if possible, preferably using powershell if possible.

CodePudding user response:

I want is the entire "sub-folder" or tree (content with all files and subfolders) inside the "Git" repository as zip.

You can use the Rest API: Items - Get to download the Repo. In the Rest API url, you can define the folder path. Then it will download the entire subfolder.

Here is a PowerShell example:

$token = "PAT"

$url="https://dev.azure.com/{ORG}/{PROJECT}/_apis/git/repositories/{RepoNameORRepoID}/items?scopePath=folderpath(e.g. doc/build/html )&download=true&`$format=zip&api-version=6.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response1 = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/zip  -OutFile "c://repo.zip"
  • Related