Skip to content

Commit a8d80d0

Browse files
Release 2.0
# Changes and Updates - Support private repos - Change the configuration file from .validationconfig to .github folder # Breaking Changes - Configuration changes
1 parent 5146d05 commit a8d80d0

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# commit-message-validator
1+
# Commit Message Lint
22
Github app to validate commit message and pull request title on a pull request
33

44
## Description
@@ -7,19 +7,19 @@ This app runs a format check on commit messages and pull request title on the cr
77
For example, let's say you specify that a commit message should have a format `DDD:message`. Here D stand for numeric digit. The app checks if the commit message follows this format. If all the commit messages follow this format, the check returns successful, otherwise failure. The reviewer can then decide if they want to go ahead with the code merge.
88

99
### App URL
10-
https://github.com/apps/commit-message-validator
10+
https://github.com/apps/commit-message-lint
1111

1212
## Installation
1313

1414
Use the Github's app section or above URL to install the app to your repository.
1515

16-
## Setup
16+
## Configuration
1717

18-
You would need to add a configuration file named `.validationconfig` to the root of your repository. The contents of that file will be:
18+
You would need to add a configuration folder named `.github` at the root of your repository. The folder should contain a file named `config.yml`. This file will serve as the configuration and the contents of that file will be:
1919

2020
```
21-
PR_TITLE_REGEX=<PR Title Regex>
22-
COMMIT_MESSAGE_REGEX=<Commit Message Regex>
21+
PR_TITLE_REGEX: <PR Title Regex>
22+
COMMIT_MESSAGE_REGEX: <Commit Message Regex>
2323
```
2424

2525
## Usage

controllers/pullRequest.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
const { checkRegex } = require('../utilities/regexUtil');
2-
const { getRegexFromConfig } = require('../helpers/config');
32
const { createCheckSuite, createCheckRun, listCheckSuite } = require('../helpers/checks');
43
const { listCommitsOfPullRequest } = require('../helpers/pullRequest');
54
const defaultJson = require('../default.json');
5+
const mergeCommitRegex = defaultJson.REGEX.MERGE_COMMIT_REGEX;
66
const conclusionStatus = defaultJson.conclusion_status;
77
const messages = defaultJson.messages;
88
const checkRunStatusCompleted = defaultJson.CHECK_RUN_STATUS_COMPLETED;
99
const checkRunName = defaultJson.CHECK_RUN_NAME;
1010
const outputTitleSuccess = defaultJson.output_title_success;
1111
const outputTitleFail = defaultJson.output_title_fail;
1212

