Skip to content

Commit

Permalink
Merge pull request #6 from jenkinsci/check-github-app-credentials
Browse files Browse the repository at this point in the history
Checks GitHub App Credentials
  • Loading branch information
XiongKezhi authored Jul 12, 2020
2 parents 8c78d54 + 9707e71 commit ef827e2
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.kohsuke.github.GHCheckRunBuilder.Output;
import org.kohsuke.github.GHCheckRunBuilder.Annotation;

import hudson.os.SU;

import io.jenkins.plugins.checks.api.ChecksAction;
import io.jenkins.plugins.checks.api.ChecksAnnotation;
import io.jenkins.plugins.checks.api.ChecksAnnotation.ChecksAnnotationLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import java.util.Optional;

import edu.hm.hafner.util.VisibleForTesting;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource;

import hudson.Extension;
import hudson.model.Run;

import jenkins.scm.api.SCMSource;

import io.jenkins.plugins.checks.api.ChecksPublisher;
import io.jenkins.plugins.checks.api.ChecksPublisherFactory;

Expand All @@ -17,9 +16,34 @@
*/
@Extension
public class GitHubChecksPublisherFactory extends ChecksPublisherFactory {
private final GitHubSCMFacade scmFacade;

/**
* Creates a new instance of {@link GitHubChecksPublisherFactory}.
*/
public GitHubChecksPublisherFactory() {
this(new GitHubSCMFacade());
}

@VisibleForTesting
GitHubChecksPublisherFactory(final GitHubSCMFacade scmFacade) {
super();

this.scmFacade = scmFacade;
}

@Override
protected Optional<ChecksPublisher> createPublisher(final Run<?, ?> run) {
SCMSource source = SCMSource.SourceByItem.findSource(run.getParent());
return source instanceof GitHubSCMSource ? Optional.of(new GitHubChecksPublisher(run)) : Optional.empty();
Optional<GitHubSCMSource> source = scmFacade.findGitHubSCMSource(run);
if (!source.isPresent()) {
return Optional.empty();
}

String credentialsId = source.get().getCredentialsId();
if (credentialsId == null || !scmFacade.findGitHubAppCredentials(run, credentialsId).isPresent()) {
return Optional.empty();
}

return Optional.of(new GitHubChecksPublisher(run));
}
}
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));
}
}
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();
}
}

0 comments on commit ef827e2

Please sign in to comment.