Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add option for single triggered job #305

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ in the __Jenkins Global Configurations__:

1. you can override the default webhook endpoint "/bitbucket-hook/" consumed by the plugin

2. you can disable the build status notifications propagation.
2. you can configure a single job to be triggered exclusively. That overrides the default behavior of having a dedicated endpoint (+ some internal matching logic) for ALL pipelines to be triggered via this plugin. In case of a non-existing job a WARNING will be logged at global jenkins log. You can omit this feature by simply leaving this field blank.

3. you can choose what build key to use for build status propagation
3. you can disable the build status notifications propagation.

4. you can set global credentials used by the plugin for the state notification
4. you can choose what build key to use for build status propagation

5. you can set global credentials used by the plugin for the state notification

![example global config jenkins bb ppr 1](docs/img/global-config.png)

Expand Down
Binary file modified docs/img/global-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ in the __Jenkins Global Configurations__:

1. you can override the default webhook endpoint "/bitbucket-hook/" consumed by the plugin

2. you can disable the build status notifications propagation.
2. you can configure a single job to be triggered exclusively. That overrides the default behavior of having a dedicated endpoint (+ some internal matching logic) for ALL pipelines to be triggered via this plugin. In case of a non-existing job a WARNING will be logged at global jenkins log. You can omit this feature by simply leaving this field blank.

3. you can choose what build key to use for build status propagation
3. you can disable the build status notifications propagation.

4. you can set global credentials used by the plugin for the state notification
4. you can choose what build key to use for build status propagation

5. you can set global credentials used by the plugin for the state notification

![example global config jenkins bb ppr 1](./img/global-config.png)

Expand Down
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 All @@ -64,6 +65,14 @@
public class BitBucketPPRJobProbe {
private static final Logger logger = Logger.getLogger(BitBucketPPRJobProbe.class.getName());

private static final BitBucketPPRPluginConfig globalConfig =

Check warning on line 68 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 68 is not covered by tests
BitBucketPPRPluginConfig.getInstance();

Check warning on line 69 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 69 is not covered by tests
private final List<SCM> scmTriggered;

public BitBucketPPRJobProbe() {

Check warning on line 72 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 72 is not covered by tests
scmTriggered = new ArrayList<>();

Check warning on line 73 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 73 is not covered by tests
}

Check warning on line 74 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 74 is not covered by tests

public void triggerMatchingJobs(BitBucketPPRHookEvent bitbucketEvent,
BitBucketPPRAction bitbucketAction, BitBucketPPRObservable observable) {

Expand All @@ -85,30 +94,66 @@
.filter(Objects::nonNull).collect(Collectors.toList());

try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
Jenkins.get().getAllItems(Job.class).stream().forEach(job -> {
if (globalConfig.isSingleJobSet()) {

Check warning on line 97 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 97 is only partially covered, 2 branches are missing
try {
triggerScm(job, remotes, bitbucketEvent, bitbucketAction, observable);
Job job = (Job) Jenkins.get().getItemByFullName(globalConfig.getSingleJob());

Check warning on line 99 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 99 is not covered by tests
if (job == null) {

Check warning on line 100 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 100 is only partially covered, 2 branches are missing
logger.log(Level.WARNING, "Job could not be found!");

Check warning on line 101 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 101 is not covered by tests
return;

Check warning on line 102 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 102 is not covered by tests
}
triggerScmForSingleJob(job, remotes, bitbucketEvent, bitbucketAction, observable);

Check warning on line 104 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 104 is not covered by tests
} catch (TriggerNotSetException e) {

Check warning on line 105 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 105 is not covered by tests
logger.log(Level.FINE, "Trigger not set");

Check warning on line 106 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 106 is not covered by tests
}

Check warning on line 107 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 107 is not covered by tests
});
}
if (!globalConfig.isSingleJobSet()) {

Check warning on line 109 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 109 is only partially covered, 2 branches are missing
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");
}
});
}
}
}

Check warning on line 119 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 119 is not covered by tests

private void triggerScmForSingleJob(@Nonnull Job<?, ?> job, List<URIish> remotes,
BitBucketPPRHookEvent bitbucketEvent, BitBucketPPRAction bitbucketAction,
BitBucketPPRObservable observable) throws TriggerNotSetException {

Trigger trigger = new Trigger(getBitBucketTrigger(job)

Check warning on line 125 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 125 is not covered by tests
.orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName())), Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job)));

