Skip to content

Commit

Permalink
chore: when multiple installations found (#776)
Browse files Browse the repository at this point in the history
for github app, print their ids for easier debugging
  • Loading branch information
carlossg authored Mar 26, 2024
1 parent b112297 commit 99802b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -227,15 +229,21 @@ static AppInstallationToken generateAppInstallationToken(
appInstallation = appInstallations.get(0);
} else {
final String ownerOrEmpty = owner != null ? owner : "";
appInstallation = appInstallations.stream()
Optional<GHAppInstallation> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 99802b6

Please sign in to comment.