Skip to content

Commit 99802b6

Browse files
authored
chore: when multiple installations found (#776)
for github app, print their ids for easier debugging
1 parent b112297 commit 99802b6

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import java.util.List;
3030
import java.util.Locale;
3131
import java.util.Map;
32+
import java.util.Optional;
3233
import java.util.concurrent.TimeUnit;
3334
import java.util.logging.Level;
3435
import java.util.logging.Logger;
36+
import java.util.stream.Collectors;
3537
import jenkins.scm.api.SCMSource;
3638
import jenkins.security.SlaveToMasterCallable;
3739
import jenkins.util.JenkinsJVM;
@@ -61,7 +63,7 @@ public class GitHubAppCredentials extends BaseStandardCredentials implements Sta
6163
private static final String ERROR_NOT_INSTALLED = ERROR_AUTHENTICATING_GITHUB_APP + NOT_INSTALLED;
6264
private static final String ERROR_NO_OWNER_MATCHING =
6365
"Found multiple installations for GitHub app ID %s but none match credential owner \"%s\". "
64-
+ "Set the right owner in the credential advanced options";
66+
+ "Set the right owner in the credential advanced options to one of: %s";
6567

6668
/**
6769
* When a new {@link AppInstallationToken} is generated, wait this many seconds before continuing.
@@ -227,15 +229,21 @@ static AppInstallationToken generateAppInstallationToken(
227229
appInstallation = appInstallations.get(0);
228230
} else {
229231
final String ownerOrEmpty = owner != null ? owner : "";
230-
appInstallation = appInstallations.stream()
232+
Optional<GHAppInstallation> appInstallationOptional = appInstallations.stream()
231233
.filter(installation -> installation
232234
.getAccount()
233235
.getLogin()
234236
.toLowerCase(Locale.ROOT)
235237
.equals(ownerOrEmpty.toLowerCase(Locale.ROOT)))
236-
.findAny()
237-
.orElseThrow(() -> new IllegalArgumentException(
238-
String.format(ERROR_NO_OWNER_MATCHING, appId, ownerOrEmpty)));
238+
.findAny();
239+
if (appInstallationOptional.isEmpty()) {
240+
String logins = appInstallations.stream()
241+
.map(installation -> installation.getAccount().getLogin())
242+
.collect(Collectors.joining(", "));
243+
throw new IllegalArgumentException(
244+
String.format(ERROR_NO_OWNER_MATCHING, appId, ownerOrEmpty, logins));
245+
}
246+
appInstallation = appInstallationOptional.get();
239247
}
240248

241249
GHAppInstallationToken appInstallationToken = appInstallation

src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public void testPassword() throws Exception {
498498
assertThat(
499499
expected.getMessage(),
500500
is("Found multiple installations for GitHub app ID 54321 but none match credential owner \"\". "
501-
+ "Set the right owner in the credential advanced options"));
501+
+ "Set the right owner in the credential advanced options to one of: cloudbeers, bogus"));
502502
} finally {
503503
GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS = notStaleSeconds;
504504
logRecorder.doClear();

0 commit comments

Comments
 (0)