build: use workflows for bumping versions and cargo publishing on the CI (#13995)

This commit is contained in:
David Sherret 2022-03-30 16:37:00 -04:00 committed by GitHub
parent f61b2c0b11
commit 5cab3e7dba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 306 additions and 74 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run=cargo
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run=cargo,git --allow-net --no-check
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { DenoWorkspace } from "./deno_workspace.ts";
import { GitLogOutput, path, semver } from "./deps.ts";
@ -8,8 +8,19 @@ const repo = workspace.repo;
const cliCrate = workspace.getCliCrate();
const originalCliVersion = cliCrate.version;
// update the std version used in the code
await updateStdVersion();
// increment the cli version
await cliCrate.promptAndIncrement();
if (Deno.args.some((a) => a === "--patch")) {
await cliCrate.increment("patch");
} else if (Deno.args.some((a) => a === "--minor")) {
await cliCrate.increment("minor");
} else if (Deno.args.some((a) => a === "--major")) {
await cliCrate.increment("major");
} else {
await cliCrate.promptAndIncrement();
}
// increment the dependency crate versions
for (const crate of workspace.getCliDependencyCrates()) {
@ -17,7 +28,7 @@ for (const crate of workspace.getCliDependencyCrates()) {
}
// update the lock file
await workspace.getCliCrate().cargoCheck();
await workspace.getCliCrate().cargoUpdate("--workspace");
// try to update the Releases.md markdown text
try {
@ -101,3 +112,26 @@ async function getGitLog() {
);
}
}
async function updateStdVersion() {
const newStdVersion = await getLatestStdVersion();
const compatFilePath = path.join(cliCrate.folderPath, "compat/mod.rs");
const text = Deno.readTextFileSync(compatFilePath);
Deno.writeTextFileSync(
compatFilePath,
text.replace(/std@[0-9]+\.[0-9]+\.[0-9]+/, `std@${newStdVersion}`),
);
}
async function getLatestStdVersion() {
const url =
"https://raw.githubusercontent.com/denoland/deno_std/main/version.ts";
const result = await fetch(url);
const text = await result.text();
const version = /"([0-9]+\.[0-9]+\.[0-9]+)"/.exec(text);
if (version == null) {
throw new Error(`Could not find version in text: ${text}`);
} else {
return version[1];
}
}

51
tools/release/02_create_pr.ts Executable file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env --allow-net --allow-run=cargo,git --no-check
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { DenoWorkspace } from "./deno_workspace.ts";
import { createOctoKit, getGitHubRepository } from "./deps.ts";
const octoKit = createOctoKit();
const workspace = await DenoWorkspace.load();
const repo = workspace.repo;
const cliCrate = workspace.getCliCrate();
const originalBranch = await repo.gitCurrentBranch();
const newBranchName = `release_${cliCrate.version.replace(/\./, "_")}`;
// Create and push branch
console.log(`Creating branch ${newBranchName}...`);
await repo.gitBranch(newBranchName);
await repo.gitAdd();
await repo.gitCommit(cliCrate.version);
console.log("Pushing branch...");
await repo.gitPush("-u", "origin", "HEAD");
// Open PR
console.log("Opening PR...");
const openedPr = await octoKit.request("POST /repos/{owner}/{repo}/pulls", {
...getGitHubRepository(),
base: originalBranch,
head: newBranchName,
draft: true,
title: cliCrate.version,
body: getPrBody(),
});
console.log(`Opened PR at ${openedPr.data.url}`);
function getPrBody() {
let text = `Bumped versions for ${cliCrate.version}\n\n` +
`Please ensure:\n` +
`- [ ] Crate versions are bumped correctly\n` +
`- [ ] deno_std version is incremented in the code\n` +
`- [ ] Releases.md is updated correctly\n\n` +
`To make edits to this PR:\n` +
"```shell\n" +
`git fetch upstream ${newBranchName} && git checkout -b ${newBranchName} upstream/${newBranchName}\n` +
"```\n";
const actor = Deno.env.get("GH_WORKFLOW_ACTOR");
if (actor != null) {
text += `\ncc @${actor}`;
}
return text;
}

View file

@ -1,23 +0,0 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run=cargo --allow-net=crates.io
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { DenoWorkspace } from "./deno_workspace.ts";
import { getCratesPublishOrder } from "./deps.ts";
const workspace = await DenoWorkspace.load();
const cliCrate = workspace.getCliCrate();
const dependencyCrates = getCratesPublishOrder(
workspace.getCliDependencyCrates(),
);
try {
for (const [i, crate] of dependencyCrates.entries()) {
await crate.publish();
console.log(`Published ${i + 1} of ${dependencyCrates.length} crates.`);
}
await cliCrate.publish();
} finally {
// system beep to notify error or completion
console.log("\x07");
}

View file

@ -0,0 +1,48 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run=cargo --allow-net=crates.io --no-check
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { DenoWorkspace } from "./deno_workspace.ts";
import { Crate, getCratesPublishOrder } from "./deps.ts";
const isReal = parseIsReal();
console.log(`Running a ${isReal ? "real" : "dry"} cargo publish...`);
const workspace = await DenoWorkspace.load();
const cliCrate = workspace.getCliCrate();
const dependencyCrates = getCratesPublishOrder(
workspace.getCliDependencyCrates(),
);
try {
for (const [i, crate] of dependencyCrates.entries()) {
await publishCrate(crate);
console.log(`Finished ${i + 1} of ${dependencyCrates.length} crates.`);
}
await publishCrate(cliCrate);
} finally {
// system beep to notify error or completion
console.log("\x07");
}
async function publishCrate(crate: Crate) {
if (isReal) {
await crate.publish();
} else {
await crate.publishDryRun();
}
}
function parseIsReal() {
const isReal = Deno.args.some((a) => a === "--real");
const isDry = Deno.args.some((a) => a === "--dry");
// force the call to be explicit and provide one of these
// so that it's obvious what's happening
if (!isDry && !isReal) {
console.error("Please run with `--dry` or `--real`.");
Deno.exit(1);
}
return isReal;
}

View file

@ -0,0 +1,17 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run=cargo --no-check
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { DenoWorkspace } from "./deno_workspace.ts";
const workspace = await DenoWorkspace.load();
const repo = workspace.repo;
const cliCrate = workspace.getCliCrate();
await repo.gitFetchTags("origin");
const tags = await repo.getGitTags();
if (tags.has(cliCrate.version)) {
console.log(`Tag ${cliCrate.version} already exists.`);
} else {
await repo.gitTag(cliCrate.version);
await repo.gitPush(cliCrate.version);
}

View file

@ -12,7 +12,10 @@ export class DenoWorkspace {
static async load(): Promise<DenoWorkspace> {
return new DenoWorkspace(
await Repo.load("deno", DenoWorkspace.rootDirPath),
await Repo.load({
name: "deno",
path: DenoWorkspace.rootDirPath,
}),
);
}

View file

@ -1,3 +1,4 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
export * from "https://raw.githubusercontent.com/denoland/automation/0.5.0/mod.ts";
export * from "https://raw.githubusercontent.com/denoland/automation/0.9.0/mod.ts";
export * from "https://raw.githubusercontent.com/denoland/automation/0.9.0/github_actions.ts";