Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions .github/scripts/validate-pr-title.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const nlp = require('compromise')
const title = process.env.PR_TITLE || ''
const nlp = require('compromise');
const title = process.env.PR_TITLE || '';

let isValidTitle = true
let isValidTitle = true;

function logSuccess(message) {
console.log(`βœ… ${message}`)
console.log(`βœ… ${message}`);
}

function logFailure(message) {
isValidTitle = false
console.error(`❌ ${message}`)
isValidTitle = false;
console.error(`❌ ${message}`);
}

function capitalized(string) {
Expand All @@ -19,48 +19,48 @@ function capitalized(string) {

// Rule 1: PR title must not be empty
if (title) {
logSuccess(`PR title is not empty`)
logSuccess(`PR title is not empty`);
} else {
logFailure(`PR title must not be empty`)
logFailure(`PR title must not be empty`);
}

// Rule 2: PR title must be 72 characters or less
if (title.length <= 72) {
logSuccess(`PR title is ${title.length} characters`)
logSuccess(`PR title is ${title.length} characters`);
} else {
logFailure(`PR title must be 72 characters or less (currently ${title.length} characters)`)
logFailure(`PR title must be 72 characters or less (currently ${title.length} characters)`);
}

// Rule 3: PR title must begin with a capital letter
if (/^[A-Z]/.test(title)) {
logSuccess(`PR title begins with a capital letter`)
logSuccess(`PR title begins with a capital letter`);
} else {
logFailure('PR title must begin with a capital letter')
logFailure('PR title must begin with a capital letter');
}

// Rule 4: PR title must end with a letter or number
if (/[A-Za-z0-9]$/.test(title)) {
logSuccess(`PR title ends with a letter or number`)
logSuccess(`PR title ends with a letter or number`);
} else {
logFailure('PR title must end with a letter or number')
logFailure('PR title must end with a letter or number');
}

// Rule 5: PR title must be written in the imperative
const firstWord = title.split(' ')[0]
const firstWordLowercased = firstWord.toLowerCase()
const firstWordCapitalized = capitalized(firstWord)
const firstWordAsImperativeVerb = nlp(firstWord).verbs().toInfinitive().out('text')
const firstWordAsImperativeVerbLowercased = firstWordAsImperativeVerb.toLowerCase()
const firstWordAsImperativeVerbCapitalized = capitalized(firstWordAsImperativeVerb)
const firstWord = title.split(' ')[0];
const firstWordLowercased = firstWord.toLowerCase();
const firstWordCapitalized = capitalized(firstWord);
const firstWordAsImperativeVerb = nlp(firstWord).verbs().toInfinitive().out('text');
const firstWordAsImperativeVerbLowercased = firstWordAsImperativeVerb.toLowerCase();
const firstWordAsImperativeVerbCapitalized = capitalized(firstWordAsImperativeVerb);

if (firstWordLowercased === firstWordAsImperativeVerbLowercased) {
logSuccess(`PR title is written in the imperative`)
logSuccess(`PR title is written in the imperative`);
} else if (firstWordAsImperativeVerb) {
logFailure(`PR title must be written in the imperative ("${firstWordAsImperativeVerbCapitalized}" instead of "${firstWordCapitalized}")`)
logFailure(`PR title must be written in the imperative ("${firstWordAsImperativeVerbCapitalized}" instead of "${firstWordCapitalized}")`);
} else {
logFailure(`PR title must begin with a verb and be written in the imperative`)
logFailure(`PR title must begin with a verb and be written in the imperative`);
}

if (!isValidTitle) {
process.exit(1)
process.exit(1);
}