Skip to content

Commit 07dcf72

Browse files
committed
Polish 'Add an issue type filter'
See gh-86
1 parent d8d28f7 commit 07dcf72

File tree

5 files changed

+80
-45
lines changed

5 files changed

+80
-45
lines changed

src/main/java/io/spring/githubchangeloggenerator/ApplicationProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2024 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -147,7 +147,7 @@ public static class Section {
147147
private final IssueType type;
148148

149149
public Section(String title, @DefaultValue("default") String group, IssueSort sort, Set<String> labels,
150-
@DefaultValue("ANY") IssueType type) {
150+
@DefaultValue("any") IssueType type) {
151151
this.title = title;
152152
this.group = (group != null) ? group : "default";
153153
this.sort = sort;
Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,12 @@
1616

1717
package io.spring.githubchangeloggenerator;
1818

19-
import java.util.Arrays;
20-
import java.util.LinkedHashSet;
21-
import java.util.Set;
19+
import java.util.function.Predicate;
2220

2321
import org.springframework.util.Assert;
24-
import org.springframework.util.CollectionUtils;
2522

2623
import io.spring.githubchangeloggenerator.ApplicationProperties.IssueSort;
27-
import io.spring.githubchangeloggenerator.ApplicationProperties.IssueType;
2824
import io.spring.githubchangeloggenerator.github.payload.Issue;
29-
import io.spring.githubchangeloggenerator.github.payload.Label;
3025

3126
/**
3227
* A single section of a changelog report.
@@ -42,22 +37,15 @@ class ChangelogSection {
4237

4338
private final IssueSort sort;
4439

45-
private final Set<String> labels;
40+
private Predicate<Issue> filter;
4641

47-
private final IssueType type;
48-
49-
ChangelogSection(String title, String group, IssueSort sort, String... labels) {
50-
this(title, group, sort, new LinkedHashSet<>(Arrays.asList(labels)), IssueType.ANY);
51-
}
52-
53-
ChangelogSection(String title, String group, IssueSort sort, Set<String> labels, IssueType type) {
42+
ChangelogSection(String title, String group, IssueSort sort, Predicate<Issue> filter) {
5443
Assert.hasText(title, "Title must not be empty");
55-
Assert.isTrue(!CollectionUtils.isEmpty(labels), "Labels must not be empty");
44+
Assert.notNull(filter, "Filter must not be null");
5645
this.title = title;
5746
this.group = group;
5847
this.sort = sort;
59-
this.labels = labels;
60-
this.type = type;
48+
this.filter = filter;
6149
}
6250

6351
String getGroup() {
@@ -68,27 +56,13 @@ IssueSort getSort() {
6856
return this.sort;
6957
}
7058

71-
boolean isMatchFor(Issue issue) {
72-
for (String candidate : this.labels) {
73-
for (Label label : issue.getLabels()) {
74-
if (label.getName().contains(candidate)) {
75-
switch (this.type) {
76-
case ISSUE:
77-
return issue.getPullRequest() == null;
78-
case PULL_REQUEST:
79-
return issue.getPullRequest() != null;
80-
default:
81-
return true;
82-
}
83-
}
84-
}
85-
}
86-
return false;
87-
}
88-
8959
@Override
9060
public String toString() {
9161
return this.title;
9262
}
9363

64+
boolean isMatchFor(Issue issue) {
65+
return this.filter.test(issue);
66+
}
67+
9468
}

src/main/java/io/spring/githubchangeloggenerator/ChangelogSections.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import java.util.Set;
2626
import java.util.SortedMap;
2727
import java.util.TreeMap;
28+
import java.util.function.Predicate;
2829
import java.util.stream.Collectors;
2930

3031
import org.springframework.util.CollectionUtils;
@@ -50,8 +51,8 @@ class ChangelogSections {
5051
DEFAULT_SECTIONS = Collections.unmodifiableList(sections);
5152
}
5253

53-
private static void add(List<ChangelogSection> sections, String title, String... labels) {
54-
sections.add(new ChangelogSection(title, null, null, labels));
54+
private static void add(List<ChangelogSection> sections, String title, String... labelNameContent) {
55+
sections.add(new ChangelogSection(title, null, null, SelectIssues.withLabelNamesContaining(labelNameContent)));
5556
}
5657

5758
private final List<ChangelogSection> sections;
@@ -74,9 +75,10 @@ private List<ChangelogSection> adapt(ApplicationProperties properties) {
7475
return customSections;
7576
}
7677

77-
private ChangelogSection adapt(ApplicationProperties.Section propertySection) {
78-
return new ChangelogSection(propertySection.getTitle(), propertySection.getGroup(), propertySection.getSort(),
79-
propertySection.getLabels(), propertySection.getType());
78+
private ChangelogSection adapt(ApplicationProperties.Section section) {
79+
Predicate<Issue> filter = SelectIssues.withLabelNamesContaining(section.getLabels());
80+
filter = filter.and(SelectIssues.withType(section.getType()));
81+
return new ChangelogSection(section.getTitle(), section.getGroup(), section.getSort(), filter);
8082
}
8183

8284
Map<ChangelogSection, List<Issue>> collate(List<Issue> issues) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2018-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.githubchangeloggenerator;
18+
19+
import java.util.Collection;
20+
import java.util.Set;
21+
import java.util.function.Predicate;
22+
23+
import io.spring.githubchangeloggenerator.ApplicationProperties.IssueType;
24+
import io.spring.githubchangeloggenerator.github.payload.Issue;
25+
import io.spring.githubchangeloggenerator.github.payload.Label;
26+
27+
/**
28+
* Utility to select issues.
29+
*
30+
* @author Phillip Webb
31+
* @author Steven Sheehy
32+
*/
33+
final class SelectIssues {
34+
35+
private SelectIssues() {
36+
}
37+
38+
static Predicate<Issue> withLabelNamesContaining(String... nameContent) {
39+
return withLabelNamesContaining(Set.of(nameContent));
40+
}
41+
42+
static Predicate<Issue> withLabelNamesContaining(Collection<String> nameContent) {
43+
return (issue) -> issue.getLabels()
44+
.stream()
45+
.map(Label::getName)
46+
.anyMatch((name) -> nameContent.stream().anyMatch(name::contains));
47+
}
48+
49+
static Predicate<? super Issue> withType(IssueType type) {
50+
return (issue) -> {
51+
return switch (type) {
52+
case ANY -> true;
53+
case ISSUE -> issue.getPullRequest() == null;
54+
case PULL_REQUEST -> issue.getPullRequest() != null;
55+
};
56+
};
57+
}
58+
59+
}

src/test/resources/io/spring/githubchangeloggenerator/test-application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ changelog:
44
- title: ":star: New Features"
55
labels: ["enhancement"]
66
sort: "created"
7-
type: ISSUE
7+
type: issue
88
- title: "Bugs"
99
labels: ["bug"]
1010
group: "test"

0 commit comments

Comments
 (0)