From 9b6c72d06a93f4c2b692e78d3a6cffe1908183a9 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 29 Jan 2024 14:00:11 +1300 Subject: [PATCH] FIX Init array, use $oldName, add REPOS_EXCLUDE, check if label exists before rename --- funcs_utils.php | 1 + labels_command.php | 50 ++++++++++++++++++++++++++++++++++++---------- run.php | 1 + 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/funcs_utils.php b/funcs_utils.php index 3e473a7..7ebbac3 100644 --- a/funcs_utils.php +++ b/funcs_utils.php @@ -88,6 +88,7 @@ function extra_repositories() $importantRepos = [ 'silverstripe/markdown-php-codesniffer', 'silverstripe/silverstripe-standards', + 'silverstripe/.github', ]; $modules = []; // iterating to page 10 will be enough to get all the repos well into the future diff --git a/labels_command.php b/labels_command.php index 58847ba..8294f1b 100644 --- a/labels_command.php +++ b/labels_command.php @@ -40,6 +40,22 @@ 'type/api-change' => 'type/api-break', ]; +// Repos that should not have labels updated because of a lack of API permissions because they are on +// non-silverstripe GitHub accounts, or because they are not applicable +const LABELS_EXCLUDE_GHREPOS = [ + 'colymba/GridFieldBulkEditingTools', + 'composer/installers', + 'dnadesign/silverstripe-elemental-subsites', + 'hafriedlander/phockito', + 'hafriedlander/silverstripe-phockito', + 'lekoala/silverstripe-debugbar', + 'tijsverkoyen/akismet', + 'tractorcow-farm/silverstripe-fluent', + 'tractorcow/classproxy', + 'tractorcow/silverstripe-proxy-db', + 'undefinedoffset/sortablegridfield', +]; + $labelsCommand = function(InputInterface $input, OutputInterface $output): int { // This is the code that is executed when running the 'labels' command @@ -61,7 +77,8 @@ foreach ([$modulesCurrentMajor, $modulesPreviousMajor] as $modulesList) { foreach ($modulesList as $module) { $repo = $module['repo']; - if (in_array($repo, $repos)) { + $ghrepo = $module['ghrepo']; + if (in_array($repo, $repos) || in_array($ghrepo, LABELS_EXCLUDE_GHREPOS)) { continue; } $modules[] = $module; @@ -93,17 +110,28 @@ // https://docs.github.com/en/rest/issues/labels#update-a-label if (array_key_exists($name, LABELS_RENAME)) { $newName = LABELS_RENAME[$name]; - info("Updating label $name to $newName in $repo"); - if ($input->getOption('dry-run')) { - info('Not updating label on GitHub because --dry-run option is set'); - } else { - github_api($url, ['new_name' => $newName], 'PATCH'); + // Don't rename if a label with the new name already exists + $alreadyExists = false; + foreach ($labels as $label) { + if ($newName === $label['name']) { + $alreadyExists = true; + break; + } + } + if (!$alreadyExists) { + info("Updating label $name to $newName in $repo"); + if ($input->getOption('dry-run')) { + info('Not updating label on GitHub because --dry-run option is set'); + } else { + github_api($url, ['new_name' => $newName], 'PATCH'); + } + $oldName = $name; + $name = $newName; + // Update $url replacing the $name at the end with $newName + $url = substr($url, 0, strlen($url) - strlen($oldName)) . $newName; + $labels[$key]['name'] = $newName; + $labels[$key]['url'] = $url; } - $name = $newName; - // Update $url replacing the $name at the end with $newName - $url = substr($url, 0, strlen($url) - strlen($name)) . $newName; - $labels[$key]['name'] = $newName; - $labels[$key]['url'] = $url; } // Delete label diff --git a/run.php b/run.php index df3dd80..726605a 100644 --- a/run.php +++ b/run.php @@ -24,6 +24,7 @@ $MODULE_DIR = ''; $PRS_CREATED = []; $REPOS_WITH_PRS_CREATED = []; +$REPOS_WITH_LABELS_UPDATED = []; $OUT = null; // options