Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ are used as part of that process:
version references in the framework source, the user guide, and the
distribution build script.
Usage: `php admin/prepare-release.php 4.x.x`
- **create-release.php** publishes the GitHub release for a new version,
crediting every PR author since the previous release except bots.
Usage: `php admin/create-release.php 4.x.x [--previous=v4.x.w] [--dry-run]`
- **create-new-changelog.php** creates the changelog and upgrade guide
stubs for the next version and adds them to their index files.
Usage: `php admin/create-new-changelog.php <current_version> <new_version>`
Expand Down
25 changes: 9 additions & 16 deletions admin/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,15 @@ existing content.
* Title: `4.x.x Ready code`
* Description: blank
* [ ] Merge the PR and wait for all tests.
* [ ] Create a new Release:
* Choose a tag: `v4.x.x` (Create new tag: v4.x.x on publish)
* Target: `master`
* Title: `CodeIgniter 4.x.x`
* Description:
```
CodeIgniter 4.x.x release.

See the changelog: https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md

## New Contributors
*

**Full Changelog**: https://github.com/codeigniter4/CodeIgniter4/compare/v4.x.w...v4.x.x
```
Click the "Generate release notes" button, and get the "New Contributors".
* [ ] Create the new Release:
* Run `php admin/create-release.php 4.x.x --dry-run` and check the output.
* Run `php admin/create-release.php 4.x.x` to publish it.
* The above command does the following:
* Fetch GitHub's auto-generated release notes for `master` since the latest
release (override with `--previous=v4.x.w`)
* Credit every PR author except bots, marking first-time contributors
* Publish the release `CodeIgniter 4.x.x` with the tag `v4.x.x` on `master`,
linking to **CHANGELOG.md** and the full comparison
* [ ] Watch the "[Verify Release](https://github.com/codeigniter4/CodeIgniter4/actions/workflows/verify-release.yml)"
workflow and make sure it passes. It does the following:
* Wait for the "[Deploy Distributable Repos](https://github.com/codeigniter4/CodeIgniter4/actions/workflows/deploy-distributables.yml)"
Expand Down
148 changes: 148 additions & 0 deletions admin/create-release.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

/**
* Creates the GitHub release for a new version, crediting every contributor
* except bots.
*
* Requires the authenticated GitHub CLI (`gh`). Run it after the
* "4.x.x Ready code" PR is merged into `master`.
*
* Usage: php admin/create-release.php <version> [--previous=<tag>] [--dry-run]
*/
chdir(__DIR__ . '/..');

$args = array_slice($argv, 1);
$options = array_values(array_filter($args, static fn (string $arg): bool => str_starts_with($arg, '--')));
$params = array_values(array_diff($args, $options));

if (count($params) !== 1 || preg_match('/\Av?(\d+\.\d+\.\d+)\z/', $params[0], $matches) !== 1) {
echo sprintf("Usage: php %s <version> [--previous=<tag>] [--dry-run]\n", $argv[0]);
echo sprintf("E.g.,: php %s 4.7.6\n", $argv[0]);

exit(1);
}

$version = $matches[1];
$tag = sprintf('v%s', $version);
$dryRun = in_array('--dry-run', $options, true);
$repo = 'codeigniter4/CodeIgniter4';

exec(sprintf('gh release view %s --repo %s 2>&1', escapeshellarg($tag), $repo), $output, $exitCode);

if ($exitCode === 0) {
echo sprintf("The release %s already exists.\n", $tag);

exit(1);
}

$previousTag = null;

foreach ($options as $option) {
if (str_starts_with($option, '--previous=')) {
$previousTag = sprintf('v%s', ltrim(substr($option, strlen('--previous=')), 'v'));
}
}

if ($previousTag === null) {
$output = [];
exec(sprintf('gh release view --repo %s --json tagName --jq .tagName 2>&1', $repo), $output, $exitCode);

if ($exitCode !== 0) {
echo "Failed to fetch the latest release from GitHub:\n";
echo implode("\n", $output) . "\n";

exit(1);
}

$previousTag = trim(implode('', $output));
}

$output = [];
exec(sprintf(
'gh api repos/%s/releases/generate-notes -f tag_name=%s -f target_commitish=master -f previous_tag_name=%s 2>&1',
$repo,
escapeshellarg($tag),
escapeshellarg($previousTag),
), $output, $exitCode);

if ($exitCode !== 0) {
echo "Failed to fetch the release notes from GitHub:\n";
echo implode("\n", $output) . "\n";

exit(1);
}

$notes = json_decode(implode("\n", $output), true)['body'] ?? '';

$contributors = [];
$firstPulls = [];
$fullChangelog = null;

foreach (preg_split('/\R/', $notes) as $line) {
if (preg_match('~ by @(\S+) in https://github\.com/\S+/pull/\d+$~', $line, $matches) === 1) {
$contributors[$matches[1]] = true;

continue;
}

if (preg_match('~^\* @(\S+) made their first contribution in (\S+)$~', $line, $matches) === 1) {
$firstPulls[$matches[1]] = $matches[2];

continue;
}

if (str_starts_with($line, '**Full Changelog**: ')) {
$fullChangelog = $line;
}
}

if ($fullChangelog === null) {
echo "Could not find the Full Changelog link in the release notes.\n";

exit(1);
}

$contributors = array_filter(
array_keys($contributors),
static fn (string $login): bool => ! str_ends_with($login, '[bot]'),
);
natcasesort($contributors);

$credits = array_map(
static fn (string $login): string => isset($firstPulls[$login])
? sprintf('* @%s made their first contribution in %s', $login, $firstPulls[$login])
: sprintf('* @%s', $login),
$contributors,
);

$body = sprintf("CodeIgniter %s release.\n\n", $version);
$body .= sprintf("See the changelog: https://github.com/%s/blob/develop/CHANGELOG.md\n\n", $repo);

if ($credits !== []) {
$body .= sprintf("## Contributors\n\n%s\n\n", implode("\n", $credits));
}

$body .= $fullChangelog . "\n";

if ($dryRun) {
echo $body;

exit(0);
}

$notesFile = tempnam(sys_get_temp_dir(), 'ci4-release-');
file_put_contents($notesFile, $body);

passthru(sprintf(
'gh release create %s --repo %s --target master --title %s --notes-file %s',
escapeshellarg($tag),
$repo,
escapeshellarg(sprintf('CodeIgniter %s', $version)),
escapeshellarg($notesFile),
), $exitCode);

unlink($notesFile);

exit($exitCode);