Check warning on line 126 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 126 is not covered by tests

trigger.scmTriggerItem.ifPresent(it -> it.getSCMs().stream().forEach(scmTrigger -> {

Check warning on line 128 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 128 is not covered by tests

if (!scmTriggered.contains(scmTrigger)) {

Check warning on line 130 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 130 is only partially covered, 2 branches are missing
scmTriggered.add(scmTrigger);

Check warning on line 131 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 131 is not covered by tests

try {
trigger.bitbucketTrigger.onPost(bitbucketEvent, bitbucketAction, scmTrigger, observable);

Check warning on line 134 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 134 is not covered by tests
return;

Check warning on line 135 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 135 is not covered by tests

} catch (Throwable e) {

Check warning on line 137 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 137 is not covered by tests
logger.log(Level.WARNING, "Error: {0}", e.getMessage());

Check warning on line 138 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 138 is not covered by tests
e.printStackTrace();

Check warning on line 139 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 139 is not covered by tests
}
}

logger.log(Level.FINE, "{0} SCM doesn't match remote repo {1} or it was already triggered.",

Check warning on line 143 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 143 is not covered by tests
new Object[] {job.getName(), remotes});

Check warning on line 144 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 144 is not covered by tests

}));

Check warning on line 146 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 146 is not covered by tests
}

Check warning on line 147 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 147 is not covered by tests

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<>();
Trigger trigger = new Trigger(getBitBucketTrigger(job)

Check warning on line 153 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 153 is not covered by tests
.orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName())), Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job)));

Check warning on line 154 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 154 is not covered by tests

Optional<SCMTriggerItem> item =
Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job));

item.ifPresent(it -> it.getSCMs().stream().forEach(scmTrigger -> {
trigger.scmTriggerItem.ifPresent(it -> it.getSCMs().stream().forEach(scmTrigger -> {

Check warning on line 156 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 156 is not covered by tests

// @todo add comments to explain what is this check for
if (job.getParent() instanceof MultiBranchProject
Expand All @@ -124,7 +169,7 @@
scmTriggered.add(scmTrigger);

try {
bitbucketTrigger.onPost(bitbucketEvent, bitbucketAction, scmTrigger, observable);
trigger.bitbucketTrigger.onPost(bitbucketEvent, bitbucketAction, scmTrigger, observable);

Check warning on line 172 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 172 is not covered by tests
return;

} catch (Throwable e) {
Expand All @@ -139,6 +184,16 @@
}));
}

private static class Trigger {
public final BitBucketPPRTrigger bitbucketTrigger;
public final Optional<SCMTriggerItem> scmTriggerItem;

public Trigger(BitBucketPPRTrigger bitbucketTrigger, Optional<SCMTriggerItem> item) {

Check warning on line 191 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 191 is not covered by tests
this.bitbucketTrigger = bitbucketTrigger;

Check warning on line 192 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 192 is not covered by tests
this.scmTriggerItem = item;

Check warning on line 193 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 193 is not covered by tests
}

Check warning on line 194 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 194 is not covered by tests
}

private boolean mPJobShouldNotBeTriggered(Job<?, ?> job, BitBucketPPRHookEvent bitbucketEvent,
BitBucketPPRAction bitbucketAction) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

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 @@
this.credentialsId = credentialsId;
}

@DataBoundSetter
public void setSingleJob(String singleJob) {
if (isEmpty(singleJob)) {

Check warning on line 94 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 94 is only partially covered, 2 branches are missing
this.singleJob = "";

Check warning on line 95 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 95 is not covered by tests
} else {
this.singleJob = singleJob.trim();

Check warning on line 97 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 97 is not covered by tests
}
save();

Check warning on line 99 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 99 is not covered by tests
}

Check warning on line 100 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 100 is not covered by tests

public boolean isSingleJobSet() {
return !isEmpty(singleJob);

Check warning on line 103 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 103 is only partially covered, 2 branches are missing
}

public String getSingleJob() {
return singleJob;

Check warning on line 107 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 107 is not covered by tests
}

@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
Loading