Add Gitea utilities module with functions to retrieve file content and download files

This commit is contained in:
2025-03-12 12:10:10 -04:00
parent cbe3e41734
commit 64f1ffebfb

View File

@@ -0,0 +1,195 @@
Function Get-GiteaFileContent {
<#
.SYNOPSIS
Retrieve the content of a file from a Gitea repository using the Gitea API.
.DESCRIPTION
This function retrieves the content of a file from a Gitea repository using the Gitea API. The function requires the URL of the Gitea server, the owner of the repository, the name of the repository, the branch to retrieve the file from, the path to the file, a personal access token, and an optional switch to decode the content of the file from Base64 encoding.
.PARAMETER giteaURL
The URL of the Gitea server.
.PARAMETER repoOwner
The owner of the repository.
.PARAMETER repoName
The name of the repository.
.PARAMETER branch
The branch to retrieve the file from. The default value is 'main'.
.PARAMETER filePath
The path to the file to retrieve. This parameter accepts pipeline input.
.PARAMETER token
A personal access token for the Gitea server.
.PARAMETER decode
A switch parameter to decode the content of the file from Base64 encoding.
.EXAMPLE
# Example 1: Retrieve the content of a single file from a Gitea repository
$file = Get-GiteaFileContent -repoOwner "cityofnorwich" -repoName "software-premierone" -filePath "README.md" -token "your_token"
.EXAMPLE
# Example 2: Retrieve the content of multiple files from a Gitea repository
$files = Get-GiteaFileContent -repoOwner "cityofnorwich" -repoName "software-premierone" -filePath @("README.md", "Manually Create Installer Package.ps1") -token "your_token"
.EXAMPLE
# Example 3: Retrieve the content of multiple files from a Gitea repository using pipeline input
$filesArray = @("README.md", "Manually Create Installer Package.ps1") | Get-GiteaFileContent -repoOwner "cityofnorwich" -repoName "software-premierone" -token "your_token"
#>
[CmdletBinding()]
param(
[string]$giteaURL = "https://gitea.norwichct.tech",
[Parameter(Mandatory)]
[string]$repoOwner,
[Parameter(Mandatory)]
[string]$repoName,
[string]$branch = "main",
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('FullName', 'Path')]
[string[]]$filePath,
[Parameter(Mandatory)]
[string]$token,
[switch]$decode
)
begin {
$results = @()
Write-Verbose "Parameters:"
Write-Verbose "giteaURL: $giteaURL"
Write-Verbose "repoOwner: $repoOwner"
Write-Verbose "repoName: $repoName"
Write-Verbose "branch: $branch"
Write-Verbose "token: $token"
Write-Verbose "decode: $decode"
$headers = @{
"Authorization" = "token $token"
"Accept" = "application/json"
}
}
process {
foreach ($file in $filePath) {
Write-Verbose "Processing file: $file"
$encodedFile = [System.Uri]::EscapeDataString($file)
Write-Verbose "Encoded file: $encodedFile"
$url = "$giteaURL"
$url += "/api/v1/repos"
$url += "/$repoOwner"
$url += "/$repoName"
$url += "/contents"
$url += "/$encodedFile"
$url += "?ref=$branch"
Write-Verbose "URL: $url"
try {
$fileContent = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
$content = if ($decode) {
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($fileContent.content))
} else {
$fileContent.content
}
$results += [PSCustomObject]@{
Path = $file
Content = $content
Size = $fileContent.size
SHA = $fileContent.sha
Success = $true
Error = $null
}
}
catch {
Write-Error "Failed to retrieve file '$file' from Gitea: $_"
$results += [PSCustomObject]@{
Path = $file
Content = $null
Size = $null
SHA = $null
Success = $false
Error = $_.Exception.Message
}
}
}
}
end {
return $results
}
}
Function Invoke-GiteaFileDownload {
<#
.SYNOPSIS
Downloads a file from a Gitea repository using a direct download URL.
.DESCRIPTION
This function downloads a file from a Gitea repository using a direct download URL. The function requires the download URL and a personal access token. You can optionally specify an output path and use the force switch to overwrite existing files.
.PARAMETER downloadURL
The direct download URL for the file from the Gitea server.
.PARAMETER token
A personal access token for the Gitea server.
.PARAMETER outputPath
The path where the downloaded file should be saved. If not specified, the file will be saved in the current directory using the filename from the URL.
.PARAMETER force
A switch parameter to force overwriting of an existing file at the output path.
.EXAMPLE
# Example 1: Download a file to the current directory
Invoke-GiteaFileDownload -downloadURL "https://gitea.example.com/api/v1/repos/owner/repo/raw/path/to/file.txt" -token "your_token"
.EXAMPLE
# Example 2: Download a file to a specific location with force overwrite
Invoke-GiteaFileDownload -downloadURL "https://gitea.example.com/api/v1/repos/owner/repo/raw/path/to/file.txt" -token "your_token" -outputPath "C:\Downloads\file.txt" -force
#>
[cmdletbinding()]
param(
[Parameter(Mandatory)]
[string]$downloadURL,
[Parameter(Mandatory)]
[string]$token,
[string]$outputPath,
[switch]$force
)
# If the output path is not specified, use the current directory and the file name from the download URL (everything after the last '/' and before the last character after the last '.', inclusive of the extension)
if (-not $outputPath) {
# Get the file name from the download URL
$outputFileName = $downloadURL.Substring($downloadURL.LastIndexOf("/") + 1)
# Clean the file name by removing any query string parameters and HTML encoded characters
$outputFileName = [System.Uri]::UnescapeDataString($outputFileName.Split("?")[0])
# Append the outputFileName to the current location
$outputPath = Join-Path -Path (Get-Location) -ChildPath $outputFileName
}
if((Test-Path -Path $outputPath -PathType Leaf) -and (-not $force)) {
Write-Error "The file '$outputPath' already exists. Use the -Force switch to overwrite the file."
return $false | Out-Null
}
$headers = @{
"Authorization" = "token $token"
}
try {
Invoke-RestMethod -Uri $downloadURL -Method Get -Headers $headers -OutFile $outputPath
return $true | Out-Null
}
catch {
Write-Error "Failed to download file from Gitea: $_"
return $false | Out-Null
}
}
Export-ModuleMember -Function Get-GiteaFileContent, Invoke-GiteaFileDownload