ci(lint): add commits linter for pr check #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: pr-name | |
| on: | |
| push: | |
| branches: | |
| - main | |
| merge_group: | |
| pull_request: | |
| types: ['opened', 'edited', 'reopened', 'synchronize'] | |
| jobs: | |
| pr_name_lint: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| with: | |
| persist-credentials: false | |
| fetch-depth: 0 | |
| - uses: actions/setup-node@v6.1.0 | |
| name: Install Node.js | |
| with: | |
| node-version: 18 | |
| - name: Install dependencies | |
| run: | | |
| npm install @commitlint/lint@18.6.1 @commitlint/load@18.6.1 @commitlint/config-conventional@18.6.2 @actions/core | |
| - name: Validate commit/PR | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd | |
| with: | |
| script: | | |
| const { execSync } = require('child_process'); | |
| const load = require('@commitlint/load').default; | |
| const lint = require('@commitlint/lint').default; | |
| const CONFIG = { | |
| extends: ['@commitlint/config-conventional'], | |
| }; | |
| async function validate(text, type) { | |
| core.info(`Validating ${type}: ${text}`); | |
| const opts = await load(CONFIG); | |
| const report = await lint(text, opts.rules, opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {}); | |
| if (report.valid) { | |
| core.info(`${type} passed: "${text}"`); | |
| } else { | |
| report.warnings.forEach((warning) => { | |
| core.warning(`${warning.message}`); | |
| }); | |
| report.errors.forEach((error) => { | |
| core.error(`${error.message}`); | |
| }); | |
| core.setFailed(`${type} must follow conventional format.`); | |
| } | |
| } | |
| (async () => { | |
| if (context.eventName === 'pull_request') { | |
| await validate(context.payload.pull_request.title, 'PR title'); | |
| } else if (context.eventName === 'push') { | |
| const commit = execSync('git log -1 --format=%s').toString().trim(); | |
| await validate(commit, 'Commit message'); | |
| } | |
| })(); |