Skip to content

Commit 90e17c4

Browse files
authored
[JENKINS-72330] Fix test connection in case a GitHub App has more than 1 installation (#748)
* Add additional check when testing the connection if the configured GitHub App has more than 1 installation, in which case the owner of the first installation is used to test the connection. Clarify the documentation of the owner field that it is not required unless you wanna restrict to a single organization. * Apply spotless complaints.
1 parent 6bfb8df commit 90e17c4

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public Secret getPrivateKey() {
123123
}
124124

125125
/**
126-
* Owner of this installation, i.e. a user or organisation, used to differeniate app installations
126+
* Owner of this installation, i.e. a user or organisation, used to differentiate app installations
127127
* when the app is installed to multiple organisations / users.
128128
*
129129
* <p>If this is null then call listInstallations and if there's only one in the list then use
@@ -692,6 +692,21 @@ public FormValidation doTestConnection(
692692
gitHubAppCredential.setOwner(owner);
693693

694694
try {
695+
// If no owner is specified, check if the app has multiple installations.
696+
if (owner == null || owner.isEmpty()) {
697+
GitHub gitHubApp = TokenProvider.createTokenRefreshGitHub(
698+
appID, privateKey, gitHubAppCredential.actualApiUri());
699+
List<GHAppInstallation> appInstallations =
700+
gitHubApp.getApp().listInstallations().toList();
701+
if (appInstallations.size() > 1) {
702+
// Just pick the owner of the first installation, so we have a valid
703+
// owner to create an access token for testing the connection.
704+
String anyInstallationOwner =
705+
appInstallations.get(0).getAccount().getLogin();
706+
gitHubAppCredential.setOwner(anyInstallationOwner);
707+
}
708+
}
709+
695710
GitHub connect = Connector.connect(apiUri, gitHubAppCredential);
696711
try {
697712
return FormValidation.ok("Success, Remaining rate limit: "
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<p>
22
The organisation or user that this app is to be used for.
3-
Only required if this app is installed to multiple organisations.
3+
Only required if this app is installed to multiple organisations and connections
4+
should be restricted to a single organisation.
45
May be omitted in case credentials are used from GitHub multibranch projects
56
(in that case the account is determined from the branch source where the credentials are used).
67
</p>

0 commit comments

Comments
 (0)