generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
ci: lint-commit messages #70
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Checks that a PR title conforms to conventional commits | ||
| // (https://www.conventionalcommits.org/). | ||
| // | ||
| // To run self-tests, run this script: | ||
| // | ||
| // node lintcommit.js test | ||
|
|
||
| import { readFileSync, appendFileSync } from "fs"; | ||
|
|
||
| const types = new Set([ | ||
| "build", | ||
| "chore", | ||
| "parity", | ||
| "ci", | ||
| "config", | ||
| "deps", | ||
| "docs", | ||
| "feat", | ||
| "fix", | ||
| "perf", | ||
| "refactor", | ||
| "revert", | ||
| "style", | ||
| "test", | ||
| "types", | ||
| ]); | ||
|
|
||
| const scopes = new Set(["testing-sdk", "examples"]); | ||
|
|
||
| /** | ||
| * Checks that a pull request title, or commit message subject, follows the expected format: | ||
| * | ||
| * type(scope): message | ||
| * | ||
| * Returns undefined if `title` is valid, else an error message. | ||
| */ | ||
| function validateTitle(title) { | ||
| const parts = title.split(":"); | ||
| const subject = parts.slice(1).join(":").trim(); | ||
|
|
||
| if (title.startsWith("Merge")) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (parts.length < 2) { | ||
| return "missing colon (:) char"; | ||
| } | ||
|
|
||
| const typeScope = parts[0]; | ||
|
|
||
| const [type, scope] = typeScope.split(/\(([^)]+)\)$/); | ||
|
|
||
| if (/\s+/.test(type)) { | ||
| return `type contains whitespace: "${type}"`; | ||
| } else if (!types.has(type)) { | ||
| return `invalid type "${type}"`; | ||
| } else if (!scope && typeScope.includes("(")) { | ||
| return `must be formatted like type(scope):`; | ||
| } else if (!scope && ["feat", "fix"].includes(type)) { | ||
| return `"${type}" type must include a scope (example: "${type}(testing-sdk)")`; | ||
| } else if (scope && scope.length > 30) { | ||
| return "invalid scope (must be <=30 chars)"; | ||
| } else if (scope && /[^- a-z0-9]+/.test(scope)) { | ||
| return `invalid scope (must be lowercase, ascii only): "${scope}"`; | ||
| } else if (scope && !scopes.has(scope)) { | ||
| return `invalid scope "${scope}" (valid scopes are ${Array.from(scopes).join(", ")})`; | ||
| } else if (subject.length === 0) { | ||
| return "empty subject"; | ||
| } else if (subject.length > 100) { | ||
| return "invalid subject (must be <=100 chars)"; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| function run() { | ||
| const eventData = JSON.parse( | ||
| readFileSync(process.env.GITHUB_EVENT_PATH, "utf8"), | ||
| ); | ||
| const pullRequest = eventData.pull_request; | ||
|
|
||
| // console.log(eventData) | ||
|
|
||
| if (!pullRequest) { | ||
| console.info("No pull request found in the context"); | ||
| return; | ||
| } | ||
|
|
||
| const title = pullRequest.title; | ||
|
|
||
| const failReason = validateTitle(title); | ||
| const msg = failReason | ||
| ? ` | ||
| Invalid pull request title: \`${title}\` | ||
|
|
||
| * Problem: ${failReason} | ||
| * Expected format: \`type(scope): subject...\` | ||
| * type: one of (${Array.from(types).join(", ")}) | ||
| * scope: optional, lowercase, <30 chars | ||
| * subject: must be <100 chars | ||
| * Hint: *close and re-open the PR* to re-trigger CI (after fixing the PR title). | ||
| ` | ||
| : `Pull request title matches the expected format`; | ||
|
|
||
| if (process.env.GITHUB_STEP_SUMMARY) { | ||
| appendFileSync(process.env.GITHUB_STEP_SUMMARY, msg); | ||
| } | ||
|
|
||
| if (failReason) { | ||
| console.error(msg); | ||
| process.exit(1); | ||
| } else { | ||
| console.info(msg); | ||
| } | ||
| } | ||
|
|
||
| function _test() { | ||
| const tests = { | ||
| " foo(scope): bar": 'type contains whitespace: " foo"', | ||
| "build: update build process": undefined, | ||
| "chore: update dependencies": undefined, | ||
| "ci: configure CI/CD": undefined, | ||
| "config: update configuration files": undefined, | ||
| "deps: bump the aws-sdk group across 1 directory with 5 updates": undefined, | ||
| "docs: update documentation": undefined, | ||
| "feat(testing-sdk): add new feature": undefined, | ||
| "feat(testing-sdk):": "empty subject", | ||
| "feat foo):": 'type contains whitespace: "feat foo)"', | ||
| "feat(foo)): sujet": 'invalid type "feat(foo))"', | ||
| "feat(foo: sujet": 'invalid type "feat(foo"', | ||
| "feat(Q Foo Bar): bar": | ||
| 'invalid scope (must be lowercase, ascii only): "Q Foo Bar"', | ||
| "feat(testing-sdk): bar": undefined, | ||
| "feat(testing-sdk): x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x ": | ||
| "invalid subject (must be <=100 chars)", | ||
| "feat: foo": '"feat" type must include a scope (example: "feat(testing-sdk)")', | ||
| "fix: foo": '"fix" type must include a scope (example: "fix(testing-sdk)")', | ||
| "fix(testing-sdk): resolve issue": undefined, | ||
| "foo (scope): bar": 'type contains whitespace: "foo "', | ||
| "invalid title": "missing colon (:) char", | ||
| "perf: optimize performance": undefined, | ||
| "refactor: improve code structure": undefined, | ||
| "revert: feat: add new feature": undefined, | ||
| "style: format code": undefined, | ||
| "test: add new tests": undefined, | ||
| "types: add type definitions": undefined, | ||
| "Merge staging into feature/lambda-get-started": undefined, | ||
| "feat(foo): fix the types": | ||
| 'invalid scope "foo" (valid scopes are testing-sdk, examples)', | ||
| }; | ||
|
|
||
| let passed = 0; | ||
| let failed = 0; | ||
|
|
||
| for (const [title, expected] of Object.entries(tests)) { | ||
| const result = validateTitle(title); | ||
| if (result === expected) { | ||
| console.log(`✅ Test passed for "${title}"`); | ||
| passed++; | ||
| } else { | ||
| console.log( | ||
| `❌ Test failed for "${title}" (expected "${expected}", got "${result}")`, | ||
| ); | ||
| failed++; | ||
| } | ||
| } | ||
|
|
||
| console.log(`\n${passed} tests passed, ${failed} tests failed`); | ||
| } | ||
|
|
||
| function main() { | ||
| const mode = process.argv[2]; | ||
|
|
||
| if (mode === "test") { | ||
| _test(); | ||
| } else { | ||
| run(); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.