Skip to content
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins.io/public/</url>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins.io/public/</url>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

package io.jenkins.plugins.bitbucketpushandpullrequest;

import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_MERGED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_SERVER_MERGED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.REPOSITORY_CLOUD_PUSH;
import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.REPOSITORY_SERVER_PUSH;

import hudson.model.Job;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitStatus;
Expand Down Expand Up @@ -53,6 +48,11 @@
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import jenkins.branch.BranchSource;
import jenkins.scm.api.SCMSource;

import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.*;

/**
*
Expand Down Expand Up @@ -152,6 +152,8 @@ private void triggerScm(@Nonnull Job<?, ?> job, List<URIish> remotes,

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

triggerMultibranchScan(job, bitbucketAction);

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

private void triggerMultibranchScan(@Nonnull Job<?, ?> job,
BitBucketPPRAction bitbucketAction) {

String getLatestCommit = bitbucketAction.getLatestCommit();
String getLatestFromCommit = bitbucketAction.getLatestFromCommit();
String pipelineName = job.getParent().getFullName();
String getPayldChgType = bitbucketAction.getPayloadChangeType();

if ((getLatestCommit != null) && (getLatestFromCommit != null) && (pipelineName != null) && (getPayldChgType != null)) {
if ((getLatestFromCommit.equals(EMPTY_HASH) && PAYLOAD_CHANGE_TYPE_ADD.equals(getPayldChgType)) ||
(getLatestCommit.equals(EMPTY_HASH) && PAYLOAD_CHANGE_TYPE_DELETE.equals(getPayldChgType))) {

Jenkins jenkins = Jenkins.get();

WorkflowMultiBranchProject mbp = jenkins.getInstance().getItemByFullName(pipelineName, WorkflowMultiBranchProject.class);

if (mbp != null) {
for (BranchSource bs : mbp.getSourcesList()) {
SCMSource src = bs.getSource();
String getOPT1CloneUrl = bitbucketAction.getOPT1CloneUrl();
String getOPT2CloneUrl = bitbucketAction.getOPT2CloneUrl();

logger.log(Level.FINEST,
"Source Type: {0}",
new String[] { src.getDescriptor().getDisplayName() });

if (src instanceof jenkins.plugins.git.GitSCMSource) {
jenkins.plugins.git.GitSCMSource git = (jenkins.plugins.git.GitSCMSource) src;
String gitRemote = git.getRemote();

logger.log(Level.FINEST,
"Branch Source URL: {0}",
new String[] { gitRemote });

if (gitRemote.equals(getOPT1CloneUrl) || gitRemote.equals(getOPT2CloneUrl)) {
logger.log(Level.FINEST,
"Branch Source URL: {0}, getOPT1CloneUrl: {1}, getOPT2CloneUrl: {2}",
new String[] { gitRemote, getOPT1CloneUrl, getOPT2CloneUrl });

mbp.scheduleBuild2(0);

logger.log(Level.INFO,
"Triggered branch indexing for: {0}",
new String[] { pipelineName });
}
}
}
} else {
logger.log(Level.WARNING,
"Multibranch job not found: {0}",
new String[] { pipelineName });
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,12 @@ default String getLatestCommitFromRef() {
default String getLatestCommitToRef() {
return null;
}

default String getLatestFromCommit() { return null; }

default String getPayloadChangeType() { return null; }

default String getOPT1CloneUrl() { return null; }

default String getOPT2CloneUrl() { return null; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,34 @@ public List<String> getCommitLinks() throws MalformedURLException {
private String getBaseUrl() {
return baseUrl.getProtocol() + "://" + baseUrl.getHost() + ":" + baseUrl.getPort();
}

@Override
public String getLatestFromCommit() {
for (BitBucketPPRServerChange change : payload.getServerChanges()) {
if(change.getRefId() != null) {
return change.getFromHash();
}
}
return null;
}

@Override
public String getPayloadChangeType() {
for (BitBucketPPRServerChange change : payload.getServerChanges()) {
if(change.getRefId() != null) {
return change.getType();
}
}
return null;
}

@Override
public String getOPT1CloneUrl() {
return payload.getServerRepository().getLinks().getCloneProperty().get(0).getHref();
}

@Override
public String getOPT2CloneUrl() {
return payload.getServerRepository().getLinks().getCloneProperty().get(1).getHref();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public final class BitBucketPPRConst {
public static final String APPLICATION_X_WWW_FORM_URLENCODED =
"application/x-www-form-urlencoded";

public static final String PAYLOAD_CHANGE_TYPE_ADD = "ADD";
public static final String PAYLOAD_CHANGE_TYPE_DELETE = "DELETE";
public static final String EMPTY_HASH = "0000000000000000000000000000000000000000";

private BitBucketPPRConst() {
throw new AssertionError();
}
Expand Down
Loading