Skip to content

Instantly share code, notes, and snippets.

@ianphil
Created September 4, 2018 15:10
Show Gist options
  • Save ianphil/0cc5b7cccc7b2804a5e20a0c6047f0b6 to your computer and use it in GitHub Desktop.
Save ianphil/0cc5b7cccc7b2804a5e20a0c6047f0b6 to your computer and use it in GitHub Desktop.
Used to remove and create labels for a repo using the Github API
# Delete label flag will only delete labels and should be run first.
# .\GitHubScrum.ps1 -OwnerName iphilpot -RepositoryName flare -AuthToken xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -DeleteLabels
# After Delete labels, run without flag to create labels.
# .\GitHubScrum.ps1 -OwnerName iphilpot -RepositoryName flare -AuthToken xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
param([string]$OwnerName = (Read-Host "What is the owner name?"),
[string]$RepositoryName = (Read-Host "What is the repository name?"),
[string]$AuthToken = (Read-Host "What is the auth token?"),
[switch]$DeleteLabels)
$headers = @{"Authorization"="token $AuthToken"}
$labelJson = @"
[
{
"name": "priority:lowest",
"color": "207de5"
},
{
"name": "priority:low",
"color": "207de5"
},
{
"name": "priority:medium",
"color": "207de5"
},
{
"name": "priority:high",
"color": "207de5"
},
{
"name": "priority:highest",
"color": "207de5"
},
{
"name": "point:1",
"color": "009800"
},
{
"name": "point:2",
"color": "009800"
},
{
"name": "point:3",
"color": "009800"
},
{
"name": "point:5",
"color": "009800"
},
{
"name": "point:8",
"color": "009800"
},
{
"name": "point:13",
"color": "009800"
},
{
"name": "type:bug",
"color": "eb6420"
},
{
"name": "type:chore",
"color": "eb6420"
},
{
"name": "type:story",
"color": "eb6420"
},
{
"name": "type:infrastructure",
"color": "eb6420"
},
{
"name": "type:performance",
"color": "eb6420"
},
{
"name": "type:refactor",
"color": "eb6420"
},
{
"name": "type:test",
"color": "eb6420"
}
]
"@
$labelList = $labelJson | ConvertFrom-Json
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function Delete-Label {
param([string]$lableName)
$url = "https://api.github.com/repos/{0}/{1}/labels/{2}" -f $OwnerName, $RepositoryName, $lableName
Invoke-WebRequest $url -Method Delete -Headers $headers
}
function Create-Label {
param([string]$lableName, [string]$labelColor)
$hashTable = @{"name"=$lableName; "color"=$labelColor}
$data = $hashTable | ConvertTo-Json
$url = "https://api.github.com/repos/{0}/{1}/labels" -f $OwnerName, $RepositoryName
Invoke-WebRequest $url -Method Post -Body $data -Headers $headers
}
function Get-CurrentLabels {
$url = "https://api.github.com/repos/{0}/{1}/labels" -f $OwnerName, $RepositoryName
$result = (Invoke-WebRequest $url -Headers $headers).Content
$labels = $result | ConvertFrom-Json
return $labels
}
if ($DeleteLabels) {
$labelList = Get-CurrentLabels
foreach ($label in $labelList) {
Write-Host "Deleting Label:" $label.name -f Yellow
$result = Delete-Label -lableName $label.name
if ($result.StatusCode -eq 204) {
Write-Host $label.name "was deleted" -f DarkYellow
} else {
Write-Host $label.name "was not deleted" -f DarkRed
}
}
}
if (!$DeleteLabels) {
foreach ($label in $labelList) {
Write-Host "Creating Label:" $label.name -f Yellow
$result = Create-Label -lableName $label.name -labelColor $label.color
if ($result.StatusCode -eq 201) {
Write-Host $label.name "was created" -f DarkYellow
} else {
Write-Host $label.name "was not created" -f DarkRed
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment