|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Checks that PRs merged since the last release carry the labels used to |
| 7 | + * generate the changelog, suggesting a label from the PR title type. |
| 8 | + * |
| 9 | + * Requires the authenticated GitHub CLI (`gh`). |
| 10 | + * |
| 11 | + * Usage: php admin/check-pr-labels.php [version] |
| 12 | + */ |
| 13 | +function color(string $text, string $code, bool $ansi): string |
| 14 | +{ |
| 15 | + return $ansi ? sprintf("\033[%sm%s\033[0m", $code, $text) : $text; |
| 16 | +} |
| 17 | + |
| 18 | +function hyperlink(string $text, string $url, bool $ansi): string |
| 19 | +{ |
| 20 | + return $ansi ? sprintf("\033]8;;%s\033\\%s\033]8;;\033\\", $url, $text) : $text; |
| 21 | +} |
| 22 | + |
| 23 | +function format_pull_line(array $pull, bool $ansi): string |
| 24 | +{ |
| 25 | + return sprintf( |
| 26 | + '* %s %s %s', |
| 27 | + hyperlink(color(sprintf('#%d', $pull['number']), '36', $ansi), $pull['url'], $ansi), |
| 28 | + $pull['title'], |
| 29 | + color(sprintf('(%s)', $pull['base']), '35', $ansi), |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +function touches_system_dir(string $repo, int $number): bool |
| 34 | +{ |
| 35 | + exec(sprintf("gh api repos/%s/pulls/%d/files --paginate --jq '.[].filename' 2>&1", $repo, $number), $files, $exitCode); |
| 36 | + |
| 37 | + if ($exitCode !== 0) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + foreach ($files as $file) { |
| 42 | + if (str_starts_with($file, 'system/')) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return false; |
| 48 | +} |
| 49 | + |
| 50 | +chdir(__DIR__ . '/..'); |
| 51 | + |
| 52 | +$ansi = stream_isatty(STDOUT); |
| 53 | +$repo = 'codeigniter4/CodeIgniter4'; |
| 54 | + |
| 55 | +// Labels used by the changelog categories. See ".github/release.yml". |
| 56 | +$changelogLabels = ['breaking change', 'bug', 'enhancement', 'new feature', 'refactor']; |
| 57 | + |
| 58 | +// PR title types that map to a changelog label. |
| 59 | +$typeToLabel = [ |
| 60 | + 'fix' => 'bug', |
| 61 | + 'feat' => 'new feature', |
| 62 | + 'perf' => 'enhancement', |
| 63 | + 'refactor' => 'refactor', |
| 64 | +]; |
| 65 | + |
| 66 | +// PR title types that need no changelog label. |
| 67 | +$typesWithoutLabel = ['chore', 'ci', 'docs', 'style', 'test']; |
| 68 | + |
| 69 | +// Release process PRs carry no labels. |
| 70 | +$releaseTitles = '/\A(?:Prep for \d+\.\d+\.\d+ release|\d+\.\d+\.\d+ (?:Ready|Merge) code)\z/'; |
| 71 | + |
| 72 | +$tag = null; |
| 73 | + |
| 74 | +foreach (array_slice($argv, 1) as $arg) { |
| 75 | + if (preg_match('/\Av?(\d+\.\d+\.\d+)\z/', $arg, $matches) === 1) { |
| 76 | + $tag = "v{$matches[1]}"; |
| 77 | + |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + echo sprintf("Usage: php %s [version]\n", $argv[0]); |
| 82 | + echo sprintf("E.g.,: php %s 4.7.3\n", $argv[0]); |
| 83 | + echo "Checks the PRs merged after the given release. Defaults to the latest release.\n"; |
| 84 | + |
| 85 | + exit(1); |
| 86 | +} |
| 87 | + |
| 88 | +$endpoint = $tag === null ? sprintf('repos/%s/releases/latest', $repo) : sprintf('repos/%s/releases/tags/%s', $repo, $tag); |
| 89 | +exec(sprintf('gh api %s 2>&1', $endpoint), $output, $exitCode); |
| 90 | + |
| 91 | +if ($exitCode !== 0) { |
| 92 | + echo sprintf("Failed to fetch the release %s from GitHub:\n", $tag ?? '(latest)'); |
| 93 | + echo implode("\n", $output) . "\n"; |
| 94 | + |
| 95 | + exit(1); |
| 96 | +} |
| 97 | + |
| 98 | +$release = json_decode(implode("\n", $output), true); |
| 99 | +$since = $release['published_at'] ?? ''; |
| 100 | + |
| 101 | +echo sprintf("Checking PRs merged since %s (%s).\n", $release['tag_name'], $since); |
| 102 | + |
| 103 | +$command = sprintf( |
| 104 | + 'gh pr list --repo %s --search %s --json number,title,labels,baseRefName,url --limit 300 2>&1', |
| 105 | + $repo, |
| 106 | + escapeshellarg(sprintf('is:merged -base:master merged:>%s', $since)), |
| 107 | +); |
| 108 | +exec($command, $prOutput, $exitCode); |
| 109 | + |
| 110 | +if ($exitCode !== 0) { |
| 111 | + echo "Failed to fetch the merged PRs from GitHub:\n"; |
| 112 | + echo implode("\n", $prOutput) . "\n"; |
| 113 | + |
| 114 | + exit(1); |
| 115 | +} |
| 116 | + |
| 117 | +$pulls = json_decode(implode("\n", $prOutput), true); |
| 118 | + |
| 119 | +if (! is_array($pulls)) { |
| 120 | + echo "Unexpected response from GitHub.\n"; |
| 121 | + |
| 122 | + exit(1); |
| 123 | +} |
| 124 | + |
| 125 | +echo sprintf("Found %d merged PRs.\n\n", count($pulls)); |
| 126 | + |
| 127 | +$missingLabel = []; |
| 128 | +$needManualLook = []; |
| 129 | + |
| 130 | +foreach ($pulls as $pull) { |
| 131 | + $labels = array_column($pull['labels'], 'name'); |
| 132 | + $hasLabel = array_intersect($changelogLabels, $labels) !== []; |
| 133 | + $type = null; |
| 134 | + |
| 135 | + if (preg_match('/\A(\w+)(?:\([^)]*\))?:/', $pull['title'], $matches) === 1) { |
| 136 | + $type = $matches[1]; |
| 137 | + } |
| 138 | + |
| 139 | + if ($hasLabel || in_array($type, $typesWithoutLabel, true)) { |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + if (preg_match($releaseTitles, $pull['title']) === 1) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + if ($type !== null && isset($typeToLabel[$type])) { |
| 148 | + // The refactor label applies only to refactoring in system/. Test-only |
| 149 | + // refactors may use the testing label, which is not in the changelog. |
| 150 | + if ($type === 'refactor' && ! touches_system_dir($repo, (int) $pull['number'])) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + $missingLabel[] = [ |
| 155 | + 'number' => $pull['number'], |
| 156 | + 'title' => $pull['title'], |
| 157 | + 'base' => $pull['baseRefName'], |
| 158 | + 'url' => $pull['url'], |
| 159 | + 'label' => $typeToLabel[$type], |
| 160 | + ]; |
| 161 | + } else { |
| 162 | + $needManualLook[] = [ |
| 163 | + 'number' => $pull['number'], |
| 164 | + 'title' => $pull['title'], |
| 165 | + 'base' => $pull['baseRefName'], |
| 166 | + 'url' => $pull['url'], |
| 167 | + ]; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +if ($missingLabel !== []) { |
| 172 | + echo color('PRs that appear to be missing a changelog label:', '1', $ansi) . "\n"; |
| 173 | + |
| 174 | + foreach ($missingLabel as $pull) { |
| 175 | + echo sprintf("%s [suggested: %s]\n", format_pull_line($pull, $ansi), color($pull['label'], '33', $ansi)); |
| 176 | + } |
| 177 | + |
| 178 | + echo "\nTo add the suggested labels, run the following commands (drop any that are not warranted):\n"; |
| 179 | + |
| 180 | + foreach ($missingLabel as $pull) { |
| 181 | + echo sprintf("gh pr edit %d --repo %s --add-label \"%s\"\n", $pull['number'], $repo, $pull['label']); |
| 182 | + } |
| 183 | + |
| 184 | + echo "\n"; |
| 185 | +} |
| 186 | + |
| 187 | +if ($needManualLook !== []) { |
| 188 | + echo color('PRs with no changelog label and no recognized title type (check manually):', '1', $ansi) . "\n"; |
| 189 | + |
| 190 | + foreach ($needManualLook as $pull) { |
| 191 | + echo format_pull_line($pull, $ansi) . "\n"; |
| 192 | + } |
| 193 | + |
| 194 | + echo "\n"; |
| 195 | +} |
| 196 | + |
| 197 | +if ($missingLabel === []) { |
| 198 | + echo color('No PRs are missing changelog labels.', '32', $ansi) . "\n"; |
| 199 | + |
| 200 | + exit(0); |
| 201 | +} |
| 202 | + |
| 203 | +exit(1); |
0 commit comments