diff --git a/src/functions/public/Environments/Get-GitHubEnvironment.ps1 b/src/functions/public/Environments/Get-GitHubEnvironment.ps1 new file mode 100644 index 000000000..027083c48 --- /dev/null +++ b/src/functions/public/Environments/Get-GitHubEnvironment.ps1 @@ -0,0 +1,91 @@ +filter Get-GitHubEnvironment { + <# + .SYNOPSIS + List environments for a repository + + .DESCRIPTION + List environments for a repository + Get an environment for a repository + + .EXAMPLE + Get-GitHubEnvironment -Owner 'PSModule' -Repository 'Github' + + Lists all environments for the 'PSModule/GitHub' repository. + + .EXAMPLE + Get-GitHubEnvironment -Owner 'PSModule' -Repository 'Github' -Name 'Production' + + Get the 'Production' environment for the 'PSModule/GitHub' repository. + + .NOTES + [List environments](https://docs.github.com/en/rest/deployments/environments#list-environments) + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param( + # The name of the organization. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [Alias('Organization', 'User')] + [string] $Owner, + + # The name of the Repository. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Repository, + + # The name of the environment. + [Parameter( + Mandatory, + ParameterSetName = 'NamedEnv', + ValueFromPipelineByPropertyName + )] + [string] $Name, + + [Parameter()] + [ValidateRange(0, 100)] + [int] $PerPage, + + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context = (Get-GitHubContext) + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT + } + + process { + $body = @{ + per_page = $PerPage + } + + $inputObject = @{ + Method = 'GET' + APIEndpoint = "/repos/$Owner/$Repository/environments" + Body = $body + Context = $Context + } + + if ($PSCmdlet.ParameterSetName -eq 'NamedEnv') { + $inputObject.APIEndpoint = "/repos/$Owner/$Repository/environments/$Name" + $inputObject.Remove('Body') + } + + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } + } + + end { + Write-Debug "[$stackPath] - End" + } +} \ No newline at end of file diff --git a/src/functions/public/Environments/Remove-GitHubEnvironment.ps1 b/src/functions/public/Environments/Remove-GitHubEnvironment.ps1 new file mode 100644 index 000000000..1864e01ba --- /dev/null +++ b/src/functions/public/Environments/Remove-GitHubEnvironment.ps1 @@ -0,0 +1,72 @@ +filter Remove-GitHubEnvironment { + <# + .SYNOPSIS + Deletes an environment from a repository + + .DESCRIPTION + Deletes an environment from a repository + + .EXAMPLE + Remove-GitHubEnvironment -Owner 'PSModule' -Repository 'GitHub' -Name 'Production' + + Deletes the 'Production' environment from the 'PSModule/GitHub' repository. + + .NOTES + [Delete environments](https://docs.github.com/en/rest/deployments/environments?#delete-an-environment) + #> + [OutputType([pscustomobject])] + [CmdletBinding(SupportsShouldProcess)] + param( + # The name of the organization. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [Alias('Organization', 'User')] + [string] $Owner, + + # The name of the Repository. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Repository, + + # The name of the environment. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Name, + + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context = (Get-GitHubContext) + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT + } + + process { + $inputObject = @{ + Method = 'DELETE' + APIEndpoint = "/repos/$Owner/$Repository/environments/$Name" + Context = $Context + } + + if ($PSCmdlet.ShouldProcess("Environment [$Name]", 'DELETE')) { + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } + } + } + + end { + Write-Debug "[$stackPath] - End" + } +} \ No newline at end of file diff --git a/src/functions/public/Environments/Set-GitHubEnvironment.ps1 b/src/functions/public/Environments/Set-GitHubEnvironment.ps1 new file mode 100644 index 000000000..0f90b099f --- /dev/null +++ b/src/functions/public/Environments/Set-GitHubEnvironment.ps1 @@ -0,0 +1,96 @@ +filter Set-GitHubEnvironment { + <# + .SYNOPSIS + Create or update an environment for a repository + + .DESCRIPTION + Create or update an environment for a repository + + + #> + [OutputType([pscustomobject])] + [CmdletBinding(SupportsShouldProcess)] + param( + # The name of the organization. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [Alias('Organization', 'User')] + [string] $Owner, + + # The name of the Repository. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Repository, + + # The name of the environment. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Name, + + # The amount of time to delay a job after the job is initially triggered. The time (in minutes). + [Parameter(ValueFromPipelineByPropertyName)] + [Alias('wait_timer')] + [ValidateRange(0, 43200)] + [int] $WaitTimer, + + # Whether or not a user who created the job is prevented from approving their own job. + [Parameter(ValueFromPipelineByPropertyName)] + [Alias('prevent_self_review')] + [switch] $PreventSelfReview, + + # The people or teams that may review jobs that reference the environment. You can list up to six users or teams as reviewers. + [Parameter(ValueFromPipelineByPropertyName)] + [Alias('reviewers')] + [array] $Reviewers, + + # The type of deployment branch policy for this environment. To allow all branches to deploy, set to null. + [parameter(ValueFromPipelineByPropertyName)] + [Alias('deployment_branch_policy')] + [object] $DeploymentBranchPolicy, + + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context = (Get-GitHubContext) + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT + } + + process { + $body = @{ + wait_timer = $PSBoundParameters.ContainsKey('WaitTimer') ? $WaitTimer : $null + prevent_self_review = $PSBoundParameters.ContainsKey('PreventSelfReview') ? $PreventSelfReview : $null + reviewers = $PSBoundParameters.ContainsKey('Reviewers') ? $Reviewers : $null + deployment_branch_policy = $PSBoundParameters.ContainsKey('DeploymentBranchPolicy') ? $DeploymentBranchPolicy : $null + } | Remove-HashtableEntry -NullOrEmptyValues + + $inputObject = @{ + Method = 'PUT' + APIEndpoint = "/repos/$Owner/$Repository/environments/$Name" + Body = $body + Context = $Context + } | Remove-HashtableEntry -NullOrEmptyValues + + + if ($PSCmdlet.ShouldProcess("Environment [$Name]", 'Set')) { + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } + } + } + + end { + Write-Debug "[$stackPath] - End" + } +} \ No newline at end of file diff --git a/src/functions/public/Variables/Get-GitHubVariable.ps1 b/src/functions/public/Variables/Get-GitHubVariable.ps1 new file mode 100644 index 000000000..139f0d7d6 --- /dev/null +++ b/src/functions/public/Variables/Get-GitHubVariable.ps1 @@ -0,0 +1,105 @@ +function Get-GitHubVariable { + <# + .SYNOPSIS + Gets the GitHub variable details for a Organisation, Repository or Environment. + + .DESCRIPTION + Gets the GitHub variable details for a Organisation, Repository or Environment. + + .PARAMETER Owner + The account owner of the repository. The name is not case-sensitive. + + .PARAMETER Repository + The name of the repository. The name is not case-sensitive. + + .PARAMETER Environment + The name of the repository environment. + + .PARAMETER Name + The name of the variable. If left blank, all variable names are returned. + + .PARAMETER Context + The context to run the command in. Used to get the details for the API call. + + .EXAMPLE + Get-GitHubVariable -Owner "octocat" -Repository "Hello-World" -Environment "dev" + Get-GitHubVariable -Owner "octocat" -Repository "Hello-World" -Name "myVariable" + Get-GitHubVariable -Owner "octocat" -Name "myVariable" + + .NOTES + [Gets an Organisation Variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#get-an-organization-variable) + [Gets an Repository Variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#get-a-repository-variable) + [Gets an Environment Variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#get-an-environment-variable) + + .OUTPUTS + psobject[] + + #> + [OutputType([psobject[]])] + [CmdletBinding()] + param( + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [Parameter(ParameterSetName = 'Organization', Mandatory)] + [Parameter(ParameterSetName = 'Repository', Mandatory)] + [Alias('Organization', 'User')] + [string] $Owner, + + # The name of the repository. The name is not case sensitive. + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [Parameter(ParameterSetName = 'Repository', Mandatory)] + [string] $Repository, + + # The name of the repository environment. + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [string] $Environment, + + # The name of the variable. + [Parameter()] + [string] $Name, + + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context = (Get-GitHubContext) + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT + } + + process { + $inputObject = @{ + Method = "Get" + APIEndpoint = switch ($PSCmdlet.ParameterSetName) { + 'Environment' { + "/repos/$Owner/$Repository/environments/$Environment/variables" + break + } + 'Repository' { + "/repos/$Owner/$Repository/actions/variables" + break + } + 'Organization' { + "/orgs/$Owner/actions/variables" + break + } + } + Context = $Context + } + + if(-not [string]::IsNullOrWhiteSpace($Name)) { + $inputObject.APIEndpoint += "/$Name" + } + + Invoke-GitHubAPI @inputObject | Foreach-Object { + Write-Output $_.Response + } + } + + end { + Write-Debug "[$stackPath] - End" + } +} \ No newline at end of file diff --git a/src/functions/public/Variables/Remove-GitHubVariable.ps1 b/src/functions/public/Variables/Remove-GitHubVariable.ps1 new file mode 100644 index 000000000..31574107d --- /dev/null +++ b/src/functions/public/Variables/Remove-GitHubVariable.ps1 @@ -0,0 +1,105 @@ +function Remove-GitHubVariable { + <# + .SYNOPSIS + Removes a GitHub variable from an Organisation, Repository or Environment. + + .DESCRIPTION + Removes a GitHub variable from an Organisation, Repository or Environment. + + .PARAMETER Owner + The account owner of the repository. The name is not case-sensitive. + + .PARAMETER Repository + The name of the repository. The name is not case-sensitive. + + .PARAMETER Environment + The name of the repository environment. + + .PARAMETER Name + The name of the variable. + + .PARAMETER Context + The context to run the command in. Used to get the details for the API call. + + .EXAMPLE + Remove-GitHubVariable -Owner "octocat" -Repository "Hello-World" -Environment "dev" -Name "myVariable" + Remove-GitHubVariable -Owner "octocat" -Repository "Hello-World" -Name "myVariable" + Remove-GitHubVariable -Owner "octocat" -Name "myVariable" + + .NOTES + [Delete an Organisation Variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#delete-an-organization-variable) + [Delete a Repository Variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#delete-a-repository-variable) + [Delete an Environment Variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#delete-an-environment-variable) + + .OUTPUTS + psobject[] + #> + [OutputType([psobject[]])] + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(ParameterSetName = 'Organization', Mandatory)] + [Parameter(ParameterSetName = 'Repository', Mandatory)] + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [Alias('Organization', 'User')] + [string] $Owner, + + # The name of the repository. The name is not case sensitive. + [Parameter(ParameterSetName = 'Repository', Mandatory)] + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [string] $Repository, + + # The name of the repository environment. + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [string] $Environment, + + # The name of the variable. + + [Parameter(ParameterSetName = 'Organization', Mandatory)] + [Parameter(ParameterSetName = 'Repository', Mandatory)] + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [string] $Name, + + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context = (Get-GitHubContext) + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT + } + + process { + $inputObject = @{ + Method = "DELETE" + APIEndpoint = switch ($PSCmdlet.ParameterSetName) { + 'Environment' { + "/repos/$Owner/$Repository/environments/$Environment/variables/$Name" + break + } + 'Repository' { + "/repos/$Owner/$Repository/actions/variables/$Name" + break + } + 'Organization' { + "/orgs/$Owner/actions/variables/$Name" + break + } + } + Context = $Context + } + + if ($PSCmdlet.ShouldProcess("Variable [$Name]", 'DELETE')) { + Invoke-GitHubAPI @inputObject | ForeEach-Object { + Write-Output $_.Response + } + } + } + + end { + write-Debug "[$stackPath] - End" + } +} \ No newline at end of file diff --git a/src/functions/public/Variables/Set-GitHubVariable.ps1 b/src/functions/public/Variables/Set-GitHubVariable.ps1 new file mode 100644 index 000000000..4c1fb01cb --- /dev/null +++ b/src/functions/public/Variables/Set-GitHubVariable.ps1 @@ -0,0 +1,166 @@ +function Set-GitHubVariable { + <# + .SYNOPSIS + Creates/Updates a GitHub variable for a Organisation, Repository or Environment. + + .DESCRIPTION + Creates/Updates a GitHub variable for a Organisation, Repository or Environment. + + .PARAMETER Owner + The account owner of the repository. The name is not case-sensitive. + + .PARAMETER Repository + The name of the repository. The name is not case-sensitive. + + .PARAMETER Environment + The name of the repository environment. + + .PARAMETER Name + The name of the variable. + + .PARAMETER Value + The value of the variable. + + .PARAMETER Private + Set visibility to private (only applicable at the organization level). + + .PARAMETER SelectedRepositoryIds + List of numeric repository IDs where the variable should be visible (only applicable at the organization level). + + .PARAMETER Context + The context to run the command in. Used to get the details for the API call. + + .EXAMPLE + Sets a variable in an environment. + Set-GitHubVariable -Owner "octocat" -Repository "Hello-World" -Environment "dev" -Name "myVariable" -Value "myValue" + + .EXAMPLE + Sets a variable in a repository. + Set-GitHubVariable -Owner "octocat" -Repository "Hello-World" -Name "myVariable" -Value "myValue" + + .EXAMPLE + Sets a variable in an organisation. + Set-GitHubVariable -Owner "octocat" -Name "myVariable" -Value "myValue" + + .EXAMPLE + Sets a variable in an organisation with visibility set to private. + Set-GitHubVariable -Owner "octocat" -Name "myVariable" -Value "myValue" -Private + + .EXAMPLE + Sets a variable in an organisation with visibility set to selected repositories. + Set-GitHubVariable -Owner "octocat" -Name "myVariable" -Value "myValue" -SelectedRepositoryIds 123456, 654362 + + .NOTES + [Create an organization variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#create-an-organization-variable) + [Create a repository variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#create-a-repository-variable) + [Create an environment variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#create-an-environment-variable) + [Update an organization variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#update-an-organization-variable) + [Update a repository variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#update-a-repository-variable) + [Update an environment variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#update-an-environment-variable) + + .OUTPUTS + psobject[] + + #> + [OutputType([psobject[]])] + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [Parameter(ParameterSetName = 'Organization', Mandatory)] + [Parameter(ParameterSetName = 'Repository', Mandatory)] + [Alias('Organization', 'User')] + [string] $Owner, + + # The name of the repository. The name is not case sensitive. + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [Parameter(ParameterSetName = 'Repository', Mandatory)] + [string] $Repository, + + # The name of the repository environment. + [Parameter(ParameterSetName = 'Environment', Mandatory)] + [string] $Environment, + + # The name of the variable. + [Parameter(Mandatory)] + [string] $Name, + + [Parameter(Mandatory)] + [string] $Value, + + [Parameter(ParameterSetName = 'Organization')] + [switch] $Private, + + [Parameter(ParameterSetName = 'Organization')] + [int[]] $SelectedRepositoryIDs, + + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context = (Get-GitHubContext) + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT + } + + process { + $getParams = $PSBoundParameters + $getParams | Remove-HashtableEntry -NullOrEmptyValues -RemoveNames 'Value','Private','SelectedRepositoryIDs','Context' + $variableFound = Get-GitHubVariable @getParams -Name $Name + + $body = @{ + name = $Name + value = $Value + } + + if ($PSCmdlet.ParameterSetName -eq 'Organization') { + if ($PSBoundParameters.ContainsKey('SelectedRepositoryIDs')) { + $body['selected_repository_ids'] = @($SelectedRepositoryIDs) + $body['visibility'] = 'selected' + } + elseif($Private) { + $body['visibility'] = 'private' + } + else{ + $body['visibility'] = 'all' + } + } + + $inputObject = @{ + Method = "Post" + APIEndpoint = switch ($PSCmdlet.ParameterSetName) { + 'Environment' { + "/repos/$Owner/$Repository/environments/$Environment/variables" + break + } + 'Repository' { + "/repos/$Owner/$Repository/actions/variables" + break + } + 'Organization' { + "/orgs/$Owner/actions/variables" + break + } + } + Body = $body + Context = $Context + } + + if ($variableFound) { + $inputObject.Method = "PATCH" + $inputObject.APIEndpoint = $inputObject.APIEndpoint + "/$Name" + } + + if($PSCmdlet.ShouldProcess("Variable [$Name]", 'CREATE/UPDATE')) { + $result = Invoke-GitHubAPI @inputObject + Write-Output $result.Response + } + } + + end { + Write-Debug "[$stackPath] - End" + } +} \ No newline at end of file