From af36d345eff8ac136691ad1fb0acd2eeb6097c1a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Nov 2023 20:59:26 +0100 Subject: [PATCH] Add the function to cover the Tags subcategory --- .../Get-GitHubRepositoryTagProtection.ps1 | 41 ++++++++++++++++ .../New-GitHubRepositoryTagProtection.ps1 | 49 +++++++++++++++++++ .../Remove-GitHubRepositoryTagProtection.ps1 | 46 +++++++++++++++++ tools/utilities/GitHubAPI.ps1 | 12 ++--- 4 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 src/GitHub/public/Repositories/Tags/Get-GitHubRepositoryTagProtection.ps1 create mode 100644 src/GitHub/public/Repositories/Tags/New-GitHubRepositoryTagProtection.ps1 create mode 100644 src/GitHub/public/Repositories/Tags/Remove-GitHubRepositoryTagProtection.ps1 diff --git a/src/GitHub/public/Repositories/Tags/Get-GitHubRepositoryTagProtection.ps1 b/src/GitHub/public/Repositories/Tags/Get-GitHubRepositoryTagProtection.ps1 new file mode 100644 index 000000000..3e040ffc9 --- /dev/null +++ b/src/GitHub/public/Repositories/Tags/Get-GitHubRepositoryTagProtection.ps1 @@ -0,0 +1,41 @@ +filter Get-GitHubRepositoryTagProtection { + <# + .SYNOPSIS + List tag protection states for a repository + + .DESCRIPTION + This returns the tag protection states of a repository. + + This information is only available to repository administrators. + + .EXAMPLE + Get-GitHubRepositoryTagProtection -Owner 'octocat' -Repo 'hello-world' + + Gets the tag protection states of the 'hello-world' repository. + + .NOTES + https://docs.github.com/rest/repos/tags#list-tag-protection-states-for-a-repository + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The account owner of the repository. The name is not case sensitive. + [Parameter()] + [Alias('org')] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + # The name of the repository without the .git extension. The name is not case sensitive. + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo) + ) + + $inputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/tags/protection" + Method = 'GET' + } + + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } +} diff --git a/src/GitHub/public/Repositories/Tags/New-GitHubRepositoryTagProtection.ps1 b/src/GitHub/public/Repositories/Tags/New-GitHubRepositoryTagProtection.ps1 new file mode 100644 index 000000000..af21cc26c --- /dev/null +++ b/src/GitHub/public/Repositories/Tags/New-GitHubRepositoryTagProtection.ps1 @@ -0,0 +1,49 @@ +filter New-GitHubRepositoryTagProtection { + <# + .SYNOPSIS + Create a tag protection state for a repository + + .DESCRIPTION + This creates a tag protection state for a repository. + This endpoint is only available to repository administrators. + + .EXAMPLE + New-GitHubRepositoryTagProtection -Owner 'octocat' -Repo 'hello-world' -Pattern 'v1.*' + + Creates a tag protection state for the 'hello-world' repository with the pattern 'v1.*'. + + .NOTES + https://docs.github.com/rest/repos/tags#create-a-tag-protection-state-for-a-repository + + #> + [OutputType([pscustomobject])] + [CmdletBinding(SupportsShouldProcess)] + param ( + # The account owner of the repository. The name is not case sensitive. + [Parameter()] + [Alias('org')] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + # The name of the repository without the .git extension. The name is not case sensitive. + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + # An optional glob pattern to match against when enforcing tag protection. + [Parameter(Mandatory)] + [string] $Pattern + ) + + $body['pattern'] = $Pattern + + $inputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/tags/protection" + Method = 'POST' + Body = $body + } + + if ($PSCmdlet.ShouldProcess("tag protection state on pattern [$Pattern] for repository [$Owner/$Repo]", 'Create')) { + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } + } +} diff --git a/src/GitHub/public/Repositories/Tags/Remove-GitHubRepositoryTagProtection.ps1 b/src/GitHub/public/Repositories/Tags/Remove-GitHubRepositoryTagProtection.ps1 new file mode 100644 index 000000000..08b225d88 --- /dev/null +++ b/src/GitHub/public/Repositories/Tags/Remove-GitHubRepositoryTagProtection.ps1 @@ -0,0 +1,46 @@ +filter Remove-GitHubRepositoryTagProtection { + <# + .SYNOPSIS + Delete a tag protection state for a repository + + .DESCRIPTION + This deletes a tag protection state for a repository. + This endpoint is only available to repository administrators. + + .EXAMPLE + Remove-GitHubRepositoryTagProtection -Owner 'octocat' -Repo 'hello-world' -TagProtectionId 1 + + Deletes the tag protection state with the id 1 for the 'hello-world' repository. + + .NOTES + https://docs.github.com/rest/repos/tags#delete-a-tag-protection-state-for-a-repository + + #> + [OutputType([pscustomobject])] + [CmdletBinding(SupportsShouldProcess)] + param ( + # The account owner of the repository. The name is not case sensitive. + [Parameter()] + [Alias('org')] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + # The name of the repository without the .git extension. The name is not case sensitive. + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + # The unique identifier of the tag protection. + [Parameter(Mandatory)] + [int] $TagProtectionId + ) + + $inputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/tags/protection/$TagProtectionId" + Method = 'DELETE' + } + + if ($PSCmdlet.ShouldProcess("tag protection state with ID [$TagProtectionId] for repository [$Owner/$Repo]", 'Delete')) { + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } + } +} diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 559c09427..23934060c 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,8 +21,8 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/repos/{owner}/{repo}/properties/values' -$method = 'get' +$path = '/repos/{owner}/{repo}/tags/protection/{tag_protection_id}' +$method = 'delete' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername $response.paths.$path.$method.operationId | clip # -> FunctionName @@ -42,11 +42,11 @@ $response.paths.$path.$method.responses.'200'.content.'application/json'.schema. $response.components.schemas.PSobject.Properties | ForEach-Object { [pscustomobject]@{ - Name = $_.Name - Title = $_.Value.title - Type = $_.Value.type + Name ame + Titlee.title + Typelype Properties = $_.Value.properties - Required = $_.Value.required + Requiredlue.required } }