mirror of
https://github.com/webiny/action-conventional-commits.git
synced 2024-11-23 10:21:51 +01:00
fix: support different variations (scopes, emojis, ...)
This commit is contained in:
parent
6c26401067
commit
a51e58c36f
3 changed files with 71 additions and 35 deletions
35
src/extractCommits.ts
Normal file
35
src/extractCommits.ts
Normal 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;
|
33
src/isValidCommitMesage.ts
Normal file
33
src/isValidCommitMesage.ts
Normal 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;
|
|
@ -1,47 +1,15 @@
|
|||
const { context } = require("@actions/github");
|
||||
const core = require("@actions/core");
|
||||
const get = require("lodash.get");
|
||||
const got = require("got");
|
||||
|
||||
type Commit = {
|
||||
message: string;
|
||||
};
|
||||
|
||||
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 [];
|
||||
};
|
||||
import isValidCommitMessage from "./isValidCommitMesage";
|
||||
import extractCommits from "./extractCommits";
|
||||
|
||||
async function run() {
|
||||
core.info(
|
||||
`ℹ️ Checking if commit messages are following the Conventional Commits specification...`
|
||||
);
|
||||
|
||||
const extractedCommits = await extractCommits();
|
||||
const extractedCommits = await extractCommits(context);
|
||||
if (extractedCommits.length === 0) {
|
||||
core.info(`No commits to check, skipping...`);
|
||||
return;
|
Loading…
Reference in a new issue