8000 Use tags and branches of the main repository for release labels by ahus1 · Pull Request #102 · keycloak/keycloak-github-bot · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Linking works for open and closed issues without requiring backport labels.

When a PR is merged into `main` or a `release/*` branch, the issues it closes (from the PR description or the merge commit message)
get the next `release/` label and lose the `backport/` label of the target branch.
The branches and tags of the repository configured as `repository.mainRepository` always determine the next version, also for PRs merged in other repositories where the bot is installed.

## Testing changes to the application

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/keycloak/gh/bot/AddReleaseLabelOnMerge.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.keycloak.gh.bot;

import io.quarkiverse.githubapp.event.PullRequest;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import org.keycloak.gh.bot.utils.Labels;
import org.keycloak.gh.bot.utils.ReleaseLabels;
Expand All @@ -10,6 +11,7 @@
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTag;
import org.kohsuke.github.GitHub;

import java.io.IOException;
import java.util.List;
Expand All @@ -23,7 +25,10 @@ public class AddReleaseLabelOnMerge {

private static final Logger logger = Logger.getLogger(AddReleaseLabelOnMerge.class);

void onClose(@PullRequest.Closed GHEventPayload.PullRequest payload) throws IOException {
@ConfigProperty(name = "repository.mainRepository")
String mainRepository;

void onClose(@PullRequest.Closed GHEventPayload.PullRequest payload, GitHub gitHub) throws IOException {
GHPullRequest pullRequest = payload.getPullRequest();
if (!pullRequest.isMerged()) {
return;
Expand All @@ -32,9 +37,16 @@ void onClose(@PullRequest.Closed GHEventPayload.PullRequest payload) throws IOEx
GHRepository repository = payload.getRepository();
String base = pullRequest.getBase().getRef();

// Only fetch what is needed for the target branch
List<String> branches = base.equals("main") ? repository.getBranches().keySet().stream().toList() : List.of();
List<String> tags = base.startsWith("release/") ? repository.listTags().toList().stream().map(GHTag::getName).toList() : List.of();
// The branches and tags of the main repository are the source of truth for the next release, also for PRs in other repositories.
// The main repository is public, so it can be read with the installation of the PR's repository.
// Only fetch what is needed for the target branch.
List<String> branches = List.of();
List<String> tags = List.of();
if (base.equals("main")) {
branches = gitHub.getRepository(mainRepository).getBranches().keySet().stream().toList();
} else if (base.startsWith("release/")) {
tags = gitHub.getRepository(mainRepository).listTags().toList().stream().map(GHTag::getName).toList();
}

ReleaseLabels.Result labels = ReleaseLabels.forBaseBranch(base, branches, tags);
if (labels == null) {
Expand Down
55 changes: 37 additions & 18 deletions src/test/java/org/keycloak/gh/bot/AddReleaseLabelOnMergeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GHTag;
import org.kohsuke.github.PagedIterable;
import org.kohsuke.github.PagedIterator;
Expand All @@ -35,12 +36,16 @@ class AddReleaseLabelOnMergeTest {
private final AddReleaseLabelOnMerge handler = new AddReleaseLabelOnMerge();
private final GHEventPayload.PullRequest payload = mock(GHEventPayload.PullRequest.class);
private final GHRepository repository = mock(GHRepository.class);
private final GitHub gitHub = mock(GitHub.class);
private final GHRepository mainRepo = mock(GHRepository.class);
private final GHPullRequest pr = mock(GHPullRequest.class);
private final GHCommitPointer base = mock(GHCommitPointer.class);
private final GHIssue issue = mock(GHIssue.class);

@BeforeEach
void setUp() throws Exception {
handler.mainRepository = "keycloak/keycloak";
when(gitHub.getRepository("keycloak/keycloak")).thenReturn(mainRepo);
when(payload.getRepository()).thenReturn(repository);
when(repository.getFullName()).thenReturn("keycloak/keycloak");
when(payload.getPullRequest()).thenReturn(pr);
Expand All @@ -49,7 +54,7 @@ void setUp() throws Exception {
when(pr.getBody()).thenReturn("Closes #123");
when(pr.getBase()).thenReturn(base);
when(base.getRef()).thenReturn("main");
when(repository.getBranches()).thenReturn(Map.of(
when(mainRepo.getBranches()).thenReturn(Map.of(
"main", mock(GHBranch.class), "release/26.7", mock(GHBranch.class), "release/26.8", mock(GHBranch.class)));
when(repository.getIssue(123)).thenReturn(issue);
issueLabels("backport/main");
Expand All @@ -58,12 +63,12 @@ void setUp() throws Exception {

@Test
void mainCreatesLabelAndMovesIssue() throws Exception {
handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(repository).createLabel("release/27.0.0", "0E8A16");
verify(issue).addLabels("release/27.0.0");
verify(issue).removeLabel("backport/main");
verify(repository, never()).listTags();
verify(mainRepo, never()).listTags();
}

@Test
Expand All @@ -72,20 +77,20 @@ void releaseBranchUsesNextMicroAndKeepsOtherBackportLabels() throws Exception {
repositoryTags("26.8.0", "26.8.2", "26.7.5", "nightly");
issueLabels("backport/26.8", "backport/main");

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(repository).createLabel("release/26.8.3", "0E8A16");
verify(issue).addLabels("release/26.8.3");
verify(issue).removeLabel("backport/26.8");
verify(issue, never()).removeLabel("backport/main");
verify(repository, never()).getBranches();
verify(mainRepo, never()).getBranches();
}

@Test
void existingLabelIsNotCreatedAgain() throws Exception {
repositoryLabels("release/27.0.0");

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(repository, never()).createLabel(anyString(), anyString());
verify(issue).addLabels("release/27.0.0");
Expand All @@ -98,7 +103,7 @@ void labelCreatedConcurrentlyIsAccepted() throws Exception {
throw new IOException("already exists");
});

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(issue).addLabels("release/27.0.0");
}
Expand All @@ -107,7 +112,7 @@ void labelCreatedConcurrentlyIsAccepted() throws Exception {
void labelCreationFailureIsPropagated() throws Exception {
when(repository.createLabel("release/27.0.0", "0E8A16")).thenThrow(new IOException("forbidden"));

assertThrows(IOException.class, () -> handler.onClose(payload));
assertThrows(IOException.class, () -> handler.onClose(payload, gitHub));

verify(issue, never()).addLabels(anyString());
}
Expand All @@ -116,7 +121,7 @@ void labelCreationFailureIsPropagated() throws Exception {
void issueWithoutBackportLabelOnlyGetsReleaseLabel() throws Exception {
issueLabels();

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(issue).addLabels("release/27.0.0");
verify(issue, never()).removeLabel(anyString());
Expand All @@ -130,7 +135,7 @@ void missingIssuesAndPullRequestsAreSkipped() throws Exception {
when(repository.getIssue(456)).thenReturn(otherPr);
when(pr.getBody()).thenReturn("Closes #999\nCloses #456\nFixes #123");

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(issue).addLabels("release/27.0.0");
verify(otherPr, never()).addLabels(anyString());
Expand All @@ -147,7 +152,7 @@ void commitMessageIsUsedWhenDescriptionHasNoIssue() throws Exception {
when(commit.getCommitShortInfo()).thenReturn(info);
when(info.getMessage()).thenReturn("Some change\n\nCloses #123");

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(issue).addLabels("release/27.0.0");
}
Expand All @@ -157,17 +162,31 @@ void commitIsNotFetchedWhenDescriptionHasIssue() throws Exception {
when(pr.getBody()).thenReturn("Closes #123");
when(pr.getMergeCommitSha()).thenReturn("abc");

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(issue).addLabels("release/27.0.0");
verify(repository, never()).getCommit(anyString());
}

@Test
void otherRepositoriesUseTagsOfMainRepository() throws Exception {
when(repository.getFullName()).thenReturn("keycloak/other-repository");
when(base.getRef()).thenReturn("release/26.8");
repositoryTags("26.8.0", "26.8.2");
issueLabels("backport/26.8");

handler.onClose(payload, gitHub);

verify(repository).createLabel("release/26.8.3", "0E8A16");
verify(issue).addLabels("release/26.8.3");
verify(repository, never()).listTags();
}

@Test
void unmergedPRsAreIgnored() throws Exception {
when(pr.isMerged()).thenReturn(false);

handler.onClose(payload);
handler.onClose(payload, gitHub);

verifyNoInteractions(repository);
}
Expand All @@ -176,18 +195,18 @@ void unmergedPRsAreIgnored() throws Exception {
void unrelatedBranchesAreIgnored() throws Exception {
when(base.getRef()).thenReturn("feature/x");

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(repository, never()).getBranches();
verify(repository, never()).listTags();
verify(mainRepo, never()).getBranches();
verify(mainRepo, never()).listTags();
verify(repository, never()).getIssue(123);
}

@Test
void noLinkedIssuesDoesNothing() throws Exception {
when(pr.getBody()).thenReturn("Just a change");

handler.onClose(payload);
handler.onClose(payload, gitHub);

verify(repository, never()).createLabel(anyString(), anyString());
verify(repository, never()).getIssue(123);
Expand Down Expand Up @@ -228,7 +247,7 @@ private void repositoryTags(String... names) throws IOException {
}).toList();
PagedIterable<GHTag> iterable = mock(PagedIterable.class);
when(iterable.toList()).thenReturn(tags);
when(repository.listTags()).thenReturn(iterable);
when(mainRepo.listTags()).thenReturn(iterable);
}

}
Loading
0