Pre support (#6)

* First implementation of pre

* Remove an unused thing

* Fix a thing

* Another fix

* Fix some stuff

* Actually fix it

* Update things
This commit is contained in:
Mitchell Hamilton
2019-11-08 09:47:05 +10:00
committed by GitHub
parent 47388214b1
commit 76f43bb56a
4 changed files with 425 additions and 41 deletions
+35 -37
View File
@@ -11,6 +11,8 @@ import {
sortTheThings
} from "./utils";
import * as semver from "semver";
import { readPreState } from "@changesets/pre";
import readChangesets from "@changesets/read";
(async () => {
let githubToken = process.env.GITHUB_TOKEN;
@@ -20,13 +22,13 @@ import * as semver from "semver";
return;
}
let repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
let branch = github.context.ref.replace("refs/heads/", "");
let preState = await readPreState(process.cwd());
let isInPreMode = preState !== undefined && preState.mode === "pre";
const octokit = new github.GitHub(githubToken);
let defaultBranchPromise = octokit.repos
.get(github.context.repo)
.then(x => x.data.default_branch);
console.log("setting git user");
await exec("git", [
"config",
@@ -47,28 +49,14 @@ import * as semver from "semver";
`machine github.com\nlogin github-actions[bot]\npassword ${githubToken}`
);
let defaultBranch = await defaultBranchPromise;
if (github.context.ref.replace("refs/heads/", "") !== defaultBranch) {
core.setFailed(
`The changesets action should only run on ${defaultBranch} but it's running on ${github.context.ref.replace(
"refs/heads/",
""
)}, please change your GitHub actions config to only run the Changesets action on ${defaultBranch}`
);
return;
let changesets = await readChangesets(process.cwd());
if (isInPreMode) {
let changesetsToFilter = new Set(preState.changesets);
changesets = changesets.filter(x => !changesetsToFilter.has(x.id));
}
let hasChangesets = fs
.readdirSync(`${process.cwd()}/.changeset`)
.some(
x =>
x !== "README.md" &&
x !== "config.js" &&
x !== "config.json" &&
!x.endsWith(".js")
);
console.log(fs.readdirSync(`${process.cwd()}/.changeset`));
let hasChangesets = changesets.length !== 0;
let publishScript = core.getInput("publish");
@@ -99,8 +87,9 @@ import * as semver from "semver";
publishCommand,
publishArgs
);
await exec("git", ["pull", "origin", branch]);
await exec("git", ["push", "origin", `HEAD:${defaultBranch}`, "--tags"]);
await exec("git", ["push", "origin", `HEAD:${branch}`, "--tags"]);
let newTagRegex = /New tag:\s+(@[^/]+\/[^@]+|[^/]+)@([^\s]+)/;
@@ -161,16 +150,15 @@ import * as semver from "semver";
}
if (hasChangesets) {
let { stderr } = await execWithOutput(
"git",
["checkout", "changeset-release"],
{ ignoreReturnCode: true }
);
let versionBranch = `changeset-release/${branch}`;
let { stderr } = await execWithOutput("git", ["checkout", versionBranch], {
ignoreReturnCode: true
});
let isCreatingChangesetReleaseBranch = !stderr
.toString()
.includes("Switched to a new branch 'changeset-release'");
.includes(`Switched to a new branch '${versionBranch}'`);
if (isCreatingChangesetReleaseBranch) {
await exec("git", ["checkout", "-b", "changeset-release"]);
await exec("git", ["checkout", "-b", versionBranch]);
}
await exec("git", ["reset", "--hard", github.context.sha]);
@@ -184,7 +172,7 @@ import * as semver from "semver";
"./node_modules/.bin/changeset",
cmd
]);
let searchQuery = `repo:${repo}+state:open+head:changeset-release+base:${defaultBranch}`;
let searchQuery = `repo:${repo}+state:open+head:${versionBranch}+base:${branch}`;
let searchResultPromise = octokit.search.issuesAndPullRequests({
q: searchQuery
});
@@ -196,8 +184,18 @@ import * as semver from "semver";
publishScript
? `the packages will be published to npm automatically`
: `publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing)`
}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${defaultBranch}, this PR will be updated.
}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${branch}, this PR will be updated.
${
isInPreMode
? `
⚠️⚠️⚠️⚠️⚠️⚠️
\`${branch}\` is currently in **pre mode** so this branch has prereleases rather than normal releases. If you want to exit prereleases, run \`changeset pre exit\` on \`${branch}\`.
⚠️⚠️⚠️⚠️⚠️⚠️
`
: ""
}
# Releases
` +
(await Promise.all(
@@ -228,14 +226,14 @@ import * as semver from "semver";
})();
await exec("git", ["add", "."]);
await exec("git", ["commit", "-m", "Version Packages"]);
await exec("git", ["push", "origin", "changeset-release", "--force"]);
await exec("git", ["push", "origin", versionBranch, "--force"]);
let searchResult = await searchResultPromise;
console.log(JSON.stringify(searchResult.data, null, 2));
if (searchResult.data.items.length === 0) {
console.log("creating pull request");
await octokit.pulls.create({
base: defaultBranch,
head: "changeset-release",
base: branch,
head: versionBranch,
title: "Version Packages",
body: await prBodyPromise,
...github.context.repo