Skip to content

Commit

Permalink
Add Update Only Feature to GitHub Labels Script (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtracey93 authored Sep 1, 2023
1 parent 96977de commit d8d669b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
4 changes: 3 additions & 1 deletion docs/content/specs/shared/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ For most scenario this is the command you'll need to call the below PowerShell s
Set-AvmGitHubLabels.ps1 -RepositoryName "Org/MyGitHubRepo" -CreateCsvLabelExports $false -NoUserPrompts $true
```

Be aware this script by default, will remove all pre-existing labels from the repository, but this can be changed by setting the `-RemoveExistingLabels` parameter to `$false`
By default this script will only update and append labels on the repository specified. However, this can be changed by setting the parameter `-UpdateAndAddLabelsOnly` to `$false`, which will remove all the labels from the repository first and then apply the AVM labels from the CSV only.

Full Script:

These `Set-AvmGitHubLabels.ps1` can be downloaded from <a href="/Azure-Verified-Modules/scripts/Set-AvmGitHubLabels.ps1" download>here</a>.

{{< include file="/static/scripts/Set-AvmGitHubLabels.ps1" language="powershell" options="linenos=false" >}}

{{< /expand >}}
Expand Down
32 changes: 21 additions & 11 deletions docs/static/scripts/Set-AvmGitHubLabels.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
.Parameter RemoveExistingLabels
If set to $true, the default value, the script will remove all pre-existing labels from the repository specified in -RepositoryName before applying the AVM labels. If set to $false, the script will not remove any pre-existing labels.
.Parameter UpdateAndAddLabelsOnly
If set to $true, the default value, the script will only update and add labels to the repository specified in -RepositoryName. If set to $false, the script will remove all pre-existing labels from the repository specified in -RepositoryName before applying the AVM labels.
.Parameter OutputDirectory
The directory to output the pre-existing and post-existing labels to in a CSV file. The default value is the current directory.
Expand Down Expand Up @@ -85,6 +88,9 @@ param (
[Parameter(Mandatory = $false)]
[bool]$RemoveExistingLabels = $true,

[Parameter(Mandatory = $false)]
[bool]$UpdateAndAddLabelsOnly = $true,

[Parameter(Mandatory = $false)]
[bool]$CreateCsvLabelExports = $true,

Expand Down Expand Up @@ -129,7 +135,7 @@ if ($null -eq $GitHubRepository) {
Write-Host "The GitHub repository $RepositoryName exists..." -ForegroundColor Green

# PRE - Get the current GitHub repository labels and export to a CSV file in the current directory or where -OutputDirectory specifies if set to a valid directory path and the directory exists or can be created if it does not exist already
if ($CreateCsvLabelExports -eq $true) {
if ($RemoveExistingLabels -or $UpdateAndAddLabelsOnly) {
Write-Host "Getting the current GitHub repository (pre) labels for $RepositoryName..." -ForegroundColor Yellow
$GitHubRepositoryLabels = gh label list -R $RepositoryName -L $GitHubCliLimit --json name,description,color

Expand All @@ -143,7 +149,7 @@ if ($CreateCsvLabelExports -eq $true) {
# Remove all pre-existing labels if -RemoveExistingLabels is set to $true and user confirms they want to remove all pre-existing labels
if ($null -ne $GitHubRepositoryLabels) {
$GitHubRepositoryLabelsJson = $GitHubRepositoryLabels | ConvertFrom-Json
if ($RemoveExistingLabels -eq $true -and $NoUserPrompts -eq $false) {
if ($RemoveExistingLabels -eq $true -and $NoUserPrompts -eq $false -and $UpdateAndAddLabelsOnly -eq $false) {
$RemoveExistingLabelsConfirmation = Read-Host "Are you sure you want to remove all $($GitHubRepositoryLabelsJson.Count) pre-existing labels from $($RepositoryName)? (Y/N)"
if ($RemoveExistingLabelsConfirmation -eq "Y") {
Write-Host "Removing all pre-existing labels from $RepositoryName..." -ForegroundColor Yellow
Expand All @@ -153,7 +159,7 @@ if ($null -ne $GitHubRepositoryLabels) {
}
}
}
if ($RemoveExistingLabels -eq $true -and $NoUserPrompts -eq $true) {
if ($RemoveExistingLabels -eq $true -and $NoUserPrompts -eq $true -and $UpdateAndAddLabelsOnly -eq $false) {
Write-Host "Removing all pre-existing labels from $RepositoryName..." -ForegroundColor Yellow
$GitHubRepositoryLabels | ConvertFrom-Json | ForEach-Object {
Write-Host "Removing label $($_.name) from $RepositoryName..." -ForegroundColor DarkRed
Expand All @@ -162,11 +168,10 @@ if ($null -ne $GitHubRepositoryLabels) {
}
}
if ($null -eq $GitHubRepositoryLabels) {
Write-Host "No pre-existing labels to remove from $RepositoryName..." -ForegroundColor Magenta
Write-Host "No pre-existing labels to remove or not selected to be removed from $RepositoryName..." -ForegroundColor Magenta
}

# Check LabelsToApplyCsvUri is valid and contains a CSV content

Write-Host "Checking $LabelsToApplyCsvUri is valid..." -ForegroundColor Yellow
$LabelsToApplyCsvUriValid = $LabelsToApplyCsvUri -match "^https?://"
if ($false -eq $LabelsToApplyCsvUriValid) {
Expand All @@ -185,12 +190,17 @@ if ($false -eq $avmLabelsCsvColumnsValid) {
}
Write-Host "The labels CSV file contains the required columns: Name, Description, HEX" -ForegroundColor Green


# Create the AVM labels in the GitHub repository
Write-Host "Creating the $($avmLabelsCsv.Count) AVM labels in $RepositoryName..." -ForegroundColor Yellow
Write-Host "Creating/Updating the $($avmLabelsCsv.Count) AVM labels in $RepositoryName..." -ForegroundColor Yellow
$avmLabelsCsv | ForEach-Object {
Write-Host "Creating label $($_.name) in $RepositoryName..." -ForegroundColor Cyan
gh label create -R $RepositoryName "$($_.Name)" -c $_.HEX -d $($_.Description) --force
if ($GitHubRepositoryLabelsJson.name -contains $_.name) {
Write-Host "The label $($_.name) already exists in $RepositoryName. Updating the label to ensure description and color are consitent..." -ForegroundColor Magenta
gh label create -R $RepositoryName "$($_.name)" -c $_.HEX -d $($_.Description) --force
}
else {
Write-Host "The label $($_.name) does not exist in $RepositoryName. Creating label $($_.name) in $RepositoryName..." -ForegroundColor Cyan
gh label create -R $RepositoryName "$($_.Name)" -c $_.HEX -d $($_.Description) --force
}
}

# POST - Get the current GitHub repository labels and export to a CSV file in the current directory or where -OutputDirectory specifies if set to a valid directory path and the directory exists or can be created if it does not exist already
Expand All @@ -206,7 +216,7 @@ if ($CreateCsvLabelExports -eq $true) {
}

# If -RemoveExistingLabels is set to $true and user confirms they want to remove all pre-existing labels check that only the avm labels exist in the repository
if ($RemoveExistingLabels -eq $true -and ($RemoveExistingLabelsConfirmation -eq "Y" -or $NoUserPrompts -eq $true)) {
if ($RemoveExistingLabels -eq $true -and ($RemoveExistingLabelsConfirmation -eq "Y" -or $NoUserPrompts -eq $true) -and $UpdateAndAddLabelsOnly -eq $false) {
Write-Host "Checking that only the AVM labels exist in $RepositoryName..." -ForegroundColor Yellow
$GitHubRepositoryLabels = gh label list -R $RepositoryName -L $GitHubCliLimit --json name,description,color
$GitHubRepositoryLabels | ConvertFrom-Json | ForEach-Object {
Expand All @@ -217,4 +227,4 @@ if ($RemoveExistingLabels -eq $true -and ($RemoveExistingLabelsConfirmation -eq
Write-Host "Only the CSV labels exist in $RepositoryName..." -ForegroundColor Green
}

Write-Host "The CSV labels have been created in $RepositoryName..." -ForegroundColor Green
Write-Host "The CSV labels have been created/updated in $RepositoryName..." -ForegroundColor Green

0 comments on commit d8d669b

Please sign in to comment.