Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: outputs the error messages #194

Merged
merged 11 commits into from Sep 26, 2022
28 changes: 28 additions & 0 deletions .github/workflows/lint-pr-title-preview-outputErrorMessage
@@ -0,0 +1,28 @@
name: "Lint PR title preview (current branch, outputErrorMessage)"
on:
pull_request:
types:
- opened
- edited
- synchronize

jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: yarn install
- run: yarn build
- uses: ./
id: lint_pr_title
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Comment on PR
if: always()
uses: marocchino/sticky-pull-request-comment@v2
with:
message: ${{ steps.lint_pr_title.outputs.ERROR_MESSAGE }}
27 changes: 16 additions & 11 deletions src/validatePrTitle.js
@@ -1,3 +1,4 @@
const core = require('@actions/core');
const conventionalCommitsConfig = require('conventional-changelog-conventionalcommits');
const conventionalCommitTypes = require('conventional-commit-types');
const parser = require('conventional-commits-parser').sync;
Expand Down Expand Up @@ -52,30 +53,29 @@ module.exports = async function validatePrTitle(
}

if (!result.type) {
throw new Error(
raiseError(
`No release type found in pull request title "${prTitle}". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/\n\n${printAvailableTypes()}`
);
}

if (!result.subject) {
throw new Error(`No subject found in pull request title "${prTitle}".`);
raiseError(`No subject found in pull request title "${prTitle}".`);
}

if (!types.includes(result.type)) {
throw new Error(
raiseError(
`Unknown release type "${
result.type
}" found in pull request title "${prTitle}". \n\n${printAvailableTypes()}`
);
}

if (requireScope && !result.scope) {
let msg = `No scope found in pull request title "${prTitle}".`;
let message = `No scope found in pull request title "${prTitle}".`;
if (scopes) {
msg += ` Use one of the available scopes: ${scopes.join(', ')}.`;
message += ` Use one of the available scopes: ${scopes.join(', ')}.`;
}

throw new Error(msg);
raiseError(message);
}

const givenScopes = result.scope
Expand All @@ -84,7 +84,7 @@ module.exports = async function validatePrTitle(

const unknownScopes = givenScopes ? givenScopes.filter(isUnknownScope) : [];
if (scopes && unknownScopes.length > 0) {
throw new Error(
raiseError(
`Unknown ${
unknownScopes.length > 1 ? 'scopes' : 'scope'
} "${unknownScopes.join(
Expand All @@ -99,7 +99,7 @@ module.exports = async function validatePrTitle(
? givenScopes.filter(isDisallowedScope)
: [];
if (disallowScopes && disallowedScopes.length > 0) {
throw new Error(
raiseError(
`Disallowed ${
disallowedScopes.length === 1 ? 'scope was' : 'scopes were'
} found: ${disallowScopes.join(', ')}`
Expand All @@ -113,8 +113,7 @@ module.exports = async function validatePrTitle(
title: prTitle
});
}

throw new Error(message);
raiseError(message);
}

if (subjectPattern) {
Expand All @@ -133,4 +132,10 @@ module.exports = async function validatePrTitle(
);
}
}

function raiseError(message) {
core.setOutput('ERROR_MESSAGE', message);

throw new Error(message);
}
};