diff --git a/github/create-release.ps1 b/github/create-release.ps1 new file mode 100644 index 0000000..bac5a01 --- /dev/null +++ b/github/create-release.ps1 @@ -0,0 +1,86 @@ +<# +.SYNOPSIS +Trigger runs on the workflow_dispatch event to create tool release + +.PARAMETER RepositoryFullName +Required parameter. The owner and repository name. For example, 'actions/versions-package-tools' +.PARAMETER AccessToken +Required parameter. PAT Token to authorize +.PARAMETER ToolVersion +Required parameter. Version of tool +.PARAMETER TagName +Required parameter. The name of the release tag +.PARAMETER ReleaseBody +Required parameter. Text describing the contents of the release +.PARAMETER EventType +Required parameter. The name of the repository dispatch event +#> + +param ( + [Parameter(Mandatory)] [string] $RepositoryFullName, + [Parameter(Mandatory)] [string] $AccessToken, + [Parameter(Mandatory)] [string] $ToolVersion, + [Parameter(Mandatory)] [string] $TagName, + [Parameter(Mandatory)] [string] $ReleaseBody, + [Parameter(Mandatory)] [string] $EventType, + [UInt32] $RetryIntervalSec = 10, + [UInt32] $RetryCount = 5 +) + +Import-Module (Join-Path $PSScriptRoot "github-api.psm1") + +function Create-Release { + param ( + [Parameter(Mandatory)] [object] $GitHubApi, + [Parameter(Mandatory)] [string] $ToolVersion, + [Parameter(Mandatory)] [string] $TagName, + [Parameter(Mandatory)] [string] $ReleaseBody, + [Parameter(Mandatory)] [string] $EventType + ) + + $eventPayload = @{ + ToolVersion = $ToolVersion + TagName = $TagName + ReleaseBody = $ReleaseBody + } + + Write-Host "Create '$EventType' repository dispatch event" + $GitHubApi.CreateRepositoryDispatch($EventType, $eventPayload) +} + +function Validate-ReleaseAvailability { + param ( + [Parameter(Mandatory)] [object] $GitHubApi, + [Parameter(Mandatory)] [string] $TagName, + [Parameter(Mandatory)] [UInt32] $RetryIntervalSec, + [Parameter(Mandatory)] [UInt32] $RetryCount + ) + + do { + $createdRelease = $GitHubApi.GetReleases() | Where-Object { $_.tag_name -eq $TagName } + if ($createdRelease) { + Write-Host "Release was successfully created: $($createdRelease.html_url)" + return + } + + $RetryCount-- + Start-Sleep -Seconds $RetryIntervalSec + } while($RetryCount -gt 0) + + Write-Host "Release was not created" + exit 1 +} + +$gitHubApi = Get-GitHubApi -RepositoryFullName $RepositoryFullName -AccessToken $AccessToken + +Create-Release -GitHubApi $gitHubApi ` + -ToolVersion $ToolVersion ` + -TagName $TagName ` + -ReleaseBody $ReleaseBody ` + -EventType $EventType + +Start-Sleep -s $RetryIntervalSec +Validate-ReleaseAvailability -GitHubApi $gitHubApi ` + -TagName $TagName ` + -RetryIntervalSec $RetryIntervalSec ` + -RetryCount $RetryCount diff --git a/github/github-api.psm1 b/github/github-api.psm1 index 8f88578..3103ed0 100644 --- a/github/github-api.psm1 +++ b/github/github-api.psm1 @@ -83,10 +83,11 @@ class GitHubApi return $releases } - [void] DispatchWorkflow([string]$EventType) { + [void] CreateRepositoryDispatch([string]$EventType, [object]$EventPayload) { $url = "dispatches" $body = @{ event_type = $EventType + client_payload = $EventPayload } | ConvertTo-Json $this.InvokeRestMethod($url, 'POST', $null, $body) @@ -104,12 +105,15 @@ class GitHubApi [void] CreateWorkflowDispatch([string]$WorkflowFileName, [string]$Ref, [object]$Inputs) { $url = "actions/workflows/${WorkflowFileName}/dispatches" - $body = @{ - ref = $Ref - inputs = $Inputs - } | ConvertTo-Json + $body = @{ ref = $Ref } + + if ($Inputs) { + $body.inputs = $Inputs + } - $this.InvokeRestMethod($url, 'POST', $null, $body) + $jsonBody = $body | ConvertTo-Json + + $this.InvokeRestMethod($url, 'POST', $null, $jsonBody) } [string] hidden BuildUrl([string]$Url, [string]$RequestParams) { diff --git a/github/run-ci-builds.ps1 b/github/run-ci-builds.ps1 index e4b8a03..e70f485 100644 --- a/github/run-ci-builds.ps1 +++ b/github/run-ci-builds.ps1 @@ -5,7 +5,7 @@ Trigger runs on the workflow_dispatch event to build and upload tool packages .PARAMETER RepositoryFullName Required parameter. The owner and repository name. For example, 'actions/versions-package-tools' .PARAMETER AccessToken -Required parameter. PAT Token to authorize +Required parameter. PAT to authorize .PARAMETER WorkflowFileName Required parameter. The name of workflow file that will be triggered .PARAMETER WorkflowDispatchRef