@@ -53,23 +53,16 @@ public class BundleChangelogsTask extends DefaultTask {
5353
5454 private final RegularFileProperty bundleFile ;
5555 private final DirectoryProperty changelogDirectory ;
56+ private final DirectoryProperty changelogBundlesDirectory ;
5657
5758 private final GitWrapper gitWrapper ;
5859
5960 @ Nullable
6061 private String branch ;
6162 @ Nullable
6263 private String bcRef ;
63- // private boolean updateExisting;
64- private boolean finalize ;
6564
66- // @Option(
67- // option = "update-existing",
68- // description = "Only update entries that are already in the bundle. Useful for updating the bundle after a BC has been cut."
69- // )
70- // public void setUpdateExisting(boolean updateExisting) {
71- // this.updateExisting = updateExisting;
72- // }
65+ private boolean finalize ;
7366
7467 @ Option (option = "branch" , description = "Branch (or other ref) to use for generating the changelog bundle." )
7568 public void setBranch (String branch ) {
@@ -105,6 +98,7 @@ public BundleChangelogsTask(ObjectFactory objectFactory, ExecOperations execOper
10598
10699 bundleFile = objectFactory .fileProperty ();
107100 changelogDirectory = objectFactory .directoryProperty ();
101+ changelogBundlesDirectory = objectFactory .directoryProperty ();
108102
109103 gitWrapper = new GitWrapper (execOperations );
110104 }
@@ -118,27 +112,34 @@ public void executeTask() throws IOException {
118112 final String upstreamRemote = gitWrapper .getUpstream ();
119113 Set <String > entriesFromBc = Set .of ();
120114
115+ var didCheckoutChangelogs = false ;
121116 try {
122117 var usingBcRef = bcRef != null && bcRef .isEmpty () == false ;
123118 if (usingBcRef ) {
124119 // Check out all the changelogs that existed at the time of the BC
125120 checkoutChangelogs (gitWrapper , upstreamRemote , bcRef );
126- entriesFromBc = this . changelogDirectory .getAsFileTree ().getFiles ().stream ().map (File ::getName ).collect (Collectors .toSet ());
121+ entriesFromBc = changelogDirectory .getAsFileTree ().getFiles ().stream ().map (File ::getName ).collect (Collectors .toSet ());
127122
128123 // Then add/update changelogs from the HEAD of the branch
129124 // We do an "add" here, rather than checking out the entire directory, in case changelogs have been removed for some reason
130125 addChangelogsFromRef (gitWrapper , upstreamRemote , branch );
131126 } else {
132127 checkoutChangelogs (gitWrapper , upstreamRemote , branch );
133128 }
129+
130+ didCheckoutChangelogs = true ;
134131 Properties props = new Properties ();
135- props .load (new StringReader (gitWrapper .runCommand ("git" , "show" , upstreamRemote + "/" + branch + ":build-tools-internal/version.properties" )));
132+ props .load (
133+ new StringReader (
134+ gitWrapper .runCommand ("git" , "show" , upstreamRemote + "/" + branch + ":build-tools-internal/version.properties" )
135+ )
136+ );
136137 String version = props .getProperty ("elasticsearch" );
137138
138139 LOGGER .info ("Finding changelog files for " + version + "..." );
139140
140141 Set <String > finalEntriesFromBc = entriesFromBc ;
141- List <ChangelogEntry > entries = this . changelogDirectory .getAsFileTree ().getFiles ().stream ().filter (f -> {
142+ List <ChangelogEntry > entries = changelogDirectory .getAsFileTree ().getFiles ().stream ().filter (f -> {
142143 // When not using a bc ref, we just take everything from the branch/sha passed in
143144 if (usingBcRef == false ) {
144145 return true ;
@@ -157,50 +158,46 @@ public void executeTask() throws IOException {
157158 return output .trim ().isEmpty () == false ;
158159 }).map (ChangelogEntry ::parse ).sorted (Comparator .comparing (ChangelogEntry ::getPr )).collect (toList ());
159160
160- ChangelogBundle existingBundle = null ;
161- // if (updateExisting) {
162- // var existingBundleFile = new File("docs/release-notes/changelog-bundles/" + version + ".yml");
163- // if (existingBundleFile.exists()) {
164- // var bundle = ChangelogBundle.parse(existingBundleFile);
165- // existingBundle = bundle;
166- // entries = entries.stream()
167- // .filter(e -> bundle.changelogs().stream().anyMatch(c -> c.getPr().equals(e.getPr())))
168- // .toList();
169- // }
170- // }
171-
172- var isReleased = finalize == true ;
173-
174- ChangelogBundle bundle = new ChangelogBundle (version , isReleased , Instant .now ().toString (), entries );
161+ ChangelogBundle bundle = new ChangelogBundle (version , finalize , Instant .now ().toString (), entries );
175162
176163 yamlMapper .writeValue (new File ("docs/release-notes/changelog-bundles/" + version + ".yml" ), bundle );
177164 } finally {
178- gitWrapper .runCommand ("git" , "restore" , "-s@" , "-SW" , "--" , "docs/changelog" );
165+ if (didCheckoutChangelogs ) {
166+ gitWrapper .runCommand ("git" , "restore" , "-s@" , "-SW" , "--" , changelogDirectory .get ().toString ());
167+ }
179168 }
180169 }
181170
182- private static void checkoutChangelogs (GitWrapper gitWrapper , String upstream , String ref ) {
171+ private void checkoutChangelogs (GitWrapper gitWrapper , String upstream , String ref ) {
183172 gitWrapper .updateRemote (upstream );
184- // TODO check for changes first
185- gitWrapper .runCommand ("rm" , "-rf" , "docs/changelog" );
173+
174+ // If the changelog directory contains modified/new files, we should error out instead of wiping them out silently
175+ var output = gitWrapper .runCommand ("git" , "status" , "--porcelain" , changelogDirectory .get ().toString ()).trim ();
176+ if (output .isEmpty () == false ) {
177+ throw new IllegalStateException (
178+ "Changelog directory contains changes that will be wiped out by this task:\n " + changelogDirectory .get () + "\n " + output
179+ );
180+ }
181+
182+ gitWrapper .runCommand ("rm" , "-rf" , changelogDirectory .get ().toString ());
186183 var refSpec = upstream + "/" + ref ;
187184 if (ref .contains ("upstream/" )) {
188185 refSpec = ref .replace ("upstream/" , upstream + "/" );
189186 } else if (ref .matches ("^[0-9a-f]+$" )) {
190187 refSpec = ref ;
191188 }
192- gitWrapper .runCommand ("git" , "checkout" , refSpec , "--" , "docs/changelog" );
189+ gitWrapper .runCommand ("git" , "checkout" , refSpec , "--" , changelogDirectory . get (). toString () );
193190 }
194191
195- private static void addChangelogsFromRef (GitWrapper gitWrapper , String upstream , String ref ) {
192+ private void addChangelogsFromRef (GitWrapper gitWrapper , String upstream , String ref ) {
196193 var refSpec = upstream + "/" + ref ;
197194 if (ref .contains ("upstream/" )) {
198195 refSpec = ref .replace ("upstream/" , upstream + "/" );
199196 } else if (ref .matches ("^[0-9a-f]+$" )) {
200197 refSpec = ref ;
201198 }
202199
203- gitWrapper .runCommand ("git" , "checkout" , refSpec , "--" , "docs/changelog /*.yaml" );
200+ gitWrapper .runCommand ("git" , "checkout" , refSpec , "--" , changelogDirectory . get () + " /*.yaml" );
204201 }
205202
206203 @ InputDirectory
@@ -212,6 +209,15 @@ public void setChangelogDirectory(Directory dir) {
212209 this .changelogDirectory .set (dir );
213210 }
214211
212+ @ InputDirectory
213+ public DirectoryProperty getChangelogBundlesDirectory () {
214+ return changelogBundlesDirectory ;
215+ }
216+
217+ public void setChangelogBundlesDirectory (Directory dir ) {
218+ this .changelogBundlesDirectory .set (dir );
219+ }
220+
215221 @ InputFiles
216222 public FileCollection getChangelogs () {
217223 return changelogs ;
0 commit comments