-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from jenkinsci/check-github-app-credentials
Checks GitHub App Credentials
- Loading branch information
Showing
4 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/io/jenkins/plugins/checks/github/GitHubSCMFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.jenkins.plugins.checks.github; | ||
|
||
import com.cloudbees.plugins.credentials.CredentialsProvider; | ||
import hudson.model.Run; | ||
import jenkins.scm.api.SCMSource; | ||
import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials; | ||
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; | ||
|
||
import java.util.Optional; | ||
|
||
class GitHubSCMFacade { | ||
/** | ||
* Returns the {@link GitHubSCMSource} used in the {@code run}. | ||
* | ||
* @param run | ||
* a Jenkins run | ||
* | ||
* @return the GitHub SCM source if exists | ||
*/ | ||
Optional<GitHubSCMSource> findGitHubSCMSource(final Run<?, ?> run) { | ||
SCMSource source = SCMSource.SourceByItem.findSource(run.getParent()); | ||
return source instanceof GitHubSCMSource ? Optional.of((GitHubSCMSource) source) : Optional.empty(); | ||
} | ||
|
||
/** | ||
* Returns {@link GitHubAppCredentials} configured in the run. | ||
* | ||
* @param run | ||
* a Jenkins run | ||
* @param credentialsId | ||
* the id of credentials | ||
* | ||
* @return the GitHub app credentials if configured | ||
*/ | ||
Optional<GitHubAppCredentials> findGitHubAppCredentials(final Run<?, ?> run, final String credentialsId) { | ||
return Optional.ofNullable( | ||
CredentialsProvider.findCredentialById(credentialsId, GitHubAppCredentials.class, run)); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.jenkins.plugins.checks.github; | ||
|
||
import hudson.model.Run; | ||
import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials; | ||
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class GitHubChecksPublisherFactoryTest { | ||
@Test | ||
void shouldCreateGitHubChecksPublisher() { | ||
Run run = mock(Run.class); | ||
GitHubSCMSource source = mock(GitHubSCMSource.class); | ||
GitHubAppCredentials credentials = mock(GitHubAppCredentials.class); | ||
GitHubSCMFacade scmFacade = mock(GitHubSCMFacade.class); | ||
|
||
when(scmFacade.findGitHubSCMSource(run)).thenReturn(Optional.of(source)); | ||
when(source.getCredentialsId()).thenReturn("credentials id"); | ||
when(scmFacade.findGitHubAppCredentials(run, "credentials id")).thenReturn(Optional.of(credentials)); | ||
|
||
GitHubChecksPublisherFactory factory = new GitHubChecksPublisherFactory(scmFacade); | ||
assertThat(factory.createPublisher(run)) | ||
.isPresent() | ||
.containsInstanceOf(GitHubChecksPublisher.class); | ||
} | ||
|
||
@Test | ||
void shouldReturnEmptyWhenNoGitHubSCMSourceIsConfigured() { | ||
Run run = mock(Run.class); | ||
|
||
GitHubChecksPublisherFactory factory = new GitHubChecksPublisherFactory(); | ||
assertThat(factory.createPublisher(run)) | ||
.isNotPresent(); | ||
} | ||
|
||
@Test | ||
void shouldReturnEmptyWhenNoCredentialsIsConfigured() { | ||
Run run = mock(Run.class); | ||
GitHubSCMSource source = mock(GitHubSCMSource.class); | ||
GitHubSCMFacade scmFacade = mock(GitHubSCMFacade.class); | ||
|
||
when(scmFacade.findGitHubSCMSource(run)).thenReturn(Optional.of(source)); | ||
when(source.getCredentialsId()).thenReturn(null); | ||
|
||
GitHubChecksPublisherFactory factory = new GitHubChecksPublisherFactory(scmFacade); | ||
assertThat(factory.createPublisher(run)) | ||
.isNotPresent(); | ||
} | ||
|
||
@Test | ||
void shouldReturnEmptyWhenNoGitHubAppCredentialsIsConfigured() { | ||
Run run = mock(Run.class); | ||
GitHubSCMSource source = mock(GitHubSCMSource.class); | ||
GitHubSCMFacade scmFacade = mock(GitHubSCMFacade.class); | ||
|
||
when(scmFacade.findGitHubSCMSource(run)).thenReturn(Optional.of(source)); | ||
when(source.getCredentialsId()).thenReturn("credentials id"); | ||
when(scmFacade.findGitHubAppCredentials(run, "credentials id")).thenReturn(Optional.empty()); | ||
|
||
GitHubChecksPublisherFactory factory = new GitHubChecksPublisherFactory(scmFacade); | ||
assertThat(factory.createPublisher(run)) | ||
.isNotPresent(); | ||
} | ||
} |