Skip to content

Commit c09e017

Browse files
authored
[JENKINS-76235] Fix 500 error when saving organization folder with credentials using an invalid owner (#909)
1 parent 4cab2c6 commit c09e017

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ public void afterSave(@NonNull SCMNavigatorOwner owner) {
17161716
} finally {
17171717
Connector.release(hub);
17181718
}
1719-
} catch (IOException e) {
1719+
} catch (Exception e) {
17201720
DescriptorImpl.LOGGER.log(Level.WARNING, e.getMessage(), e);
17211721
}
17221722
}

src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigatorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525

2626
package org.jenkinsci.plugins.github_branch_source;
2727

28+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
29+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
30+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
2831
import static org.hamcrest.MatcherAssert.assertThat;
2932
import static org.hamcrest.Matchers.*;
3033
import static org.junit.Assert.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3135

3236
import com.cloudbees.plugins.credentials.Credentials;
3337
import com.cloudbees.plugins.credentials.CredentialsScope;
@@ -67,6 +71,7 @@
6771
import org.hamcrest.Matchers;
6872
import org.junit.Before;
6973
import org.junit.Test;
74+
import org.jvnet.hudson.test.Issue;
7075
import org.jvnet.hudson.test.MockAuthorizationStrategy;
7176
import org.jvnet.hudson.test.MockFolder;
7277
import org.mockito.Mock;
@@ -576,6 +581,30 @@ public void doFillScanCredentials() throws Exception {
576581
}
577582
}
578583

584+
@Issue("JENKINS-76235")
585+
@Test
586+
public void afterSaveWithGitHubAppOwnerMismatch() {
587+
// Given GitHub App credentials
588+
githubApi.stubFor(get(urlEqualTo("/app"))
589+
.willReturn(aResponse()
590+
.withHeader("Content-Type", "application/json")
591+
.withBody("{\"id\":54321,\"name\":\"test-app\"}")));
592+
githubApi.stubFor(get(urlEqualTo("/app/installations"))
593+
.willReturn(aResponse()
594+
.withHeader("Content-Type", "application/json")
595+
.withBody("[{\"id\":654321,\"account\":{\"login\":\"corp\"},\"app_id\":54321}]")));
596+
GitHubAppCredentials appCredentials = GitHubApp.createCredentials("test-app-creds");
597+
appCredentials.setApiUri(githubApi.baseUrl());
598+
// And using a owner that doesn't have the application installed
599+
appCredentials.setOwner("org-or-user-without-app");
600+
setCredentials(Collections.singletonList(appCredentials));
601+
602+
// When creating a SCMNavigator and calling afterSave
603+
navigator = navigatorForRepoOwner("corp", appCredentials.getId());
604+
assertDoesNotThrow(() -> navigator.afterSave(Mockito.mock(SCMNavigatorOwner.class)));
605+
// Then no exception
606+
}
607+
579608
private SCMSourceObserver getObserver(Collection<String> names) {
580609
return new SCMSourceObserver() {
581610
@NonNull

0 commit comments

Comments
 (0)