-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
154 lines (122 loc) · 4.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @fileoverview Validates that commit messages are formatted according
* to the guidelines.
* @author Gyandeep Singh
*/
"use strict";
//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------
const commentMessage = require("./createMessage");
const { TAG_LABELS } = require("./util");
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
const TAG_REGEX = /^(?:feat|build|chore|docs|fix|refactor|test|ci|perf)!?: /u;
const TAG_SPACE_REGEX = /^(?:[a-z]+!?: )/u;
const LOWERCASE_TAG_REGEX = /^[a-z]/u;
const MESSAGE_LENGTH_LIMIT = 72;
const EXCLUDED_REPOSITORY_NAMES = new Set([
"eslint.github.io",
"tsc-meetings"
]);
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
/**
* Apply different checks on the commit message
* @param {string} message commit message
* @returns {boolean} `true` if the commit message is valid
* @private
*/
function getCommitMessageErrors(message) {
const commitTitle = message.split(/\r?\n/u)[0];
const errors = [];
if (message.startsWith("Revert \"")) {
return errors;
}
// First, check tag and summary length
if (!TAG_REGEX.test(commitTitle)) {
errors.push("NON_MATCHED_TAG");
}
// Check if there is any whitespace after the <Tag>:
if (!TAG_SPACE_REGEX.test(commitTitle)) {
errors.push("SPACE_AFTER_TAG_COLON");
}
if (!LOWERCASE_TAG_REGEX.test(commitTitle)) {
errors.push("NON_LOWERCASE_FIRST_LETTER_TAG");
}
if (!(commitTitle.length <= MESSAGE_LENGTH_LIMIT)) {
errors.push("LONG_MESSAGE");
}
return errors;
}
/**
* Apply different checks on the commit message
* @param {string} message commit message
* @returns {Array<string>} The labels to add to the PR.
* @private
*/
function getCommitMessageLabels(message) {
const commitTitle = message.split(/\r?\n/u)[0];
const [tag] = commitTitle.match(TAG_REGEX) || [""];
return TAG_LABELS.get(tag.trim());
}
/**
* If the first commit message is not legal then it adds a comment
* @param {Object} context context given by the probot
* @returns {Promise<void>} promise
* @private
*/
async function processCommitMessage(context) {
/*
* We care about the default commit message that will appear when the
* PR is merged. If the PR has exactly one commit, this is the commit
* message of that commit. If the PR has more than one commit, this
* is the title of the PR.
*/
const { payload, github } = context;
if (EXCLUDED_REPOSITORY_NAMES.has(payload.repository.name)) {
return;
}
const allCommits = await github.pullRequests.listCommits(context.issue());
const messageToCheck = payload.pull_request.title;
const errors = getCommitMessageErrors(messageToCheck);
let description;
let state;
if (errors.length === 0) {
state = "success";
description = "PR title follows commit message guidelines";
const labels = getCommitMessageLabels(messageToCheck);
if (labels) {
await context.github.issues.addLabels(context.issue({ labels }));
}
} else {
state = "failure";
description = "PR title doesn't follow commit message guidelines";
}
// create status on the last commit
await github.repos.createStatus(
context.repo({
sha: allCommits.data[allCommits.data.length - 1].sha,
state,
target_url: "https://github.com/eslint/eslint-github-bot/blob/main/docs/commit-message-check.md",
description,
context: "commit-message"
})
);
if (state === "failure") {
await github.issues.createComment(context.issue({
body: commentMessage(errors, payload.pull_request.user.login)
}));
}
}
/**
* check commit message
*/
module.exports = robot => {
robot.on("pull_request.opened", processCommitMessage);
robot.on("pull_request.reopened", processCommitMessage);
robot.on("pull_request.synchronize", processCommitMessage);
robot.on("pull_request.edited", processCommitMessage);
};