Skip to content

Conversation

@vwagh-dev
Copy link
Contributor

@vwagh-dev vwagh-dev commented May 29, 2025

Description

This is a follow up PR for JENKINS-73474 where I'm fixing the issue while validating configuration when owner is set to empty.

Issue:

On click of validate, it iterates through all the GHAppInstallation & check for valid repo owner match but since its empty hence no match founds & throws an error.

final String ownerOrEmpty = owner != null ? owner : "";
Optional<GHAppInstallation> appInstallationOptional = appInstallations.stream()
.filter(installation -> installation
.getAccount()
.getLogin()
.toLowerCase(Locale.ROOT)
.equals(ownerOrEmpty.toLowerCase(Locale.ROOT)))
.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));
}

whereas when user configures the Github app credentials at time also Test Connections executes successfully without an error while owner is empty. It executes successfully because it reads the owner name from GHAppInstallation.

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);
}
}

Fix

Reading the repo owner name from the path & then verifying the connection. If its valid url then its executes successfully else it throws an error.

Manual Tests

Prerequisites:
New Item -> Multibranch pipeline -> Branch Source -> Add Github -> In Github section ->
Add new credentials where kind 'Github App' -> Add the appid & pem key -> In Advanced section keep owner as blank

  • vwagh-org1 has the repo named as multibranch-p1
  • vwagh-org2 not installed the github auth yet

Case1: Invalid repo path

In Multibranch pipeline -> select the github credentials -> put the repository HTTPS url
i.e. https://github.com/vwagh-org2/multibranch-p1 -> Validate -> Angry jenkins

2025-05-29 09:10:52.599+0000 [id=128]	WARNING	h.i.i.InstallUncaughtExceptionHandler#handleException: Caught unhandled exception with ID a9c246e9-f79b-4dcc-bf3d-fe78c5f9054f
java.lang.IllegalArgumentException: Owner mismatch: vwagh-org1 vs. vwagh-org2
	at PluginClassLoader for github-branch-source//org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials.withOwner(GitHubAppCredentials.java:342)
	at PluginClassLoader for github-branch-source//org.jenkinsci.plugins.github_branch_source.Connector.lookupScanCredentials(Connector.java:311)

Case2: Valid repo path

Configure the repo url to https://github.com/vwagh-org1/multibranch-p1 -> Validate successfully.

Case3: Configure the wrong owner (i.e. org name) + test connection

Update credentials -> Advanced -> set owner to 'vwagh-org2' -> Test Connection -> Error

because vwagh-org2 don't have install the github auth yet

java.lang.IllegalArgumentException: Found multiple installations for GitHub app ID 1333318 but none match credential owner "vwagh-org2". Set the right owner in the credential advanced options to one of: vwagh-org1, vwagh-dev
	at PluginClassLoader for github-branch-source//org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials.generateAppInstallationToken(GitHubAppCredentials.java:243)
	at PluginClassLoader for github-branch-source//org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials.getToken(GitHubAppCredentials.java:294)

Case4: Install git auth + test connection

Next install the github auth on vwagh-org2 -> Test Connection -> Its successful.

Case5: Configured wrong org + but valid repo path

Configure the wrong owner name i.e. vwagh-org2 -> Goto multibranch pipeline -> Configure -> put correct repo url
https://github.com/vwagh-org1/multibranch-p1 -> Angry Jenkins

Error logs

2025-05-29 07:16:48.889+0000 [id=38]	WARNING	h.i.i.InstallUncaughtExceptionHandler#handleException: Caught unhandled exception with ID 7e581786-f584-4218-88a4-de2772dfac52
java.lang.IllegalArgumentException: Owner mismatch: vwagh-org2 vs. vwagh-org1
	at PluginClassLoader for github-branch-source//org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials.withOwner(GitHubAppCredentials.java:342)
	at PluginClassLoader for github-branch-source//org.jenkinsci.plugins.github_branch_source.Connector.lookupScanCredentials(Connector.java:311)

Case6: Configured valid org + valid repo path + Scan the repo

Configure the wrong owner name i.e. vwagh-org1 -> Goto multibranch pipeline -> Configure -> put correct repo url
https://github.com/vwagh-org1/multibranch-p1 -> Validates successfully

Next scan the repo -> pipeline for main branch run successfully

Submitter checklist

  • Link to JIRA ticket in description, if appropriate.
  • Change is code complete and matches issue description
  • Automated tests have been added to exercise the changes
  • Reviewer's manual test instructions provided in PR description. See Reviewer's first task below.

Reviewer checklist

  • Run the changes and verify that the change matches the issue description
  • Reviewed the code
  • Verified that the appropriate tests have been written or valid explanation given

Documentation changes

  • Link to jenkins.io PR, or an explanation for why no doc changes are needed

Users/aliases to notify

@vwagh-dev vwagh-dev marked this pull request as ready for review May 29, 2025 09:32
@vwagh-dev vwagh-dev requested a review from a team as a code owner May 29, 2025 09:32
@jglick jglick added the bug label May 30, 2025
Copy link
Member

@jglick jglick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not really follow, but list of manual tests sounds right.

@jglick jglick merged commit 9eec8e5 into jenkinsci:master May 30, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants