|
| 1 | +const nlp = require('compromise'); |
| 2 | +const title = process.env.PR_TITLE || ''; |
| 3 | + |
| 4 | +let isValidTitle = true; |
| 5 | + |
| 6 | +function logSuccess(message) { |
| 7 | + console.log(`✅ ${message}`); |
| 8 | +} |
| 9 | + |
| 10 | +function logFailure(message) { |
| 11 | + isValidTitle = false; |
| 12 | + console.error(`❌ ${message}`); |
| 13 | +} |
| 14 | + |
| 15 | +function capitalized(string) { |
| 16 | + if (!string) return ''; |
| 17 | + return string[0].toUpperCase() + string.substring(1); |
| 18 | +} |
| 19 | + |
| 20 | +// Rule 1: PR title must not be empty |
| 21 | +if (title) { |
| 22 | + logSuccess(`PR title is not empty`); |
| 23 | +} else { |
| 24 | + logFailure(`PR title must not be empty`); |
| 25 | +} |
| 26 | + |
| 27 | +// Rule 2: PR title must be 72 characters or less |
| 28 | +if (title.length <= 72) { |
| 29 | + logSuccess(`PR title is ${title.length} characters`); |
| 30 | +} else { |
| 31 | + logFailure(`PR title must be 72 characters or less (currently ${title.length} characters)`); |
| 32 | +} |
| 33 | + |
| 34 | +// Rule 3: PR title must begin with a capital letter |
| 35 | +if (/^[A-Z]/.test(title)) { |
| 36 | + logSuccess(`PR title begins with a capital letter`); |
| 37 | +} else { |
| 38 | + logFailure('PR title must begin with a capital letter'); |
| 39 | +} |
| 40 | + |
| 41 | +// Rule 4: PR title must end with a letter or number |
| 42 | +if (/[A-Za-z0-9]$/.test(title)) { |
| 43 | + logSuccess(`PR title ends with a letter or number`); |
| 44 | +} else { |
| 45 | + logFailure('PR title must end with a letter or number'); |
| 46 | +} |
| 47 | + |
| 48 | +// Rule 5: PR title must be written in the imperative |
| 49 | +const firstWord = title.split(' ')[0]; |
| 50 | +const firstWordLowercased = firstWord.toLowerCase(); |
| 51 | +const firstWordCapitalized = capitalized(firstWord); |
| 52 | +const firstWordAsImperativeVerb = nlp(firstWord).verbs().toInfinitive().out('text'); |
| 53 | +const firstWordAsImperativeVerbLowercased = firstWordAsImperativeVerb.toLowerCase(); |
| 54 | +const firstWordAsImperativeVerbCapitalized = capitalized(firstWordAsImperativeVerb); |
| 55 | + |
| 56 | +if (firstWordLowercased === firstWordAsImperativeVerbLowercased) { |
| 57 | + logSuccess(`PR title is written in the imperative`); |
| 58 | +} else if (firstWordAsImperativeVerb) { |
| 59 | + logFailure(`PR title must be written in the imperative ("${firstWordAsImperativeVerbCapitalized}" instead of "${firstWordCapitalized}")`); |
| 60 | +} else { |
| 61 | + logFailure(`PR title must begin with a verb and be written in the imperative`); |
| 62 | +} |
| 63 | + |
| 64 | +if (!isValidTitle) { |
| 65 | + process.exit(1); |
| 66 | +} |
0 commit comments