Skip to content

Commit

Permalink
Fix Remove-GitHubWorkflowRun
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Sep 18, 2023
1 parent fcdd186 commit 233267c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 346 deletions.
29 changes: 10 additions & 19 deletions src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,18 @@
)

begin {}

process {
# API Reference
# https://docs.github.com/en/rest/reference/actions#delete-a-workflow-run
$APICall = @{
Uri = "$APIBaseURI/repos/$Owner/$Repo/actions/runs/$ID"
Headers = @{
Authorization = "token $Token"
'Content-Type' = 'application/json'
}
Method = 'DELETE'
Body = @{} | ConvertTo-Json -Depth 100
}
try {
if ($PSBoundParameters.ContainsKey('Verbose')) {
$APICall
}
$Response = Invoke-RestMethod @APICall
} catch {
throw $_
$InputObject = @{
APIEndpoint = "repos/$Owner/$Repo/actions/runs/$ID"
Method = 'DELETE'
Token = $Token
}
return $Response

$Response = Invoke-GitHubAPI @InputObject

$Response
}

end {}
}
327 changes: 0 additions & 327 deletions tests/StopWorkflowsCustom.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,333 +4,6 @@ $Token = Read-Host -Prompt 'Enter GitHub Token'

$APIBaseURI = 'https://api.github.com'

#region Functions

function Invoke-GitHubAPI {
[CmdletBinding()]
param (
[Parameter()]
[ValidateSet('GET', 'POST', 'PATCH', 'DELETE', 'PUT')]
[String] $Method = 'GET',

[Parameter()]
[string] $APIEndpoint,

[Parameter()]
[hashtable] $Body = @{},

[Parameter()]
[string] $Owner = $Owner,

[Parameter()]
[string] $Repo = $Repo,

[Parameter()]
[string] $Token = $Token
)
$APICall = @{
Uri = ("$APIBaseURI/$APIEndpoint")#.Replace('\', '/').Replace('//', '/')
Headers = @{
Authorization = "token $Token"
'Content-Type' = 'application/vnd.github.v3+json' #'application/json'
}
Method = $Method
Body = $Body | ConvertTo-Json -Depth 100
}
# write text if verbose is enabled
if ($VerbosePreference -eq 'Continue') {
Write-Verbose -Message "API call: $($APICall | ConvertTo-Json -Depth 100)"
}
# if ($PSBoundParameters.ContainsKey('Verbose')) {
# $APICall
# }
try {
$Response = Invoke-RestMethod @APICall
} catch {
throw $_
}
return $Response
}

function Get-GitHubWorkflow {
[CmdletBinding(DefaultParameterSetName = 'ByName')]
param (
[Parameter()]
[string] $Owner = $Owner,

[Parameter()]
[string] $Repo = $Repo,

[Parameter()]
[string] $Token = $Token,

[Parameter(ParameterSetName = 'ByName')]
[string] $Name,

[Parameter(ParameterSetName = 'ByID')]
[string] $ID,

[Parameter()]
[int] $PageSize = 100
)

$processedPages = 0
$workflows = @()
do {
$processedPages++
$InputObject = @{
Owner = $Owner
Repo = $Repo
Token = $Token
Method = 'GET'
APIEndpoint = "repos/$Owner/$Repo/actions/workflows?per_page=$PageSize&page=$processedPages"
}
$Response = Invoke-GitHubAPI @InputObject
$workflows += $Response.workflows | Where-Object name -Match $name | Where-Object id -Match $id
} while ($workflows.count -ne $Response.total_count)

return $workflows
}

function Disable-GitHubWorkflow {
[CmdletBinding()]
param (
[Parameter()]
[string] $Owner = $Owner,

[Parameter()]
[string] $Repo = $Repo,

[Parameter()]
[string] $Token = $Token,

[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string[]] $ID
)

begin {}

process {
$InputObject = @{
Owner = $Owner
Repo = $Repo
Token = $Token
Method = 'PUT'
APIEndpoint = "repos/$Owner/$Repo/actions/workflows/$ID/disable"
}

$Response = Invoke-GitHubAPI @InputObject

}
end {
return $Response
}
}

