Merge pull request #10 from thehanimo/add-message-customization

Add message customization
This commit is contained in:
Hani Mohammed
2021-11-22 23:33:15 +05:30
committed by GitHub
4 changed files with 1892 additions and 44892 deletions
+6 -1
View File
@@ -20,6 +20,11 @@ Create a config file `.github/pr-title-checker-config.json` like this one below:
"regexp": "docs\\(v[0-9]\\): ",
"regexpFlags": "i",
"ignoreLabels" : ["dont-check-PRs-with-this-label", "meta"]
},
"MESSAGES": {
"success": "All OK",
"failure": "Failing CI test",
"notice": ""
}
}
```
@@ -51,7 +56,7 @@ jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: thehanimo/[email protected].2
- uses: thehanimo/[email protected].3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
pass_on_octokit_error: false
+1837 -44867
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+48 -23
View File
@@ -21,15 +21,20 @@ async function run() {
core.setFailed(`Couldn't retrieve the config file specified - ${e}`);
return;
}
let { CHECKS, LABEL } = JSON.parse(config);
let { CHECKS, LABEL, MESSAGES } = JSON.parse(config);
LABEL.name = LABEL.name || "title needs formatting";
LABEL.color = LABEL.color || "eee";
CHECKS.ignoreLabels = CHECKS.ignoreLabels || [];
MESSAGES = MESSAGES || {};
MESSAGES.success = MESSAGES.success || "All OK";
MESSAGES.failure = MESSAGES.failure || "Failing CI test";
MESSAGES.notice = MESSAGES.notice || "";
for (let i = 0; i < labels.length; i++) {
for (let j = 0; j < CHECKS.ignoreLabels.length; j++) {
if (labels[i].name == CHECKS.ignoreLabels[j]) {
core.info(`Ignoring Title Check for label - ${labels[i].name}`);
removeLabel(labels, LABEL.name, true);
return;
}
}
@@ -51,52 +56,72 @@ async function run() {
if (CHECKS.prefixes && CHECKS.prefixes.length) {
for (let i = 0; i < CHECKS.prefixes.length; i++) {
if (title.startsWith(CHECKS.prefixes[i])) {
removeLabel(LABEL.name);
removeLabel(labels, LABEL.name, CHECKS.alwaysPassCI);
core.info(MESSAGES.success);
return;
}
}
}
if (CHECKS.regexp) {
let re = new RegExp(CHECKS.regexp, CHECKS.regexpFlags || '');
let re = new RegExp(CHECKS.regexp, CHECKS.regexpFlags || "");
if (re.test(title)) {
removeLabel(LABEL.name);
removeLabel(labels, LABEL.name, CHECKS.alwaysPassCI);
core.info(MESSAGES.success);
return;
}
}
addLabel(LABEL.name, CHECKS.alwaysPassCI);
await titleCheckFailed(CHECKS, LABEL, MESSAGES);
} catch (error) {
core.info(error);
}
}
async function addLabel(name, alwaysPassCI) {
async function titleCheckFailed(CHECKS, LABEL, MESSAGES) {
try {
core.info(`Adding label (${name}) to PR...`);
let addLabelResponse = await octokit.issues.addLabels({
owner,
repo,
issue_number,
labels: [name],
});
core.info(`Added label (${name}) to PR - ${addLabelResponse.status}`);
if (!alwaysPassCI) {
core.setFailed("Failing CI test");
if (MESSAGES.notice.length) {
core.notice(MESSAGES.notice);
}
core.info("All OK");
} catch (error) {
core.info(error);
if (alwaysPassCI) {
core.info(`Failed to add label (${name}) to PR`);
await addLabel(LABEL.name);
if (CHECKS.alwaysPassCI) {
core.info(MESSAGES.failure);
} else {
core.setFailed(`Failed to add label (${name}) to PR`);
core.setFailed(MESSAGES.failure);
}
} catch (error) {
core.info(error);
if (CHECKS.alwaysPassCI) {
core.info(`Failed to add label (${LABEL.name}) to PR`);
} else {
core.setFailed(`Failed to add label (${LABEL.name}) to PR`);
}
}
}
async function removeLabel(name) {
async function addLabel(name) {
core.info(`Adding label (${name}) to PR...`);
let addLabelResponse = await octokit.issues.addLabels({
owner,
repo,
issue_number,
labels: [name],
});
core.info(`Added label (${name}) to PR - ${addLabelResponse.status}`);
}
async function removeLabel(labels, name, alwaysPassCI) {
try {
if (
!labels
.map((label) => label.name.toLowerCase())
.includes(name.toLowerCase())
) {
return;
}
core.info("No formatting necessary. Removing label...");
let removeLabelResponse = await octokit.issues.removeLabel({
owner,