Skip to content

Commit af4ebc7

Browse files
Feature/multibranch scan (#372) (#374)
* Added functionality for Multibranch Scan Functionality added to trigger a Jenkins Multibranch scan when a branch is added or deleted in bitbucket. * Added functions for data capture Added a function to capture both the from Hash and change type items that are inside of the payload received from Bitbucket * Added full functions for payload data Functions added get the from hash and change type items from the Bitbucket payload * Added constant variables Added constant variables for the change type values along with the value of an empty hash. These are used to determine when a branch is being added or deleted. * Update repository URL Updating the plugin repository to have the correct URL for a successful Maven Build * Update Logic for Multibranch Pipeline Scan Trigger Previously when triggering the scan it was done so for all pipelines. New logic ensures that it only occurs for the repo captured from the payload if it matches the branch source specified in the multibranch pipeline * Adding functions to pull clone links from payload Functiions have been added to pull http and ssh clone links from bitbucket payload * Added functions for capturing clone links Added Functions to pull the SSH and HTTP clone links from the Bitbucket payload * Update BitBucketPPRJobProbe.java Ran into null pointer issue causing issues with other triggers. Updates made to resolve null pointer occurrence. Co-authored-by: s78258819-svg <s78258819@gmail.com>
1 parent 414f09f commit af4ebc7

File tree

4 files changed

+104
-5
lines changed

4 files changed

+104
-5
lines changed

src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121

2222
package io.jenkins.plugins.bitbucketpushandpullrequest;
2323

24-
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_MERGED;
25-
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_SERVER_MERGED;
26-
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.REPOSITORY_CLOUD_PUSH;
27-
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.REPOSITORY_SERVER_PUSH;
28-
2924
import hudson.model.Job;
3025
import hudson.plugins.git.GitSCM;
3126
import hudson.plugins.git.GitStatus;
@@ -53,6 +48,11 @@
5348
import jenkins.model.ParameterizedJobMixIn;
5449
import jenkins.triggers.SCMTriggerItem;
5550
import org.eclipse.jgit.transport.URIish;
51+
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
52+
import jenkins.branch.BranchSource;
53+
import jenkins.scm.api.SCMSource;
54+
55+
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.*;
5656

5757
/**
5858
*
@@ -152,6 +152,8 @@ private void triggerScm(@Nonnull Job<?, ?> job, List<URIish> remotes,
152152

153153
jobTrigger.scmTriggerItem.ifPresent(it -> it.getSCMs().forEach(scm -> {
154154

155+
triggerMultibranchScan(job, bitbucketAction);
156+
155157
// @todo add comments to explain what is this check for
156158
if (job.getParent() instanceof MultiBranchProject
157159
&& mPJobShouldNotBeTriggered(job, bitbucketEvent, bitbucketAction)) {
@@ -244,4 +246,59 @@ private boolean matchGitScm(SCM scm, URIish remote) {
244246
.anyMatch((repo) -> repo.getURIs().stream().anyMatch((repoUrl) -> GitStatus.looselyMatches(repoUrl, remote)));
245247
}
246248

249+
private void triggerMultibranchScan(@Nonnull Job<?, ?> job,
250+
BitBucketPPRAction bitbucketAction) {
251+
252+
String getLatestCommit = bitbucketAction.getLatestCommit();
253+
String getLatestFromCommit = bitbucketAction.getLatestFromCommit();
254+
String pipelineName = job.getParent().getFullName();
255+
String getPayldChgType = bitbucketAction.getPayloadChangeType();
256+
257+
if ((getLatestCommit != null) && (getLatestFromCommit != null) && (pipelineName != null) && (getPayldChgType != null)) {
258+
if ((getLatestFromCommit.equals(EMPTY_HASH) && PAYLOAD_CHANGE_TYPE_ADD.equals(getPayldChgType)) ||
259+
(getLatestCommit.equals(EMPTY_HASH) && PAYLOAD_CHANGE_TYPE_DELETE.equals(getPayldChgType))) {
260+
261+
Jenkins jenkins = Jenkins.get();
262+
263+
WorkflowMultiBranchProject mbp = jenkins.getInstance().getItemByFullName(pipelineName, WorkflowMultiBranchProject.class);
264+
265+
if (mbp != null) {
266+
for (BranchSource bs : mbp.getSourcesList()) {
267+
SCMSource src = bs.getSource();
268+
String getOPT1CloneUrl = bitbucketAction.getOPT1CloneUrl();
269+
String getOPT2CloneUrl = bitbucketAction.getOPT2CloneUrl();
270+
271+
logger.log(Level.FINEST,
272+
"Source Type: {0}",
273+
new String[] { src.getDescriptor().getDisplayName() });
274+
275+
if (src instanceof jenkins.plugins.git.GitSCMSource) {
276+
jenkins.plugins.git.GitSCMSource git = (jenkins.plugins.git.GitSCMSource) src;
277+
String gitRemote = git.getRemote();
278+
279+
logger.log(Level.FINEST,
280+
"Branch Source URL: {0}",
281+
new String[] { gitRemote });
282+
283+
if (gitRemote.equals(getOPT1CloneUrl) || gitRemote.equals(getOPT2CloneUrl)) {
284+
logger.log(Level.FINEST,
285+
"Branch Source URL: {0}, getOPT1CloneUrl: {1}, getOPT2CloneUrl: {2}",
286+
new String[] { gitRemote, getOPT1CloneUrl, getOPT2CloneUrl });
287+
288+
mbp.scheduleBuild2(0);
289+
290+
logger.log(Level.INFO,
291+
"Triggered branch indexing for: {0}",
292+
new String[] { pipelineName });
293+
}
294+
}
295+
}
296+
} else {
297+
logger.log(Level.WARNING,
298+
"Multibranch job not found: {0}",
299+
new String[] { pipelineName });
300+
}
301+
}
302+
}
303+
}
247304
}

src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,12 @@ default String getLatestCommitFromRef() {
141141
default String getLatestCommitToRef() {
142142
return null;
143143
}
144+
145+
default String getLatestFromCommit() { return null; }
146+
147+
default String getPayloadChangeType() { return null; }
148+
149+
default String getOPT1CloneUrl() { return null; }
150+
151+
default String getOPT2CloneUrl() { return null; }
144152
}

src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,34 @@ public List<String> getCommitLinks() throws MalformedURLException {
186186
private String getBaseUrl() {
187187
return baseUrl.getProtocol() + "://" + baseUrl.getHost() + ":" + baseUrl.getPort();
188188
}
189+
190+
@Override
191+
public String getLatestFromCommit() {
192+
for (BitBucketPPRServerChange change : payload.getServerChanges()) {
193+
if(change.getRefId() != null) {
194+
return change.getFromHash();
195+
}
196+
}
197+
return null;
198+
}
199+
200+
@Override
201+
public String getPayloadChangeType() {
202+
for (BitBucketPPRServerChange change : payload.getServerChanges()) {
203+
if(change.getRefId() != null) {
204+
return change.getType();
205+
}
206+
}
207+
return null;
208+
}
209+
210+
@Override
211+
public String getOPT1CloneUrl() {
212+
return payload.getServerRepository().getLinks().getCloneProperty().get(0).getHref();
213+
}
214+
215+
@Override
216+
public String getOPT2CloneUrl() {
217+
return payload.getServerRepository().getLinks().getCloneProperty().get(1).getHref();
218+
}
189219
}

src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/common/BitBucketPPRConst.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public final class BitBucketPPRConst {
6161
public static final String APPLICATION_X_WWW_FORM_URLENCODED =
6262
"application/x-www-form-urlencoded";
6363

64+
public static final String PAYLOAD_CHANGE_TYPE_ADD = "ADD";
65+
public static final String PAYLOAD_CHANGE_TYPE_DELETE = "DELETE";
66+
public static final String EMPTY_HASH = "0000000000000000000000000000000000000000";
67+
6468
private BitBucketPPRConst() {
6569
throw new AssertionError();
6670
}

0 commit comments

Comments
 (0)