From 7e2bc9a237e028a58d029b46ca7164be4af66fa1 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 27 Aug 2020 15:26:35 +0300 Subject: [PATCH 1/5] Add create-release.ps1 script --- github/create-release.ps1 | 87 +++++++++++++++++++++++++++++++++++++++ github/github-api.psm1 | 3 +- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 github/create-release.ps1 diff --git a/github/create-release.ps1 b/github/create-release.ps1 new file mode 100644 index 0000000..2dcc18d --- /dev/null +++ b/github/create-release.ps1 @@ -0,0 +1,87 @@ +<# +.SYNOPSIS +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 +.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 + + $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..d939104 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) From 2ea972432a12477aff20e15f5f77a95daac0b7cb Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 27 Aug 2020 18:53:42 +0300 Subject: [PATCH 2/5] Update CreateWorkflowDispatch method --- github/create-release.ps1 | 1 - github/github-api.psm1 | 15 +++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/github/create-release.ps1 b/github/create-release.ps1 index 2dcc18d..b7cd2ab 100644 --- a/github/create-release.ps1 +++ b/github/create-release.ps1 @@ -62,7 +62,6 @@ function Validate-ReleaseAvailability { Write-Host "Release was successfully created: $($createdRelease.html_url)" return } - $RetryCount $RetryCount-- Start-Sleep -Seconds $RetryIntervalSec diff --git a/github/github-api.psm1 b/github/github-api.psm1 index d939104..496d211 100644 --- a/github/github-api.psm1 +++ b/github/github-api.psm1 @@ -103,14 +103,17 @@ class GitHubApi return $this.InvokeRestMethod($url, 'GET', $null, $null) } - [void] CreateWorkflowDispatch([string]$WorkflowFileName, [string]$Ref, [object]$Inputs) { + [void] CreateWorkflowDispatch([string]$WorkflowFileName, [string]$Ref, [string]$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) { From 7aa3827a98f3b2e1e78d29091120a49ebe5fec7c Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Fri, 28 Aug 2020 10:16:06 +0300 Subject: [PATCH 3/5] Fix inputs type --- github/github-api.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/github-api.psm1 b/github/github-api.psm1 index 496d211..3103ed0 100644 --- a/github/github-api.psm1 +++ b/github/github-api.psm1 @@ -103,7 +103,7 @@ class GitHubApi return $this.InvokeRestMethod($url, 'GET', $null, $null) } - [void] CreateWorkflowDispatch([string]$WorkflowFileName, [string]$Ref, [string]$Inputs) { + [void] CreateWorkflowDispatch([string]$WorkflowFileName, [string]$Ref, [object]$Inputs) { $url = "actions/workflows/${WorkflowFileName}/dispatches" $body = @{ ref = $Ref } From 8e8ae73c1d5ca1593f95bca7ad14a7504ad9eaae Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Fri, 28 Aug 2020 11:43:35 +0300 Subject: [PATCH 4/5] Fix SYNOPSIS --- github/create-release.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/create-release.ps1 b/github/create-release.ps1 index b7cd2ab..bac5a01 100644 --- a/github/create-release.ps1 +++ b/github/create-release.ps1 @@ -1,6 +1,6 @@ <# .SYNOPSIS -Trigger runs on the workflow_dispatch event to build and upload tool packages +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' From 8463c1f4c0f2583b28319a4952ca55171844e1b3 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Fri, 28 Aug 2020 12:51:16 +0300 Subject: [PATCH 5/5] Remove redundant string --- github/run-ci-builds.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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