function Enable-GitHubWorkflow {
[CmdletBinding()]
param (
[Parameter()]
[string] $Owner = $Owner,

[Parameter()]
[string] $Repo = $Repo,

[Parameter()]
[string] $Token = $Token,

[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string[]] $ID
)

begin {}

process {
$InputObject = @{
Owner = $Owner
Repo = $Repo
Token = $Token
Method = 'PUT'
APIEndpoint = "repos/$Owner/$Repo/actions/workflows/$ID/enable"
}

$Response = Invoke-GitHubAPI @InputObject

}
end {
return $Response
}
}

function Get-GitHubWorkflowRun {
[CmdletBinding()]
param (
$Owner = $script:Owner,
$Repo = $script:Repo,
$Token = $script:Token ,
$Name,
$ID
)

$processedPages = 0
$workflowRuns = @()
do {
$processedPages++
$APICall = @{
Uri = "$APIBaseURI/repos/$Owner/$Repo/actions/runs?per_page=100&page=$processedPages"
Headers = @{
Authorization = "token $Token"
'Content-Type' = 'application/json'
}
Method = 'GET'
Body = @{} | ConvertTo-Json -Depth 100
}
try {
if ($PSBoundParameters.ContainsKey('Verbose')) {
$APICall
}
$Response = Invoke-RestMethod @APICall
} catch {
throw $_
}
$workflowRuns += $Response.workflow_runs
} while ($WorkflowRuns.count -eq 100)
return $workflowRuns | Where-Object Name -Match $Name | Where-Object workflow_id -Match $ID
}

function Start-GitHubWorkflow {
[CmdletBinding()]
param (
$Owner = $script:Owner,
$Repo = $script:Repo,
$Token = $script:Token ,
[Alias('workflow_id')]
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string] $ID,

[Parameter(
ValueFromPipelineByPropertyName
)]
[Alias('branch', 'tag')]
[string] $Ref = 'main',

[Parameter()]
[hashtable] $Inputs = @{}
)
begin {}
process {
# API Reference
# https://docs.github.com/en/free-pro-team@latest/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event
$APICall = @{
Uri = "$APIBaseURI/repos/$Owner/$Repo/actions/workflows/$ID/dispatches"
Headers = @{
Authorization = "token $Token"
'Content-Type' = 'application/json'
}
Method = 'POST'
Body = @{
ref = $Ref
inputs = $Inputs
} | ConvertTo-Json -Depth 100
}
try {
if ($PSBoundParameters.ContainsKey('Verbose')) {
$APICall
}
$Response = Invoke-RestMethod @APICall
} catch {
throw $_
}
return $Response
}
end {}
}

function Stop-GitHubWorkflowRun {
[CmdletBinding()]
[alias('Cancel-GitHubWorkflowRun')]
param (
$Owner = $script:Owner,
$Repo = $script:Repo,
$Token = $script:Token ,
[Alias('workflow_id')]
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string] $ID
)
begin {}
process {
# API Reference
# https://docs.github.com/en/rest/reference/actions#cancel-a-workflow-run
$APICall = @{
Uri = "$APIBaseURI/repos/$Owner/$Repo/actions/runs/$ID/cancel"
Headers = @{
Authorization = "token $Token"
'Content-Type' = 'application/json'
}
Method = 'POST'
Body = @{} | ConvertTo-Json -Depth 100
}
try {
if ($PSBoundParameters.ContainsKey('Verbose')) {
$APICall
}
$Response = Invoke-RestMethod @APICall
} catch {
throw $_
}
return $Response
}
end {}
}

function Remove-GitHubWorkflowRun {
[CmdletBinding()]
param (
[Parameter()]
[string] $Owner = $Owner,

[Parameter()]
[string] $Repo = $Repo,

[Parameter()]
[string] $Token = $Token,

[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string] $ID
)

begin {}
process {
$InputObject = @{
Owner = $Owner
Repo = $Repo
Token = $Token
Method = 'DELETE'
APIEndpoint = "repos/$Owner/$Repo/actions/runs/$ID"
}

$Response = Invoke-GitHubAPI @InputObject
return $Response
}
end {}
}
#endregion


$Token = Read-Host -Prompt 'Enter GitHub Token'

Expand Down

0 comments on commit 233267c

Please sign in to comment.