Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Created November 9, 2020 17:10
Show Gist options
  • Save jgcmarins/3bfd75b7978acb7d7b1c97a8564d2e64 to your computer and use it in GitHub Desktop.
Save jgcmarins/3bfd75b7978acb7d7b1c97a8564d2e64 to your computer and use it in GitHub Desktop.
Setup to generate git tag and GitHub release with changelog based on commit messages
#!/bin/env node
// @ts-check
import fs from 'fs';
import { exec as execCb } from 'child_process';
import util from 'util';
import moment from 'moment';
import git from 'simple-git/promise';
import changelog from 'generate-changelog';
import idx from 'idx';
const argv = require('minimist')(process.argv.slice(1));
const { Octokit } = require('@octokit/rest');
const exec = util.promisify(execCb);
const owner = 'foo';
const repo = 'bar';
const createPullRequest = async (branchName, tag) => {
if (!process.env.GITHUB_TOKEN) {
return;
}
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
const now = moment().format('YYYY-MM-DD');
// https://octokit.github.io/rest.js/#api-Repos-getReleases
// https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
const latestReleases = await octokit.repos.listReleases({ owner, repo, per_page: 1 });
const latestReleaseTag =
latestReleases && latestReleases.data && latestReleases.data.length ? latestReleases.data[0].tag_name : 'master';
await octokit.pulls.create({
owner,
repo,
title: `Deploy Production - ${tag} - ${now}`,
head: branchName,
base: 'master',
body: `https://github.com/${owner}/${repo}/compare/${latestReleaseTag}...master`,
});
};
(async () => {
try {
await git().tags();
const currentChangelog = fs.readFileSync('./CHANGELOG.md');
const changelogContent = await changelog.generate({
major: argv.major,
minor: argv.minor,
patch: argv.patch,
});
const rxVersion = /\d+\.\d+\.\d+/;
const newVersion = argv.version || `${idx(changelogContent.match(rxVersion), _ => _[0])}`;
const newChangelogContent = changelogContent.replace(rxVersion, newVersion) + currentChangelog;
fs.writeFileSync('./CHANGELOG.md', newChangelogContent);
await exec(`npm version --no-git-tag-version ${newVersion}`);
await exec(`lerna version --no-git-tag-version --yes ${newVersion}`);
const tag = `v${newVersion}`;
const today = new Date();
const branchName = `feature-production/${today.getFullYear()}${today.getMonth() + 1}${today.getDate()}`;
await git().checkout(['-B', branchName]);
await git().add(['package.json', 'lerna.json', 'CHANGELOG.md', 'packages/*/package.json']);
await git().commit(`build(change-log): ${tag}`, [], '-n');
await git().addAnnotatedTag(`${tag}`, `build(tag): ${tag}`);
await git().push(['--follow-tags', '-u', 'origin', branchName]);
await createPullRequest(branchName, tag);
} catch (err) {
// eslint-disable-next-line
console.log('err:', err);
}
})();
module.exports = {
rules: {
'body-leading-blank': [1, 'always'],
'footer-leading-blank': [1, 'always'],
'header-max-length': [2, 'always', 100],
'scope-case': [2, 'always', 'lower-case'],
'subject-case': [2, 'never', ['sentence-case', 'start-case', 'pascal-case', 'upper-case']],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'type-enum': [
2,
'always',
[
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test',
'migration',
'type',
'lint',
],
],
},
};
module.exports = {
hooks: {
'pre-commit': 'lint-staged',
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
},
};
@jgcmarins
Copy link
Author

GitHub Release

Screen Shot 2020-11-09 at 14 06 21

git diff available on "4 commits" link

Screen Shot 2020-11-09 at 14 07 00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment