Skip to content

Commit

Permalink
[JENKINS-72330] Fix test connection in case a GitHub App has more tha…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
netomi authored Nov 16, 2023
1 parent 6bfb8df commit 90e17c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Secret getPrivateKey() {
}

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

try {
// If no owner is specified, check if the app has multiple installations.
if (owner == null || owner.isEmpty()) {
GitHub gitHubApp = TokenProvider.createTokenRefreshGitHub(
appID, privateKey, gitHubAppCredential.actualApiUri());
List<GHAppInstallation> appInstallations =
gitHubApp.getApp().listInstallations().toList();
if (appInstallations.size() > 1) {
// Just pick the owner of the first installation, so we have a valid
// owner to create an access token for testing the connection.
String anyInstallationOwner =
appInstallations.get(0).getAccount().getLogin();
gitHubAppCredential.setOwner(anyInstallationOwner);

Check warning on line 706 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 696-706 are not covered by tests
}
}

GitHub connect = Connector.connect(apiUri, gitHubAppCredential);
try {
return FormValidation.ok("Success, Remaining rate limit: "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<p>
The organisation or user that this app is to be used for.
Only required if this app is installed to multiple organisations.
Only required if this app is installed to multiple organisations and connections
should be restricted to a single organisation.
May be omitted in case credentials are used from GitHub multibranch projects
(in that case the account is determined from the branch source where the credentials are used).
</p>

0 comments on commit 90e17c4

Please sign in to comment.