|
34 | 34 | import com.google.common.collect.Sets; |
35 | 35 | import com.google.common.collect.Sets.SetView; |
36 | 36 | import com.google.common.collect.Streams; |
37 | | -import com.google.common.io.CharStreams; |
38 | | -import com.google.common.io.Files; |
39 | 37 | import java.io.File; |
40 | 38 | import java.io.IOException; |
41 | | -import java.io.InputStreamReader; |
42 | 39 | import java.nio.charset.StandardCharsets; |
43 | 40 | import java.util.ArrayList; |
44 | 41 | import java.util.HashMap; |
|
49 | 46 | import java.util.concurrent.TimeUnit; |
50 | 47 | import java.util.regex.Matcher; |
51 | 48 | import java.util.regex.Pattern; |
52 | | -import org.apache.commons.codec.Charsets; |
53 | 49 | import org.eclipse.aether.RepositorySystem; |
54 | 50 | import org.eclipse.aether.artifact.Artifact; |
55 | 51 | import org.eclipse.aether.artifact.DefaultArtifact; |
@@ -111,21 +107,50 @@ public static void main(String[] arguments) |
111 | 107 | ReleaseNoteGeneration generation = new ReleaseNoteGeneration(); |
112 | 108 | String report = generation.generateReport(bom, googleCloudJavaVersion); |
113 | 109 |
|
114 | | - Files.asCharSink(new File(RELEASE_NOTE_FILE_NAME), Charsets.UTF_8).write(report); |
| 110 | + com.google.common.io.Files.asCharSink(new File(RELEASE_NOTE_FILE_NAME), StandardCharsets.UTF_8) |
| 111 | + .write(report); |
115 | 112 | System.out.println("Wrote " + RELEASE_NOTE_FILE_NAME); |
116 | 113 | } |
117 | 114 |
|
118 | 115 | @VisibleForTesting final StringBuilder report = new StringBuilder(); |
| 116 | + private boolean monorepoReleaseExists = true; |
119 | 117 |
|
120 | 118 | @VisibleForTesting |
121 | 119 | ReleaseNoteGeneration() {} |
122 | 120 |
|
| 121 | + private static boolean releaseExists(String repository, String tag) { |
| 122 | + Process process = null; |
| 123 | + try { |
| 124 | + ProcessBuilder builder = |
| 125 | + new ProcessBuilder( |
| 126 | + "gh", "release", "--repo", GOOGLEAPIS_ORG + "/" + repository, "view", tag); |
| 127 | + builder.redirectOutput(ProcessBuilder.Redirect.DISCARD); |
| 128 | + builder.redirectError(ProcessBuilder.Redirect.DISCARD); |
| 129 | + process = builder.start(); |
| 130 | + boolean finished = process.waitFor(1, TimeUnit.MINUTES); |
| 131 | + if (!finished) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + return process.exitValue() == 0; |
| 135 | + } catch (IOException e) { |
| 136 | + return false; |
| 137 | + } catch (InterruptedException e) { |
| 138 | + Thread.currentThread().interrupt(); |
| 139 | + return false; |
| 140 | + } finally { |
| 141 | + if (process != null && process.isAlive()) { |
| 142 | + process.destroyForcibly(); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
123 | 147 | @VisibleForTesting |
124 | 148 | String generateReport(Bom bom, String googleCloudJavaVersion) |
125 | 149 | throws MavenRepositoryException, |
126 | 150 | ArtifactDescriptorException, |
127 | 151 | IOException, |
128 | 152 | InterruptedException { |
| 153 | + monorepoReleaseExists = releaseExists("google-cloud-java", "v" + googleCloudJavaVersion); |
129 | 154 | Bom previousBom = previousBom(bom); |
130 | 155 |
|
131 | 156 | DefaultArtifact bomArtifact = new DefaultArtifact(bom.getCoordinates()); |
@@ -425,10 +450,15 @@ private static String releaseUrlForSplitRepo(String libraryName, String version) |
425 | 450 | "https://github.com/googleapis/java-%s/releases/tag/v%s", libraryName, version); |
426 | 451 | } |
427 | 452 |
|
428 | | - private static String releaseUrlForMonorepo(String libraryName, String version) { |
429 | | - // libraryName is unused for the monorepo release note as of Dec 2022 |
430 | | - return String.format( |
431 | | - "https://github.com/googleapis/google-cloud-java/releases/tag/v%s", version); |
| 453 | + private String releaseUrlForMonorepo(String libraryName, String version) { |
| 454 | + if (monorepoReleaseExists) { |
| 455 | + return String.format( |
| 456 | + "https://github.com/googleapis/google-cloud-java/releases/tag/v%s", version); |
| 457 | + } else { |
| 458 | + return String.format( |
| 459 | + "https://github.com/googleapis/google-cloud-java/releases/tag/v%s-%s", |
| 460 | + version, libraryName); |
| 461 | + } |
432 | 462 | } |
433 | 463 |
|
434 | 464 | /** |
@@ -615,16 +645,41 @@ static String fetchReleaseNote(String owner, String repository, String tag) |
615 | 645 | throws IOException, InterruptedException { |
616 | 646 | // gh release --repo googleapis/java-storage view v2.16.0 |
617 | 647 |
|
618 | | - ProcessBuilder builder = |
619 | | - new ProcessBuilder("gh", "release", "--repo", owner + "/" + repository, "view", tag); |
620 | | - builder.redirectErrorStream(true); |
621 | | - Process process = builder.start(); |
622 | | - String output = |
623 | | - CharStreams.toString( |
624 | | - new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); |
625 | | - boolean finished = process.waitFor(1, TimeUnit.MINUTES); |
626 | | - Verify.verify(finished, "The process timed out"); |
627 | | - Verify.verify(0 == process.exitValue(), "The command failed: %s", output); |
628 | | - return output; |
| 648 | + File tempFile = java.nio.file.Files.createTempFile("gh-release-notes", ".txt").toFile(); |
| 649 | + tempFile.deleteOnExit(); |
| 650 | + |
| 651 | + Process process = null; |
| 652 | + try { |
| 653 | + ProcessBuilder builder = |
| 654 | + new ProcessBuilder("gh", "release", "--repo", owner + "/" + repository, "view", tag); |
| 655 | + builder.redirectErrorStream(true); |
| 656 | + builder.redirectOutput(ProcessBuilder.Redirect.to(tempFile)); |
| 657 | + |
| 658 | + process = builder.start(); |
| 659 | + boolean finished = process.waitFor(1, TimeUnit.MINUTES); |
| 660 | + if (!finished) { |
| 661 | + throw new IOException("The process timed out"); |
| 662 | + } |
| 663 | + |
| 664 | + String output = |
| 665 | + new String(java.nio.file.Files.readAllBytes(tempFile.toPath()), StandardCharsets.UTF_8); |
| 666 | + |
| 667 | + if (process.exitValue() != 0) { |
| 668 | + String trimmedOutput = output.trim(); |
| 669 | + String lowerOutput = trimmedOutput.toLowerCase(); |
| 670 | + if (lowerOutput.contains("not found") || lowerOutput.contains("404")) { |
| 671 | + System.err.println( |
| 672 | + "Warning: The command failed (release likely not found): " + trimmedOutput); |
| 673 | + return ""; |
| 674 | + } |
| 675 | + throw new IOException("The command failed: " + trimmedOutput); |
| 676 | + } |
| 677 | + return output; |
| 678 | + } finally { |
| 679 | + if (process != null && process.isAlive()) { |
| 680 | + process.destroyForcibly(); |
| 681 | + } |
| 682 | + tempFile.delete(); |
| 683 | + } |
629 | 684 | } |
630 | 685 | } |
0 commit comments