-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathParameters.psd1
More file actions
223 lines (197 loc) · 6.62 KB
/
Parameters.psd1
File metadata and controls
223 lines (197 loc) · 6.62 KB
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
@{
Parameters = @(
@{
Name = 'repository'
Type = 'string'
IfNullOrEmpty = {
param($ErrorTarget)
$ErrorDetails = @{
Name = 'Repository'
Type = 'Missing'
Message = @(
'Could not determine repository;'
'was it passed as an input to the action?'
) -join ' '
Target = $ErrorTarget
}
$Record = New-InvalidParameterError @ErrorDetails
throw $Record
}
Process = {
param($Parameters, $Value, $ErrorTarget)
$Parameters.Owner, $Parameters.Repo = $Value -split '/'
Write-HostParameter -Name Owner -Value $Parameters.Owner
Write-HostParameter -Name Repo -Value $Parameters.Repo
return $Parameters
}
}
@{
Name = 'authorized_accounts'
Type = 'String[]'
Process = {
param($Parameters, $Value, $ErrorTarget)
[string[]]$SpecifiedAccounts = $Value -split ',' | Where-Object {
-not [string]::IsNullOrEmpty($_)
}
if ($SpecifiedAccounts.Count -gt 0) {
$Parameters.AuthorizedAccounts = $SpecifiedAccounts
Write-HostParameter -Name AuthorizedAccounts -Value $Parameters.AuthorizedAccounts
}
return $Parameters
}
}
@{
Name = 'permissions'
Type = 'String[]'
IfNullOrEmpty = {
param($ErrorTarget)
$ErrorDetails = @{
Name = 'Permissions'
Type = 'Missing'
Message = @(
'Could not determine valid permission list;'
'was it passed as an input to the action?'
) -join ' '
Target = $ErrorTarget
}
$Record = New-InvalidParameterError @ErrorDetails
throw $Record
}
Process = {
param($Parameters, $Value, $ErrorTarget)
$SpecifiedPermissions = $Value -split ','
$InvalidPermissions = @()
$ValidPermissions = @(
'Admin'
'Maintain'
'Pull'
'Push'
'Triage'
)
$Parameters.ValidPermissions = $SpecifiedPermissions | ForEach-Object {
if ($_ -notin $ValidPermissions) {
$InvalidPermissions += $_
} else {
$_
}
}
if ($InvalidPermissions.Count -gt 0) {
$Message = New-Object -TypeName System.Text.StringBuilder
$null = $Message.Append('Specified invalid permissions;')
$null = $Message.AppendLine('Permissions must be any of:')
foreach ($Permission in $ValidPermissions) {
$null = $Message.AppendLine("`t$Permission")
}
$null = $Message.AppendLine(
'But the following invalid permissions were specified:'
)
foreach ($Permission in $InvalidPermissions) {
$null = $Message.AppendLine("`t$Permission")
}
$ErrorDetails = @{
Name = 'Permissions'
Type = 'Invalid'
Message = $Message.ToString()
Target = $ErrorTarget
}
$Record = New-InvalidParameterError @ErrorDetails
throw $Record
} else {
Write-HostParameter -Name Permissions -Value $Parameters.ValidPermissions
return $Parameters
}
}
}
@{
Name = 'target'
Type = 'String[]'
IfNullOrEmpty = {
param($ErrorTarget)
$ErrorDetails = @{
Name = 'Target'
Type = 'Missing'
Message = @(
'Could not determine target to check for authorization;'
'was it passed as an input to the action?'
) -join ' '
Target = $ErrorTarget
}
$Record = New-InvalidParameterError @ErrorDetails
throw $Record
}
Process = {
param($Parameters, $Value, $ErrorTarget)
$Type, $Targets = $Value -split ':' | ForEach-Object -Process { $_.Trim() }
# Munge all invalid specifications here to Branch, since we can't apply validation on the
# action input directly at this time.
if ($Type -ne 'Path') {
$Type = 'Branch'
}
if ([string]::IsNullOrEmpty($Targets)) {
if ($Type -eq 'Branch') {
# Even though this is the default, we need to handle if someone manages to get a null
# value here in a way we didn't predict.
$Targets = 'live'
} else {
# In this case, the user passed `path:` without specifying any paths.
$ErrorDetails = @{
Name = 'Target'
Type = 'Missing'
Message = @(
'Determined that the target to check for authorization is a path,'
'but no paths were specified after the colon.'
'You must specify one or more paths, separated by a comma or a newline.'
) -join ' '
Target = $ErrorTarget
}
$Record = New-InvalidParameterError @ErrorDetails
throw $Record
}
} else {
# Split on commas and newlines, trim whitespace and wrapping quotes from all outputs, then
# discard any empty strings to make sure that only valid targets get passed.
$Targets = $Targets -split "[,`n]"
| ForEach-Object -Process { $_.Trim().Trim('"').Trim("'") }
| Where-Object -FilterScript { -not [string]::IsNullOrEmpty($_) }
}
switch ($Type) {
'Path' {
$Parameters.TargetPath = $Targets
Write-HostParameter -Name TargetPath -Value $Parameters.TargetPath
}
default {
$Parameters.TargetBranch = $Targets
Write-HostParameter -Name TargetBranch -Value $Parameters.TargetBranch
}
}
return $Parameters
}
}
@{
Name = 'user'
Type = 'String'
IfNullOrEmpty = {
param($ErrorTarget)
$ErrorDetails = @{
Name = 'User'
Type = 'Missing'
Message = @(
'Could not determine user to check for authorization;'
'was it passed as an input to the action?'
'If the action was not triggered on pull request,'
'it needs to pass the user explicitly.'
) -join ' '
Target = $ErrorTarget
}
$Record = New-InvalidParameterError @ErrorDetails
throw $Record
}
Process = {
param($Parameters, $Value, $ErrorTarget)
$Parameters.User = $Value
Write-HostParameter -Name User -Value $Parameters.User
return $Parameters
}
}
)
}