Skip to content

Commit d499705

Browse files
authored
GitHubRepositories: Add Get/Set GitHub Repository Actions Permissions (#301)
Adds the following functions to the `GitHubRepositories` module: - `Get-GitHubRepositoryActionsPermission` - `Set-GitHubRepositoryActionsPermission` #### References - [Get GitHub Actions permissions for a repository](https://docs.github.com./en/free-pro-team@latest/rest/reference/actions#get-github-actions-permissions-for-a-repository) - [Set GitHub Actions permissions for a repository](https://docs.github.com./en/free-pro-team@latest/rest/reference/actions#set-github-actions-permissions-for-a-repository)
1 parent 8a29c7f commit d499705

6 files changed

+481
-0
lines changed

Formatters/GitHubRepositories.Format.ps1xml

+27
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,32 @@
177177
</TableRowEntries>
178178
</TableControl>
179179
</View>
180+
<!--=============== GitHub.RepositoryActionsPermission Type View ===============-->
181+
<View>
182+
<Name>GitHub.RepositoryActionsPermission</Name>
183+
<ViewSelectedBy>
184+
<TypeName>GitHub.RepositoryActionsPermission</TypeName>
185+
</ViewSelectedBy>
186+
<ListControl>
187+
<ListEntries>
188+
<ListEntry>
189+
<ListItems>
190+
<ListItem>
191+
<PropertyName>RepositoryName</PropertyName>
192+
</ListItem>
193+
<ListItem>
194+
<PropertyName>RepositoryUrl</PropertyName>
195+
</ListItem>
196+
<ListItem>
197+
<PropertyName>Enabled</PropertyName>
198+
</ListItem>
199+
<ListItem>
200+
<PropertyName>AllowedActions</PropertyName>
201+
</ListItem>
202+
</ListItems>
203+
</ListEntry>
204+
</ListEntries>
205+
</ListControl>
206+
</View>
180207
</ViewDefinitions>
181208
</Configuration>

GitHubRepositories.ps1

+328
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Licensed under the MIT License.
33

44
@{
5+
GitHubRepositoryActionsPermissionTypeName = 'GitHub.RepositoryActionsPermission'
56
GitHubRepositoryTypeName = 'GitHub.Repository'
67
GitHubRepositoryTopicTypeName = 'GitHub.RepositoryTopic'
78
GitHubRepositoryContributorTypeName = 'GitHub.RepositoryContributor'
@@ -2692,6 +2693,257 @@ filter Disable-GitHubRepositorySecurityFix
26922693
Invoke-GHRestMethod @params | Out-Null
26932694
}
26942695

2696+
filter Get-GitHubRepositoryActionsPermission
2697+
{
2698+
<#
2699+
.SYNOPSIS
2700+
Gets GitHub Actions permission for a repository on GitHub.
2701+
2702+
.DESCRIPTION
2703+
Gets GitHub Actions permission for a repository on GitHub.
2704+
2705+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
2706+
2707+
.PARAMETER OwnerName
2708+
Owner of the repository.
2709+
If not supplied here, the DefaultOwnerName configuration property value will be used.
2710+
2711+
.PARAMETER RepositoryName
2712+
Name of the repository.
2713+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
2714+
2715+
.PARAMETER Uri
2716+
Uri for the repository.
2717+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
2718+
them individually.
2719+
2720+
.PARAMETER AccessToken
2721+
If provided, this will be used as the AccessToken for authentication with the
2722+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
2723+
2724+
.INPUTS
2725+
GitHub.Branch
2726+
GitHub.Content
2727+
GitHub.Event
2728+
GitHub.Issue
2729+
GitHub.IssueComment
2730+
GitHub.Label
2731+
GitHub.Milestone
2732+
GitHub.PullRequest
2733+
GitHub.Project
2734+
GitHub.ProjectCard
2735+
GitHub.ProjectColumn
2736+
GitHub.Release
2737+
GitHub.Repository
2738+
2739+
.OUTPUTS
2740+
GitHub.RepositoryActionsPermission
2741+
2742+
.NOTES
2743+
The authenticated user must have admin access to the repository.
2744+
2745+
.EXAMPLE
2746+
Get-GitHubRepositoryActionsPermission -OwnerName Microsoft -RepositoryName PowerShellForGitHub
2747+
2748+
Gets GitHub Actions permissions for the PowerShellForGithub repository.
2749+
2750+
.EXAMPLE
2751+
Get-GitHubRepositoryActionsPermission -Uri https://github.com./PowerShell/PowerShellForGitHub
2752+
2753+
Gets GitHub Actions permissions for the PowerShellForGithub repository.
2754+
#>
2755+
[CmdletBinding(
2756+
PositionalBinding = $false,
2757+
DefaultParameterSetName='Elements')]
2758+
param(
2759+
[Parameter(
2760+
ParameterSetName='Elements')]
2761+
[string] $OwnerName,
2762+
2763+
[Parameter(ParameterSetName='Elements')]
2764+
[string] $RepositoryName,
2765+
2766+
[Parameter(
2767+
Mandatory,
2768+
Position = 1,
2769+
ValueFromPipelineByPropertyName,
2770+
ParameterSetName='Uri')]
2771+
[Alias('RepositoryUrl')]
2772+
[string] $Uri,
2773+
2774+
[string] $AccessToken
2775+
)
2776+
2777+
Write-InvocationLog
2778+
2779+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
2780+
$OwnerName = $elements.ownerName
2781+
$RepositoryName = $elements.repositoryName
2782+
2783+
$telemetryProperties = @{
2784+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
2785+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
2786+
}
2787+
2788+
$params = @{
2789+
UriFragment = "/repos/$OwnerName/$RepositoryName/actions/permissions"
2790+
Description = "Getting GitHub Actions permissions for $RepositoryName"
2791+
Method = 'Get'
2792+
AccessToken = $AccessToken
2793+
TelemetryEventName = $MyInvocation.MyCommand.Name
2794+
TelemetryProperties = $telemetryProperties
2795+
}
2796+
2797+
return (Invoke-GHRestMethod @params |
2798+
Add-GitHubRepositoryActionsPermissionAdditionalProperties -RepositoryName $RepositoryName -OwnerName $OwnerName)
2799+
}
2800+
2801+
filter Set-GitHubRepositoryActionsPermission
2802+
{
2803+
<#
2804+
.SYNOPSIS
2805+
Sets GitHub Actions permissions for a repository on GitHub.
2806+
2807+
.DESCRIPTION
2808+
Sets GitHub Actions permissions for a repository on GitHub.
2809+
2810+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
2811+
2812+
.PARAMETER OwnerName
2813+
Owner of the repository.
2814+
If not supplied here, the DefaultOwnerName configuration property value will be used.
2815+
2816+
.PARAMETER RepositoryName
2817+
Name of the repository.
2818+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
2819+
2820+
.PARAMETER Uri
2821+
Uri for the repository.
2822+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
2823+
them individually.
2824+
2825+
.PARAMETER AllowedActions
2826+
The permissions policy that controls the actions that are allowed to run.
2827+
Can be one of: 'All', 'LocalOnly', 'Selected' or 'Disabled'.
2828+
2829+
.PARAMETER AccessToken
2830+
If provided, this will be used as the AccessToken for authentication with the
2831+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
2832+
2833+
.INPUTS
2834+
GitHub.Branch
2835+
GitHub.Content
2836+
GitHub.Event
2837+
GitHub.Issue
2838+
GitHub.IssueComment
2839+
GitHub.Label
2840+
GitHub.Milestone
2841+
GitHub.PullRequest
2842+
GitHub.Project
2843+
GitHub.ProjectCard
2844+
GitHub.ProjectColumn
2845+
GitHub.Release
2846+
GitHub.Repository
2847+
2848+
.OUTPUTS
2849+
None
2850+
2851+
.NOTES
2852+
The authenticated user must have admin access to the repository.
2853+
2854+
If the repository belongs to an organization or enterprise that has set restrictive
2855+
permissions at the organization or enterprise levels, such as 'AllowedActions' to 'Selected'
2856+
actions, then you cannot override them for the repository.
2857+
2858+
.EXAMPLE
2859+
Set-GitHubRepositoryActionsPermission -OwnerName Microsoft -RepositoryName PowerShellForGitHub -AllowedActions All
2860+
2861+
Sets GitHub Actions permissions to 'All' for the PowerShellForGithub repository.
2862+
2863+
.EXAMPLE
2864+
Set-GitHubRepositoryActionsPermission -Uri https://github.com./PowerShell/PowerShellForGitHub -AllowedActions Disabled
2865+
2866+
Sets GitHub Actions permissions to 'Disabled' for the PowerShellForGithub repository.
2867+
#>
2868+
[CmdletBinding(
2869+
PositionalBinding = $false,
2870+
SupportsShouldProcess,
2871+
DefaultParameterSetName='Elements')]
2872+
param(
2873+
[Parameter(
2874+
ParameterSetName='Elements')]
2875+
[string] $OwnerName,
2876+
2877+
[Parameter(ParameterSetName='Elements')]
2878+
[string] $RepositoryName,
2879+
2880+
[Parameter(
2881+
Mandatory,
2882+
Position = 1,
2883+
ValueFromPipelineByPropertyName,
2884+
ParameterSetName='Uri')]
2885+
[Alias('RepositoryUrl')]
2886+
[string] $Uri,
2887+
2888+
[Parameter(Mandatory)]
2889+
[ValidateSet('All', 'LocalOnly', 'Selected', 'Disabled')]
2890+
[string] $AllowedActions,
2891+
2892+
[string] $AccessToken
2893+
)
2894+
2895+
Write-InvocationLog
2896+
2897+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
2898+
$OwnerName = $elements.ownerName
2899+
$RepositoryName = $elements.repositoryName
2900+
2901+
$telemetryProperties = @{
2902+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
2903+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
2904+
}
2905+
2906+
$allowedActionsConverter = @{
2907+
All = 'all'
2908+
LocalOnly = 'local_only'
2909+
Selected = 'selected'
2910+
Disabled = 'disabled'
2911+
}
2912+
2913+
$hashBodyAllowedActions = $allowedActionsConverter[$AllowedActions]
2914+
2915+
if ($AllowedActions -eq 'Disabled')
2916+
{
2917+
$hashBody = @{
2918+
'enabled' = $false
2919+
}
2920+
}
2921+
else
2922+
{
2923+
$hashBody = @{
2924+
'enabled' = $true
2925+
'allowed_actions' = $hashBodyAllowedActions
2926+
}
2927+
}
2928+
2929+
if (-not $PSCmdlet.ShouldProcess($RepositoryName, 'Set GitHub Repository Actions Permissions'))
2930+
{
2931+
return
2932+
}
2933+
2934+
$params = @{
2935+
UriFragment = "/repos/$OwnerName/$RepositoryName/actions/permissions"
2936+
Description = "Setting GitHub Actions permissions for $RepositoryName"
2937+
Method = 'Put'
2938+
Body = (ConvertTo-Json -InputObject $hashBody)
2939+
AccessToken = $AccessToken
2940+
TelemetryEventName = $MyInvocation.MyCommand.Name
2941+
TelemetryProperties = $telemetryProperties
2942+
}
2943+
2944+
Invoke-GHRestMethod @params | Out-Null
2945+
}
2946+
26952947
filter Add-GitHubRepositoryAdditionalProperties
26962948
{
26972949
<#
@@ -2967,3 +3219,79 @@ filter Add-GitHubRepositoryCollaboratorAdditionalProperties
29673219
Write-Output $item
29683220
}
29693221
}
3222+
3223+
filter Add-GitHubRepositoryActionsPermissionAdditionalProperties
3224+
{
3225+
<#
3226+
.SYNOPSIS
3227+
Adds type name and additional properties to ease pipelining to GitHub Repository Actions Permissions objects.
3228+
3229+
.PARAMETER InputObject
3230+
The GitHub object to add additional properties to.
3231+
3232+
.PARAMETER TypeName
3233+
The type that should be assigned to the object.
3234+
3235+
.PARAMETER OwnerName
3236+
Owner of the repository. This information might be obtainable from InputObject, so this
3237+
is optional based on what InputObject contains.
3238+
3239+
.PARAMETER RepositoryName
3240+
Name of the repository. This information might be obtainable from InputObject, so this
3241+
is optional based on what InputObject contains.
3242+
3243+
.INPUTS
3244+
PSCustomObject
3245+
3246+
.OUTPUTS
3247+
GitHub.RepositoryActionsPermission
3248+
#>
3249+
[CmdletBinding()]
3250+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '',
3251+
Justification='Internal helper that is definitely adding more than one property.')]
3252+
param(
3253+
[Parameter(
3254+
Mandatory,
3255+
ValueFromPipeline)]
3256+
[AllowNull()]
3257+
[PSCustomObject[]] $InputObject,
3258+
3259+
[ValidateNotNullOrEmpty()]
3260+
[string] $TypeName = $script:GitHubRepositoryActionsPermissionTypeName,
3261+
3262+
[Parameter(Mandatory)]
3263+
[string] $OwnerName,
3264+
3265+
[Parameter(Mandatory)]
3266+
[string] $RepositoryName
3267+
)
3268+
3269+
foreach ($item in $InputObject)
3270+
{
3271+
$item.PSObject.TypeNames.Insert(0, $TypeName)
3272+
3273+
$repositoryUrl = (Join-GitHubUri -OwnerName $OwnerName -RepositoryName $RepositoryName)
3274+
3275+
Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force
3276+
Add-Member -InputObject $item -Name 'RepositoryName' -Value $RepositoryName -MemberType NoteProperty -Force
3277+
3278+
$allowedActionsConverter = @{
3279+
all = 'All'
3280+
local_only = 'LocalOnly'
3281+
selected = 'Selected'
3282+
}
3283+
3284+
if ([String]::IsNullOrEmpty($item.allowed_actions))
3285+
{
3286+
$allowedActions = 'Disabled'
3287+
}
3288+
else
3289+
{
3290+
$allowedActions = $allowedActionsConverter[$item.allowed_actions]
3291+
}
3292+
3293+
Add-Member -InputObject $item -Name 'AllowedActions' -Value $allowedActions -MemberType NoteProperty -Force
3294+
3295+
Write-Output $item
3296+
}
3297+
}

PowerShellForGitHub.psd1

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
'Get-GitHubRelease',
102102
'Get-GitHubReleaseAsset',
103103
'Get-GitHubRepository',
104+
'Get-GitHubRepositoryActionsPermission',
104105
'Get-GitHubRepositoryBranch',
105106
'Get-GitHubRepositoryBranchProtectionRule',
106107
'Get-GitHubRepositoryCollaborator',
@@ -188,6 +189,7 @@
188189
'Set-GitHubRelease',
189190
'Set-GitHubReleaseAsset',
190191
'Set-GitHubRepository',
192+
'Set-GitHubRepositoryActionsPermission',
191193
'Set-GitHubRepositoryTopic',
192194
'Set-GitHubTeam',
193195
'Split-GitHubUri',

0 commit comments

Comments
 (0)