diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java index a516721..e138ac3 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java @@ -37,6 +37,8 @@ import java.util.logging.Logger; import java.util.stream.Collectors; import javax.annotation.Nonnull; + +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.transport.URIish; import hudson.model.Job; @@ -47,7 +49,6 @@ import hudson.security.ACL; import hudson.security.ACLContext; import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction; -import io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRUtils; import io.jenkins.plugins.bitbucketpushandpullrequest.exception.TriggerNotSetException; import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRHookEvent; import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObservable; @@ -93,30 +94,66 @@ public void triggerMatchingJobs(BitBucketPPRHookEvent bitbucketEvent, .filter(Objects::nonNull).collect(Collectors.toList()); try (ACLContext ctx = ACL.as(ACL.SYSTEM)) { - Jenkins.get().getAllItems(Job.class).stream().forEach(job -> { + if (globalConfig.isSingleJobSet()) { try { - triggerScm(job, remotes, bitbucketEvent, bitbucketAction, observable); + Job job = (Job) Jenkins.get().getItemByFullName(globalConfig.getSingleJob()); + if (job == null) { + logger.log(Level.WARNING, "Job could not be found!"); + return; + } + triggerScmForSingleJob(job, remotes, bitbucketEvent, bitbucketAction, observable); } catch (TriggerNotSetException e) { logger.log(Level.FINE, "Trigger not set"); } - }); + } + if (!globalConfig.isSingleJobSet()) { + Jenkins.get().getAllItems(Job.class).stream().forEach(job -> { + try { + triggerScm(job, remotes, bitbucketEvent, bitbucketAction, observable); + } catch (TriggerNotSetException e) { + logger.log(Level.FINE, "Trigger not set"); + } + }); + } } } + private void triggerScmForSingleJob(@Nonnull Job job, List remotes, + BitBucketPPRHookEvent bitbucketEvent, BitBucketPPRAction bitbucketAction, + BitBucketPPRObservable observable) throws TriggerNotSetException { + + Trigger trigger = new Trigger(getBitBucketTrigger(job) + .orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName())), Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job))); + + trigger.scmTriggerItem.ifPresent(it -> it.getSCMs().stream().forEach(scmTrigger -> { + + if (!scmTriggered.contains(scmTrigger)) { + scmTriggered.add(scmTrigger); + + try { + trigger.bitbucketTrigger.onPost(bitbucketEvent, bitbucketAction, scmTrigger, observable); + return; + + } catch (Throwable e) { + logger.log(Level.WARNING, "Error: {0}", e.getMessage()); + e.printStackTrace(); + } + } + + logger.log(Level.FINE, "{0} SCM doesn't match remote repo {1} or it was already triggered.", + new Object[] {job.getName(), remotes}); + + })); + } + private void triggerScm(@Nonnull Job job, List remotes, BitBucketPPRHookEvent bitbucketEvent, BitBucketPPRAction bitbucketAction, BitBucketPPRObservable observable) throws TriggerNotSetException { - BitBucketPPRTrigger bitbucketTrigger = getBitBucketTrigger(job) - .orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName())); - - // @todo shouldn't be an instance variable? - List scmTriggered = new ArrayList<>(); - - Optional item = - Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job)); + Trigger trigger = new Trigger(getBitBucketTrigger(job) + .orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName())), Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job))); - item.ifPresent(it -> it.getSCMs().stream().forEach(scmTrigger -> { + trigger.scmTriggerItem.ifPresent(it -> it.getSCMs().stream().forEach(scmTrigger -> { // @todo add comments to explain what is this check for if (job.getParent() instanceof MultiBranchProject diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java index b22b494..f2e2fb3 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java @@ -36,6 +36,8 @@ public class BitBucketPPRPluginConfig extends GlobalConfiguration { public String credentialsId; + public String singleJob; + public BitBucketPPRPluginConfig() { logger.fine("Read bitbucket push and pull request plugin global configuration."); this.notifyBitBucket = true; @@ -87,6 +89,24 @@ public void setCredentialsId(@CheckForNull String credentialsId) { this.credentialsId = credentialsId; } + @DataBoundSetter + public void setSingleJob(String singleJob) { + if (isEmpty(singleJob)) { + this.singleJob = ""; + } else { + this.singleJob = singleJob; + } + save(); + } + + public boolean isSingleJobSet() { + return !isEmpty(singleJob); + } + + public String getSingleJob() { + return singleJob; + } + @Override public String getDisplayName() { return "Bitbucket Push and Pull Request"; diff --git a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly index e754d2a..9246173 100644 --- a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly +++ b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly @@ -6,6 +6,9 @@ + + +