import * as core from "@actions/core";
import lint from "@commitlint/lint";
import load from "@commitlint/load";

const CONFIG = {
  extends: ["@commitlint/config-conventional"],
};

async function main() {
  try {
    const { rules, parserPreset } = await load(CONFIG);

    const commits = payload.commits;
    core.debug(commits);
    const outcomes = await Promise.all(
      commits.map((commit) =>
        lint(commit.message, rules, parserPreset ? parserPreset.parserOpts : {})
      )
    );
    core.debug(outcomes);
    const failed = outcomes.filter((outcome) => !outcome.valid);

    if (failed.length) {
      core.setFailed(failed);
    }
  } catch (error) {
    core.setFailed(error.message);
  }
}

main();
