From 1b404ef2e5f0936cccd26244ad9ad3300322127e Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 7 Sep 2021 15:04:13 -0400 Subject: [PATCH 1/5] Adding parameter and functionality to upload a local path to GitHub --- GitHubContents.ps1 | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 index 9962e91e..2d00be8a 100644 --- a/GitHubContents.ps1 +++ b/GitHubContents.ps1 @@ -231,6 +231,9 @@ filter Set-GitHubContent .PARAMETER Content The new file content. + .PARAMETER ContentPath + The local path to file content. + .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 @@ -322,7 +325,6 @@ filter Set-GitHubContent [string] $CommitMessage, [Parameter( - Mandatory, Position = 4)] [string] $Content, @@ -340,9 +342,11 @@ filter Set-GitHubContent [string] $AuthorEmail, + [string] $ContentPath, + [switch] $PassThru, - [string] $AccessToken + [string] $AccessToken, ) Write-InvocationLog @@ -358,7 +362,12 @@ filter Set-GitHubContent $uriFragment = "/repos/$OwnerName/$RepositoryName/contents/$Path" - $encodedContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Content)) + if ($ContentPath) { + $encodedContent = [Convert]::ToBase64String((Get-Content -path $ContentPath -AsByteStream -Raw)) + } + else { + $encodedContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Content)) + } $hashBody = @{ message = $CommitMessage From 38a20bd80dcee362080d9835027d570fda707841 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 7 Sep 2021 15:09:55 -0400 Subject: [PATCH 2/5] Formatting --- GitHubContents.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 index 2d00be8a..f08694c4 100644 --- a/GitHubContents.ps1 +++ b/GitHubContents.ps1 @@ -346,7 +346,7 @@ filter Set-GitHubContent [switch] $PassThru, - [string] $AccessToken, + [string] $AccessToken ) Write-InvocationLog From 1865d962a913e8c7b3a8ceaee83728805789d953 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 29 Sep 2021 10:10:46 -0400 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Howard Wolosky --- GitHubContents.ps1 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 index f08694c4..d7d13368 100644 --- a/GitHubContents.ps1 +++ b/GitHubContents.ps1 @@ -362,10 +362,12 @@ filter Set-GitHubContent $uriFragment = "/repos/$OwnerName/$RepositoryName/contents/$Path" - if ($ContentPath) { - $encodedContent = [Convert]::ToBase64String((Get-Content -path $ContentPath -AsByteStream -Raw)) + if ($ContentPath) + { + $encodedContent = [Convert]::ToBase64String((Get-Content -Path $ContentPath -AsByteStream -Raw)) } - else { + else + { $encodedContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Content)) } From 909d8eab12a12a0b06e666aba7a79dffd334e35f Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 29 Sep 2021 10:13:26 -0400 Subject: [PATCH 4/5] Changing parameter definition --- GitHubContents.ps1 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 index d7d13368..dd7b4afb 100644 --- a/GitHubContents.ps1 +++ b/GitHubContents.ps1 @@ -325,9 +325,17 @@ filter Set-GitHubContent [string] $CommitMessage, [Parameter( - Position = 4)] + Mandatory, + Position = 4, + ParameterSetName = 'Content')] [string] $Content, + [Parameter( + Mandatory, + Position = 4, + ParameterSetName = 'Content')] + [string] $ContentPath, + [Parameter(ValueFromPipelineByPropertyName)] [string] $Sha, @@ -342,8 +350,6 @@ filter Set-GitHubContent [string] $AuthorEmail, - [string] $ContentPath, - [switch] $PassThru, [string] $AccessToken From 13a260df6320107a5ec36b7650c8280669e1d764 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 29 Sep 2021 10:25:57 -0400 Subject: [PATCH 5/5] Adding Test --- Tests/GitHubContents.tests.ps1 | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/Tests/GitHubContents.tests.ps1 b/Tests/GitHubContents.tests.ps1 index 10e85c79..8f66509f 100644 --- a/Tests/GitHubContents.tests.ps1 +++ b/Tests/GitHubContents.tests.ps1 @@ -335,6 +335,67 @@ try } } + Context 'When setting new file content by path' { + BeforeAll { + $filePath = 'notes' + $fileName = 'hello.txt' + $commitMessage = 'Commit Message' + $content = 'This is the content for test.txt' + $branchName = 'master' + $committerName = 'John Doe' + $committerEmail = 'john.doe@testdomain.com' + $authorName = 'Jane Doe' + $authorEmail = 'jane.doe@testdomain.com' + + $contentPath = New-TemporaryFile + $content | Out-File -FilePath $contentPath + + $setGitHubContentParms = @{ + Path = "$filePath/$fileName" + CommitMessage = $commitMessage + Branch = $branchName + ContentPath = $contentPath + 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' { BeforeAll { $filePath = 'notes'