2022-07-19 16:35:58 -04:00
|
|
|
#!/usr/bin/env -S deno run -A --lock=tools/deno.lock.json
|
2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-04-04 14:56:29 -04:00
|
|
|
import { DenoWorkspace } from "./deno_workspace.ts";
|
2022-07-19 16:35:58 -04:00
|
|
|
import { $, createOctoKit, getGitHubRepository } from "./deps.ts";
|
2022-04-04 14:56:29 -04:00
|
|
|
|
|
|
|
const workspace = await DenoWorkspace.load();
|
|
|
|
const repo = workspace.repo;
|
|
|
|
const cliCrate = workspace.getCliCrate();
|
|
|
|
|
2022-07-19 16:35:58 -04:00
|
|
|
$.logStep("Creating release tag...");
|
2022-04-04 14:56:29 -04:00
|
|
|
await createReleaseTag();
|
|
|
|
|
2022-07-19 16:35:58 -04:00
|
|
|
$.logStep("Forwarding release commit to main...");
|
2022-04-04 14:56:29 -04:00
|
|
|
try {
|
|
|
|
await forwardReleaseCommitToMain();
|
|
|
|
} catch (err) {
|
2022-07-19 16:35:58 -04:00
|
|
|
$.logError("Failed. Please manually open a PR.", err);
|
2022-04-04 14:56:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function createReleaseTag() {
|
|
|
|
await repo.gitFetchTags("origin");
|
|
|
|
const tags = await repo.getGitTags();
|
|
|
|
const tagName = `v${cliCrate.version}`;
|
|
|
|
|
|
|
|
if (tags.has(tagName)) {
|
2022-07-19 16:35:58 -04:00
|
|
|
$.log(`Tag ${tagName} already exists.`);
|
2022-04-04 14:56:29 -04:00
|
|
|
} else {
|
|
|
|
await repo.gitTag(tagName);
|
2022-04-07 14:40:20 -04:00
|
|
|
await repo.gitPush("origin", tagName);
|
2022-04-04 14:56:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function forwardReleaseCommitToMain() {
|
|
|
|
// if this is a patch release, open a PR to forward the most recent commit back to main
|
|
|
|
const currentBranch = await repo.gitCurrentBranch();
|
|
|
|
const isPatchRelease = currentBranch !== "main";
|
|
|
|
|
|
|
|
if (!isPatchRelease) {
|
2022-07-19 16:35:58 -04:00
|
|
|
$.log("Not doing a patch release. Skipping.");
|
2022-04-04 14:56:29 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-19 16:35:58 -04:00
|
|
|
await repo.command("git fetch origin main");
|
|
|
|
const releaseCommitHash = await repo.command("git rev-parse HEAD").text();
|
2022-04-04 14:56:29 -04:00
|
|
|
const newBranchName = `forward_v${cliCrate.version}`;
|
2022-07-19 16:35:58 -04:00
|
|
|
$.logStep(`Creating branch ${newBranchName}...`);
|
|
|
|
await repo.command([
|
2022-04-04 14:56:29 -04:00
|
|
|
"git",
|
|
|
|
"checkout",
|
|
|
|
"-b",
|
|
|
|
newBranchName,
|
2022-04-15 09:39:41 -04:00
|
|
|
"origin/main",
|
2022-04-04 14:56:29 -04:00
|
|
|
]);
|
2023-04-01 12:58:55 -04:00
|
|
|
const cherryPickResult = await repo.command([
|
2022-04-04 14:56:29 -04:00
|
|
|
"git",
|
|
|
|
"cherry-pick",
|
|
|
|
releaseCommitHash,
|
2023-04-01 12:58:55 -04:00
|
|
|
]).noThrow();
|
|
|
|
if (cherryPickResult.code !== 0) {
|
|
|
|
// commit with conflicts that can be resolved in the PR
|
|
|
|
await repo.command("git add .");
|
|
|
|
await repo.command(
|
|
|
|
'git commit --no-verify -m "Cherry-pick version bump commit with conflicts"',
|
|
|
|
).noThrow();
|
|
|
|
}
|
2022-04-04 14:56:29 -04:00
|
|
|
await repo.gitPush("origin", newBranchName);
|
|
|
|
|
2022-07-19 16:35:58 -04:00
|
|
|
$.logStep(`Opening PR...`);
|
2023-11-10 08:40:00 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
const openedPr = await createOctoKit().request(
|
|
|
|
"POST /repos/{owner}/{repo}/pulls",
|
|
|
|
{
|
|
|
|
...getGitHubRepository(),
|
|
|
|
base: "main",
|
|
|
|
head: newBranchName,
|
|
|
|
draft: true,
|
|
|
|
title: `chore: forward v${cliCrate.version} release commit to main`,
|
|
|
|
body: getPrBody(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
$.log(`Opened PR at ${openedPr.data.url}`);
|
|
|
|
} catch (err) {
|
|
|
|
$.logError(
|
|
|
|
`Failed to open PR. Please open one manually: https://github.com/denoland/deno/pull/new/${newBranchName}`,
|
|
|
|
err,
|
|
|
|
);
|
|
|
|
}
|
2022-04-04 14:56:29 -04:00
|
|
|
|
|
|
|
function getPrBody() {
|
2023-04-01 12:58:55 -04:00
|
|
|
let text = "";
|
|
|
|
|
|
|
|
if (cherryPickResult.code !== 0) {
|
|
|
|
text += `**THIS PR HAS GIT CONFLICTS THAT MUST BE RESOLVED**\n\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
text +=
|
2022-04-04 14:56:29 -04:00
|
|
|
`This is the release commit being forwarded back to main for ${cliCrate.version}\n\n` +
|
|
|
|
`Please ensure:\n` +
|
|
|
|
`- [ ] Everything looks ok in the PR\n` +
|
|
|
|
`- [ ] The release has been published\n\n` +
|
|
|
|
`To make edits to this PR:\n` +
|
|
|
|
"```shell\n" +
|
|
|
|
`git fetch upstream ${newBranchName} && git checkout -b ${newBranchName} upstream/${newBranchName}\n` +
|
|
|
|
"```\n\n" +
|
|
|
|
"Don't need this PR? Close it.\n";
|
|
|
|
|
|
|
|
const actor = Deno.env.get("GH_WORKFLOW_ACTOR");
|
|
|
|
if (actor != null) {
|
|
|
|
text += `\ncc @${actor}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|