Merge pull request '1.2.0-rc1' (#1) from 1.2.0-rc1 into main

Reviewed-on: public/ps-modules-giteautilities#1
This commit was merged in pull request #1.
This commit is contained in:
2025-04-25 16:12:25 -04:00

View File

@@ -31,12 +31,42 @@ Function Set-GiteaConfiguration {
Function Get-GiteaConfiguration { Function Get-GiteaConfiguration {
[CmdletBinding()] [CmdletBinding()]
param() param(
[switch]$LoadVariables,
[switch]$Force
)
$configPath = Join-Path -Path $env:USERPROFILE -ChildPath ".giteautils\config.xml" $configPath = Join-Path -Path $env:USERPROFILE -ChildPath ".giteautils\config.xml"
if (Test-Path -Path $configPath) { if (Test-Path -Path $configPath) {
return Import-Clixml -Path $configPath $config = Import-Clixml -Path $configPath
# If LoadVariables switch is used, set each config value as a variable in the global scope
if ($LoadVariables) {
foreach ($key in $config.Keys) {
# Check if variable exists in global scope
$variableExists = $false
try {
$existingVar = Get-Variable -Name $key -Scope Global -ErrorAction Stop
$variableExists = $true
}
catch {
$variableExists = $false
}
# Set variable if it doesn't exist or if Force is used
if (-not $variableExists -or $Force) {
Write-Verbose "Loading configuration variable: $key = $($config[$key])"
Set-Variable -Name $key -Value $config[$key] -Scope Global
Write-Host "Created global variable: `$$key" -ForegroundColor Green
}
else {
Write-Verbose "Skipping existing variable: $key (use -Force to override)"
}
}
}
return $config
} }
else { else {
Write-Warning "Gitea configuration not found. Use Set-GiteaConfiguration to set up." Write-Warning "Gitea configuration not found. Use Set-GiteaConfiguration to set up."
@@ -139,7 +169,7 @@ Function Get-GiteaFileContent {
Write-Verbose "repoOwner: $repoOwner" Write-Verbose "repoOwner: $repoOwner"
Write-Verbose "repoName: $repoName" Write-Verbose "repoName: $repoName"
Write-Verbose "branch: $branch" Write-Verbose "branch: $branch"
Write-Verbose "token: $token" Write-Debug "Token: $token"
Write-Verbose "decode: $decode" Write-Verbose "decode: $decode"
$headers = @{ $headers = @{
@@ -249,7 +279,7 @@ Function Get-GiteaLFSConfiguration {
Write-Verbose "giteaURL: $giteaURL" Write-Verbose "giteaURL: $giteaURL"
Write-Verbose "repoOwner: $repoOwner" Write-Verbose "repoOwner: $repoOwner"
Write-Verbose "repoName: $repoName" Write-Verbose "repoName: $repoName"
Write-Verbose "token: $token" Write-Debug "Token: $token"
$filePath = ".gitattributes" $filePath = ".gitattributes"
@@ -359,7 +389,7 @@ Function Invoke-GiteaFileDownload {
} }
Write-Verbose "Parameters:" Write-Verbose "Parameters:"
Write-Verbose "token: $token (length: $(if($token){$token.Length}else{0}))" Write-Debug "Token: $token (length: $(if($token){$token.Length}else{0}))"
Write-Verbose "PreserveRelativePath: $PreserveRelativePath" Write-Verbose "PreserveRelativePath: $PreserveRelativePath"
# Create a WebClient to be reused # Create a WebClient to be reused
@@ -615,7 +645,7 @@ Function Get-GiteaChildItem {
Write-Verbose "repoOwner: $repoOwner" Write-Verbose "repoOwner: $repoOwner"
Write-Verbose "repoName: $repoName" Write-Verbose "repoName: $repoName"
Write-Verbose "Path: $Path" Write-Verbose "Path: $Path"
Write-Verbose "token: $token" Write-Debug "Token: $token"
Write-Verbose "File: $File" Write-Verbose "File: $File"
$headers = @{ $headers = @{
@@ -668,21 +698,31 @@ Function Get-GiteaChildItem {
} }
if ($isLFS) { if ($isLFS) {
$item.type = "lfs" $item.type = "lfs"
# Save the original download URL for LFS pointers to be used to populate the size and sha fields
$GitLFSPointerURL = $item.download_url
# Use the Gitea API to get the LFS pointer details
# For LFS files, we need to get the pointer file and parse it for details
$lfsPointerContent = Invoke-RestMethod -Uri $GitLFSPointerURL -Method Get -Headers $headers -ErrorAction Stop
Write-Debug "LFS Pointer Content: $($lfsPointerContent)"
# Parse the LFS pointer to extract size and SHA
if ($lfsPointerContent -match 'oid sha256:([a-f0-9]+)') {
$item.sha = $matches[1]
}
if ($lfsPointerContent -match 'size (\d+)') {
$item.size = [long]$matches[1]
}
# Set the download URL to the media endpoint for LFS files to download the actual file
$item.download_url = "$giteaURL/api/v1/repos/$repoOwner/$repoName/media/$($item.path)" $item.download_url = "$giteaURL/api/v1/repos/$repoOwner/$repoName/media/$($item.path)"
} }
$itemObj = [PSCustomObject]@{ $itemObj = [PSCustomObject]@{
filePath = $item.path
Path = $item.path
repoOwner = $repoOwner
repoName = $repoName
giteaURL = $giteaURL
downloadURL = $item.download_url
branch = $branch
type = $item.type
name = $item.name name = $item.name
Path = $item.path
type = $item.type
size = $item.size size = $item.size
sha = $item.sha sha = $item.sha
downloadURL = $item.download_url
Success = $true Success = $true
Error = $null Error = $null
Level = 0 Level = 0
@@ -741,21 +781,31 @@ Function Get-GiteaChildItem {
} }
if ($isLFS) { if ($isLFS) {
$subItem.type = "lfs" $subItem.type = "lfs"
# Save the original download URL for LFS pointers to be used to populate the size and sha fields
$GitLFSPointerURL = $subItem.download_url
# Use the Gitea API to get the LFS pointer details
# For LFS files, we need to get the pointer file and parse it for details
$lfsPointerContent = Invoke-RestMethod -Uri $GitLFSPointerURL -Method Get -Headers $headers -ErrorAction Stop
Write-Debug "LFS Pointer Content: $($lfsPointerContent)"
# Parse the LFS pointer to extract size and SHA
if ($lfsPointerContent -match 'oid sha256:([a-f0-9]+)') {
$subItem.sha = $matches[1]
}
if ($lfsPointerContent -match 'size (\d+)') {
$subItem.size = [long]$matches[1]
}
# Set the download URL to the media endpoint for LFS files to download the actual file
$subItem.download_url = "$giteaURL/api/v1/repos/$repoOwner/$repoName/media/$($subItem.path)" $subItem.download_url = "$giteaURL/api/v1/repos/$repoOwner/$repoName/media/$($subItem.path)"
} }
$subItemObj = [PSCustomObject]@{ $subItemObj = [PSCustomObject]@{
filePath = $subItem.path
Path = $subItem.path
repoOwner = $repoOwner
repoName = $repoName
giteaURL = $giteaURL
downloadURL = $subItem.download_url
branch = $branch
type = $subItem.type
name = $subItem.name name = $subItem.name
Path = $subItem.path
type = $subItem.type
size = $subItem.size size = $subItem.size
sha = $subItem.sha sha = $subItem.sha
downloadURL = $subItem.download_url
Success = $true Success = $true
Error = $null Error = $null
Level = $currentLevel Level = $currentLevel
@@ -793,7 +843,6 @@ Function Get-GiteaChildItem {
Write-Error "Failed to retrieve '$normalizedPath' from Gitea: $($_.Exception.Message)`n$($errorDetails | ConvertTo-Json -Depth 1 -Compress)" Write-Error "Failed to retrieve '$normalizedPath' from Gitea: $($_.Exception.Message)`n$($errorDetails | ConvertTo-Json -Depth 1 -Compress)"
$results += [PSCustomObject]@{ $results += [PSCustomObject]@{
filePath = $normalizedPath
Path = $normalizedPath Path = $normalizedPath
repoOwner = $repoOwner repoOwner = $repoOwner
repoName = $repoName repoName = $repoName
@@ -823,4 +872,4 @@ Function Get-GiteaChildItem {
} }
} }
Export-ModuleMember -Function Set-GiteaConfiguration, Get-GiteaConfiguration, Get-GiteaFileContent, Invoke-GiteaFileDownload, Get-GiteaChildItem Export-ModuleMember -Function Set-GiteaConfiguration, Get-GiteaConfiguration, Get-GiteaFileContent, Invoke-GiteaFileDownload, Get-GiteaChildItem, Get-GiteaLFSFile