13-
module.exports.commitAndTitleValidator = async (app, context) => {
13+
/**
14+
* Commit messages and PR title Validator
15+
* @param {Object} app
16+
* @param {Object} context
17+
* @param {Object} configuration
18+
*/
19+
module.exports.commitAndTitleValidator = async (app, context, configuration) => {
1420
try {
21+
const prTitleRegex = (configuration && configuration.PR_TITLE_REGEX) ? configuration.PR_TITLE_REGEX: '';
22+
const commitTitleRegex = (configuration && configuration.COMMIT_MESSAGE_REGEX) ? configuration.COMMIT_MESSAGE_REGEX : '';
1523
let owner = context.payload.repository.owner.login;
1624
let repository = context.payload.repository.name;
1725
let pullRequestTitle = context.payload.pull_request.title;
1826
let pullNumber = context.payload.number;
19-
let regexData = await getRegexFromConfig(app, owner, repository, process.env.REGEX_CONFIG_FILE_NAME);
2027
// find commits
2128
let commits = await listCommitsOfPullRequest(context, owner, repository, pullNumber);
22-
let result = checkMessagesFormat(pullRequestTitle, commits.data, regexData.prTitle, regexData.commitMsg);
29+
let result = checkMessagesFormat(pullRequestTitle, commits.data, prTitleRegex, commitTitleRegex);
2330
if (result && result.commitIds && Array.isArray(result.commitIds) && result.commitIds.length) {
2431
for (let index = 0; index < result.commitIds.length; index++) {
2532
const commitId = result.commitIds[index];
@@ -44,8 +51,8 @@ module.exports.commitAndTitleValidator = async (app, context) => {
4451
* check Messages Format
4552
* @param {String} pullRequestTitle
4653
* @param {Array} commits
47-
* @param {Object} prTitleRegex
48-
* @param {Object} commitMsgRegex
54+
* @param {String} prTitleRegex
55+
* @param {String} commitMsgRegex
4956
*/
5057
function checkMessagesFormat(pullRequestTitle, commits, prTitleRegex, commitMsgRegex) {
5158
try {
@@ -65,7 +72,6 @@ function checkMessagesFormat(pullRequestTitle, commits, prTitleRegex, commitMsgR
6572
* pull Request Title check : starts
6673
*/
6774
// pull request title format
68-
let mergePattern = /^(Merge pull request)/;
6975
if (checkRegex(pullRequestTitle, prTitleRegex)) {
7076
pullReqTitleStatus = true;
7177
pullReqTitleStatusMsg = messages.valid_pull_request_message;
@@ -87,7 +93,7 @@ function checkMessagesFormat(pullRequestTitle, commits, prTitleRegex, commitMsgR
8793
const element = commits[index];
8894
const commitMessage = element.commit.message;
8995
commitIds.push(commits[index].sha);
90-
if (!checkRegex(commitMessage, commitMsgRegex) && !checkRegex(commitMessage, mergePattern)) {
96+
if (!checkRegex(commitMessage, commitMsgRegex) && !checkRegex(commitMessage, mergeCommitRegex)) {
9197
invalidCommitsCount++;
9298
commitMsgStatus = false;
9399
commitMsgStatusMsg = messages.invalid_commit_message;
@@ -120,15 +126,15 @@ function checkMessagesFormat(pullRequestTitle, commits, prTitleRegex, commitMsgR
120126
summary: `${pullReqTitleStatusMsg}<br/>${commitMsgStatusMsg}<br/>${invalidCommits}<br/>`
121127
};
122128
let status = checkRunStatusCompleted;
123-
if ((!prTitleRegex || !prTitleRegex.regexPattern) && (!commitMsgRegex || !commitMsgRegex.regexPattern)) {
129+
if (!prTitleRegex && !commitMsgRegex) {
124130
// pull request and commit message configration regex not set
125131
output.title = `${messages.pr_and_commit_message_configuration_not_set}`;
126132
output.summary = `${messages.pr_and_commit_message_configuration_not_set}<br/>`;
127-
} else if (!commitMsgRegex || !commitMsgRegex.regexPattern) {
133+
} else if (!commitMsgRegex) {
128134
// commit message configration regex not set
129135
output.title = `${messages.commit_message_configuration_not_set}`;
130136
output.summary = `${pullReqTitleStatusMsg}<br/>${messages.commit_message_configuration_not_set}<br/>`;
131-
} else if (!prTitleRegex || !prTitleRegex.regexPattern) {
137+
} else if (!prTitleRegex) {
132138
// pull request configration regex not set
133139
output.title = `${messages.pr_configuration_not_set}`;
134140
output.summary = `${messages.pr_configuration_not_set}<br/>${commitMsgStatusMsg}<br/>${invalidCommits}<br/>`;

default.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616
"single_other_invalid_message": "other message is invalid",
1717
"pr_configuration_not_set": "Pull request title format is not configured",
1818
"commit_message_configuration_not_set": "Commit message format is not configured",
19-
"pr_and_commit_message_configuration_not_set": "Pull request title and commit message format are not configured"
19+
"pr_and_commit_message_configuration_not_set": "Pull request title and commit message format are not configured",
20+
"home_page_message": "Commit Message Lint App"
2021
},
2122
"CHECK_RUN_STATUS_COMPLETED": "completed",
22-
"CHECK_RUN_NAME": "commit message validator",
23+
"CHECK_RUN_NAME": "Commit Message Lint",
2324
"output_title_success": "Message validation passed!!!",
2425
"output_title_fail": "Message validation failed!!!",
2526
"INVALID_COMMIT_LIMIT": 3,
26-
"USER_AGENT": "commit-message-validator-app",
27+
"USER_AGENT": "commit-message-lint-app",
2728
"invalid_commit_list": {
2829
"commit_id": "sha:",
2930
"commit_message": "message:"
3031
},
3132
"REGEX": {
32-
"PR_TITLE_REGEX": "PR_TITLE_REGEX=",
33-
"COMMIT_MESSAGE_REGEX": "COMMIT_MESSAGE_REGEX="
33+
"MERGE_COMMIT_REGEX": "/^(Merge pull request)/"
34+
},
35+
"events": {
36+
"PULL_REQUEST_OPEN": "pull_request.opened"
3437
}
3538
}

index.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
const { commitAndTitleValidator } = require('./controllers/pullRequest');
2+
const { messages, events } = require('./default.json');
23
const express = require('express');
34
const expressApp = express();
5+
const path = require('path');
6+
const publicDirectory = `${__dirname}/public`;
7+
48
/**
59
* This is the main entrypoint to your Probot app
610
* @param {import('probot').Application} app
711
*/
12+
const ConfigFilename = process.env.REGEX_CONFIG_FILE_NAME;
13+
814
module.exports = async app => {
915
/**
10-
* pull request event listener
16+
* Created pull request event listener
1117
*/
12-
app.on('pull_request.opened', async (context) => {
13-
await commitAndTitleValidator(app, context);
18+
app.on(events.PULL_REQUEST_OPEN, async (context) => {
19+
const configuration = await context.config(ConfigFilename);
20+
await commitAndTitleValidator(app, context, configuration);
1421
});
1522

1623
expressApp.get('/', (req, res) => {
17-
res.send('Commit Validator Message App');
24+
res.send(messages.home_page_message);
25+
});
26+
27+
expressApp.get('/privacy', (req, res) => {
28+
res.sendFile(path.join(`${publicDirectory}/privacy.html`));
1829
});
1930

2031
app.router.use('/', expressApp);
2132
};
22-

utilities/regexUtil.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
/**
2+
* Match input string with regex
3+
* @param {String} inputString
4+
* @param {String} pattern
5+
*/
16
module.exports.checkRegex = (inputString, pattern) => {
27
try {
38
let patt = '';
49
let status = false;
5-
if (pattern && pattern.regexPattern && pattern.flags) {
6-
patt = new RegExp(pattern.regexPattern, pattern.flags);
10+
if (pattern) {
11+
let regexObj = module.exports.regexExtractor(pattern);
12+
patt = new RegExp(regexObj.regexPattern, regexObj.flags);
713
status = patt.test(inputString);
814
} else {
9-
patt = new RegExp(pattern);
10-
status = patt.test(inputString);
15+
status = true;
1116
}
1217
return status;
1318
} catch (error) {

0 commit comments

Comments
 (0)