Powershell download file from gitlab using correct path for subdirectory

Viewed 686

Our tools team setup Ansible Tower to use inventories contained in .ini files hosted on internal gitlab. I can not change this. I need to modify these files as the contents change in our environment (new builds, decommissions, etc)

I think I am SO close.

I can connect to gitlab with a private access token and get results, but I can not figure out what the path is to download files other than the README.md file like this

Invoke-RestMethod -Uri 'https://git.toolsite.com/api/v4/projects/13001/repository/files/README.md?ref=main' -Headers $Headers -Method GET

file_name : README.md file_path : README.md size : 139 encoding : base64

I can also recursively list the files:

Invoke-RestMethod -Uri 'https://git.toolsite.com/api/v4/projects/13001/repository/tree?ref=main&per_page=100&recursive=true' -Headers $Headers -Method GET
   
id   : 4cf897a48c777c58b6cfdb1705d9e0fe28602a10
name : Windows_Non-Prod_Servers_Inventory.ini
type : blob
path : Inventory/Windows_Non-Prod_Servers_Inventory.ini
mode : 100644

id   : 0ce79694fb4511b8a90379250ee829cd20b281f3
name : Windows_Prod_Servers_Inventory.ini
type : blob
path : Inventory/Windows_Prod_Servers_Inventory.ini

BUT I can not get the path right for Inventory/Windows_Prod_Servers_Inventory.ini

Invoke-RestMethod -Uri 'https://git.toolsite.com/api/v4/projects/13001/repository/files/Inventory/Windows_Non-Prod_Servers_Inventory.ini?ref=main' -Headers $Headers -Method GET

Invoke-RestMethod : {"error":"404 Not Found"}

Also, in a browser, the URL to that file is https://git.toolsite.com/bswh/bswh_windows/-/blob/main/Inventory/Windows_Non-Prod_Servers_Inventory.ini

I need a little help

thanks

1 Answers

I figured it out using info from https://docs.gitlab.com/ee/api/index.html#namespaced-path-encoding

You have to 'encode' any '/' AFTER the directory name. So in my case it was good up to ../files/Inventory, but have to encode '/' after that.

Invoke-RestMethod -Uri 'https://git.toolsite.com/api/v4/projects/13001/repository/files/Inventory%2FWindows_Non-Prod_Servers_Inventory.ini?ref=main' -Headers $Headers -Method GET

and also anyone wondering about the header. Once you get a 'private access token' from gitlab, the header looks like this:

$privateAccessToken = "xxxxxxxxxxxxxxx" 
$Headers = @{
    'PRIVATE-TOKEN' = $privateAccessToken
}
Related