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

GitHubRepositories: Add Get/Set GitHub Repository Actions Permissions #301

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion Formatters/GitHubRepositories.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
<PropertyName>RepositoryName</PropertyName>
</ListItem>
<ListItem>
<PropertyName>RepositoryUri</PropertyName>
<PropertyName>RepositoryUrl</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Enabled</PropertyName>
Expand Down
108 changes: 95 additions & 13 deletions GitHubRepositories.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# Licensed under the MIT License.

@{
GitHubRepositoryActionsPermissionTypeName = 'GitHub.RepositoryActionsPermission'
GitHubRepositoryTypeName = 'GitHub.Repository'
GitHubRepositoryTopicTypeName = 'GitHub.RepositoryTopic'
GitHubRepositoryContributorTypeName = 'GitHub.RepositoryContributor'
GitHubRepositoryCollaboratorTypeName = 'GitHub.RepositoryCollaborator'
GitHubRepositoryContributorStatisticsTypeName = 'GitHub.RepositoryContributorStatistics'
GitHubRepositoryLanguageTypeName = 'GitHub.RepositoryLanguage'
GitHubRepositoryTagTypeName = 'GitHub.RepositoryTag'
GitHubRepositoryActionsPermissionTypeName = 'GitHub.RepositoryActionsPermission'
}.GetEnumerator() | ForEach-Object {
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
}
Expand Down Expand Up @@ -2794,15 +2794,8 @@ filter Get-GitHubRepositoryActionsPermission
TelemetryProperties = $telemetryProperties
}

$result = Invoke-GHRestMethod @params

return [PSCustomObject]@{
PSTypeName = $GitHubRepositoryActionsPermissionTypeName
RepositoryName = $RepositoryName
RepositoryUri = "https://github.com/$OwnerName/$RepositoryName"
Enabled = $result.enabled
AllowedActions = $result.allowed_actions
}
return (Invoke-GHRestMethod @params |
Add-GitHubRepositoryActionsPermissionAdditionalProperties -RepositoryName $RepositoryName -OwnerName $OwnerName)
}

filter Set-GitHubRepositoryActionsPermission
Expand Down Expand Up @@ -2831,7 +2824,7 @@ filter Set-GitHubRepositoryActionsPermission

.PARAMETER AllowedActions
The permissions policy that controls the actions that are allowed to run.
Can be one of: 'All', 'Local_Only', 'Selected' or 'Disabled'.
Can be one of: 'All', 'LocalOnly', 'Selected' or 'Disabled'.

.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
Expand All @@ -2858,6 +2851,10 @@ filter Set-GitHubRepositoryActionsPermission
.NOTES
The authenticated user must have admin access to the repository.
HowardWolosky marked this conversation as resolved.
Show resolved Hide resolved

If the repository belongs to an organization or enterprise that has set restrictive
permissions at the organization or enterprise levels, such as 'AllowedActions' to 'Selected'
actions, then you cannot override them for the repository.

.EXAMPLE
Set-GitHubRepositoryActionsPermission -OwnerName Microsoft -RepositoryName PowerShellForGitHub -AllowedActions All

Expand Down Expand Up @@ -2889,7 +2886,7 @@ filter Set-GitHubRepositoryActionsPermission
[string] $Uri,

[Parameter(Mandatory)]
[ValidateSet('All', 'Local_Only', 'Selected', 'Disabled')]
[ValidateSet('All', 'LocalOnly', 'Selected', 'Disabled')]
[string] $AllowedActions,

[string] $AccessToken
Expand All @@ -2906,6 +2903,15 @@ filter Set-GitHubRepositoryActionsPermission
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
}

$allowedActionsConverter = @{
All = 'all'
LocalOnly = 'local_only'
Selected = 'selected'
Disabled = 'disabled'
}

$hashBodyAllowedActions = $allowedActionsConverter[$AllowedActions]

if ($AllowedActions -eq 'Disabled')
{
$hashBody = @{
Expand All @@ -2916,7 +2922,7 @@ filter Set-GitHubRepositoryActionsPermission
{
$hashBody = @{
'enabled' = $true
'allowed_actions' = $AllowedActions.ToLower()
'allowed_actions' = $hashBodyAllowedActions
}
}

Expand Down Expand Up @@ -3213,3 +3219,79 @@ filter Add-GitHubRepositoryCollaboratorAdditionalProperties
Write-Output $item
}
}

filter Add-GitHubRepositoryActionsPermissionAdditionalProperties
{
<#
.SYNOPSIS
Adds type name and additional properties to ease pipelining to GitHub Repository Actions Permissions objects.

.PARAMETER InputObject
The GitHub object to add additional properties to.

.PARAMETER TypeName
The type that should be assigned to the object.

.PARAMETER OwnerName
Owner of the repository. This information might be obtainable from InputObject, so this
is optional based on what InputObject contains.

.PARAMETER RepositoryName
Name of the repository. This information might be obtainable from InputObject, so this
is optional based on what InputObject contains.

.INPUTS
PSCustomObject

.OUTPUTS
GitHub.RepositoryCollaborator
HowardWolosky marked this conversation as resolved.
Show resolved Hide resolved
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '',
Justification='Internal helper that is definitely adding more than one property.')]
param(
[Parameter(
Mandatory,
ValueFromPipeline)]
[AllowNull()]
[PSCustomObject[]] $InputObject,

[ValidateNotNullOrEmpty()]
[string] $TypeName = $script:GitHubRepositoryActionsPermissionTypeName,

[Parameter(Mandatory)]
[string] $OwnerName,

[Parameter(Mandatory)]
[string] $RepositoryName
)

foreach ($item in $InputObject)
{
$item.PSObject.TypeNames.Insert(0, $TypeName)

$repositoryUrl = (Join-GitHubUri -OwnerName $OwnerName -RepositoryName $RepositoryName)

Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force
Add-Member -InputObject $item -Name 'RepositoryName' -Value $RepositoryName -MemberType NoteProperty -Force

$allowedActionsConverter = @{
all = 'All'
local_only = 'LocalOnly'
selected = 'Selected'
}

if ([String]::IsNullOrEmpty($item.allowed_actions))
{
$allowedActions = 'Disabled'
}
else
{
$allowedActions = $allowedActionsConverter[$item.allowed_actions]
}

Add-Member -InputObject $item -Name 'AllowedActions' -Value $allowedActions -MemberType NoteProperty -Force

Write-Output $item
}
}
6 changes: 3 additions & 3 deletions Tests/GitHubRepositories.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ try
$repoName = [Guid]::NewGuid().Guid
$repo = New-GitHubRepository -RepositoryName $repoName

$allowedActions = 'All', 'Local_Only', 'Selected', 'Disabled'
$allowedActions = 'All', 'LocalOnly', 'Selected', 'Disabled'
}

foreach ($allowedAction in $allowedActions)
Expand All @@ -1406,7 +1406,7 @@ try
$permissions.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryActionsPermission'

$permissions.RepositoryName | Should -Be $repoName
$permissions.RepositoryUri | Should -Be $repo.svn_url
$permissions.RepositoryUrl | Should -Be $repo.svn_url

if ($allowedAction -eq 'Disabled')
{
Expand Down Expand Up @@ -1443,7 +1443,7 @@ try
BeforeAll {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid)

$allowedActions = 'All', 'Local_Only', 'Selected', 'Disabled'
$allowedActions = 'All', 'LocalOnly', 'Selected', 'Disabled'
}

foreach ($allowedAction in $allowedActions)
Expand Down