fix: support different variations (scopes, emojis, ...)

This commit is contained in:
Adrian Smijulj 2020-06-11 12:52:41 +02:00
parent 6c26401067
commit a51e58c36f
3 changed files with 71 additions and 35 deletions

35
src/extractCommits.ts Normal file
View file

@ -0,0 +1,35 @@
const get = require("lodash.get");
const got = require("got");
type Commit = {
message: string;
};
const extractCommits = async (context): Promise<Commit[]> => {
// For "push" events, commits can be found in the "context.payload.commits".
const pushCommits = Array.isArray(get(context, "payload.commits"));
if (pushCommits) {
return context.payload.commits;
}
// For PRs, we need to get a list of commits via the GH API:
const prCommitsUrl = get(context, "payload.pull_request.commits_url");
if (prCommitsUrl) {
try {
const { body } = await got.get(prCommitsUrl, {
responseType: "json",
});
if (Array.isArray(body)) {
return body.map((item) => item.commit);
}
return [];
} catch {
return [];
}
}
return [];
};
export default extractCommits;

View file

@ -0,0 +1,33 @@
const DEFAULT_COMMIT_TYPES = [
"feat",
"fix",
"docs",
"style",
"refactor",
"test",
"build",
"ci",
"chore",
"revert",
"merge",
"wip",
];
const isValidCommitMessage = (message, availableTypes = DEFAULT_COMMIT_TYPES): boolean => {
let [possiblyValidCommitType] = message.split(":");
possiblyValidCommitType = possiblyValidCommitType.toLowerCase();
// Let's remove scope if present.
if (possiblyValidCommitType.match(/\([a-z]*?\)/)) {
possiblyValidCommitType = possiblyValidCommitType.replace(/\([a-z]*?\)/, "");
}
possiblyValidCommitType = possiblyValidCommitType
.replace(/\s/g, "") // Remove all whitespace
.replace(/()/g, "") // Remove all whitespace
.replace(/[^a-z]/g, ""); // Only leave [a-z] characters.
return availableTypes.includes(possiblyValidCommitType);
};
export default isValidCommitMessage;

View file

@ -1,47 +1,15 @@
const { context } = require("@actions/github"); const { context } = require("@actions/github");
const core = require("@actions/core"); const core = require("@actions/core");
const get = require("lodash.get");
const got = require("got");
type Commit = { import isValidCommitMessage from "./isValidCommitMesage";
message: string; import extractCommits from "./extractCommits";
};
const isValidCommitMessage = (message): boolean => message.match(/^[a-z].*:/);
const extractCommits = async (): Promise<Commit[]> => {
// For "push" events, commits can be found in the "context.payload.commits".
const pushCommits = Array.isArray(get(context, "payload.commits"));
if (pushCommits) {
return context.payload.commits;
}
// For PRs, we need to get a list of commits via the GH API:
const prCommitsUrl = get(context, "payload.pull_request.commits_url");
if (prCommitsUrl) {
try {
const { body } = await got.get(prCommitsUrl, {
responseType: "json",
});
if (Array.isArray(body)) {
return body.map((item) => item.commit);
}
return [];
} catch {
return [];
}
}
return [];
};
async function run() { async function run() {
core.info( core.info(
` Checking if commit messages are following the Conventional Commits specification...` ` Checking if commit messages are following the Conventional Commits specification...`
); );
const extractedCommits = await extractCommits(); const extractedCommits = await extractCommits(context);
if (extractedCommits.length === 0) { if (extractedCommits.length === 0) {
core.info(`No commits to check, skipping...`); core.info(`No commits to check, skipping...`);
return; return;