Skip to content

Commit 467fe25

Browse files
committed
#35 [synchronize-github-releases] get targetCommitish parameter from repo branches list
1 parent 4a04cfe commit 467fe25

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

yupiik-tools-maven-plugin/src/main/java/io/yupiik/maven/mojo/SynchronizeReleasesToGithubReleasesMojo.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ public class SynchronizeReleasesToGithubReleasesMojo extends AbstractMojo {
122122
@Parameter(property = "yupiik.synchronize-github-releases.tagPattern")
123123
private String tagPattern;
124124

125-
@Parameter(property = "yupiik.synchronize-github-releases.targetCommitish", defaultValue = "master")
126-
private String targetCommitish;
127-
128125
@Parameter(property = "yupiik.synchronize-github-releases.dryRun")
129126
private boolean dryRun;
130127

@@ -171,14 +168,15 @@ public Thread newThread(final Runnable r) {
171168
final var workDir = Files.createDirectories(Paths.get(workdir));
172169
findTags(httpClient, jsonb, "")
173170
.thenCompose(tags -> findExistingReleases(httpClient, jsonb, "")
174-
.thenCompose(releases -> allOf(artifacts.stream()
175-
.map(spec -> safe(() -> updateArtifact(httpClient, threadPool, jsonb, spec, workDir, tags, releases)))
176-
.toArray(CompletableFuture<?>[]::new))
177-
.whenComplete((r, e) -> {
178-
if (e != null) {
179-
getLog().error(e);
180-
}
181-
})))
171+
.thenCompose(releases -> findBranches(httpClient, jsonb, "")
172+
.thenCompose(branches -> allOf(artifacts.stream()
173+
.map(spec -> safe(() -> updateArtifact(httpClient, threadPool, jsonb, spec, workDir, tags, releases, branches)))
174+
.toArray(CompletableFuture<?>[]::new))
175+
.whenComplete((r, e) -> {
176+
if (e != null) {
177+
getLog().error(e);
178+
}
179+
}))))
182180
.toCompletableFuture().get();
183181
} catch (final InterruptedException e) {
184182
Thread.currentThread().interrupt();
@@ -200,6 +198,7 @@ public Thread newThread(final Runnable r) {
200198
private CompletableFuture<?> synchronizeReleases(final HttpClient httpClient, final Jsonb jsonb,
201199
final ReleaseSpec spec, final String version,
202200
final Map<String, GithubRelease> githubExistingReleases,
201+
final Map<String, GithubBranch> githubBranches,
203202
final Map<String, GithubTag> tags, final Path workDir) {
204203
final var existing = dryRun ? null : githubExistingReleases.get(version);
205204
if (!force && existing != null && !attachIfExists) {
@@ -233,7 +232,7 @@ private CompletableFuture<?> synchronizeReleases(final HttpClient httpClient, fi
233232
release.setDraft(false);
234233
release.setPrerelease(false);
235234
release.setTagName(tagName);
236-
release.setTargetCommitish(targetCommitish);
235+
release.setTargetCommitish(githubBranches.keySet().stream().filter("main"::equals).findAny().orElse("master"));
237236

238237
final var url = URI.create(
239238
githubBaseApi + (githubBaseApi.endsWith("/") ? "" : "/") + "repos/" + githubRepository + "/releases" +
@@ -545,11 +544,11 @@ private String toFilename(final ReleaseSpec spec, final Artifact artifact, final
545544

546545
private CompletableFuture<?> updateArtifact(final HttpClient httpClient, final ExecutorService executorService, final Jsonb jsonb,
547546
final ReleaseSpec spec, final Path workDir,
548-
final Map<String, GithubTag> tags, final Map<String, GithubRelease> ghReleases) {
547+
final Map<String, GithubTag> tags, final Map<String, GithubRelease> ghReleases, final Map<String, GithubBranch> ghBranches) {
549548
final var availableVersions = findAvailableVersions(httpClient, spec);
550549
return availableVersions
551550
.thenComposeAsync(versions -> allOf(versions.stream()
552-
.map(it -> synchronizeReleases(httpClient, jsonb, spec, it, ghReleases, tags, workDir))
551+
.map(it -> synchronizeReleases(httpClient, jsonb, spec, it, ghReleases, ghBranches, tags, workDir))
553552
.toArray(CompletableFuture<?>[]::new)),
554553
executorService);
555554
}
@@ -581,6 +580,33 @@ private CompletableFuture<Map<String, GithubRelease>> findExistingReleases(final
581580
});
582581
}
583582

583+
private CompletableFuture<Map<String, GithubBranch>> findBranches(final HttpClient httpClient, final Jsonb jsonb, final String nextUrl) {
584+
if (nextUrl == null) {
585+
return CompletableFuture.completedFuture(null);
586+
}
587+
final var url = URI.create(nextUrl.isBlank() ?
588+
githubBaseApi + (githubBaseApi.endsWith("/") ? "" : "/") + "repos/" + githubRepository + "/branches?per_page=100" :
589+
nextUrl);
590+
final var reqBuilder = HttpRequest.newBuilder();
591+
findServer(githubServerId).ifPresent(s -> reqBuilder.header("Authorization", toAuthorizationHeaderValue(s)));
592+
return httpClient.sendAsync(reqBuilder
593+
.GET()
594+
.uri(url)
595+
.header("accept", "application/vnd.github.v3+json")
596+
.build(),
597+
HttpResponse.BodyHandlers.ofString())
598+
.thenCompose(r -> {
599+
ensure200(url, r);
600+
final List<GithubBranch> versions = jsonb.fromJson(r.body().trim(), new JohnzonParameterizedType(List.class, GithubBranch.class));
601+
final var versionNames = versions.stream().collect(toMap(GithubBranch::getName, identity()));
602+
return findNextLink(r.headers().firstValue("Link").orElse(null))
603+
.map(next -> findBranches(httpClient, jsonb, next)
604+
.thenApply(added -> Stream.concat(versionNames.entrySet().stream(), added.entrySet().stream())
605+
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a))))
606+
.orElseGet(() -> completedFuture(versionNames));
607+
});
608+
}
609+
584610
private CompletableFuture<Map<String, GithubTag>> findTags(final HttpClient httpClient, final Jsonb jsonb, final String nextUrl) {
585611
if (nextUrl == null) {
586612
return CompletableFuture.completedFuture(Map.of());
@@ -802,4 +828,12 @@ private static class SimpleVersion {
802828
private final int minor;
803829
private final int patch;
804830
}
831+
832+
@Data
833+
public static class GithubBranch {
834+
private String name;
835+
private GithubTagCommit commit;
836+
@JsonbProperty("protected")
837+
private boolean protectedBranch;
838+
}
805839
}

0 commit comments

Comments
 (0)