-
Notifications
You must be signed in to change notification settings - Fork 38
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 #34 from jenkinsci/validate-github-scm-source
Validate GitHub scm source
- Loading branch information
Showing
9 changed files
with
401 additions
and
278 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
71 changes: 24 additions & 47 deletions
71
src/main/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherFactory.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 |
---|---|---|
@@ -1,87 +1,64 @@ | ||
package io.jenkins.plugins.checks.github; | ||
|
||
import java.util.Optional; | ||
|
||
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; | ||
|
||
import edu.hm.hafner.util.FilteredLog; | ||
import edu.hm.hafner.util.VisibleForTesting; | ||
import io.jenkins.plugins.checks.api.ChecksPublisher; | ||
import io.jenkins.plugins.checks.api.ChecksPublisherFactory; | ||
import io.jenkins.plugins.util.PluginLogger; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Job; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import io.jenkins.plugins.checks.api.ChecksPublisher; | ||
import io.jenkins.plugins.checks.api.ChecksPublisherFactory; | ||
import io.jenkins.plugins.util.PluginLogger; | ||
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* An factory which produces {@link GitHubChecksPublisher}. | ||
*/ | ||
@Extension | ||
public class GitHubChecksPublisherFactory extends ChecksPublisherFactory { | ||
private final SCMFacade scmFacade; | ||
private final DisplayURLProvider urlProvider; | ||
|
||
/** | ||
* Creates a new instance of {@link GitHubChecksPublisherFactory}. | ||
*/ | ||
public GitHubChecksPublisherFactory() { | ||
this(new SCMFacade()); | ||
this(new SCMFacade(), DisplayURLProvider.get()); | ||
} | ||
|
||
@VisibleForTesting | ||
GitHubChecksPublisherFactory(final SCMFacade scmFacade) { | ||
GitHubChecksPublisherFactory(final SCMFacade scmFacade, final DisplayURLProvider urlProvider) { | ||
super(); | ||
|
||
this.scmFacade = scmFacade; | ||
this.urlProvider = urlProvider; | ||
} | ||
|
||
@Override | ||
protected Optional<ChecksPublisher> createPublisher(final Run<?, ?> run, final TaskListener listener) { | ||
return createPublisher(run, DisplayURLProvider.get().getRunURL(run), listener); | ||
} | ||
|
||
@VisibleForTesting | ||
Optional<ChecksPublisher> createPublisher(final Run<?, ?> run, final String runURL, final TaskListener listener) { | ||
PluginLogger logger = createLogger(getListener(listener)); | ||
|
||
GitSCMChecksContext gitSCMContext = new GitSCMChecksContext(run, runURL); | ||
if (gitSCMContext.isValid(logger)) { | ||
return Optional.of(new GitHubChecksPublisher(gitSCMContext, getListener(listener))); | ||
} | ||
|
||
return createPublisher(listener, logger, new GitHubSCMSourceChecksContext(run, runURL, scmFacade)); | ||
final String runURL = urlProvider.getRunURL(run); | ||
return createPublisher(listener, new GitSCMChecksContext(run, runURL, scmFacade), | ||
new GitHubSCMSourceChecksContext(run, runURL, scmFacade)); | ||
} | ||
|
||
@Override | ||
protected Optional<ChecksPublisher> createPublisher(final Job<?, ?> job, final TaskListener listener) { | ||
return createPublisher(job, DisplayURLProvider.get().getJobURL(job), listener); | ||
} | ||
|
||
@VisibleForTesting | ||
Optional<ChecksPublisher> createPublisher(final Job<?, ?> job, final String jobURL, final TaskListener listener) { | ||
PluginLogger logger = createLogger(getListener(listener)); | ||
|
||
return createPublisher(listener, logger, new GitHubSCMSourceChecksContext(job, jobURL, scmFacade)); | ||
return createPublisher(listener, new GitHubSCMSourceChecksContext(job, urlProvider.getJobURL(job), scmFacade)); | ||
} | ||
|
||
private Optional<ChecksPublisher> createPublisher(final TaskListener listener, final PluginLogger logger, | ||
final GitHubChecksContext gitHubSCMSourceContext) { | ||
if (gitHubSCMSourceContext.isValid(logger)) { | ||
return Optional.of(new GitHubChecksPublisher(gitHubSCMSourceContext, getListener(listener))); | ||
} | ||
return Optional.empty(); | ||
} | ||
private Optional<ChecksPublisher> createPublisher(final TaskListener listener, final GitHubChecksContext... contexts) { | ||
FilteredLog causeLogger = new FilteredLog("Causes for no suitable publisher found: "); | ||
PluginLogger consoleLogger = new PluginLogger(listener.getLogger(), "GitHub Checks"); | ||
|
||
|
||
private TaskListener getListener(final TaskListener taskListener) { | ||
// FIXME: checks-API should use a Null listener | ||
if (taskListener == null) { | ||
return TaskListener.NULL; | ||
for (GitHubChecksContext ctx : contexts) { | ||
if (ctx.isValid(causeLogger)) { | ||
return Optional.of(new GitHubChecksPublisher(ctx, consoleLogger)); | ||
} | ||
} | ||
return taskListener; | ||
} | ||
|
||
private PluginLogger createLogger(final TaskListener listener) { | ||
return new PluginLogger(listener.getLogger(), "GitHub Checks"); | ||
consoleLogger.logEachLine(causeLogger.getErrorMessages()); | ||
return Optional.empty(); | ||
} | ||
} |
Oops, something went wrong.