Skip to content

Commit

Permalink
refactor: update conventional-commits-parser and accomodate for chang…
Browse files Browse the repository at this point in the history
…e in behavior
  • Loading branch information
Mark Owsiak committed Oct 3, 2018
1 parent 4fdd017 commit 5bb4c5b
Show file tree
Hide file tree
Showing 45 changed files with 196 additions and 5,955 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "groupon/node4"
"extends": "groupon/node6"
}
8 changes: 4 additions & 4 deletions lib/git/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@

'use strict';

const commitParser = require('../../vendor/conventional-commits-parser');
const commitParser = require('conventional-commits-parser');
const debug = require('debug')('nlm:git:commits');
const _ = require('lodash');

const run = require('../run');

const SEPARATOR = '---nlm-split---';
const GIT_LOG_FORMAT = `--format=%H %P\n%B${SEPARATOR}`;
const PR_MERGE_PATTERN = /^Merge pull request #(\d+) from ([^/]+)\/([\S]+)/;
const PR_MERGE_PATTERN = /^Merge pull request #(\d+) from ([^\/]+)\/([\S]+)/;

function parseCommit(commit) {
const metaEndIdx = commit.indexOf('\n');
Expand All @@ -54,14 +54,14 @@ function parseCommit(commit) {
const parentSha = meta.shift() || null;

const data = commitParser.sync(message, {
issuePrefixes: ['#', 'https?://\\w[\\w.-]*[\\w/-]+?'],
issuePrefixes: ['#', 'https?://\\w[\\w.-]*[\\D/-]+'],
});
const prMatch = message.match(PR_MERGE_PATTERN);
if (prMatch) {
const prId = prMatch[1];
data.type = 'pr';
data.pullId = prId;
data.references.push({
Object.assign(data.references[0], {
action: 'Merges',
owner: prMatch[2],
repository: null,
Expand Down
2 changes: 1 addition & 1 deletion lib/github/setup-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const REQUIRED_LABELS = [
];

function findMissingLabels(labels) {
return _.reject(REQUIRED_LABELS, function exists(label) {
return _.reject(REQUIRED_LABELS, label => {
return _.find(labels, { name: label.name });
});
}
Expand Down
8 changes: 4 additions & 4 deletions lib/license/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const COMMENT_TYPES = {
getLicenseHeader: function getLicenseHeader(licenseText) {
const body = licenseText
.split('\n')
.map(function prefixLine(line) {
.map(line => {
return ` ${`* ${line}`.trim()}`;
})
.join('\n');
Expand Down Expand Up @@ -78,7 +78,7 @@ function collectFiles(cwd, whitelist, optionalExclude) {

return Bluebird.map(whitelist || ['.'], scanDirectory)
.then(_.flatten)
.map(function loadFile(relFilename) {
.map(relFilename => {
const filename = path.join(cwd, relFilename);
return Bluebird.props({
filename: filename,
Expand All @@ -103,7 +103,7 @@ function addMissingLicenseHeaders(licenseText, files) {
'.coffee': COMMENT_TYPES['.coffee'].getLicenseHeader(licenseText),
};
return files
.map(function buildLicenseHeader(file) {
.map(file => {
file.licenseHeader = licenseHeaders[path.extname(file.filename)];
return file;
})
Expand All @@ -114,7 +114,7 @@ function addMissingLicenseHeaders(licenseText, files) {
function getLicenseText(cwd) {
return readFileAsync(path.join(cwd, 'LICENSE'), 'utf8')
.then(_.trim)
.catch(function catchNotFound(error) {
.catch(error => {
if (error.code === 'ENOENT') {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ const _ = require('lodash');

module.exports = function run(command, args, options) {
debug(command, args, _.omit(options, 'env'));
return new Bluebird(function resolveExec(resolve, reject) {
return new Bluebird((resolve, reject) => {
function onExecDone(error, stdout) {
if (error) return reject(error);
resolve(stdout);
}

const child = childProcess.execFile(command, args, options, onExecDone);
child.stdout.on('data', function forwardStdOut(chunk) {
child.stdout.on('data', chunk => {
debug('stdout', chunk.toString().trim());
});
child.stderr.on('data', function forwardStdErr(chunk) {
child.stderr.on('data', chunk => {
debug('stderr', chunk.toString().trim());
});
});
Expand Down
24 changes: 11 additions & 13 deletions lib/steps/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ function addPullRequestCommits(pkg, commits, pr) {
github.pull.get(pr.pullId),
github.pull.commits(pr.pullId),
])
.spread(function expandCommitInfo(info, prCommits) {
.spread((info, prCommits) => {
pr.author = {
name: info.user.login,
href: info.user.html_url,
};
pr.href = info.html_url;
pr.title = info.title || info.header;
const shas = (pr.shas = _.map(prCommits, 'sha'));
pr.commits = commits.filter(function isPartOfPR(commit) {
pr.commits = commits.filter(commit => {
return shas.indexOf(commit.sha) !== -1;
});
})
.catch(function handle404(err) {
.catch(err => {
if (err.statusCode !== 404) throw err;
// If the PR doesn't exist, handle it gracefully.
pr.commits = pr.shas = null;
Expand All @@ -65,7 +65,7 @@ function addPullRequestCommits(pkg, commits, pr) {

function removePRCommits(commits, prs) {
const prShas = _.flatten(_.map(prs, 'shas'));
return _.filter(commits, function isNotInAnyPR(commit) {
return _.filter(commits, commit => {
return commit.type !== 'pr' && prShas.indexOf(commit.sha) === -1;
});
}
Expand All @@ -74,16 +74,14 @@ function extractBreakingChanges(commit) {
if (!commit.notes || !commit.notes.length) {
return [];
}
return _.filter(commit.notes, { title: 'BREAKING CHANGE' }).map(
function buildChangeNote(note) {
return { text: note.text, commit: commit };
}
);
return _.filter(commit.notes, { title: 'BREAKING CHANGE' }).map(note => {
return { text: note.text, commit: commit };
});
}

function removeInvalidPRs(prs) {
// Warning: We're doing something evil here and mutate the input array.
const filtered = prs.filter(function allCommitsFound(pr) {
const filtered = prs.filter(pr => {
return pr.shas && pr.shas.length === pr.commits.length;
});
prs.length = filtered.length;
Expand Down Expand Up @@ -152,7 +150,7 @@ function generateChangeLog(cwd, pkg, options) {
}

function formatPR(pr) {
const changes = pr.commits.map(formatCommit).map(function withDashes(line) {
const changes = pr.commits.map(formatCommit).map(line => {
return ` - ${line}`;
});

Expand All @@ -172,7 +170,7 @@ function generateChangeLog(cwd, pkg, options) {
const changes = prs
.map(formatPR)
.concat(orphans.map(formatCommit))
.map(function star(line) {
.map(line => {
return `* ${line}`;
});

Expand All @@ -184,7 +182,7 @@ function generateChangeLog(cwd, pkg, options) {
.then(_.partial(removePRCommits, commits, prs))
.then(formatCommits)
.then(prependBreakingChanges)
.then(function setChangelog(changelog) {
.then(changelog => {
options.changelog = changelog;
return changelog;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/steps/pending-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function normalizeReferences(meta, commit) {

function getPendingChanges(cwd, pkg, options) {
const meta = parseRepository(pkg.repository);
return getCommits(cwd, `v${pkg.version}`).then(function setCommits(commits) {
return getCommits(cwd, `v${pkg.version}`).then(commits => {
options.commits = commits.map(_.partial(normalizeReferences, meta));
});
}
Expand Down
8 changes: 4 additions & 4 deletions lib/steps/publish-to-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function checkPublishRequired(cwd, pkg, options) {
cwd: cwd,
env: options.npmEnv,
})
.then(function parseNpmList(content) {
.then(content => {
// If we get an empty response, we'll assume it was not found.
if (content.trim() === '') {
return 'publish';
Expand All @@ -87,7 +87,7 @@ function checkPublishRequired(cwd, pkg, options) {
}
return 'none';
})
.catch(function handle404(error) {
.catch(error => {
if (error.message.indexOf('ERR! 404') !== -1) {
return 'publish';
}
Expand Down Expand Up @@ -176,7 +176,7 @@ function publishToNPM(cwd, pkg, options) {
checkPublishRequired(cwd, pkg, options),
getCurrentCommit(cwd),
])
.spread(function checkAndPublish(publishRequired, currentCommit) {
.spread((publishRequired, currentCommit) => {
if (currentCommit !== `v${pkg.version}`) {
console.log(
'[nlm] Skipping publish, not a version commit:',
Expand Down Expand Up @@ -204,7 +204,7 @@ function publishToNPM(cwd, pkg, options) {
return null;
}
})
.finally(function removeTmpRcFile() {
.finally(() => {
fs.unlinkSync(rcFile);
});
}
Expand Down
28 changes: 13 additions & 15 deletions lib/steps/tag-pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,19 @@ function tagPullRequest(cwd, pkg, options) {
return null;
}
const github = Github.forRepository(pkg.repository);
return github.labels
.listByIssue(options.pr)
.then(function checkAndChangeLabels(labels) {
const releaseType = options.releaseType;
const name = `semver-${releaseType}`;
if (_.find(labels, { name: name })) {
debug('Already tagged with %j', name);
return null;
}
const newLabels = _.map(labels, 'name')
.filter(nonSemverTag)
.concat(name);
return github.labels.listByIssue(options.pr).then(labels => {
const releaseType = options.releaseType;
const name = `semver-${releaseType}`;
if (_.find(labels, { name: name })) {
debug('Already tagged with %j', name);
return null;
}
const newLabels = _.map(labels, 'name')
.filter(nonSemverTag)
.concat(name);

debug('Tagging %s', options.pr, newLabels);
return github.labels.setForIssue(options.pr, newLabels);
});
debug('Tagging %s', options.pr, newLabels);
return github.labels.setForIssue(options.pr, newLabels);
});
}
module.exports = tagPullRequest;
2 changes: 1 addition & 1 deletion lib/steps/version-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function createVersionCommit(cwd, pkg, options) {
return addFiles(cwd)
.then(_.partial(commit, cwd, `v${pkg.version}`))
.then(_.partial(getHEAD, cwd))
.then(function setVersionCommitSha(output) {
.then(output => {
options.versionCommitSha = output.trim();
return options.versionCommitSha;
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"bluebird": "^3.3.3",
"conventional-commits-parser": "^3.0.0",
"debug": "^2.2.0",
"glob": "^6.0.1",
"gofer": "^3.2.0",
Expand Down
18 changes: 9 additions & 9 deletions test/commands/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,43 @@ const withFixture = require('../fixture');

const CLI_PATH = require.resolve('../../lib/cli');

describe('nlm verify', function() {
describe('in non-git directory', function() {
describe('nlm verify', () => {
describe('in non-git directory', () => {
const dirname = withFixture('non-git');
const output = {};

before(function() {
before(() => {
return run(process.execPath, [CLI_PATH, 'verify'], {
cwd: dirname,
env: _.assign({}, process.env, {
GH_TOKEN: '',
}),
}).then(function(stdout) {
}).then(stdout => {
output.stdout = stdout;
});
});

it('ignores directories that are not git repos', function() {
it('ignores directories that are not git repos', () => {
assert.equal('', output.stdout);
});
});

describe('in git directory', function() {
describe('in git directory', () => {
const dirname = withFixture('released');
const output = {};

before(function() {
before(() => {
return run(process.execPath, [CLI_PATH, 'verify'], {
cwd: dirname,
env: _.assign({}, process.env, {
GH_TOKEN: '',
}),
}).then(function(stdout) {
}).then(stdout => {
output.stdout = stdout;
});
});

it('reports the change type', function() {
it('reports the change type', () => {
assert.include('Changes are "none"', output.stdout);
});
});
Expand Down
10 changes: 5 additions & 5 deletions test/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ function withFixture(name) {
const dirname = path.join(__dirname, '..', 'tmp', name);
const script = path.join(__dirname, 'fixtures', name);

before('remove fixture directory', function rmDir(done) {
before('remove fixture directory', done => {
execFile('rm', ['-rf', dirname], done);
});

before('create fixture directory', function createDir(done) {
before('create fixture directory', done => {
execFile('mkdir', ['-p', dirname], done);
});

before('running fixture setup', function runFixture(done) {
before('running fixture setup', done => {
execFile(
script,
[script],
Expand All @@ -61,7 +61,7 @@ function withFixture(name) {
GIT_COMMITTER_EMAIL: 'rdev@example.com',
},
},
function logErrorDetails(error, stdout, stderr) {
(error, stdout, stderr) => {
if (error) {
process.stdout.write(`${stdout}\n`);
process.stderr.write(`${stderr}\n`);
Expand All @@ -71,7 +71,7 @@ function withFixture(name) {
);
});

after('remove fixture directory', function removeFixture(done) {
after('remove fixture directory', done => {
execFile('rm', ['-rf', dirname], done);
});

Expand Down
Loading

0 comments on commit 5bb4c5b

Please sign in to comment.