Skip to content

Commit

Permalink
Display more useful errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lhorie authored and fusion-bot[bot] committed Jan 5, 2018
1 parent 3659727 commit 7df9bf7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
6 changes: 3 additions & 3 deletions __tests__/validate-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const t = require('assert');

const isValidTitle = require('../validate-title.js');
const validateTitle = require('../validate-title.js');

test('valid commits pass', () => {
const valid = [
Expand Down Expand Up @@ -42,7 +42,7 @@ test('valid commits pass', () => {
];

valid.forEach(msg => {
t.equal(isValidTitle(msg), true, `"${msg}", was expected to be valid`);
t.equal(validateTitle(msg).length, 0, `"${msg}", was expected to be valid`);
});
});

Expand All @@ -65,6 +65,6 @@ test('invalid commits fail', () => {
];

invalid.forEach(msg => {
t.equal(isValidTitle(msg), false);
t.notEqual(validateTitle(msg).length, 0);
});
});
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* LICENSE file in the root directory of this source tree.
*/

const isValidTitle = require('./validate-title.js');
const validateTitle = require('./validate-title.js');

module.exports = robot => {
robot.on('pull_request.opened', check);
Expand All @@ -13,23 +13,24 @@ module.exports = robot => {

async function check(context) {
const pr = context.payload.pull_request;
const passed = isValidTitle(pr.title);
setStatus(context, passed);
const errors = validateTitle(pr.title);
setStatus(context, errors);
}
};

function setStatus(context, passing) {
function setStatus(context, errors) {
const {github} = context;

const status = passing
? {
state: 'success',
description: 'PR title is valid',
}
: {
state: 'failure',
description: 'PR title is invalid',
};
const status =
errors.length === 0
? {
state: 'success',
description: 'PR title is valid',
}
: {
state: 'failure',
description: 'PR title is invalid:\n' + errors.join('\n'),
};

github.repos.createStatus(
context.repo({
Expand Down
47 changes: 27 additions & 20 deletions validate-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@

const nlp = require('compromise');

module.exports = function isValidTitle(text) {
module.exports = function validateTitle(text) {
const parsed = nlp(text);
return isValidSentence(parsed) && firstTermIsValidVerb(parsed);
return [...validateSentence(parsed), ...validateFirstTerm(parsed)];
};

// Validate text is a single sentence with no end punctuation
function isValidSentence(parsed) {
function validateSentence(parsed) {
const sentences = parsed.sentences();
// must be one and only one sentence
if (sentences.length !== 1) {
return false;
}
// must be at least two words long
if (parsed.terms().length < 2) {
return false;
}
// must not have any end punctuation
if (sentences.list[0].endPunctuation() !== null) {
return false;
}
return true;
return [
!isSingleSentence(sentences) ? 'Must be one and only one sentence.' : null,
!isNotTooShort(parsed) ? 'Must be at least two words long.' : null,
!isNotPunctuated(sentences) ? 'Must not have any end punctuation.' : null,
].filter(Boolean);
}

// Validate first word is a capitalized imperative mood verb
function validateFirstTerm(parsed) {
const term = parsed.terms().data()[0];
return [
!isValidVerb(term) ? 'First word must be imperative verb.' : null,
!isCapitalized(term) ? 'First word must be capitalized.' : null,
].filter(Boolean);
}

// The following verbs starting with `re` are categorized as singular nouns
Expand All @@ -47,10 +48,16 @@ const nounWhitelist = new Set([
'upgrade',
]);

// Validate first word is a capitalized imperative mood verb
function firstTermIsValidVerb(parsed) {
const term = parsed.terms().data()[0];
return isValidVerb(term) && isCapitalized(term);
function isSingleSentence(sentences) {
return sentences.length === 1;
}

function isNotTooShort(parsed) {
return parsed.terms().length >= 2;
}

function isNotPunctuated(sentences) {
return sentences.list[0].endPunctuation() === null;
}

function isValidVerb(term) {
Expand Down

0 comments on commit 7df9bf7

Please sign in to comment.