Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ae0d450e2e | |||
| 08bf6a709c | |||
| b6a46b38bd | |||
| 3f66adb4ca | |||
| d647600568 | |||
| ed8f3ab02e |
@@ -12,7 +12,7 @@
|
|||||||
RootModule = 'PS-GiteaUtilities.psm1'
|
RootModule = 'PS-GiteaUtilities.psm1'
|
||||||
|
|
||||||
# Version number of this module.
|
# Version number of this module.
|
||||||
ModuleVersion = '1.5.0'
|
ModuleVersion = '1.5.4'
|
||||||
|
|
||||||
# Supported PSEditions
|
# Supported PSEditions
|
||||||
# CompatiblePSEditions = @()
|
# CompatiblePSEditions = @()
|
||||||
|
|||||||
@@ -434,8 +434,8 @@ Function Invoke-GiteaFileDownload {
|
|||||||
return [PSCustomObject]@{
|
return [PSCustomObject]@{
|
||||||
SourceURL = $downloadURL
|
SourceURL = $downloadURL
|
||||||
Type = $type
|
Type = $type
|
||||||
Result = 'Success'
|
Result = 'Skipped'
|
||||||
Error = "Skipped - item is a directory"
|
Error = $null
|
||||||
Timestamp = Get-Date
|
Timestamp = Get-Date
|
||||||
FileSize = $null
|
FileSize = $null
|
||||||
}
|
}
|
||||||
@@ -472,7 +472,6 @@ Function Invoke-GiteaFileDownload {
|
|||||||
$branchIndex = [Array]::IndexOf($pathSegments, "branch") + 2
|
$branchIndex = [Array]::IndexOf($pathSegments, "branch") + 2
|
||||||
$DownloadFilePath = $pathSegments[$branchIndex..($pathSegments.Length - 2)] -join '/'
|
$DownloadFilePath = $pathSegments[$branchIndex..($pathSegments.Length - 2)] -join '/'
|
||||||
# Replace any URL encoded characters in the file name
|
# Replace any URL encoded characters in the file name
|
||||||
$DownloadFilePath = [System.Uri]::UnescapeDataString($DownloadFilePath)
|
|
||||||
$DownloadFileName = $pathSegments[-1]
|
$DownloadFileName = $pathSegments[-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -514,6 +513,8 @@ Function Invoke-GiteaFileDownload {
|
|||||||
# Normalize path separators
|
# Normalize path separators
|
||||||
$fileOutputPath = $fileOutputPath.Replace("/", [System.IO.Path]::DirectorySeparatorChar)
|
$fileOutputPath = $fileOutputPath.Replace("/", [System.IO.Path]::DirectorySeparatorChar)
|
||||||
|
|
||||||
|
# Replace any URL encoded characters in the file output path
|
||||||
|
$fileOutputPath = [System.Uri]::UnescapeDataString($fileOutputPath)
|
||||||
|
|
||||||
Write-Verbose "Output path: $fileOutputPath"
|
Write-Verbose "Output path: $fileOutputPath"
|
||||||
|
|
||||||
@@ -521,7 +522,7 @@ Function Invoke-GiteaFileDownload {
|
|||||||
Path = $fileOutputPath
|
Path = $fileOutputPath
|
||||||
SourceURL = $downloadURL
|
SourceURL = $downloadURL
|
||||||
Type = "file"
|
Type = "file"
|
||||||
Result = 'Failure'
|
Result = $null
|
||||||
Error = $null
|
Error = $null
|
||||||
Timestamp = Get-Date
|
Timestamp = Get-Date
|
||||||
FileSize = $null
|
FileSize = $null
|
||||||
@@ -545,6 +546,7 @@ Function Invoke-GiteaFileDownload {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
$result.Result = 'Skipped'
|
||||||
$errorMsg = "The file '$fileOutputPath' already exists. Use the -Force switch to overwrite the file."
|
$errorMsg = "The file '$fileOutputPath' already exists. Use the -Force switch to overwrite the file."
|
||||||
Write-Error $errorMsg
|
Write-Error $errorMsg
|
||||||
$result.Error = $errorMsg
|
$result.Error = $errorMsg
|
||||||
@@ -578,6 +580,7 @@ Function Invoke-GiteaFileDownload {
|
|||||||
return $result
|
return $result
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
$result.Result = 'Failure'
|
||||||
$errorMsg = "Failed to download file from Gitea: $_"
|
$errorMsg = "Failed to download file from Gitea: $_"
|
||||||
Write-Error $errorMsg
|
Write-Error $errorMsg
|
||||||
$result.Error = $errorMsg
|
$result.Error = $errorMsg
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ Describe 'Get-GiteaFileContent' {
|
|||||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'README.md' -token 'abc123'
|
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'README.md' -token 'abc123'
|
||||||
|
|
||||||
$result | Should -Not -BeNullOrEmpty
|
$result | Should -Not -BeNullOrEmpty
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
$result.Content | Should -Be ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello World')))
|
$result.Content | Should -Be ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello World')))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ Describe 'Get-GiteaFileContent' {
|
|||||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'README.md' -token 'abc123' -decode
|
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'README.md' -token 'abc123' -decode
|
||||||
|
|
||||||
$result | Should -Not -BeNullOrEmpty
|
$result | Should -Not -BeNullOrEmpty
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
$result.Content | Should -Be 'Hello World'
|
$result.Content | Should -Be 'Hello World'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ Describe 'Get-GiteaFileContent' {
|
|||||||
It 'Should capture error and mark result as unsuccessful' {
|
It 'Should capture error and mark result as unsuccessful' {
|
||||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'badfile.ps1' -token 'abc123'
|
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'badfile.ps1' -token 'abc123'
|
||||||
|
|
||||||
$result.Success | Should -Be $false
|
$result.Result | Should -Be 'Failure'
|
||||||
$result.Error | Should -Match 'API call failed'
|
$result.Error | Should -Match 'API call failed'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ Describe 'Get-GiteaFileContent' {
|
|||||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'testfile.txt' -token 'abc123'
|
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'testfile.txt' -token 'abc123'
|
||||||
|
|
||||||
$result | Should -Not -BeNullOrEmpty
|
$result | Should -Not -BeNullOrEmpty
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
$result.Content | Should -Be ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello from API!')))
|
$result.Content | Should -Be ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Hello from API!')))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ Describe 'Get-GiteaFileContent' {
|
|||||||
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'testfile.txt' -token 'abc123' -decode
|
$result = Get-GiteaFileContent -repoOwner 'user' -repoName 'repo' -filePath 'testfile.txt' -token 'abc123' -decode
|
||||||
|
|
||||||
$result | Should -Not -BeNullOrEmpty
|
$result | Should -Not -BeNullOrEmpty
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
$result.Content | Should -Be 'Hello from API!'
|
$result.Content | Should -Be 'Hello from API!'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,51 +46,59 @@ Describe 'Invoke-GiteaFileDownload' {
|
|||||||
|
|
||||||
Context 'When all parameters are valid' {
|
Context 'When all parameters are valid' {
|
||||||
It 'Should download a file successfully' {
|
It 'Should download a file successfully' {
|
||||||
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/raw/path/to/file.txt' -token 'abc123'
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/path/to/file.txt' -token 'abc123'
|
||||||
|
|
||||||
$result | Should -Not -BeNullOrEmpty
|
$result | Should -Not -BeNullOrEmpty
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
$result.Path | Should -Match 'file\.txt$'
|
$result.Path | Should -Match 'file\.txt$'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Context 'When specifying an outputPath' {
|
Context 'When specifying an outputPath' {
|
||||||
It 'Should create a path based on outputPath' {
|
It 'Should create a path based on outputPath' {
|
||||||
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/raw/path/to/file.txt' `
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/path/to/file.txt' `
|
||||||
-outputPath 'docs/manual/' -token 'abc123'
|
-outputPath 'docs/manual/' -token 'abc123'
|
||||||
|
|
||||||
$result.Path | Should -Match 'docs[\\/]+manual[\\/]+file\.txt$'
|
$result.Path | Should -Match 'docs[\\/]+manual[\\/]+file\.txt$'
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Context 'When specifying an outputName' {
|
Context 'When specifying an outputName' {
|
||||||
It 'Should download file and name it based on outputName' {
|
It 'Should download file and name it based on outputName' {
|
||||||
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/raw/path/to/file.txt' `
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/path/to/file.txt' `
|
||||||
-outputName 'newfile.txt' -token 'abc123'
|
-outputName 'newfile.txt' -token 'abc123'
|
||||||
|
|
||||||
$result.Path | Should -Match 'newfile\.txt$'
|
$result.Path | Should -Match 'newfile\.txt$'
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Context 'When specifying an outputName and outputPath' {
|
Context 'When specifying an outputName and outputPath' {
|
||||||
It 'Should download file and name it based on outputName' {
|
It 'Should download file and name it based on outputName' {
|
||||||
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/raw/path/to/file.txt' `
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/path/to/file.txt' `
|
||||||
-outputPath 'docs/manual/' -outputName 'newfile.txt' -token 'abc123'
|
-outputPath 'docs/manual/' -outputName 'newfile.txt' -token 'abc123'
|
||||||
|
|
||||||
$result.Path | Should -Match 'docs[\\/]+manual[\\/]+newfile\.txt$'
|
$result.Path | Should -Match 'docs[\\/]+manual[\\/]+newfile\.txt$'
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Context 'When using -PreserveRelativePath' {
|
Context 'When using -PreserveRelativePath' {
|
||||||
It 'Should preserve the relative path structure' {
|
It 'Should preserve the relative path structure' {
|
||||||
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/raw/path/to/file.txt' `
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/path/to/file.txt' `
|
||||||
-PreserveRelativePath -token 'abc123'
|
-PreserveRelativePath -token 'abc123'
|
||||||
|
|
||||||
$result.Path | Should -Match 'path[\\/]+to[\\/]+file\.txt$'
|
$result.Path | Should -Match 'path[\\/]+to[\\/]+file\.txt$'
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Success'
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'Should ensure the outputPath does not contain any URL encoding' {
|
||||||
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/Client%20Files/Certificates/file name.txt' `
|
||||||
|
-PreserveRelativePath -token 'abc123'
|
||||||
|
|
||||||
|
$result.Path | Should -Match 'Client[ ]Files[\\/]+Certificates[\\/]+file name\.txt$'
|
||||||
|
$result.Result | Should -Be 'Success'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,8 +106,7 @@ Describe 'Invoke-GiteaFileDownload' {
|
|||||||
It 'Should skip items with type = dir' {
|
It 'Should skip items with type = dir' {
|
||||||
$result = Invoke-GiteaFileDownload -outputPath 'docs/' -type 'dir' -token 'abc123'
|
$result = Invoke-GiteaFileDownload -outputPath 'docs/' -type 'dir' -token 'abc123'
|
||||||
|
|
||||||
$result.Success | Should -Be $true
|
$result.Result | Should -Be 'Skipped'
|
||||||
$result.Error | Should -Match 'Skipped'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,10 +121,9 @@ Describe 'Invoke-GiteaFileDownload' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
It 'Should not overwrite existing file without Force' {
|
It 'Should not overwrite existing file without Force' {
|
||||||
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/raw/path/to/existingfile.txt' -token 'abc123'
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/path/to/existingfile.txt' -token 'abc123'
|
||||||
|
|
||||||
$result.Success | Should -Be $false
|
$result.Result | Should -Be 'Skipped'
|
||||||
$result.Error | Should -Match 'already exists'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,9 +149,9 @@ Describe 'Invoke-GiteaFileDownload' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
It 'Should capture error and mark download as failed' {
|
It 'Should capture error and mark download as failed' {
|
||||||
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/raw/path/to/badfile.txt' -token 'abc123'
|
$result = Invoke-GiteaFileDownload -downloadURL 'https://gitea.example.com/test/example/raw/branch/dev/path/to/badfile.txt' -token 'abc123'
|
||||||
|
|
||||||
$result.Success | Should -Be $false
|
$result.Result | Should -Be 'Failure'
|
||||||
$result.Error | Should -Match 'Failed to download'
|
$result.Error | Should -Match 'Failed to download'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user