From 2e5129edfdddb4c1f81abe72a23b363806dc7f45 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Tue, 26 Mar 2024 18:39:40 +0100 Subject: [PATCH] chore: when multiple installations found for github app, print their ids for easier debugging --- .../GitHubAppCredentials.java | 18 +++++++++++++----- .../GithubAppCredentialsTest.java | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java index 23cca32f1..19f105e99 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java @@ -29,9 +29,11 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import jenkins.scm.api.SCMSource; import jenkins.security.SlaveToMasterCallable; import jenkins.util.JenkinsJVM; @@ -61,7 +63,7 @@ public class GitHubAppCredentials extends BaseStandardCredentials implements Sta private static final String ERROR_NOT_INSTALLED = ERROR_AUTHENTICATING_GITHUB_APP + NOT_INSTALLED; private static final String ERROR_NO_OWNER_MATCHING = "Found multiple installations for GitHub app ID %s but none match credential owner \"%s\". " - + "Set the right owner in the credential advanced options"; + + "Set the right owner in the credential advanced options to one of: %s"; /** * When a new {@link AppInstallationToken} is generated, wait this many seconds before continuing. @@ -227,15 +229,21 @@ static AppInstallationToken generateAppInstallationToken( appInstallation = appInstallations.get(0); } else { final String ownerOrEmpty = owner != null ? owner : ""; - appInstallation = appInstallations.stream() + Optional appInstallationOptional = appInstallations.stream() .filter(installation -> installation .getAccount() .getLogin() .toLowerCase(Locale.ROOT) .equals(ownerOrEmpty.toLowerCase(Locale.ROOT))) - .findAny() - .orElseThrow(() -> new IllegalArgumentException( - String.format(ERROR_NO_OWNER_MATCHING, appId, ownerOrEmpty))); + .findAny(); + if (appInstallationOptional.isEmpty()) { + String logins = appInstallations.stream() + .map(installation -> installation.getAccount().getLogin()) + .collect(Collectors.joining(", ")); + throw new IllegalArgumentException( + String.format(ERROR_NO_OWNER_MATCHING, appId, ownerOrEmpty, logins)); + } + appInstallation = appInstallationOptional.get(); } GHAppInstallationToken appInstallationToken = appInstallation diff --git a/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsTest.java b/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsTest.java index b73219d03..cf1938519 100644 --- a/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsTest.java +++ b/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsTest.java @@ -498,7 +498,7 @@ public void testPassword() throws Exception { assertThat( expected.getMessage(), is("Found multiple installations for GitHub app ID 54321 but none match credential owner \"\". " - + "Set the right owner in the credential advanced options")); + + "Set the right owner in the credential advanced options to one of: cloudbeers, bogus")); } finally { GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS = notStaleSeconds; logRecorder.doClear();