forked from microsoft/PowerShellForGitHub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitHubLabels.psm1
404 lines (369 loc) · 12.9 KB
/
GitHubLabels.psm1
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<#
.SYNOPSIS PowerShell module for GitHub labels
#>
# Import module which defines $global:gitHubApiToken with GitHub API access token. Create this file it if it doesn't exist.
$apiTokensFilePath = "$PSScriptRoot\ApiTokens.psm1"
if (Test-Path $apiTokensFilePath)
{
Write-Host "Importing $apiTokensFilePath"
Import-Module -force $apiTokensFilePath
}
else
{
Write-Host "$apiTokensFilePath does not exist, skipping import"
Write-Host @'
This module should define $global:gitHubApiToken with your GitHub API access token in ApiTokens.psm1. Create this file if it doesn't exist.
You can simply rename ApiTokensTemplate.psm1 to ApiTokens.psm1 and update value of $global:gitHubApiToken.
You can get GitHub token from https://github.com/settings/tokens
If you don't provide it, you can still use this module, but you will be limited to 60 queries per hour.
'@
}
$script:gitHubToken = $global:gitHubApiToken
$script:gitHubApiUrl = "https://api.github.com"
$script:gitHubApiReposUrl = "https://api.github.com/repos"
<#
.SYNOPSIS Function to get single or all labels of given repository
.PARAM
repositoryName Name of the repository
.PARAM
ownerName Owner of the repository
.PARAM
labelName Name of the label to get. Function will return all labels for given repository if labelName is not specified.
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
Get-GitHubLabel -repositoryName DesiredStateConfiguration -ownerName Powershell -labelName TestLabel
Get-GitHubLabel -repositoryName DesiredStateConfiguration -ownerName Powershell
#>
function Get-GitHubLabel
{
param(
[Parameter(Mandatory=$true)]
[string]$repositoryName,
[Parameter(Mandatory=$true)]
[string]$ownerName,
[string]$labelName,
[string]$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
$index = 0
$headers = @{"Authorization"="token $gitHubAccessToken"}
if ($labelName -eq "")
{
$query = "$script:gitHubApiReposUrl/{0}/{1}/labels" -f $ownerName, $repositoryName
Write-Host "Getting all labels for repository $repositoryName"
do
{
try
{
$jsonResult = Invoke-WebRequest $query -Method Get -Headers $headers
$labels = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($label in $labels)
{
Write-Verbose "$index. $($label.name)"
$index++
$resultToReturn += $label
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
}
else
{
$query = "$script:gitHubApiReposUrl/{0}/{1}/labels/{2}" -f $ownerName, $repositoryName, $labelName
Write-Host "Getting label $labelName for repository $repositoryName"
try
{
$jsonResult = Invoke-WebRequest $query -Method Get -Headers $headers
$label = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
Write-Verbose "$index. $($label.name)"
$resultToReturn = $label
}
return $resultToReturn
}
<#
.SYNOPSIS Function to create label in given repository
.PARAM
repositoryName Name of the repository
.PARAM
ownerName Owner of the repository
.PARAM
labelName Name of the label to create
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
New-GitHubLabel -repositoryName DesiredStateConfiguration -ownerName PowerShell -labelName TestLabel -labelColor BBBBBB
#>
function New-GitHubLabel
{
param(
[Parameter(Mandatory=$true)]
[string]$repositoryName,
[Parameter(Mandatory=$true)]
[string]$ownerName,
[Parameter(Mandatory=$true)]
[string]$labelName,
[string]$labelColor = "EEEEEE",
[string]$gitHubAccessToken = $script:gitHubToken
)
$headers = @{"Authorization"="token $gitHubAccessToken"}
$hashTable = @{"name"=$labelName; "color"=$labelColor}
$data = $hashTable | ConvertTo-Json
$url = "$script:gitHubApiReposUrl/{0}/{1}/labels" -f $ownerName, $repositoryName
Write-Host "Creating Label:" $labelName
$result = Invoke-WebRequest $url -Method Post -Body $data -Headers $headers
if ($result.StatusCode -eq 201)
{
Write-Host $labelName "was created"
}
else
{
Write-Error $labelName "was not created. Result: $result"
}
}
<#
.SYNOPSIS Function to remove label from given repository
.PARAM
repositoryName Name of the repository
.PARAM
ownerName Owner of the repository
.PARAM
labelName Name of the label to delete
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
Remove-GitHubLabel -repositoryName desiredstateconfiguration -ownerName powershell -labelName TestLabel
#>
function Remove-GitHubLabel
{
param(
[Parameter(Mandatory=$true)]
[string]$repositoryName,
[Parameter(Mandatory=$true)]
[string]$ownerName,
[Parameter(Mandatory=$true)]
[string]$labelName,
[string]$gitHubAccessToken = $script:gitHubToken
)
$headers = @{"Authorization"="token $gitHubAccessToken"}
$url = "$script:gitHubApiReposUrl/{0}/{1}/labels/{2}" -f $ownerName, $repositoryName, $labelName
Write-Host "Deleting Label:" $labelName
$result = Invoke-WebRequest $url -Method Delete -Headers $headers
if ($result.StatusCode -eq 204)
{
Write-Host $labelName "was deleted"
}
else
{
Write-Error $labelName "was not deleted. Result: $result"
}
}
<#
.SYNOPSIS Function to update label in given repository
.PARAM
repositoryName Name of the repository
.PARAM
ownerName Owner of the repository
.PARAM
labelName Name of the label to update
.PARAM
newLabelName New name of the label
.PARAM
labelColor New color of the label
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
Update-GitHubLabel -repositoryName DesiredStateConfiguration -ownerName Powershell -labelName TestLabel -newLabelName NewTestLabel -labelColor BBBB00
#>
function Update-GitHubLabel
{
param(
[Parameter(Mandatory=$true)]
[string]$repositoryName,
[Parameter(Mandatory=$true)]
[string]$ownerName,
[Parameter(Mandatory=$true)]
[string]$labelName,
[Parameter(Mandatory=$true)]
[string]$newLabelName,
[string]$labelColor = "EEEEEE",
[string]$gitHubAccessToken = $script:gitHubToken
)
$headers = @{"Authorization"="token $gitHubAccessToken"}
$hashTable = @{"name"=$newLabelName; "color"=$labelColor}
$data = $hashTable | ConvertTo-Json
$url = "$script:gitHubApiReposUrl/{0}/{1}/labels/{2}" -f $ownerName, $repositoryName, $labelName
Write-Host "Updating label '$labelName' to name '$newLabelName' and color '$labelColor'"
$result = Invoke-WebRequest $url -Method Patch -Body $data -Headers $headers
if ($result.StatusCode -eq 200)
{
Write-Host $labelName "was updated"
}
else
{
Write-Error $labelName "was not updated. Result: $result"
}
}
<#
.SYNOPSIS Function to create labels for given repository.
It get all labels from repo, remove the ones which aren't on our approved label list, update the ones which already exist to desired color and add the ones which weren't there before.
.PARAM
repositoryName Name of the repository
.PARAM
ownerName Owner of the repository
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
New-GitHubLabels -repositoryName DesiredStateConfiguration -ownerName Powershell
#>
function New-GitHubLabels
{
param(
[Parameter(Mandatory=$true)]
[string]$repositoryName,
[Parameter(Mandatory=$true)]
[string]$ownerName,
[string]$gitHubAccessToken = $script:gitHubToken
)
$labelJson = @"
[
{
"name": "pri:lowest",
"color": "4285F4"
},
{
"name": "pri:low",
"color": "4285F4"
},
{
"name": "pri:medium",
"color": "4285F4"
},
{
"name": "pri:high",
"color": "4285F4"
},
{
"name": "pri:highest",
"color": "4285F4"
},
{
"name": "bug",
"color": "fc2929"
},
{
"name": "duplicate",
"color": "cccccc"
},
{
"name": "enhancement",
"color": "121459"
},
{
"name": "up for grabs",
"color": "159818"
},
{
"name": "question",
"color": "cc317c"
},
{
"name": "discussion",
"color": "fe9a3d"
},
{
"name": "wontfix",
"color": "dcb39c"
},
{
"name": "in progress",
"color": "f0d218"
},
{
"name": "ready",
"color": "145912"
}
]
"@
$labelList = $labelJson | ConvertFrom-Json
$labelListNames = $labelList.name
$existingLabels = Get-GitHubLabel -repositoryName $repositoryName -ownerName $ownerName -gitHubAccessToken $gitHubAccessToken
$existingLabelsNames = $existingLabels.name
foreach ($label in $labelList)
{
if ($label.name -notin $existingLabelsNames)
{
# Create label if it doesn't exist
New-GitHubLabel -repositoryName $repositoryName -ownerName $ownerName -labelName $label.name -labelColor $label.color -gitHubAccessToken $gitHubAccessToken
}
else
{
# Update label's color if it already exists
Update-GitHubLabel -repositoryName $repositoryName -ownerName $ownerName -labelName $label.name -newLabelName $label.name -labelColor $label.color -gitHubAccessToken $gitHubAccessToken
}
}
foreach ($label in $existingLabelsNames)
{
if($label -notin $labelListNames)
{
# Remove label if it exists but is not on desired label list
Remove-GitHubLabel -repositoryName $repositoryName -ownerName $ownerName -labelName $label -gitHubAccessToken $gitHubAccessToken
}
}
}
<#
.SYNOPSIS Function to get next page with results from query to GitHub API
.PARAM
jsonResult Result from the query to GitHub API
#>
function Get-NextResultPage
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$jsonResult
)
if($jsonResult.Headers.Link -eq $null)
{
return $null
}
$nextLinkString = $jsonResult.Headers.Link.Split(',')[0]
# Get url query for the next page
$query = $nextLinkString.Split(';')[0].replace('<','').replace('>','')
if ($query -notmatch 'page=1')
{
return $query
}
else
{
return $null
}
}