1919import static java .util .stream .Collectors .toList ;
2020import static org .slf4j .LoggerFactory .getLogger ;
2121import static se .fortnox .changesets .ChangesetWriter .CHANGESET_DIR ;
22+ import static se .fortnox .changesets .Level .DEPENDENCY ;
2223import static se .fortnox .changesets .Level .MAJOR ;
2324import static se .fortnox .changesets .Level .MINOR ;
2425import static se .fortnox .changesets .Level .PATCH ;
@@ -27,9 +28,15 @@ public class ChangelogAggregator {
2728 private static final Logger LOG = getLogger (ChangelogAggregator .class );
2829 public static final String CHANGELOG_FILE = "CHANGELOG.md" ;
2930 private final Path baseDir ;
31+ private final DependencyUpdatesParser dependencyUpdatesParser ;
3032
3133 public ChangelogAggregator (Path baseDir ) {
34+ this (baseDir , new DependencyUpdatesParser ());
35+ }
36+
37+ public ChangelogAggregator (Path baseDir , DependencyUpdatesParser dependencyUpdatesParser ) {
3238 this .baseDir = baseDir ;
39+ this .dependencyUpdatesParser = dependencyUpdatesParser ;
3340 }
3441
3542 /**
@@ -38,23 +45,26 @@ public ChangelogAggregator(Path baseDir) {
3845 *
3946 * @param packageName The package name to get changesets for
4047 * @param version The version number of the merged changes
48+ * @return
4149 */
42- public void mergeChangesetsToChangelog (String packageName , String version ) {
50+ public Path mergeChangesetsToChangelog (String packageName , String version ) {
4351 Path changesetsDir = this .baseDir .resolve (CHANGESET_DIR );
4452
4553 ChangesetLocator changesetLocator = new ChangesetLocator (this .baseDir );
4654 List <Changeset > changesets = changesetLocator .getChangesets (packageName );
4755 if (changesets .isEmpty ()) {
4856 LOG .info ("No changesets found in {}" , this .baseDir );
49- return ;
57+ return changesetsDir ;
5058 }
5159
5260 String changelog = generateChangelog (packageName , version , changesets );
5361
62+ Path changelogFile ;
5463 try {
55- writeChangelog (changelog );
64+ changelogFile = writeChangelog (changelog );
5665 } catch (ChangelogException exception ) {
5766 LOG .error ("Failed to update changelog at {}" , changesetsDir , exception );
67+ return changesetsDir ;
5868 }
5969
6070 changesets .forEach (changeset -> {
@@ -65,26 +75,25 @@ public void mergeChangesetsToChangelog(String packageName, String version) {
6575 LOG .error ("Failed to delete {}" , file , e );
6676 }
6777 });
78+ return changelogFile ;
6879 }
6980
70- private static String generateChangelog (String packageName , String version , List <Changeset > changesets ) {
81+ private String generateChangelog (String packageName , String version , List <Changeset > changesets ) {
7182 String changes = changesets
7283 .stream ()
7384 .collect (groupingBy (Changeset ::level , mapping (Changeset ::message , toList ())))
7485 .entrySet ()
7586 .stream ()
7687 .sorted (sortChangesets ())
7788 .map (entry -> {
78- String level = entry .getKey ().getPresentationString ();
79- String levelChanges = entry .getValue ().stream ()
80- .map (ChangelogAggregator ::formatChangeAsBulletPoint )
81- .sorted ()
82- .collect (Collectors .joining ("\n " ));
89+ Level level = entry .getKey ();
90+ String levelString = level .getPresentationString ();
91+ String levelChanges = formatChangeset (level , entry .getValue ());
8392
8493 return """
85- ### %s Changes
94+ ### %s
8695
87- %s""" .formatted (level , levelChanges );
96+ %s""" .formatted (levelString , levelChanges );
8897 })
8998 .collect (Collectors .joining ("\n \n " ));
9099
@@ -99,6 +108,23 @@ private static String generateChangelog(String packageName, String version, List
99108 return MarkdownFormatter .format (markdown );
100109 }
101110
111+ private String formatChangeset (Level level , List <String > changes ) {
112+ if (level == DEPENDENCY ) {
113+ // Extract all dependencies from each dependency change and put them into a single list
114+ return changes .stream ()
115+ .flatMap (change -> dependencyUpdatesParser .parseDependencyChangeset (change ).stream ())
116+ .map (ChangelogAggregator ::formatChangeAsBulletPoint )
117+ .distinct ()
118+ .sorted ()
119+ .collect (Collectors .joining ("\n " ));
120+ }
121+
122+ return changes .stream ()
123+ .map (ChangelogAggregator ::formatChangeAsBulletPoint )
124+ .sorted ()
125+ .collect (Collectors .joining ("\n " ));
126+ }
127+
102128 private static String formatChangeAsBulletPoint (String change ) {
103129 // Add the change as a bullet point, with leading dash and each subsequent line indented with two spaces
104130 String firstLinePrefix = "- " ;
@@ -118,7 +144,7 @@ private static String formatChangeAsBulletPoint(String change) {
118144 }
119145
120146 private static Comparator <Map .Entry <Level , List <String >>> sortChangesets () {
121- List <Level > levelOrder = List .of (MAJOR , MINOR , PATCH );
147+ List <Level > levelOrder = List .of (MAJOR , MINOR , PATCH , DEPENDENCY );
122148
123149 return (o1 , o2 ) -> {
124150 // Sort levels in the order specified in levelOrder
@@ -135,9 +161,10 @@ private static Comparator<Map.Entry<Level, List<String>>> sortChangesets() {
135161 * If the file already exists, trim the first header before prepending it with the new changelog entries.
136162 *
137163 * @param changelog The new changelog content
164+ * @return
138165 * @throws ChangelogException Thrown if file operations on the existing or new file are unsuccessful
139166 */
140- private void writeChangelog (String changelog ) throws ChangelogException {
167+ private Path writeChangelog (String changelog ) throws ChangelogException {
141168 Path changelogFile = this .baseDir .resolve (CHANGELOG_FILE );
142169
143170 if (Files .exists (changelogFile )) {
@@ -146,6 +173,7 @@ private void writeChangelog(String changelog) throws ChangelogException {
146173
147174 try {
148175 Files .writeString (changelogFile , changelog , TRUNCATE_EXISTING , CREATE );
176+ return changelogFile ;
149177
150178 } catch (IOException e ) {
151179 throw new ChangelogException ("Failed to write " + changelogFile , e );
0 commit comments