-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaction.ps1
188 lines (155 loc) · 6.57 KB
/
action.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env pwsh
$ErrorActionPreference = "STOP"
. $PSScriptRoot/lib/ActionsCore.ps1
## Pull in some inputs
$gist_token = Get-ActionInput gist_token -Required
$repo_full_name = Get-ActionInput repo_full_name
$workflow_name = Get-ActionInput workflow_name
$version_key = Get-ActionInput version_key
$skip_bump = 'TRUE' -ieq (Get-ActionInput skip_bump)
$set_env = 'TRUE' -ieq (Get-ActionInput set_env)
if (-not $repo_name) {
$repo_full_name = $env:GITHUB_REPOSITORY
}
if (-not $workflow_name) {
$workflow_name = $env:GITHUB_WORKFLOW
}
($repo_owner, $repo_name) = $repo_full_name -split '/'
Write-ActionInfo "Resolved Repository..........: [$repo_full_name]"
Write-ActionInfo "Resolved Repository Owner....: [$repo_owner]"
Write-ActionInfo "Resolved Repository Name.....: [$repo_name]"
Write-ActionInfo "Resolved Workflow............: [$workflow_name]"
$stateGistName = "GITHUB_BUILDNUM_METADATA:$repo_name"
Write-ActionInfo "Resolved State Gist Name.....: [$stateGistName]"
$gistsApiUrl = "https://api.github.com/gists"
$apiHeaders = @{
Accept = "application/vnd.github.v2+json"
Authorization = "token $gist_token"
}
$stateGistBanner = @"
/* THIS FILE IS AUTO-GENERATED AND MANAGED BY GITHUB ACTIONS. MANUAL MODIFICATIONS
** CAN BREAK THINGS IF YOU DO NOT KNOW WHAT YOU ARE DOING! *YOU* HAVE BEEN WARNED!
*/
"@
## We represent each scope as its own class to form a tree structure of nodes
## and this also gives us the ability to add more metadata fields in the future
class VersionBuildNum {
[int]$build_num = 0
}
class WorkflowBuildNum {
[int]$build_num = 0
[System.Collections.Generic.Dictionary[
string, VersionBuildNum]]$version_buildnums =
[System.Collections.Generic.Dictionary[string, VersionBuildNum]]::new()
}
class GlobalBuildNum {
[int]$build_num = 0
[System.Collections.Generic.Dictionary[
string, WorkflowBuildNum]]$workflow_buildnums =
[System.Collections.Generic.Dictionary[string, WorkflowBuildNum]]::new()
}
## Request all Gists for the current user
$listGistsResp = Invoke-WebRequest -Headers $apiHeaders -Uri $gistsApiUrl
## Parse response content as JSON
$listGists = $listGistsResp.Content | ConvertFrom-Json -AsHashtable
Write-ActionInfo "Got [$($listGists.Count)] Gists for current account"
## Isolate the first Gist with a file matching the expected metadata name
$stateGist = $listGists | Where-Object { $_.files.$stateGistName } | Select-Object -First 1
$stateData = $null
if ($stateGist) {
Write-ActionInfo "Found the build number state!"
$stateDataRawUrl = $stateGist.files.$stateGistName.raw_url
Write-ActionInfo "Fetching state content from Raw Url"
$stateDataRawResp = Invoke-WebRequest -Headers $apiHeaders -Uri $stateDataRawUrl
$stateDataContent = $stateDataRawResp.Content
$stateData = $stateDataContent | ConvertFrom-Json -AsHashtable -ErrorAction Continue
if (-not $stateData) {
Write-ActionWarning "State content seems to be either missing or unparsable JSON:"
Write-ActionWarning "[$($stateGist.files.$stateGistName)]"
Write-ActionWarning "[$stateDataContent]"
Write-ActionWarning "RESETTING STATE DATA"
}
else {
Write-Information "Got state data"
}
}
## Create initial global state data if it doesn't exist
if (-not $stateData) {
Write-ActionWarning "BuildNum state for Repo not found, INITIALIZING"
$stateData = [GlobalBuildNum]::new()
}
## Create initial state data for current workflow if it doesn't exist
if (-not $stateData.workflow_buildnums.ContainsKey($workflow_name)) {
Write-ActionDebug "BuildNum state for Workflow not found, initializing"
$stateData.workflow_buildnums[$workflow_name] = [WorkflowBuildNum]::new()
}
## Create initial state data for specified version key if it doesn't exist
if ($version_key -and (-not $stateData.workflow_buildnums[
$workflow_name].version_buildnums.ContainsKey($version_key))) {
Write-ActionDebug "BuildNum state for Version not found, initializing"
$stateData.workflow_buildnums[$workflow_name].version_buildnums[$version_key] =
[VersionBuildNum]::new()
}
if (-not $stateGist) {
Write-ActionInfo "Creating initial state Gist"
$createGistResp = Invoke-WebRequest -Headers $apiHeaders -Uri $gistsApiUrl -Method Post -Body (@{
public = $false
files = @{
$stateGistName = @{
content = @"
$stateGistBanner
$($stateData | ConvertTo-Json -Depth 10)
"@
}
}
} | ConvertTo-Json)
$createGist = $createGistResp.Content | ConvertFrom-Json -AsHashtable
$stateGist = $createGist
}
Write-ActionDebug "Resolved starting state data:"
Write-ActionDebug ($stateData | ConvertTo-Json -Depth 10)
if (-not $skip_bump) {
Write-ActionInfo "Bumping up version numbers"
$stateData.build_num += 1;
Write-Debug "New Global build_num = [$($stateData.build_num)]"
$stateData.workflow_buildnums[$workflow_name].build_num += 1;
Write-Debug "New Workflow build_num = [$($stateData.workflow_buildnums[$workflow_name].build_num)]"
if ($version_key) {
$stateData.workflow_buildnums[$workflow_name].version_buildnums[
$version_key].build_num += 1
Write-Debug "New Workflow build_num = [$($stateData.workflow_buildnums[$workflow_name].version_buildnums[
$version_key].build_num)]"
}
Write-ActionDebug "Resolved updated state data:"
Write-ActionDebug ($stateData | ConvertTo-Json -Depth 10)
Write-ActionInfo "Updating state Gist"
$updateGistUrl = "$gistsApiUrl/$($stateGist.id)"
$updateGistResp = Invoke-WebRequest -Headers $apiHeaders -Uri $updateGistUrl -Method Patch -Body (@{
files = @{
$stateGistName = @{
content = @"
$stateGistBanner
$($stateData | ConvertTo-Json -Depth 10)
"@
}
}
} | ConvertTo-Json)
}
Write-ActionDebug "Setting outputs"
Set-ActionOutput global_build_number ($stateData.build_num)
Set-ActionOutput workflow_build_number ($stateData.workflow_buildnums[
$workflow_name].build_num)
if ($version_key) {
Set-ActionOutput version_build_number ($stateData.workflow_buildnums[
$workflow_name].version_buildnums[$version_key].build_num)
}
if ($set_env) {
Write-ActionDebug "Setting env vars"
Set-ActionVariable BUILDNUM_FOR_GLOBAL ($stateData.build_num)
Set-ActionVariable BUILDNUM_FOR_WORKFLOW ($stateData.workflow_buildnums[
$workflow_name].build_num)
if ($version_key) {
Set-ActionVariable BUILDNUM_FOR_VERSION ($stateData.workflow_buildnums[
$workflow_name].version_buildnums[$version_key].build_num)
}
}