2022-07-19 16:35:58 -04:00
|
|
|
#!/usr/bin/env -S deno run -A --lock=tools/deno.lock.json
|
2022-04-04 14:56:29 -04:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
|
|
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
|
|
|
]);
|
2022-07-19 16:35:58 -04:00
|
|
|
await repo.command([
|
2022-04-04 14:56:29 -04:00
|
|
|
"git",
|
|
|
|
"cherry-pick",
|
2022-07-05 11:44:22 -04:00
|
|
|
"--strategy-option",
|
|
|
|
"theirs",
|
2022-04-04 14:56:29 -04:00
|
|
|
releaseCommitHash,
|
|
|
|
]);
|
|
|
|
await repo.gitPush("origin", newBranchName);
|
|
|
|
|
2022-07-19 16:35:58 -04:00
|
|
|
$.logStep(`Opening PR...`);
|
2022-04-04 14:56:29 -04:00
|
|
|
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(),
|
|
|
|
},
|
|
|
|
);
|
2022-07-19 16:35:58 -04:00
|
|
|
$.log(`Opened PR at ${openedPr.data.url}`);
|
2022-04-04 14:56:29 -04:00
|
|
|
|
|
|
|
function getPrBody() {
|
|
|
|
let text =
|
|
|
|
`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;
|
|
|
|
}
|
|
|
|
}
|