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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: breaking changes are now recognized as meaningful #140

Closed
wants to merge 3 commits into from
Closed
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: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ This is a release commit. Returning false.
Based on a commit's conventional commit message type:

1. If the type is `feat` `fix`, or `perf`, it's considered "meaningful"
2. If the type is `docs`, `refactor`, `style`, or `test`, it's ignored
3. If the message looks like `v1.2.3`, `chore: release 1.2.3`, or similar, it's considered a "release"
2. If the commit is marked as being a breaking change, either via a note or via an `!` appended to the type, it's considered "meaningful"
3. If the type is `docs`, `refactor`, `style`, or `test`, it's ignored
4. If the message looks like `v1.2.3`, `chore: release 1.2.3`, or similar, it's considered a "release"

See [`getCommitMeaning`](./src/getCommitMeaning.ts) for the exact logic used.

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
"*": "prettier --ignore-unknown --write"
},
"dependencies": {
"@pkgjs/parseargs": "^0.11.0",
"conventional-commits-parser": "^5.0.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This jalopy does not recognize the feat!: message form of commits.

"@conventional-commits/parser": "^0.4.1",
"@pkgjs/parseargs": "^0.11.0"
},
"devDependencies": {
"@release-it/conventional-changelog": "^8.0.0",
"@types/conventional-commits-parser": "^3.0.4",
"@types/eslint": "^8.44.3",
"@types/pkgjs__parseargs": "^0.10.1",
"@typescript-eslint/eslint-plugin": "^6.7.3",
Expand Down
50 changes: 37 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/getCommitMeaning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ describe("getCommitMeaning", () => {
["chore(deps): release", "release"],
["chore(deps): release 1.2.3", "release"],
["chore(deps): release v1.2.3", "release"],
// Breaking changes are meaningful no matter the type of the commit.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Style] Just nitpicking here, I think the comment is explanatory from the test cases.

Suggested change
// Breaking changes are meaningful no matter the type of the commit.

["chore!: message", "meaningful"],
["docs!: message", "meaningful"],
["style!: message", "meaningful"],
["chore!: release", "meaningful"],
["chore: bump\n\nBREAKING CHANGE: breaks things", "meaningful"],
["chore: bump\n\nBREAKING-CHANGE: breaks things", "meaningful"],
["docs: bump\n\nBREAKING CHANGE: breaks things", "meaningful"],
["docs: bump\n\nBREAKING-CHANGE: breaks things", "meaningful"],
["style: bump\n\nBREAKING CHANGE: breaks things", "meaningful"],
["style: bump\n\nBREAKING-CHANGE: breaks things", "meaningful"],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Testing] Thinking about test edge cases...

  • ! in the commit body (so not a breaking/meaningful change)
  • Footer note other than BREAKING CHANGE
  • Multiple footer notes

])("returns %j for %s", (input, expected) => {
expect(getCommitMeaning(input)).toEqual(expected);
});
Expand Down
24 changes: 18 additions & 6 deletions src/getCommitMeaning.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import conventionalCommitsParser from "conventional-commits-parser";
import {
parser,
toConventionalChangelogFormat,
} from "@conventional-commits/parser";

Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Refactor] 🤔 @conventional-commits/parser hasn't had a commit or sustained GitHub issue triage in 3 years, and uses throw as a form of control flow. Whereas conventional-commits-parser released 3 months ago. I'm a little averse to going down to an older, less-maintained package if it's avoidable.

What do you think about instead using a small regex to check for a subject, optionally an area/scope, and ! at the beginning? /^\w+(\(\w+\))?!:/ or something like that?

Maybe: also filing an issue on conventional-commits-parser to add support for !?

const alwaysMeaningfulTypes = new Set(["feat", "fix", "perf"]);

Expand All @@ -8,22 +11,31 @@ const releaseCommitTester =
/^(?:chore(?:\(.*\))?:?)?\s*release|v?\d+\.\d+\.\d+/;

export function getCommitMeaning(message: string) {
// Some types are always meaningful or ignored, regardless of potentially release-like messages
const { type } = conventionalCommitsParser.sync(message);
if (type) {
if (alwaysMeaningfulTypes.has(type)) {
let type = undefined;
try {
const messageAst = parser(message);
const commit = toConventionalChangelogFormat(messageAst);
type = commit.type;

// Some types are always meaningful or ignored, regardless of potentially release-like messages
if (
alwaysMeaningfulTypes.has(type) ||
commit.notes.some(({ title }) => title === "BREAKING CHANGE")
) {
return "meaningful";
}

if (alwaysIgnoredTypes.has(type)) {
return { type };
}
} catch {
/* empty */
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Refactor] I'm not a big fan of try/catch as a control flow concept. It's not type-safe, is hard to mentally model, and can accidentally wrap around unintentionally throwing code too. Let's restrict what's being wrapped to just the toConventionalChangelogFormat call.


// If we've hit a release commit, we know we don't need to release
if (releaseCommitTester.test(message)) {
return "release";
}

return { type: type ?? undefined };
return { type };
}
Loading