From a51e58c36f1a74825d9ac59490832ae30b158def Mon Sep 17 00:00:00 2001 From: Adrian Smijulj Date: Thu, 11 Jun 2020 12:52:41 +0200 Subject: [PATCH] fix: support different variations (scopes, emojis, ...) --- src/extractCommits.ts | 35 +++++++++++++++++++++++++++++++++++ src/isValidCommitMesage.ts | 33 +++++++++++++++++++++++++++++++++ main.ts => src/main.ts | 38 +++----------------------------------- 3 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 src/extractCommits.ts create mode 100644 src/isValidCommitMesage.ts rename main.ts => src/main.ts (50%) diff --git a/src/extractCommits.ts b/src/extractCommits.ts new file mode 100644 index 0000000..980a929 --- /dev/null +++ b/src/extractCommits.ts @@ -0,0 +1,35 @@ +const get = require("lodash.get"); +const got = require("got"); + +type Commit = { + message: string; +}; + +const extractCommits = async (context): Promise => { + // 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; diff --git a/src/isValidCommitMesage.ts b/src/isValidCommitMesage.ts new file mode 100644 index 0000000..e3eaac3 --- /dev/null +++ b/src/isValidCommitMesage.ts @@ -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; diff --git a/main.ts b/src/main.ts similarity index 50% rename from main.ts rename to src/main.ts index d1d1a11..243fb81 100644 --- a/main.ts +++ b/src/main.ts @@ -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 => { - // 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;