Skip to content

Commit

Permalink
chore: Automate release creation
Browse files Browse the repository at this point in the history
Resolves #1535
  • Loading branch information
Gerrit0 committed Mar 15, 2021
1 parent 876c0f9 commit 9a627d8
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/publish.yml
Expand Up @@ -10,6 +10,8 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 0
- id: check
uses: EndBug/version-check@v1
with:
Expand All @@ -30,3 +32,11 @@ jobs:
- name: Publish
if: steps.check.outputs.changed == 'true'
run: npm publish
- name: Generate Release
if: steps.check.outputs.changed == 'true'
run: |
git config user.email "typedoc@gerritbirkeland.com"
git config user.name "TypeDoc Bot"
node scripts/create_release.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -18,3 +18,7 @@ tmp

src/test/renderer/specs/assets/*
src/test/renderer/specs/specs.json

# This is maintained in the typedoc-site repo, but might be created here
# if someone is experimenting with the generate_changelog script.
CHANGELOG.md
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -7,7 +7,7 @@ Documentation generator for TypeScript projects.

## Documentation

For more detailed documentation, and TypeDoc documentation rendered with TypeDoc, see https://typedoc.org.
For more detailed documentation, the changelog, and TypeDoc documentation rendered with TypeDoc, see https://typedoc.org.

## Installation

Expand Down
98 changes: 98 additions & 0 deletions scripts/create_release.js
@@ -0,0 +1,98 @@
// @ts-check

const cp = require("child_process");
const { join } = require("path");
const https = require("https");

const REMOTE = "origin";
const REPO = "TypeStrong/typedoc";

/**
* @param {string} cmd
* @returns {Promise<string>}
*/
function exec(cmd) {
return new Promise((resolve, reject) => {
cp.exec(cmd, { encoding: "utf-8" }, (err, stdout, stderr) => {
if (err) return reject(err);

if (stderr.trim().length) {
return reject(new Error(stderr));
}

resolve(stdout.trim());
});
});
}

async function createGitHubRelease(args) {
const data = JSON.stringify(args);

const options = {
hostname: "api.github.com",
path: `/repos/${REPO}/releases`,
method: "POST",
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github.v3+json",
"Content-Type": "application/json",
"Content-Length": data.length,
"User-Agent": "Node",
},
};

return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
if (res.statusCode !== 201) {
reject(new Error(res.statusMessage || "Unknown status"));
}

const result = [];
res.on("data", (d) => result.push(d.toString("utf-8")));
res.on("close", () => resolve(result.join("")));
});

req.on("error", reject);
req.write(data);
req.end();
});
}

async function main() {
const lastTag = await exec("git describe --tags --abbrev=0");
const currentVersion = `v${
require(join(__dirname, "..", "package.json")).version
}`;

if (lastTag == currentVersion) {
console.log("No version change, not publishing.");
return;
}

console.log(`Creating release ${currentVersion}`);

console.log("Creating tag...");
// Delete the tag if it exists already.
await exec(`git tag -d ${currentVersion}`).catch(() => void 0);
await exec(`git tag ${currentVersion}`);
await exec(
`git push ${REMOTE} refs/tags/${currentVersion} --quiet --force`
);

console.log("Creating release...");
const changelog = await exec(
`node ${join(__dirname, "generate_changelog.js")} ${lastTag} - github`
);
await createGitHubRelease({
tag_name: currentVersion,
name: currentVersion,
body: changelog,
});

console.log("OK");
}

main().catch((err) => {
console.error(err);
process.exitCode = 1;
});

0 comments on commit 9a627d8

Please sign in to comment.