forked from StartAutomating/PSDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-GitHubAction.ps1
231 lines (194 loc) · 7.31 KB
/
New-GitHubAction.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
function New-GitHubAction {
<#
.Synopsis
Creates a new GitHub action
.Example
New-GitHubAction -Job TestPowerShellOnLinux
.Link
New-GitHubWorkflow
.Link
Import-BuildStep
.Link
Convert-BuildStep
.Link
Expand-BuildStep
#>
[CmdletBinding(PositionalBinding=$false)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification = "Does not change state")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSPossibleIncorrectComparisonWithNull", "", Justification = "Explicitly checking for null (0 is ok)")]
[OutputType([string])]
param(
# The name of the action.
[Parameter(Mandatory,Position=0)]
[string]
$Name,
# A description of the action.
[Parameter(Mandatory,Position=0)]
[string]
$Description,
# The git hub action steps.
[Parameter(ValueFromPipelineByPropertyName)]
[Management.Automation.ArgumentCompleter({
# While we don't want to restrict the steps here, we _do_ want to be able to suggest steps that are built-in.
$psDevOpsModule = Get-Module PSDevOps
if ($psDevOpsModule) {
& $psDevOpsModule {
$script:ComponentMetaData.GitHubAction.Values |
Where-Object Type -eq Action |
Select-object -ExpandProperty Name
}
}
})]
[PSObject[]]$Action,
# The DockerImage used for a GitHub Action.
[Parameter(ValueFromPipelineByPropertyName)]
[string]$DockerImage,
# The NodeJS main script used for a GitHub Action.
[Parameter(ValueFromPipelineByPropertyName)]
[string]$NodeJSScript,
# The git hub action inputs.
[Parameter(ValueFromPipelineByPropertyName)]
[Collections.IDictionary]$ActionInput,
# The git hub action outputs.
[Parameter(ValueFromPipelineByPropertyName)]
[Collections.IDictionary]$ActionOutput,
# Optional changes to a component.
# A table of additional settings to apply wherever a part is used.
# For example -Option @{RunPester=@{env=@{"SYSTEM_ACCESSTOKEN"='$(System.AccessToken)'}}
[Collections.IDictionary]
$Option,
# The name of parameters that should be excluded.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('ExcludeParameters')]
[string[]]
$ExcludeParameter,
# The name of parameters that should be referred to uniquely.
# For instance, if converting function foo($bar) {} and -UniqueParameter is 'bar'
# The build parameter would be foo_bar.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('UniqueParameters')]
[string[]]
$UniqueParameter,
# A collection of default parameters.
[Parameter(ValueFromPipelineByPropertyName)]
[Collections.IDictionary]
$DefaultParameter = @{},
# If set, will output the created objects instead of creating YAML.
[switch]
$PassThru,
# A list of build scripts. Each build script will run as a step in the action.
[string[]]
$BuildScript,
# The icon used for branding. By default, a terminal icon.
[string]
$Icon = "terminal",
# The color used for branding. By default, blue.
[ValidateSet('white', 'yellow', 'blue', 'green', 'orange', 'red', 'purple', 'gray-dark')]
[string]
$Color = 'blue'
)
begin {
$expandBuildStepCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand('Expand-BuildStep','Function')
$workflowOptions = @{}
$mynoun = "GitHubAction"
$expandGitHubBuildStep = @{
BuildSystem = $mynoun
SingleItemName = 'On','Name'
DictionaryItemName = 'Jobs', 'Inputs','Outputs'
BuildOption = $workflowOptions
}
$DoNotExpandParameters = 'InputObject', 'BuildScript', 'DockerImage', 'NodeJSScript', 'Icon', 'Color', 'ActionInput', 'ActionOutput'
}
process {
$myParams = [Ordered]@{ } + $PSBoundParameters
$stepsByType = [Ordered]@{
Name = $Name
Description = $Description
}
$ThingNames = $script:ComponentNames.$mynoun
foreach ($kv in $myParams.GetEnumerator()) {
if ($ThingNames[$kv.Key]) {
$stepsByType[$kv.Key] = $kv.Value
} elseif ($DoNotExpandParameters -notcontains $kv.Key) {
$workflowOptions[$kv.Key] = $kv.Value
}
}
#region Map InputObject
if ($InputObject -is [Collections.IDictionary]) {
foreach ($key in $InputObject.Keys) {
$stepsByType[$key] = $InputObject.$key
}
}
elseif ($InputObject) {
foreach ($property in $InputObject.psobject.properties) {
$stepsByType[$property.name] = $InputObject.$key
}
}
$stepsByType["branding"] = [Ordered]@{
icon = $Icon
color = $Color
}
#endregion Map InputObject
#region Expand Input
$expandSplat = @{} + $PSBoundParameters
foreach ($k in @($expandSplat.Keys)) {
if (-not $expandBuildStepCmd.Parameters[$k]) {
$expandSplat.Remove($k)
}
}
if ($ActionInput) {
$stepsByType.inputs = $ActionInput
}
if ($ActionOutput) {
$stepsByType.outputs = $ActionOutput
}
#endregion Expand Input
$yamlToBe = Expand-BuildStep -StepMap $stepsByType @expandSplat @expandGitHubBuildStep -InputParameter @{'*'='*'}
if ($yamlToBe.actions.run) {
$actionSteps = $yamlToBe.actions
$yamlToBe.runs = [Ordered]@{
using = "composite"
steps = @($actionSteps)
}
$yamlToBe.Remove('actions')
}
elseif ($DockerImage) {
$yamlToBe.runs = [Ordered]@{
using = "docker"
image = $DockerImage
}
}
elseif ($NodeJSScript) {
$yamlToBe.runs = [Ordered]@{
using = "node12"
main = $NodeJSScript
}
}
$yamlToBe.Remove('On')
if ($BuildScript) {
if (-not $yamlToBe.steps) {
$yamlToBe.steps = [Ordered]@{}
}
$yamlToBe.steps = [Ordered]@{
steps = @(
Get-Item $BuildScript -ErrorAction SilentlyContinue |
Convert-BuildStep -BuildSystem GitHubWorkflow
)
}
}
if ($Environment) {
if (-not $yamlToBe.env) {
$yamlToBe.env = [Ordered]@{}
}
foreach ($kv in $Environment.GetEnumerator()) {
$yamlToBe.env[$kv.Key] = $kv.Value
}
}
if ($PassThru) {
$yamlToBe
} else {
@($yamlToBe | & $toYaml -Indent -2) -join '' -replace
"$([Environment]::NewLine * 2)", [Environment]::NewLine
}
}
}