Skip to content

Commit

Permalink
Use octokit instead of restling
Browse files Browse the repository at this point in the history
This also refactors the code so that we can inject the octokit
dependency.
  • Loading branch information
lo1tuma committed Mar 6, 2018
1 parent e260aab commit fa54ad2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 64 deletions.
12 changes: 9 additions & 3 deletions bin/pr-log.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node

import program from 'commander';
import createGithubClient from '@octokit/rest';
import config from '../../package.json';
import cli from '../cli';
import createCliAgent from '../cli';

program
.version(config.version)
Expand All @@ -11,8 +12,13 @@ program
.parse(process.argv);

const options = { sloppy: program.sloppy };
cli
.run(program.args[0], options)
const dependencies = {
githubClient: createGithubClient()
};
const cliAgent = createCliAgent(dependencies);

cliAgent
.run(program.args[0], options, dependencies)
.catch((error) => {
// eslint-disable-next-line no-console, no-warning-comments
console.error(error);
Expand Down
48 changes: 25 additions & 23 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@ 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: 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');
const prLogConfig = packageConfig['pr-log'];
const validLabels = getValidLabels(prLogConfig);

if (!newVersionNumber) {
throw new Error('version-number not specified');
}
export default function createCliAgent(dependencies) {
async function generateChangelog(options, githubRepo, validLabels, newVersionNumber) {
const preconditions = options.sloppy ? true : await ensureCleanLocalGitState(githubRepo);

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

const changelog = await generateChangelog(options, githubRepo, validLabels, newVersionNumber);
await prependFile(changelogPath, changelog);
return stripTrailingEmptyLine(changelog);
}
};

return {
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');
const prLogConfig = packageConfig['pr-log'];
const validLabels = getValidLabels(prLogConfig);

if (!newVersionNumber) {
throw new Error('version-number not specified');
}

const changelog = await generateChangelog(options, githubRepo, validLabels, newVersionNumber);
await prependFile(changelogPath, changelog);
}
};
}
9 changes: 4 additions & 5 deletions lib/getPullRequestLabel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import rest from 'restling';

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

const { data: labels } = await rest.get(url);
const [ owner, repo ] = githubRepo.split('/');
const params = { owner, repo, number: pullRequestId };
const { data: labels } = await githubClient.issues.getIssueLabels(params);

const listOfLabels = validLabelNames.join(', ');
const filteredLabels = labels.filter((label) => {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
"author": "Mathias Schreck <schreck.mathias@gmail.com>",
"license": "MIT",
"dependencies": {
"@octokit/rest": "^14.0.9",
"bluebird": "3.5.1",
"commander": "2.14.1",
"git-promise": "0.3.1",
"git-url-parse": "8.1.0",
"install": "^0.10.4",
"moment": "2.20.1",
"npm": "^5.7.1",
"parse-github-repo-url": "1.4.1",
"prepend": "1.0.2",
"ramda": "0.25.0",
"restling": "0.9.1",
"semver": "^5.3.0",
"util.promisify": "^1.0.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 @@ -32,7 +32,7 @@ describe('CLI', function () {
prepend: prependFile
};

cli = proxyquire('../../../lib/cli', requireStubs).default;
cli = proxyquire('../../../lib/cli', requireStubs).default({});
});

afterEach(function () {
Expand Down
57 changes: 26 additions & 31 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 rest from 'restling';
import getPullRequestLabel from '../../../lib/getPullRequestLabel';
import defaultValidLabels from '../../../lib/validLabels';

Expand All @@ -11,67 +10,63 @@ const expect = chai.expect;
chai.use(sinonChai);
chai.use(chaiAsPromised);

function createGithubClient(labels = []) {
return {
issues: {
getIssueLabels: sinon.stub().resolves({ data: labels })
}
};
}

describe('getPullRequestLabel', function () {
const anyRepo = 'any/repo';
const anyPullRequestId = 123;
const response = {};

let getStub;

beforeEach(function () {
response.data = [ { name: 'bug' } ];

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

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

it('should request the correct URL', async function () {
const expectedUrl = `https://api.github.com/repos/${anyRepo}/issues/${anyPullRequestId}/labels`;
it('should request the the labels for the correct repo and pull request', async function () {
const githubClient = createGithubClient([ { name: 'bug' } ]);

await getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId);
await getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId, { githubClient });

expect(getStub).to.have.been.calledOnce;
expect(getStub).to.have.been.calledWith(expectedUrl);
expect(githubClient.issues.getIssueLabels).to.have.been.calledOnce;
expect(githubClient.issues.getIssueLabels)
.to.have.been.calledWithExactly({ owner: 'any', repo: 'repo', number: 123 });
});

it('should fulfill with the correct label name', function () {
const githubClient = createGithubClient([ { name: 'bug' } ]);

const expectedLabelName = 'bug';

return expect(getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId))
return expect(getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId, { githubClient }))
.to.become(expectedLabelName);
});

it('should use custom labels when provided', function () {
const githubClient = createGithubClient([ { name: 'addons' } ]);

const expectedLabelName = 'addons';
const customValidLabels = {
addons: 'Addons'
};
response.data = [ { name: 'addons' } ];
const customValidLabels = { addons: 'Addons' };

return expect(getPullRequestLabel(anyRepo, customValidLabels, anyPullRequestId))
return expect(getPullRequestLabel(anyRepo, customValidLabels, anyPullRequestId, { githubClient }))
.to.become(expectedLabelName);
});

it('should reject if the pull request doesn’t have one valid label', function () {
const githubClient = createGithubClient([]);

// eslint-disable-next-line max-len
const expectedErrorMessage = 'Pull Request #123 has no label of bug, upgrade, documentation, feature, enhancement, build, breaking';

response.data = [];

return expect(getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId))
return expect(getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId, { githubClient }))
.to.be.rejectedWith(expectedErrorMessage);
});

it('should reject if the pull request has more than one valid label', function () {
const githubClient = createGithubClient([ { name: 'bug' }, { name: 'documentation' } ]);
// eslint-disable-next-line max-len
const expectedErrorMessage = 'Pull Request #123 has multiple labels of bug, upgrade, documentation, feature, enhancement, build, breaking';

response.data = [ { name: 'bug' }, { name: 'documentation' } ];

return expect(getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId))
return expect(getPullRequestLabel(anyRepo, defaultValidLabels, anyPullRequestId, { githubClient }))
.to.be.rejectedWith(expectedErrorMessage);
});
});

0 comments on commit fa54ad2

Please sign in to comment.