From db1e3fa9622c7bdd2e6311521c86637938658e02 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 9 Jan 2020 17:32:55 +1100 Subject: [PATCH 1/4] Initial commit --- GitHubContents.ps1 | 101 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 GitHubContents.ps1 diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 new file mode 100644 index 00000000..c295236c --- /dev/null +++ b/GitHubContents.ps1 @@ -0,0 +1,101 @@ +function Get-GitHubContents +{ +<# + .SYNOPSIS + Retrieve content from files on GitHub. + .DESCRIPTION + Retrieve content from files on GitHub. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + .PARAMETER Path + The file path for which to retrieve contents + .PARAMETER MediaType + The format in which the API will return the body of the issue. + Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type. + Text - Return a text only representation of the markdown body. Response will include body_text. + Html - Return HTML rendered from the body's markdown. Response will include body_html. + Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html. + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + param( + [Parameter(Mandatory, ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(Mandatory, ParameterSetName='Elements')] + [string] $RepositoryName, + + [string] $Path, + + [ValidateSet('Raw', 'Text', 'Html', 'Full')] + [string] $MediaType ='Raw', + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog + + $elements = Resolve-RepositoryElements -DisableValidation + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'Path' = $PSBoundParameters.ContainsKey('Path') + } + + $uriFragment = [String]::Empty + $description = [String]::Empty + if ($OwnerName -xor $RepositoryName) + { + $message = 'You must specify BOTH Owner Name and Repository Name when one is provided.' + Write-Log -Message $message -Level Error + throw $message + } + + #repos/:owner/:repo/contents/:path + $uriFragment = "/repos/$OwnerName/$RepositoryName/content" + + + if ($PSBoundParameters.ContainsKey('Path')) + { + $uriFragment += "/$Path" + $description = "Getting content for $Path in $RepositoryName" + } + else + { + $description = "Getting all content for in $RepositoryName" + } + + $params = @{ + 'UriFragment' = $uriFragment + 'Description' = $description + 'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader) + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + $result = Invoke-GHRestMethodMultipleResult @params + + return $result +} From 23e7467bc04536dcf6ac17f94b4132572cde0aa8 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 9 Jan 2020 18:56:54 +1100 Subject: [PATCH 2/4] Fixes up the path to be correct, removes unneeded check --- GitHubContents.ps1 | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 index c295236c..ba783d72 100644 --- a/GitHubContents.ps1 +++ b/GitHubContents.ps1 @@ -63,16 +63,10 @@ function Get-GitHubContents } $uriFragment = [String]::Empty - $description = [String]::Empty - if ($OwnerName -xor $RepositoryName) - { - $message = 'You must specify BOTH Owner Name and Repository Name when one is provided.' - Write-Log -Message $message -Level Error - throw $message - } + $description = [String]::Empty #repos/:owner/:repo/contents/:path - $uriFragment = "/repos/$OwnerName/$RepositoryName/content" + $uriFragment = "/repos/$OwnerName/$RepositoryName/contents" if ($PSBoundParameters.ContainsKey('Path')) From 71ecd1e0642c9411e3a2fdf479e23c69ba40e3f9 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 9 Jan 2020 18:57:38 +1100 Subject: [PATCH 3/4] remove comment --- GitHubContents.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 index ba783d72..d7cfb044 100644 --- a/GitHubContents.ps1 +++ b/GitHubContents.ps1 @@ -65,9 +65,7 @@ function Get-GitHubContents $uriFragment = [String]::Empty $description = [String]::Empty - #repos/:owner/:repo/contents/:path - $uriFragment = "/repos/$OwnerName/$RepositoryName/contents" - + $uriFragment = "/repos/$OwnerName/$RepositoryName/contents" if ($PSBoundParameters.ContainsKey('Path')) { From 79a46b17822bba6f2ace044d09be368bb4cb16b2 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 4 Feb 2020 13:51:07 +1100 Subject: [PATCH 4/4] Pushes udpated/cleaned up code, need to get a test going --- GitHubContents.ps1 | 67 +++++++++++++++++++++++----------------- GitHubCore.ps1 | 31 +++++++++++++++++++ PowerShellForGitHub.psd1 | 4 ++- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/GitHubContents.ps1 b/GitHubContents.ps1 index d7cfb044..526ad9e9 100644 --- a/GitHubContents.ps1 +++ b/GitHubContents.ps1 @@ -1,8 +1,7 @@ -function Get-GitHubContents -{ -<# +function Get-GitHubContent { + <# .SYNOPSIS - Retrieve content from files on GitHub. + Retrieve the contents of a file or directory in a repository on GitHub. .DESCRIPTION Retrieve content from files on GitHub. The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub @@ -16,10 +15,8 @@ function Get-GitHubContents The file path for which to retrieve contents .PARAMETER MediaType The format in which the API will return the body of the issue. - Raw - Return the raw markdown body. Response will include body. This is the default if you do not pass any specific media type. - Text - Return a text only representation of the markdown body. Response will include body_text. - Html - Return HTML rendered from the body's markdown. Response will include body_html. - Full - Return raw, text and HTML representations. Response will include body, body_text, and body_html. + Raw - Use the Raw media type to retrieve the contents of the file. + Html - For markup files such as Markdown or AsciiDoc, you can retrieve the rendered HTML using the Html media type. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. @@ -28,22 +25,37 @@ function Get-GitHubContents with no commandline status update. When not specified, those commands run in the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path README.md -MediaType Html + + Get the Html output for the README.md file + + .EXAMPLE + Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path LICENSE + + Get the Binary file output for the LICENSE file + + .EXAMPLE + Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path Tests + + List the files within the "Tests" path of the repository #> [CmdletBinding( SupportsShouldProcess, - DefaultParametersetName='Elements')] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + DefaultParametersetName = 'Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] param( - [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName = 'Elements')] [string] $OwnerName, - [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName = 'Elements')] [string] $RepositoryName, [string] $Path, - [ValidateSet('Raw', 'Text', 'Html', 'Full')] - [string] $MediaType ='Raw', + [ValidateSet('Raw', 'Html')] + [string] $MediaType = 'Raw', [string] $AccessToken, @@ -57,34 +69,33 @@ function Get-GitHubContents $RepositoryName = $elements.repositoryName $telemetryProperties = @{ - 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'Path' = $PSBoundParameters.ContainsKey('Path') + 'Path' = $PSBoundParameters.ContainsKey('Path') } $uriFragment = [String]::Empty - $description = [String]::Empty + $description = [String]::Empty - $uriFragment = "/repos/$OwnerName/$RepositoryName/contents" + $uriFragment = "/repos/$OwnerName/$RepositoryName/contents" - if ($PSBoundParameters.ContainsKey('Path')) - { + if ($PSBoundParameters.ContainsKey('Path')) { + $Path = $Path.TrimStart("\\", "/") $uriFragment += "/$Path" $description = "Getting content for $Path in $RepositoryName" } - else - { + else { $description = "Getting all content for in $RepositoryName" } $params = @{ - 'UriFragment' = $uriFragment - 'Description' = $description - 'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType -AcceptHeader $symmetraAcceptHeader) - 'AccessToken' = $AccessToken - 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'UriFragment' = $uriFragment + 'Description' = $description + 'AcceptHeader' = (Get-ContentMediaType -MediaType $MediaType) + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } $result = Invoke-GHRestMethodMultipleResult @params diff --git a/GitHubCore.ps1 b/GitHubCore.ps1 index ea61d95d..577a5f99 100644 --- a/GitHubCore.ps1 +++ b/GitHubCore.ps1 @@ -973,3 +973,34 @@ function Get-MediaAcceptHeader return ($acceptHeaders -join ',') } + +function Get-ContentMediaType +{ +<# + .DESCRIPTION + Returns a formatted AcceptHeader based on the requested MediaType for working with GitHub Content, + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER MediaType + The format in which the API will return the body of the comment or issue. + + Raw - Return the raw contents of a file. This is the default if you do not pass any specific media type. + Html - For markup files such as Markdown or AsciiDoc, you can retrieve the rendered HTML using the Html media type. + + .PARAMETER AcceptHeader + The accept header that should be included with the MediaType accept header. + + .EXAMPLE + Get-ContentMediaType -MediaType Raw + + Returns a formatted AcceptHeader for v3 of the response object +#> + [CmdletBinding()] + param( + [ValidateSet('Raw', 'Html')] + [string] $MediaType = 'Raw' + ) + + return "application/vnd.github.$mediaTypeVersion.$($MediaType.ToLower())" +} diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index c12a0081..885d3e40 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -25,6 +25,7 @@ 'GitHubBranches.ps1', 'GitHubCore.ps1', 'GitHubComments.ps1', + 'GitHubContents.ps1', 'GitHubEvents.ps1', 'GitHubIssues.ps1', 'GitHubLabels.ps1', @@ -53,8 +54,9 @@ 'Get-GitHubAssignee', 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', - 'Get-GitHubComment', + 'Get-GitHubComment', 'Get-GitHubConfiguration', + 'Get-GitHubContent', 'Get-GitHubEmoji', 'Get-GitHubEvent', 'Get-GitHubGitIgnore',