|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Creates the GitHub release for a new version, crediting every contributor |
| 7 | + * except bots. |
| 8 | + * |
| 9 | + * Requires the authenticated GitHub CLI (`gh`). Run it after the |
| 10 | + * "4.x.x Ready code" PR is merged into `master`. |
| 11 | + * |
| 12 | + * Usage: php admin/create-release.php <version> [--previous=<tag>] [--dry-run] |
| 13 | + */ |
| 14 | +chdir(__DIR__ . '/..'); |
| 15 | + |
| 16 | +$args = array_slice($argv, 1); |
| 17 | +$options = array_values(array_filter($args, static fn (string $arg): bool => str_starts_with($arg, '--'))); |
| 18 | +$params = array_values(array_diff($args, $options)); |
| 19 | + |
| 20 | +if (count($params) !== 1 || preg_match('/\Av?(\d+\.\d+\.\d+)\z/', $params[0], $matches) !== 1) { |
| 21 | + echo sprintf("Usage: php %s <version> [--previous=<tag>] [--dry-run]\n", $argv[0]); |
| 22 | + echo sprintf("E.g.,: php %s 4.7.6\n", $argv[0]); |
| 23 | + |
| 24 | + exit(1); |
| 25 | +} |
| 26 | + |
| 27 | +$version = $matches[1]; |
| 28 | +$tag = sprintf('v%s', $version); |
| 29 | +$dryRun = in_array('--dry-run', $options, true); |
| 30 | +$repo = 'codeigniter4/CodeIgniter4'; |
| 31 | + |
| 32 | +exec(sprintf('gh release view %s --repo %s 2>&1', escapeshellarg($tag), $repo), $output, $exitCode); |
| 33 | + |
| 34 | +if ($exitCode === 0) { |
| 35 | + echo sprintf("The release %s already exists.\n", $tag); |
| 36 | + |
| 37 | + exit(1); |
| 38 | +} |
| 39 | + |
| 40 | +$previousTag = null; |
| 41 | + |
| 42 | +foreach ($options as $option) { |
| 43 | + if (str_starts_with($option, '--previous=')) { |
| 44 | + $previousTag = sprintf('v%s', ltrim(substr($option, strlen('--previous=')), 'v')); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +if ($previousTag === null) { |
| 49 | + $output = []; |
| 50 | + exec(sprintf('gh release view --repo %s --json tagName --jq .tagName 2>&1', $repo), $output, $exitCode); |
| 51 | + |
| 52 | + if ($exitCode !== 0) { |
| 53 | + echo "Failed to fetch the latest release from GitHub:\n"; |
| 54 | + echo implode("\n", $output) . "\n"; |
| 55 | + |
| 56 | + exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + $previousTag = trim(implode('', $output)); |
| 60 | +} |
| 61 | + |
| 62 | +$output = []; |
| 63 | +exec(sprintf( |
| 64 | + 'gh api repos/%s/releases/generate-notes -f tag_name=%s -f target_commitish=master -f previous_tag_name=%s 2>&1', |
| 65 | + $repo, |
| 66 | + escapeshellarg($tag), |
| 67 | + escapeshellarg($previousTag), |
| 68 | +), $output, $exitCode); |
| 69 | + |
| 70 | +if ($exitCode !== 0) { |
| 71 | + echo "Failed to fetch the release notes from GitHub:\n"; |
| 72 | + echo implode("\n", $output) . "\n"; |
| 73 | + |
| 74 | + exit(1); |
| 75 | +} |
| 76 | + |
| 77 | +$notes = json_decode(implode("\n", $output), true)['body'] ?? ''; |
| 78 | + |
| 79 | +$contributors = []; |
| 80 | +$firstPulls = []; |
| 81 | +$fullChangelog = null; |
| 82 | + |
| 83 | +foreach (preg_split('/\R/', $notes) as $line) { |
| 84 | + if (preg_match('~ by @(\S+) in https://github\.com/\S+/pull/\d+$~', $line, $matches) === 1) { |
| 85 | + $contributors[$matches[1]] = true; |
| 86 | + |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (preg_match('~^\* @(\S+) made their first contribution in (\S+)$~', $line, $matches) === 1) { |
| 91 | + $firstPulls[$matches[1]] = $matches[2]; |
| 92 | + |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if (str_starts_with($line, '**Full Changelog**: ')) { |
| 97 | + $fullChangelog = $line; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +if ($fullChangelog === null) { |
| 102 | + echo "Could not find the Full Changelog link in the release notes.\n"; |
| 103 | + |
| 104 | + exit(1); |
| 105 | +} |
| 106 | + |
| 107 | +$contributors = array_filter( |
| 108 | + array_keys($contributors), |
| 109 | + static fn (string $login): bool => ! str_ends_with($login, '[bot]'), |
| 110 | +); |
| 111 | +natcasesort($contributors); |
| 112 | + |
| 113 | +$credits = array_map( |
| 114 | + static fn (string $login): string => isset($firstPulls[$login]) |
| 115 | + ? sprintf('* @%s made their first contribution in %s', $login, $firstPulls[$login]) |
| 116 | + : sprintf('* @%s', $login), |
| 117 | + $contributors, |
| 118 | +); |
| 119 | + |
| 120 | +$body = sprintf("CodeIgniter %s release.\n\n", $version); |
| 121 | +$body .= sprintf("See the changelog: https://github.com/%s/blob/develop/CHANGELOG.md\n\n", $repo); |
| 122 | + |
| 123 | +if ($credits !== []) { |
| 124 | + $body .= sprintf("## Contributors\n\n%s\n\n", implode("\n", $credits)); |
| 125 | +} |
| 126 | + |
| 127 | +$body .= $fullChangelog . "\n"; |
| 128 | + |
| 129 | +if ($dryRun) { |
| 130 | + echo $body; |
| 131 | + |
| 132 | + exit(0); |
| 133 | +} |
| 134 | + |
| 135 | +$notesFile = tempnam(sys_get_temp_dir(), 'ci4-release-'); |
| 136 | +file_put_contents($notesFile, $body); |
| 137 | + |
| 138 | +passthru(sprintf( |
| 139 | + 'gh release create %s --repo %s --target master --title %s --notes-file %s', |
| 140 | + escapeshellarg($tag), |
| 141 | + $repo, |
| 142 | + escapeshellarg(sprintf('CodeIgniter %s', $version)), |
| 143 | + escapeshellarg($notesFile), |
| 144 | +), $exitCode); |
| 145 | + |
| 146 | +unlink($notesFile); |
| 147 | + |
| 148 | +exit($exitCode); |
0 commit comments