Skip to content

Commit 4f41ff3

Browse files
authored
chore(release): automatically update branch protection during release (#1325)
1 parent d0a4e1e commit 4f41ff3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

bin/release

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use Composer\Semver\VersionParser;
1818
use Tempest\Console\Console;
1919
use Tempest\Console\ConsoleApplication;
2020
use Tempest\Console\Exceptions\InterruptException;
21+
use Tempest\Http\Status;
22+
use Tempest\HttpClient\HttpClient;
23+
use Tempest\Support\Json;
2124

2225
use function Tempest\get;
2326
use function Tempest\Support\arr;
@@ -156,11 +159,37 @@ function performPreReleaseChecks(string $remote, string $branch): void
156159
throw new Exception("You must be on the {$remote}/{$branch} branch to release.");
157160
}
158161

162+
if (null === Tempest\env('RELEASE_GITHUB_TOKEN')) {
163+
throw new Exception('`RELEASE_GITHUB_TOKEN` environment variable must be set to release.');
164+
}
165+
159166
if ($behindCount = trim(shell_exec("git rev-list HEAD..{$remote}/{$branch} --count") ?? '0') !== '0') {
160167
throw new Exception("Local branch is behind {$remote}/{$branch} by {$behindCount} commits. Please pull first.");
161168
}
162169
}
163170

171+
/**
172+
* Disables the protection ruleset, so the release branch can be pushed to without a pull request.
173+
*/
174+
function updateBranchProtection(bool $enabled): void
175+
{
176+
// https://github.com/tempestphp/tempest-framework/settings/rules/1879240
177+
$ruleset = '1879240';
178+
$token = Tempest\env('RELEASE_GITHUB_TOKEN');
179+
$url = "https://api.github.com/repos/tempestphp/tempest-framework/rulesets/{$ruleset}";
180+
181+
$httpClient = Tempest\get(HttpClient::class);
182+
$response = $httpClient->put(
183+
uri: $url,
184+
headers: ['Authorization' => "Bearer {$token}"],
185+
body: Json\encode(['enforcement' => $enabled ? 'active' : 'disabled'])
186+
);
187+
188+
if ($response->status !== Status::OK) {
189+
throw new Exception('Failed to update branch ruleset.');
190+
}
191+
}
192+
164193
/**
165194
* Gets the current version.
166195
*/
@@ -338,6 +367,9 @@ try {
338367
handler: fn () => updateChangelog($version),
339368
);
340369

370+
// Disable protection
371+
updateBranchProtection(enabled: false);
372+
341373
// Push tags
342374
$console->task(
343375
label: 'Releasing',
@@ -360,6 +392,9 @@ try {
360392
],
361393
);
362394

395+
// Re-enable protection
396+
updateBranchProtection(enabled: true);
397+
363398
$console->success(sprintf(
364399
'Released <em>%1$s</em>. The <href="https://github.com/tempestphp/tempest-framework/releases/tag/%1$s">GitHub release</href> will be created automatically in a few seconds.',
365400
$tag,

0 commit comments

Comments
 (0)