|
| 1 | +const core = require("@actions/core"); |
| 2 | + |
| 3 | +async function runCommitlint(message) { |
| 4 | + const { default: load } = await import("@commitlint/load"); |
| 5 | + const { default: lint } = await import("@commitlint/lint"); |
| 6 | + const { default: format } = await import("@commitlint/format"); |
| 7 | + |
| 8 | + console.log("Checking for Conventional Commits format"); |
| 9 | + |
| 10 | + const config = await load(); |
| 11 | + const report = await lint(message, config.rules, { |
| 12 | + parserOpts: config.parserPreset?.parserOpts, |
| 13 | + }); |
| 14 | + const reportMessage = format({ results: [report] }, { verbose: true }); |
| 15 | + |
| 16 | + if (report.valid) console.log(reportMessage + "\n"); |
| 17 | + else console.error(reportMessage + "\n"); |
| 18 | + |
| 19 | + return report.valid; |
| 20 | +} |
| 21 | + |
| 22 | +function checkIssueNumber(message) { |
| 23 | + console.log( |
| 24 | + "Checking for a '#xxx' reference to a corresponding GitHub issue" |
| 25 | + ); |
| 26 | + if (/#[0-9]/.test(message)) { |
| 27 | + console.log("Successfully found issue number\n"); |
| 28 | + return true; |
| 29 | + } else { |
| 30 | + console.error("Issue number missing\n"); |
| 31 | + return false; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function checkMessage() { |
| 36 | + const message = core.getInput("message"); |
| 37 | + const failedChecks = []; |
| 38 | + if (!checkIssueNumber(message)) failedChecks.push("issue number"); |
| 39 | + if (!(await runCommitlint(message))) |
| 40 | + failedChecks.push("Conventional Commits"); |
| 41 | + if (failedChecks.length > 0) |
| 42 | + core.setFailed( |
| 43 | + `Failed ${failedChecks.join(" and ")} check${failedChecks.length > 1 ? "s" : ""}` |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +checkMessage().catch((e) => core.setFailed(e.message)); |
0 commit comments