Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Set-GitHubContent to Upload binary files #364

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c924319
Adding parameter and functionality to upload a local path to GitHub
StanleyGoldman Sep 7, 2021
da023d9
Formatting
StanleyGoldman Sep 7, 2021
6f563c0
Apply suggestions from code review
StanleyGoldman Sep 29, 2021
725a4ee
Changing parameter definition
StanleyGoldman Sep 29, 2021
2db770e
Adding Test
StanleyGoldman Sep 29, 2021
83beb95
Correcting Mandatory Parameters
StanleyGoldman Aug 15, 2022
62d84e1
Update GitHubContents.ps1
StanleyGoldman Mar 20, 2023
c561302
Merge branch 'master' into pr/StanleyGoldman/364
StanleyGoldman Mar 20, 2023
78ddcac
Update Tests/GitHubContents.tests.ps1
StanleyGoldman Mar 20, 2023
f2fec71
Missing brace
StanleyGoldman Mar 20, 2023
73ed735
Update GitHubContents.ps1
StanleyGoldman Mar 20, 2023
86266b0
Update Tests/GitHubContents.tests.ps1
StanleyGoldman Mar 27, 2023
5c6163a
Update Tests/GitHubContents.tests.ps1
StanleyGoldman Mar 27, 2023
7e2d558
Update Tests/GitHubContents.tests.ps1
StanleyGoldman Mar 27, 2023
bb4a113
Update GitHubContents.ps1
StanleyGoldman Mar 27, 2023
b785642
Moving Tests
StanleyGoldman Mar 27, 2023
03ba727
Merge branch 'master' into upload-binary-content
HowardWolosky Apr 28, 2023
05148af
Using read all bytes since AsByteStream may not exist
StanleyGoldman Apr 29, 2023
c5bba1c
Update GitHubContents.ps1
StanleyGoldman May 2, 2023
f2b93f4
Update GitHubContents.ps1
StanleyGoldman May 2, 2023
28e5253
Fixing "When overwriting file content by path"
StanleyGoldman May 15, 2023
b33cb31
Merge branch 'master' into upload-binary-content
StanleyGoldman May 15, 2023
42f2311
Tweak parameter name to match file
StanleyGoldman May 15, 2023
97f2390
Update GitHubContents.ps1
StanleyGoldman May 15, 2023
e9a81cc
Using WriteAllText to prevent newline
StanleyGoldman May 16, 2023
d1f1382
Including Helpers to use Resolve-UnverifiedPath
StanleyGoldman May 16, 2023
91438d3
Removing Resolve-UnverifiedPath from test
StanleyGoldman May 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions GitHubContents.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ filter Get-GitHubContent
them individually.

.PARAMETER Path
The file path for which to retrieve contents
The remote file path in the repository whose content should be changed.

.PARAMETER BranchName
The branch, or defaults to the default branch of not specified.
Expand Down Expand Up @@ -231,6 +231,9 @@ filter Set-GitHubContent
.PARAMETER Content
The new file content.

.PARAMETER ContentPath
The local path to a file whose content should replace what is currently stored remotely in the repo at the Path location.
StanleyGoldman marked this conversation as resolved.
Show resolved Hide resolved

.PARAMETER Sha
The SHA value of the current file if present. If this parameter is not provided, and the
file currently exists in the specified branch of the repo, it will be read to obtain this
Expand Down Expand Up @@ -294,19 +297,30 @@ filter Set-GitHubContent
param(
[Parameter(
Mandatory,
ParameterSetName = 'Elements')]
ParameterSetName = 'ElementsContent')]
[Parameter(
Mandatory,
ParameterSetName = 'ElementsContentPath')]
[string] $OwnerName,

[Parameter(
Mandatory,
ParameterSetName = 'Elements')]
ParameterSetName = 'ElementsContent')]
[Parameter(
Mandatory,
ParameterSetName = 'ElementsContentPath')]
[string] $RepositoryName,

[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1,
ParameterSetName='Uri')]
ParameterSetName='UriContent')]
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
Position = 1,
ParameterSetName='UriContentPath')]
[Alias('RepositoryUrl')]
[string] $Uri,

Expand All @@ -323,9 +337,22 @@ filter Set-GitHubContent

[Parameter(
Mandatory,
Position = 4)]
Position = 4,
ParameterSetName = 'ElementsContent')]
[Parameter(
Mandatory,
Position = 4,
ParameterSetName = 'UriContent')]
[string] $Content,

[Parameter(
Mandatory,
ParameterSetName = 'ElementsContentPath')]
[Parameter(
Mandatory,
ParameterSetName = 'UriContentPath')]
[string] $ContentPath,

[Parameter(ValueFromPipelineByPropertyName)]
[string] $Sha,

Expand Down Expand Up @@ -358,7 +385,15 @@ filter Set-GitHubContent

$uriFragment = "/repos/$OwnerName/$RepositoryName/contents/$Path"

$encodedContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Content))
if ($PSBoundParameters.ContainsKey('ContentPath'))
{
$ContentPath = Resolve-UnverifiedPath -Path $ContentPath
$encodedContent = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes($ContentPath))
StanleyGoldman marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
$encodedContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Content))
}

$hashBody = @{
message = $CommitMessage
Expand Down
122 changes: 121 additions & 1 deletion Tests/GitHubContents.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Describe 'GitHubContents/Set-GitHubContent' {
$setGitHubContentParms = @{
Path = "$filePath/$fileName"
CommitMessage = $commitMessage
Branch = $branchName
BranchName = $branchName
Content = $content
Uri = $repo.svn_url
CommitterName = $committerName
Expand Down Expand Up @@ -399,6 +399,126 @@ Describe 'GitHubContents/Set-GitHubContent' {
}
}

Context 'When setting new file content by path' {
BeforeAll {
$filePath = 'notes'
$fileName = 'hello-by-path.txt'
$commitMessage = 'Commit Message'
$content = 'This is the content for hello-by-path.txt'
$branchName = 'master'
$committerName = 'John Doe'
$committerEmail = '[email protected]'
$authorName = 'Jane Doe'
$authorEmail = '[email protected]'

$contentPath = New-TemporaryFile
[System.IO.File]::WriteAllText($contentPath, $content)

$setGitHubContentParms = @{
Path = "$filePath/$fileName"
CommitMessage = $commitMessage
BranchName = $branchName
ContentPath = $contentPath.FullName
Uri = $repo.svn_url
CommitterName = $committerName
CommitterEmail = $committerEmail
authorName = $authorName
authorEmail = $authorEmail
}

$result = Set-GitHubContent @setGitHubContentParms -PassThru
}

It 'Should have the expected type and additional properties' {
$result.PSObject.TypeNames[0] | Should -Be 'GitHub.Content'
$result.content.name | Should -Be $fileName
$result.content.path | Should -Be "$filePath/$fileName"
$result.content.url | Should -Be ("https://api.github.com/repos/$($script:ownerName)" +
"/$repoName/contents/$filePath/$($fileName)?ref=$branchName")
$result.commit.author.name | Should -Be $authorName
$result.commit.author.email | Should -Be $authorEmail
$result.commit.committer.name | Should -Be $committerName
$result.commit.committer.email | Should -Be $committerEmail
$result.commit.message | Should -Be $commitMessage
}

It 'Should have written the correct content' {
$getGitHubContentParms = @{
Path = "$filePath/$fileName"
Uri = $repo.svn_url
MediaType = 'Raw'
ResultAsString = $true
}

$writtenContent = Get-GitHubContent @getGitHubContentParms

$content | Should -Be $writtenContent
}

AfterAll {
Remove-Item -Path $contentPath
}
}

Context 'When overwriting file content by path' {
StanleyGoldman marked this conversation as resolved.
Show resolved Hide resolved
BeforeAll {
$filePath = 'notes'
$fileName = 'hello-by-path.txt'
$commitMessage = 'Commit Message 2'
$content = 'This is the new content for hello-by-path.txt'
$branchName = 'master'
$committerName = 'John Doe'
$committerEmail = '[email protected]'
$authorName = 'Jane Doe'
$authorEmail = '[email protected]'

$contentPath = New-TemporaryFile
[System.IO.File]::WriteAllText($contentPath, $content)

$setGitHubContentParms = @{
Path = "$filePath/$fileName"
CommitMessage = $commitMessage
Branch = $branchName
ContentPath = $contentPath.FullName
Uri = $repo.svn_url
CommitterName = $committerName
CommitterEmail = $committerEmail
authorName = $authorName
authorEmail = $authorEmail
}

$result = Set-GitHubContent @setGitHubContentParms -PassThru
}

It 'Should have the expected type and additional properties' {
$result.PSObject.TypeNames[0] | Should -Be 'GitHub.Content'
$result.content.name | Should -Be $fileName
$result.content.path | Should -Be "$filePath/$fileName"
$result.content.url | Should -Be ("https://api.github.com/repos/$($script:ownerName)" +
"/$repoName/contents/$filePath/$($fileName)?ref=$branchName")
$result.commit.author.name | Should -Be $authorName
$result.commit.author.email | Should -Be $authorEmail
$result.commit.committer.name | Should -Be $committerName
$result.commit.committer.email | Should -Be $committerEmail
$result.commit.message | Should -Be $commitMessage
}

It 'Should have written the correct content' {
$getGitHubContentParms = @{
Path = "$filePath/$fileName"
Uri = $repo.svn_url
MediaType = 'Raw'
ResultAsString = $true
}

$content | Should -Be (Get-GitHubContent @getGitHubContentParms)
}

AfterAll {
Remove-Item -Path $contentPath
}
}

Context 'When Specifying only one Committer parameter' {
BeforeAll {
$setGitHubContentParms = @{
Expand Down