Skip to content

Commit

Permalink
feat: scope helpers to support chore(deps)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 16, 2024
1 parent 24c1c24 commit ef0c63e
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 8 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ Conditional, renders a block if given `List<Commits>` contains given `type`.
{{/ifContainsType}}
```

### `ifContainsScope <List<Commit>>`

Conditional, renders a block if given `List<Commits>` contains given `scope`.

```hbs
{{#ifContainsScope commits scope="deps"}}
commits contains deps
{{/ifContainsScope}}
```

### `ifContainsTypeAndScope <List<Commit>>`

Conditional, renders a block if given `List<Commits>` contains given `type` and `scope`.

```hbs
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
commits contains chore with deps
{{/ifContainsScope}}
```

### `ifContainsTypeOtherThan <List<Commit>>`

Conditional, renders a block if given `List<Commits>` contains commits that don't match the given `type`.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.2'

testImplementation 'org.slf4j:slf4j-simple:1.8.0-beta2'
testImplementation 'org.slf4j:slf4j-simple:2.0.13'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.assertj:assertj-core:3.26.3'
testImplementation 'com.approvaltests:approvaltests:24.5.0'
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/se/bjurr/gitchangelog/api/helpers/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.containsIssueLabelOtherThan;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.containsIssueType;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.containsIssueTypeOtherThan;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.containsScope;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.containsType;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.containsTypeAndScope;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.containsTypeOtherThan;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.getDate;
import static se.bjurr.gitchangelog.internal.semantic.ConventionalCommitParser.getMessageParts;
Expand Down Expand Up @@ -87,6 +89,16 @@ public static Map<String, Helper<?>> getAll() {
(final List<Commit> commits, final Options options) -> {
return conditional(options, containsType(commits, options));
});
helpers.put(
"ifContainsScope",
(final List<Commit> commits, final Options options) -> {
return conditional(options, containsScope(commits, options));
});
helpers.put(
"ifContainsTypeAndScope",
(final List<Commit> commits, final Options options) -> {
return conditional(options, containsTypeAndScope(commits, options));
});
helpers.put(
"ifContainsTypeOtherThan",
(final List<Commit> commits, final Options options) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ public static boolean containsType(final List<Commit> commits, final Options opt
return false;
}

public static boolean containsScope(final List<Commit> commits, final Options options) {
for (final Commit commit : commits) {
if (commitScope(commit.getMessage(), options)) {
return true;
}
}
return false;
}

public static boolean containsTypeAndScope(final List<Commit> commits, final Options options) {
for (final Commit commit : commits) {
if (commitType(commit.getMessage(), options) && commitScope(commit.getMessage(), options)) {
return true;
}
}
return false;
}

public static boolean containsTypeOtherThan(final List<Commit> commits, final Options options) {
for (final Commit commit : commits) {
if (!commitType(commit.getMessage(), options)) {
Expand Down Expand Up @@ -209,6 +227,11 @@ public static boolean commitType(final String commitMessage, final Options optio
return getType(commitMessage).matches(type);
}

public static boolean commitScope(final String commitMessage, final Options options) {
final String scope = options.hash("scope");
return commitScopes(commitMessage).contains(scope);
}

private static String getType(final String commitMessage) {
final Matcher matcher = CONVENTIONAL_PATTERN.matcher(commitMessage);
if (!matcher.find()) {
Expand Down
13 changes: 12 additions & 1 deletion src/main/resources/changelog-prepend.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@
{{/commits}}

{{/ifContainsType}}
{{#ifContainsType commits type='^($|(?!fix|feat|breaking))'}}
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
### Dependency updates

{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
- {{{commitDescription .}}} ([{{subString hash 0 5}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}}) {{#eachCommitRefs .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitRefs}} {{#eachCommitFixes .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitFixes}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}
{{#ifContainsType commits type='^($^|(?!fix|feat|breaking|chore).*)'}}
### Other changes

{{#commits}}
Expand Down
13 changes: 12 additions & 1 deletion src/main/resources/changelog.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ Changelog of {{repoName}}.
{{/commits}}

{{/ifContainsType}}
{{#ifContainsType commits type='^($|(?!fix|feat|breaking))'}}
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
### Dependency updates

{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
- {{{commitDescription .}}} ([{{subString hash 0 5}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}}) {{#eachCommitRefs .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitRefs}} {{#eachCommitFixes .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitFixes}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}
{{#ifContainsType commits type='^($^|(?!fix|feat|breaking|chore).*)'}}
### Other changes

{{#commits}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ Changelog of {{repoName}}.
{{/commits}}

{{/ifContainsType}}
{{#ifContainsType commits type='^($|(?!fix|feat|breaking))'}}
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
### Dependency updates

{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
- {{{commitDescription .}}} ([{{subString hash 0 5}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}}) {{#eachCommitRefs .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitRefs}} {{#eachCommitFixes .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitFixes}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}
{{#ifContainsType commits type='^($^|(?!fix|feat|breaking|chore).*)'}}
### Other changes

{{#commits}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ Changelog of {{repoName}}.
{{/commits}}

{{/ifContainsType}}
{{#ifContainsType commits type='^($|(?!fix|feat|breaking))'}}
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
### Dependency updates

{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
- {{{commitDescription .}}} ([{{subString hash 0 5}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}}) {{#eachCommitRefs .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitRefs}} {{#eachCommitFixes .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitFixes}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}
{{#ifContainsType commits type='^($^|(?!fix|feat|breaking|chore).*)'}}
### Other changes

{{#commits}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ Changelog of {{repoName}}.
{{/commits}}

{{/ifContainsType}}
{{#ifContainsType commits type='^($|(?!fix|feat|breaking))'}}
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
### Dependency updates

{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
- {{{commitDescription .}}} ([{{subString hash 0 5}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}}) {{#eachCommitRefs .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitRefs}} {{#eachCommitFixes .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitFixes}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}
{{#ifContainsType commits type='^($^|(?!fix|feat|breaking|chore).*)'}}
### Other changes

{{#commits}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ Changelog of {{repoName}}.
{{/commits}}

{{/ifContainsType}}
{{#ifContainsType commits type='^($|(?!fix|feat|breaking))'}}
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
### Dependency updates

{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
- {{{commitDescription .}}} ([{{subString hash 0 5}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}}) {{#eachCommitRefs .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitRefs}} {{#eachCommitFixes .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitFixes}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}
{{#ifContainsType commits type='^($^|(?!fix|feat|breaking|chore).*)'}}
### Other changes

{{#commits}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ template:
{{/commits}}

{{/ifContainsType}}
{{#ifContainsType commits type='^($|(?!fix|feat|breaking))'}}
{{#ifContainsTypeAndScope commits type="chore" scope="deps"}}
### Dependency updates

{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
- {{{commitDescription .}}} ([{{subString hash 0 5}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}}) {{#eachCommitRefs .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitRefs}} {{#eachCommitFixes .}}{{#ifMatches . "^#[0-9]+"}} [{{.}}](https://github.com/{{ownerName}}/{{repoName}}/issues/{{subString . 1}}) {{/ifMatches}}{{/eachCommitFixes}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}
{{#ifContainsType commits type='^($^|(?!fix|feat|breaking|chore).*)'}}
### Other changes

{{#commits}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ ifMatches and subString:
{{/ifContainsBreaking}}


## feat utils:
{{#ifContainsTypeAndScope commits type="feat" scope="utils"}}
feat and utils exist!
{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
is feat and utils: {{hash}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}


## Footers
{{#commits}}
{{#ifCommitHasFooters}}
Expand Down Expand Up @@ -241,6 +254,11 @@ ifMatches and subString:
is breaking: 2abbccdf2039e7c


## feat utils:
feat and utils exist!
is feat and utils: 5fb068c6ccad741


## Footers
Has footers: 65cfb90e2922ae5
- "first-token-here"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ ifMatches and subString:
{{/ifContainsBreaking}}


## feat utils:
{{#ifContainsTypeAndScope commits type="feat" scope="utils"}}
feat and utils exist!
{{#commits}}
{{#ifCommitType . type="feat"}}
{{#ifCommitScope . scope="utils"}}
is feat and utils: {{hash}}
{{/ifCommitScope}}
{{/ifCommitType}}
{{/commits}}
{{/ifContainsTypeAndScope}}


## Footers
{{#commits}}
{{#ifCommitHasFooters}}
Expand Down

0 comments on commit ef0c63e

Please sign in to comment.