Skip to content

Commit

Permalink
Feature: Add option for single triggered job only
Browse files Browse the repository at this point in the history
- Add global feature to restrict jenkins instance to one (remote)
  triggerable job only
  • Loading branch information
solarlodge committed Sep 19, 2023
1 parent 9aa388a commit 5a1fdf4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<URIish> 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<URIish> 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<SCM> scmTriggered = new ArrayList<>();

Optional<SCMTriggerItem> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<f:entry title="Hook URL" field="hookUrlTitle">
<f:textbox field="hookUrl" />
</f:entry>
<f:entry title="Single job to build" field="singleJobTitle">
<f:textbox field="singleJob" />
</f:entry>
<f:entry title="${%Send build status to BitBucket}" field="notifyBitBucket">
<f:checkbox default="true" />
</f:entry>
Expand Down

0 comments on commit 5a1fdf4

Please sign in to comment.