|
| 1 | +function Get-GitHubContent |
| 2 | +{ |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Retrieve the contents of a file or directory in a repository on GitHub. |
| 6 | +
|
| 7 | + .DESCRIPTION |
| 8 | + Retrieve content from files on GitHub. |
| 9 | + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub |
| 10 | +
|
| 11 | + .PARAMETER OwnerName |
| 12 | + Owner of the repository. |
| 13 | + If not supplied here, the DefaultOwnerName configuration property value will be used. |
| 14 | +
|
| 15 | + .PARAMETER RepositoryName |
| 16 | + Name of the repository. |
| 17 | + If not supplied here, the DefaultRepositoryName configuration property value will be used. |
| 18 | +
|
| 19 | + .PARAMETER Uri |
| 20 | + Uri for the repository. |
| 21 | + The OwnerName and RepositoryName will be extracted from here instead of needing to provide |
| 22 | + them individually. |
| 23 | +
|
| 24 | + .PARAMETER Path |
| 25 | + The file path for which to retrieve contents |
| 26 | +
|
| 27 | + .PARAMETER MediaType |
| 28 | + The format in which the API will return the body of the issue. |
| 29 | + Object - Return a json object representation a file or folder. This is the default if you do not pass any specific media type. |
| 30 | + Raw - Return the raw contents of a file. |
| 31 | + Html - For markup files such as Markdown or AsciiDoc, you can retrieve the rendered HTML using the Html media type. |
| 32 | +
|
| 33 | + .PARAMETER ResultAsString |
| 34 | + If this switch is specified and the MediaType is either Raw or Html then the resulting bytes will be decoded the result will be |
| 35 | + returned as a string instead of bytes. If the MediaType is Object, then an additional property on the object is returned 'contentAsString' |
| 36 | + which will be the decoded base64 result as a string. |
| 37 | +
|
| 38 | + .PARAMETER AccessToken |
| 39 | + If provided, this will be used as the AccessToken for authentication with the |
| 40 | + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. |
| 41 | +
|
| 42 | + .PARAMETER NoStatus |
| 43 | + If this switch is specified, long-running commands will run on the main thread |
| 44 | + with no commandline status update. When not specified, those commands run in |
| 45 | + the background, enabling the command prompt to provide status information. |
| 46 | + If not supplied here, the DefaultNoStatus configuration property value will be used. |
| 47 | +
|
| 48 | + .EXAMPLE |
| 49 | + Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path README.md -MediaType Html |
| 50 | +
|
| 51 | + Get the Html output for the README.md file |
| 52 | +
|
| 53 | + .EXAMPLE |
| 54 | + Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path LICENSE |
| 55 | +
|
| 56 | + Get the Binary file output for the LICENSE file |
| 57 | +
|
| 58 | + .EXAMPLE |
| 59 | + Get-GitHubContent -OwnerName microsoft -RepositoryName PowerShellForGitHub -Path Tests |
| 60 | +
|
| 61 | + List the files within the "Tests" path of the repository |
| 62 | +#> |
| 63 | + [CmdletBinding( |
| 64 | + SupportsShouldProcess, |
| 65 | + DefaultParametersetName = 'Elements')] |
| 66 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] |
| 67 | + param( |
| 68 | + [Parameter(Mandatory, ParameterSetName = 'Elements')] |
| 69 | + [string] $OwnerName, |
| 70 | + |
| 71 | + [Parameter(Mandatory, ParameterSetName = 'Elements')] |
| 72 | + [string] $RepositoryName, |
| 73 | + |
| 74 | + [Parameter( |
| 75 | + Mandatory, |
| 76 | + ParameterSetName='Uri')] |
| 77 | + [string] $Uri, |
| 78 | + |
| 79 | + [string] $Path, |
| 80 | + |
| 81 | + [ValidateSet('Raw', 'Html', 'Object')] |
| 82 | + [string] $MediaType = 'Object', |
| 83 | + |
| 84 | + [switch] $ResultAsString, |
| 85 | + |
| 86 | + [string] $AccessToken, |
| 87 | + |
| 88 | + [switch] $NoStatus |
| 89 | + ) |
| 90 | + |
| 91 | + Write-InvocationLog |
| 92 | + |
| 93 | + $elements = Resolve-RepositoryElements -DisableValidation |
| 94 | + $OwnerName = $elements.ownerName |
| 95 | + $RepositoryName = $elements.repositoryName |
| 96 | + |
| 97 | + $telemetryProperties = @{ |
| 98 | + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) |
| 99 | + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) |
| 100 | + } |
| 101 | + |
| 102 | + $description = [String]::Empty |
| 103 | + |
| 104 | + $uriFragment = "/repos/$OwnerName/$RepositoryName/contents" |
| 105 | + |
| 106 | + if ($PSBoundParameters.ContainsKey('Path')) |
| 107 | + { |
| 108 | + $Path = $Path.TrimStart("\", "/") |
| 109 | + $uriFragment += "/$Path" |
| 110 | + $description = "Getting content for $Path in $RepositoryName" |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + $description = "Getting all content for in $RepositoryName" |
| 115 | + } |
| 116 | + |
| 117 | + $params = @{ |
| 118 | + 'UriFragment' = $uriFragment |
| 119 | + 'Description' = $description |
| 120 | + 'AcceptHeader' = (Get-MediaAcceptHeader -MediaType $MediaType) |
| 121 | + 'AccessToken' = $AccessToken |
| 122 | + 'TelemetryEventName' = $MyInvocation.MyCommand.Name |
| 123 | + 'TelemetryProperties' = $telemetryProperties |
| 124 | + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) |
| 125 | + } |
| 126 | + |
| 127 | + $result = Invoke-GHRestMethodMultipleResult @params |
| 128 | + |
| 129 | + if ($ResultAsString) |
| 130 | + { |
| 131 | + if ($MediaType -eq 'Raw' -or $MediaType -eq 'Html') |
| 132 | + { |
| 133 | + # Decode bytes to string |
| 134 | + $result = [System.Text.Encoding]::UTF8.GetString($result) |
| 135 | + } |
| 136 | + elseif ($MediaType -eq 'Object') |
| 137 | + { |
| 138 | + # Convert from base64 |
| 139 | + $decoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($result.content)) |
| 140 | + Add-Member -InputObject $result -NotePropertyName "contentAsString" -NotePropertyValue $decoded |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return $result |
| 145 | +} |
0 commit comments