2020-06-11 08:33:49 +02:00
|
|
|
|
const { context } = require("@actions/github");
|
|
|
|
|
const core = require("@actions/core");
|
2020-06-11 09:37:37 +02:00
|
|
|
|
const get = require("lodash.get");
|
|
|
|
|
const got = require("got");
|
2020-06-11 08:33:49 +02:00
|
|
|
|
|
2020-06-11 09:37:37 +02:00
|
|
|
|
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:
|
2020-06-11 09:41:10 +02:00
|
|
|
|
const prCommitsUrl = get(context, "payload.pull_request.commits_url");
|
2020-06-11 09:37:37 +02:00
|
|
|
|
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 [];
|
|
|
|
|
};
|
2020-06-11 08:33:49 +02:00
|
|
|
|
|
|
|
|
|
async function run() {
|
|
|
|
|
core.info(
|
|
|
|
|
`ℹ️ Checking if commit messages are following the Conventional Commits specification...`
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-11 09:37:37 +02:00
|
|
|
|
const extractedCommits = await extractCommits();
|
|
|
|
|
if (extractedCommits.length === 0) {
|
2020-06-11 08:33:49 +02:00
|
|
|
|
core.info(`No commits to check, skipping...`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 08:42:06 +02:00
|
|
|
|
let hasErrors;
|
2020-06-11 08:51:04 +02:00
|
|
|
|
core.startGroup("Commit messages:");
|
2020-06-11 09:37:37 +02:00
|
|
|
|
for (let i = 0; i < extractedCommits.length; i++) {
|
|
|
|
|
let commit = extractedCommits[i];
|
2020-06-11 08:42:06 +02:00
|
|
|
|
if (isValidCommitMessage(commit.message)) {
|
|
|
|
|
core.info(`✅ ${commit.message}`);
|
|
|
|
|
} else {
|
|
|
|
|
core.info(`🚩 ${commit.message}`);
|
|
|
|
|
hasErrors = true;
|
2020-06-11 08:33:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-11 08:44:55 +02:00
|
|
|
|
core.endGroup();
|
2020-06-11 08:33:49 +02:00
|
|
|
|
|
2020-06-11 08:42:06 +02:00
|
|
|
|
if (hasErrors) {
|
|
|
|
|
core.setFailed(
|
|
|
|
|
`🚫 According to the conventional-commits specification, some of the commit messages are not valid.`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
core.info("🎉 All commit messages are following the Conventional Commits specification.");
|
|
|
|
|
}
|
2020-06-11 08:33:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run();
|