Skip to content

Commit

Permalink
[changelog] ad a key property to categories. Resolves #619
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Dec 22, 2021
1 parent c1a81fc commit f0065e3
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 33 deletions.
Expand Up @@ -36,6 +36,7 @@
import java.util.TreeSet;
import java.util.stream.Collectors;

import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;
import static org.jreleaser.util.StringUtils.toSafeRegexPattern;

Expand Down Expand Up @@ -334,11 +335,13 @@ public static Set<Category> sort(Set<Category> categories) {
}

private final Set<String> labels = new LinkedHashSet<>();
private String key;
private String title;
private String format;
private Integer order;

void setAll(Category category) {
this.key = category.key;
this.title = category.title;
this.format = category.format;
this.order = category.order;
Expand All @@ -353,12 +356,23 @@ public void setFormat(String format) {
this.format = format;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
if (isBlank(this.key)) {
this.key = title;
}
}

public Set<String> getLabels() {
Expand All @@ -385,6 +399,7 @@ public void setOrder(Integer order) {
@Override
public Map<String, Object> asMap(boolean full) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("key", key);
map.put("title", title);
map.put("labels", labels);
map.put("format", format);
Expand All @@ -397,16 +412,17 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Category category = (Category) o;
return title.equals(category.title);
return key.equals(category.key);
}

@Override
public int hashCode() {
return Objects.hash(title);
}

public static Category of(String title, String format, String... labels) {
public static Category of(String key, String title, String format, String... labels) {
Category category = new Category();
category.key = key;
category.title = title;
category.format = format;
category.labels.addAll(Arrays.asList(labels));
Expand Down
Expand Up @@ -247,8 +247,8 @@ private static void validateChangelog(JReleaserContext context, GitService servi
}

if (changelog.getCategories().isEmpty()) {
changelog.getCategories().add(Changelog.Category.of(RB.$("default.category.feature"), "", "feature", "enhancement"));
changelog.getCategories().add(Changelog.Category.of(RB.$("default.category.bug.fix"), "", "bug", "fix"));
changelog.getCategories().add(Changelog.Category.of("feature", RB.$("default.category.feature"), "", "feature", "enhancement"));
changelog.getCategories().add(Changelog.Category.of("fix", RB.$("default.category.bug.fix"), "", "bug", "fix"));
} else {
int i = 0;
for (Changelog.Category category : changelog.getCategories()) {
Expand Down Expand Up @@ -325,19 +325,19 @@ private static void loadPreset(JReleaserContext context, Changelog changelog, Er
replacersCopy.addAll(loaded.getReplacers());
changelog.setReplacers(replacersCopy);

Map<String, List<Changelog.Category>> categoriesByTitle = changelog.getCategories().stream()
.collect(groupingBy(Changelog.Category::getTitle));
Map<String, List<Changelog.Category>> loadedCategoriesByTitle = loaded.getCategories().stream()
.collect(groupingBy(Changelog.Category::getTitle));
categoriesByTitle.forEach((categoryTitle, categories) -> {
if (loadedCategoriesByTitle.containsKey(categoryTitle)) {
Changelog.Category loadedCategory = loadedCategoriesByTitle.remove(categoryTitle).get(0);
Map<String, List<Changelog.Category>> categoriesByKey = changelog.getCategories().stream()
.collect(groupingBy(Changelog.Category::getKey));
Map<String, List<Changelog.Category>> loadedCategoriesByKey = loaded.getCategories().stream()
.collect(groupingBy(Changelog.Category::getKey));
categoriesByKey.forEach((categoryKey, categories) -> {
if (loadedCategoriesByKey.containsKey(categoryKey)) {
Changelog.Category loadedCategory = loadedCategoriesByKey.remove(categoryKey).get(0);
Changelog.Category category = categories.get(0);
category.addLabels(loadedCategory.getLabels());
}
});

loadedCategoriesByTitle.values().forEach(list -> changelog.getCategories().add(list.get(0)));
loadedCategoriesByKey.values().forEach(list -> changelog.getCategories().add(list.get(0)));
// sort categories once again as order might have changed
changelog.setCategories(Changelog.Category.sort(changelog.getCategories()));

Expand Down
Expand Up @@ -41,35 +41,42 @@ labelers:

categories:
- title: '🔀 Merge'
key: 'merge'
order: 0
labels:
- 'merge'
- title: '🚀 Features'
key: 'features'
order: 10
labels:
- 'feat'
- title: '🐛 Fixes'
key: 'fixes'
order: 20
labels:
- 'fix'
- title: '♻️ Changes'
- title: '🔄️ Changes'
key: 'changes'
order: 30
labels:
- 'perf'
- 'refactor'
- 'revert'
- 'style'
- title: '🧰 Tasks'
key: 'tasks'
order: 40
labels:
- 'chore'
- title: '🛠 Build'
key: 'build'
order: 50
labels:
- 'test'
- 'build'
- 'ci'
- title: '📝 Documentation'
key: 'docs'
order: 60
labels:
- 'docs'
Expand Down
Expand Up @@ -143,51 +143,63 @@ labelers:
title: 'regex:^(🔖|:bookmark:).*'

categories:
- title: 'Merge'
- title: '🔀 Merge'
key: 'merge'
order: 0
labels:
- 'merge'
- title: 'Added'
- title: '🚀 Added'
key: 'added'
order: 10
labels:
- 'added'
- title: 'Changed'
- title: '🔄️ Changed'
key: 'changed'
order: 20
labels:
- 'changed'
- title: 'Breaking'
- title: '🚨 Breaking'
key: 'breaking'
order: 30
labels:
- 'breaking_changes'
- title: 'Deprecated'
- title: '💥 Deprecated'
key: 'deprecated'
order: 40
labels:
- 'deprecated'
- title: 'Removed'
- title: '🗑 Removed'
key: 'removed'
order: 50
labels:
- 'removed'
- title: 'Fixed'
- title: '🐛 Fixed'
key: 'fixed'
order: 60
labels:
- 'fixed'
- title: 'Security'
- title: '🔒️ Security'
key: 'security'
order: 70
labels:
- 'security'
- title: 'Dependencies'
- title: '📦 Dependencies'
key: 'deps'
order: 80
labels:
- 'dependencies'
- title: 'Documentation'
- title: '📝 Documentation'
key: 'docs'
order: 90
labels:
- 'documentation'
- title: 'Miscellaneous'
- title: '🎲 Miscellaneous'
key: 'misc'
order: 100
labels:
- 'misc'
- title: 'Release'
- title: '🏁 Release'
key: 'release'
order: 110
labels:
- 'release'
Expand Down
Expand Up @@ -88,6 +88,8 @@ interface Changelog {
void contributors(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Contributors) Closure<Void> action)

interface Category {
Property<String> getKey()

Property<String> getTitle()

SetProperty<String> getLabels()
Expand Down
Expand Up @@ -228,13 +228,15 @@ class ChangelogImpl implements Changelog {

@CompileStatic
static class CategoryImpl implements Category {
final Property<String> key
final Property<String> title
final SetProperty<String> labels
final Property<String> format
final Property<Integer> order

@Inject
CategoryImpl(ObjectFactory objects) {
key = objects.property(String).convention(Providers.notDefined())
title = objects.property(String).convention(Providers.notDefined())
labels = objects.setProperty(String).convention(Providers.notDefined())
format = objects.property(String).convention(Providers.notDefined())
Expand All @@ -243,6 +245,7 @@ class ChangelogImpl implements Changelog {

org.jreleaser.model.Changelog.Category toModel() {
org.jreleaser.model.Changelog.Category category = new org.jreleaser.model.Changelog.Category()
category.key = key.orNull
category.title = title.orNull
category.labels = (Set<String>) labels.getOrElse([] as Set)
category.format = format.orNull
Expand Down
Expand Up @@ -259,17 +259,27 @@ public enum Sort {

public static class Category {
private final Set<String> labels = new LinkedHashSet<>();
private String key;
private String title;
private String format;
private Integer order;

void setAll(Category category) {
this.key = category.key;
this.title = category.title;
setLabels(category.labels);
this.format = category.format;
this.order = category.order;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getTitle() {
return title;
}
Expand Down Expand Up @@ -310,14 +320,6 @@ public Integer getOrder() {
public void setOrder(Integer order) {
this.order = order;
}

public static Category of(String title, String format, String... labels) {
Category category = new Category();
category.title = title;
category.format = format;
category.labels.addAll(Arrays.asList(labels));
return category;
}
}

public static class Replacer {
Expand Down
Expand Up @@ -364,6 +364,7 @@ private static Set<org.jreleaser.model.Changelog.Category> convertCategories(Col
Set<org.jreleaser.model.Changelog.Category> set = new LinkedHashSet<>();
for (Changelog.Category category : categories) {
org.jreleaser.model.Changelog.Category c = new org.jreleaser.model.Changelog.Category();
c.setKey(tr(category.getKey()));
c.setTitle(tr(category.getTitle()));
c.setLabels(category.getLabels());
c.setFormat(tr(category.getFormat()));
Expand Down

0 comments on commit f0065e3

Please sign in to comment.