Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: when multiple installations found #776

Merged
merged 1 commit into from
Mar 26, 2024
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
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