Skip to content

Commit

Permalink
Use async/await instead of bluebird
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Mar 6, 2018
1 parent 32d8b11 commit 5efb320
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 52 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},

"parserOptions": {
"ecmaVersion": "2017",
"ecmaFeatures": {
"globalReturn": false
}
Expand Down
6 changes: 5 additions & 1 deletion bin/pr-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ program
const options = { sloppy: program.sloppy };
cli
.run(program.args[0], options)
.done();
.catch((error) => {
// eslint-disable-next-line no-console, no-warning-comments
console.error(error);
process.exitCode = 1;
});
26 changes: 14 additions & 12 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path';
import prepend from 'prepend';
import BluebirdPromise from 'bluebird';
import promisify from 'util.promisify';
import ensureCleanLocalGitState from './ensureCleanLocalGitState';
import getMergedPullRequests from './getMergedPullRequests';
import createChangelog from './createChangelog';
import getGithubRepo from './getGithubRepo';
import defaultValidLabels from './validLabels';

const prependFile = BluebirdPromise.promisify(prepend);
const prependFile = promisify(prepend);

function stripTrailingEmptyLine(text) {
if (text.lastIndexOf('\n\n') === text.length - 2) {
Expand All @@ -17,11 +17,20 @@ function stripTrailingEmptyLine(text) {
return text;
}

async function generateChangelog(options, githubRepo, validLabels, newVersionNumber) {
const preconditions = options.sloppy ? true : await ensureCleanLocalGitState(githubRepo);

const pullRequests = await getMergedPullRequests(githubRepo, validLabels, preconditions);
const changelog = await createChangelog(newVersionNumber, validLabels, pullRequests);

return stripTrailingEmptyLine(changelog);
}

function getValidLabels(prLogConfig) {
return prLogConfig && prLogConfig.validLabels || defaultValidLabels;
}
export default {
run: (newVersionNumber, options) => {
run: async (newVersionNumber, options) => {
const packageConfig = require(path.join(process.cwd(), 'package.json'));
const githubRepo = getGithubRepo(packageConfig.repository.url);
const changelogPath = path.join(process.cwd(), 'CHANGELOG.md');
Expand All @@ -32,14 +41,7 @@ export default {
throw new Error('version-number not specified');
}

const preconditions = options.sloppy ?
BluebirdPromise.resolve(true) :
BluebirdPromise.try(ensureCleanLocalGitState.bind(null, githubRepo));

return preconditions
.then(getMergedPullRequests.bind(null, githubRepo, validLabels))
.then(createChangelog.bind(null, newVersionNumber, validLabels))
.then(stripTrailingEmptyLine)
.then(prependFile.bind(null, changelogPath));
const changelog = await generateChangelog(options, githubRepo, validLabels, newVersionNumber);
await prependFile(changelogPath, changelog);
}
};
15 changes: 8 additions & 7 deletions lib/ensureCleanLocalGitState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import git from 'git-promise';
import BluebirdPromise from 'bluebird';
import findRemoteAlias from './findRemoteAlias';

function ensureCleanLocalCopy() {
Expand Down Expand Up @@ -53,12 +52,14 @@ function ensureLocalIsEqualToRemote(remoteAlias) {
});
}

function ensureCleanLocalGitState(githubRepo) {
return BluebirdPromise.try(ensureCleanLocalCopy)
.then(ensureMasterBranch)
.then(findRemoteAlias.bind(null, githubRepo))
.tap(fetchRemote)
.then(ensureLocalIsEqualToRemote);
async function ensureCleanLocalGitState(githubRepo) {
await ensureCleanLocalCopy();
await ensureMasterBranch();

const remoteAlias = await findRemoteAlias(githubRepo);

await fetchRemote(remoteAlias);
await ensureLocalIsEqualToRemote(remoteAlias);
}

export default ensureCleanLocalGitState;
20 changes: 9 additions & 11 deletions lib/getMergedPullRequests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import git from 'git-promise';
import BluebirdPromise from 'bluebird';
import semver from 'semver';
import getPullRequestLabel from './getPullRequestLabel';

Expand Down Expand Up @@ -27,18 +26,17 @@ function getPullRequests(fromTag) {
}

function extendWithLabel(githubRepo, validLabels, pullRequests) {
const promises = pullRequests.map((pullRequest) => {
return getPullRequestLabel(githubRepo, validLabels, pullRequest.id)
.then((label) => {
return {
id: pullRequest.id,
title: pullRequest.title,
label
};
});
const promises = pullRequests.map(async (pullRequest) => {
const label = await getPullRequestLabel(githubRepo, validLabels, pullRequest.id);

return {
id: pullRequest.id,
title: pullRequest.title,
label
};
});

return BluebirdPromise.all(promises);
return Promise.all(promises);
}

function getMergedPullRequests(githubRepo, validLabels) {
Expand Down
28 changes: 13 additions & 15 deletions lib/getPullRequestLabel.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import rest from 'restling';

function getPullRequestLabel(githubRepo, validLabels, pullRequestId) {
async function getPullRequestLabel(githubRepo, validLabels, pullRequestId) {
const validLabelNames = Object.keys(validLabels);
const url = `https://api.github.com/repos/${githubRepo}/issues/${pullRequestId}/labels`;

return rest.get(url)
.get('data')
.then((labels) => {
const listOfLabels = validLabelNames.join(', ');
const filteredLabels = labels.filter((label) => {
return validLabelNames.indexOf(label.name) !== -1;
});
const { data: labels } = await rest.get(url);

if (filteredLabels.length > 1) {
throw new Error(`Pull Request #${pullRequestId} has multiple labels of ${listOfLabels}`);
} else if (filteredLabels.length === 0) {
throw new Error(`Pull Request #${pullRequestId} has no label of ${listOfLabels}`);
}
const listOfLabels = validLabelNames.join(', ');
const filteredLabels = labels.filter((label) => {
return validLabelNames.indexOf(label.name) !== -1;
});

return filteredLabels[0].name;
});
if (filteredLabels.length > 1) {
throw new Error(`Pull Request #${pullRequestId} has multiple labels of ${listOfLabels}`);
} else if (filteredLabels.length === 0) {
throw new Error(`Pull Request #${pullRequestId} has no label of ${listOfLabels}`);
}

return filteredLabels[0].name;
}

export default getPullRequestLabel;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"prepend": "1.0.2",
"ramda": "0.25.0",
"restling": "0.9.1",
"semver": "^5.3.0"
"semver": "^5.3.0",
"util.promisify": "^1.0.0"
},
"devDependencies": {
"babel-cli": "6.26.0",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/cliSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('CLI', function () {
});

it('should throw if no version number was specified', function () {
expect(cli.run).to.throw('version-number not specified');
expect(cli.run()).to.be.rejectedWith('version-number not specified');
});

it('should throw if the repository is dirty', function () {
Expand Down
7 changes: 3 additions & 4 deletions test/unit/lib/getPullRequestLabelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import chai from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import chaiAsPromised from 'chai-as-promised';
import BluebirdPromise from 'bluebird';
import rest from 'restling';
import getPullRequestLabel from '../../../lib/getPullRequestLabel';
import defaultValidLabels from '../../../lib/validLabels';
Expand All @@ -22,17 +21,17 @@ describe('getPullRequestLabel', function () {
beforeEach(function () {
response.data = [ { name: 'bug' } ];

getStub = sinon.stub(rest, 'get').usingPromise(BluebirdPromise).resolves(response);
getStub = sinon.stub(rest, 'get').resolves(response);
});

afterEach(function () {
getStub.restore();
});

it('should request the correct URL', function () {
it('should request the correct URL', async function () {
const expectedUrl = `https://api.github.com/repos/${anyRepo}/issues/${anyPullRequestId}/labels`;

getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId);
await getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId);

expect(getStub).to.have.been.calledOnce;
expect(getStub).to.have.been.calledWith(expectedUrl);
Expand Down

0 comments on commit 5efb320

Please sign in to comment.