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

New field for status propagation of pipeline #338

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@
String.format("Unsupported SCM type %s", bitbucketAction.getScm()));
}

Function<String, URIish> f = a -> {
Function<String, URIish> makeUrl = a -> {
try {
return new URIish(a);
} catch (URISyntaxException e) {
logger.warning(String.format("Invalid URI %s.", e.getMessage()));
return null;
}
};
List<URIish> remotes = bitbucketAction.getScmUrls().stream().map(f)


List<URIish> remoteScmUrls = bitbucketAction.getScmUrls().stream().map(makeUrl)
.filter(Objects::nonNull).collect(Collectors.toList());

try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
Expand All @@ -98,150 +100,150 @@
logger.log(Level.WARNING, "Job could not be found!");
return;
}
triggerScmForSingleJob(job, remotes, bitbucketEvent, bitbucketAction, observable);
triggerScmForSingleJob(job, remoteScmUrls, bitbucketEvent, bitbucketAction, observable);
} catch (TriggerNotSetException e) {
logger.log(Level.FINE, "Trigger not set");
}
} else {
Jenkins.get().getAllItems(Job.class).forEach(job -> {
try {
triggerScm(job, remotes, bitbucketEvent, bitbucketAction, observable);
triggerScm(job, remoteScmUrls, 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)
Trigger jobTrigger = new Trigger(getBitBucketTrigger(job)
.orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName())), Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job)));

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

if (!scmTriggered.contains(scmTrigger)) {
scmTriggered.add(scmTrigger);
if (!scmTriggered.contains(scm)) {
scmTriggered.add(scm);

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

} catch (Exception e) {
logger.log(Level.WARNING, "Error: {0}", e.getMessage());
}
}

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 {

Trigger trigger = new Trigger(getBitBucketTrigger(job)
Trigger jobTrigger = new Trigger(getBitBucketTrigger(job)
.orElseThrow(() -> new TriggerNotSetException(job.getFullDisplayName())), Optional.ofNullable(SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job)));

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

// @todo add comments to explain what is this check for
if (job.getParent() instanceof MultiBranchProject
&& mPJobShouldNotBeTriggered(job, bitbucketEvent, bitbucketAction)) {
logger.log(Level.FINEST, "Skipping job {0}.", job.getDisplayName());
return;
}

Predicate<URIish> p = (url) -> scmTrigger instanceof GitSCM && matchGitScm(scmTrigger, url);
Predicate<URIish> checkSCM = (url) -> scm instanceof GitSCM && matchGitScm(scm, url);

if (remotes.stream().anyMatch(p) && !scmTriggered.contains(scmTrigger)) {
scmTriggered.add(scmTrigger);
if (remotes.stream().anyMatch(checkSCM) && !scmTriggered.contains(scm)) {
scmTriggered.add(scm);

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

} catch (Exception e) {
logger.log(Level.WARNING, "Error: {0}", e.getMessage());
}
}

logger.log(Level.FINE, "{0} SCM doesn't match remote repo {1} or it was already triggered.",
new Object[] { job.getName(), remotes.stream().map(URIish::toString).collect(Collectors.joining(", ")) });

}));
}

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

public Trigger(BitBucketPPRTrigger bitbucketTrigger, Optional<SCMTriggerItem> item) {
this.bitbucketTrigger = bitbucketTrigger;
this.scmTriggerItem = item;
}
}

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

if (job.getDisplayName() != null) {
String displayName = job.getDisplayName();
String sourceBranchName = bitbucketAction.getSourceBranch();
String targetBranchName = bitbucketAction.getTargetBranch();

logger.log(Level.FINEST,
"Bitbucket event is : {0}, Job Name : {1}, sourceBranchName: {2}, targetBranchName: {3}",
new String[] {
bitbucketEvent.getAction(), displayName, sourceBranchName,
targetBranchName });

if (PULL_REQUEST_MERGED.equalsIgnoreCase(bitbucketEvent.getAction())) {
return !displayName.equalsIgnoreCase(targetBranchName);
}

if (PULL_REQUEST_SERVER_MERGED.equalsIgnoreCase(bitbucketEvent.getAction())) {
return !displayName.equalsIgnoreCase(targetBranchName);
}

if (sourceBranchName != null) {
return !displayName.equalsIgnoreCase(sourceBranchName);
}

if (REPOSITORY_CLOUD_PUSH.equalsIgnoreCase(bitbucketEvent.getAction())
&& targetBranchName != null) {
return !displayName.equals(targetBranchName);
}

if (REPOSITORY_SERVER_PUSH.equalsIgnoreCase(bitbucketEvent.getAction())
&& targetBranchName != null) {
return !displayName.equals(targetBranchName);
}
}

return true;
}

private Optional<BitBucketPPRTrigger> getBitBucketTrigger(Job<?, ?> job) {
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
ParameterizedJobMixIn.ParameterizedJob<?, ?> pJob =
(ParameterizedJobMixIn.ParameterizedJob<?, ?>) job;

return pJob.getTriggers().values().stream().filter(BitBucketPPRTrigger.class::isInstance)
.findFirst().map(BitBucketPPRTrigger.class::cast);
}
return Optional.empty();
}


private boolean matchGitScm(SCM scm, URIish remote) {
return ((GitSCM) scm).getRepositories().stream()
.anyMatch((a) -> a.getURIs().stream().anyMatch((b) -> GitStatus.looselyMatches(b, remote)));
.anyMatch((repo) -> repo.getURIs().stream().anyMatch((repoUrl) -> GitStatus.looselyMatches(repoUrl, remote)));

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 103-246 are not covered by tests
}

}
Loading
Loading