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

Adding Get Repository.Contents functionality #143

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
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
93 changes: 93 additions & 0 deletions GitHubContents.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
function Get-GitHubContents
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
{
<#
.SYNOPSIS
Retrieve content from files on GitHub.
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
.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
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
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.
#>
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
[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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't anything that we really gain here from logging Path. There's no change in behavior that we'd make with that data, so given that, we wouldn't log it. What would be interesting would be to log something that differentiates usage for getting a single file vs a directory of files. But Path by itself doesn't need to be logged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this logging it's not really possible to differentiate between whether it's a file or a directory since it's just a name and GitHub figures out if it's a file or directory so we'd only know that based on the results. What do you suggest in this case, should i just remove the Path parameter from being logged? Or do you have another idea?

}

$uriFragment = [String]::Empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no value gained with this initialization given that you then go and explicitly set it to a new value at line 68 right after this in the same scope.

$description = [String]::Empty

$uriFragment = "/repos/$OwnerName/$RepositoryName/contents"
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved

if ($PSBoundParameters.ContainsKey('Path'))
{
$uriFragment += "/$Path"
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
$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)
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
Shazwazza marked this conversation as resolved.
Show resolved Hide resolved
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

$result = Invoke-GHRestMethodMultipleResult @params

return $result
}