Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ You can customize the regex pattern used for validation:
## Default Conventional Commits Format

The default pattern validates the following format:

```
<type>[optional scope]: <description>
```

Where `type` is one of:

- feat: A new feature
- fix: A bug fix
- docs: Documentation changes
Expand All @@ -76,4 +78,5 @@ Where `type` is one of:

## License

This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.

17 changes: 9 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
name: 'Conventional Commit Validator'
description: 'Validates that commit messages follow the Conventional Commits format'
name: "Conventional Commit Validator"
description: "Validates that commit messages follow the Conventional Commits format"
inputs:
github-token:
description: 'GitHub token for API access'
description: "GitHub token for API access"
required: true
default: ${{ github.token }}
pattern:
description: 'Regex pattern for commit message validation'
description: "Regex pattern for commit message validation"
required: false
default: '^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(\w+\))?: .+$'
runs:
using: 'node20'
main: 'dist/index.js'
using: "node20"
main: "dist/index.js"
branding:
icon: 'check-circle'
color: 'green'
icon: "check-circle"
color: "green"

16 changes: 8 additions & 8 deletions commit-message-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ async function run() {
const token = core.getInput('github-token', { required: true });
const pattern = core.getInput('pattern') || '^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\\(\\w+\\))?: .+$';
const regexPattern = new RegExp(pattern);

// Create octokit client
const octokit = github.getOctokit(token);
const context = github.context;

// Get current PR
const pullRequest = context.payload.pull_request;
if (!pullRequest) {
core.info('No pull request found. Skipping commit message validation.');
return;
}

// Get commits in PR
const { data: commits } = await octokit.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullRequest.number,
});

let hasError = false;
let errorMessages = [];

// Validate each commit message
commits.forEach((commit) => {
const commitMessage = commit.commit.message.split('\n')[0].trim();
const sha = commit.sha.substring(0, 7);

if (!regexPattern.test(commitMessage)) {
const errorMsg = `❌ Commit ${sha} has an invalid message format: "${commitMessage}"`;
core.error(errorMsg);
Expand All @@ -43,7 +43,7 @@ async function run() {
core.info(`✅ Commit ${sha} has a valid message: "${commitMessage}"`);
}
});

// Fail the workflow if errors are found
if (hasError) {
core.setFailed(`One or more commits have invalid message format.\n${errorMessages.join('\n')}\n\nPlease follow the Conventional Commits format: <type>[optional scope]: <description>\nExample: feat(auth): add login functionality\n\nFor more information, visit: https://www.conventionalcommits.org/`);
Expand All @@ -55,4 +55,4 @@ async function run() {
}
}

run();
run();