Skip to content

Commit

Permalink
Merge branch '331-build-status-not-propagating-to-bitbucket-on-prem' …
Browse files Browse the repository at this point in the history
…into fix_#330_Empty-env-BITBUCKET_PULL_REQUEST_LINK
  • Loading branch information
cdelmonte-zg authored Sep 12, 2024
2 parents 7275123 + 994f26b commit 2ed65b5
Show file tree
Hide file tree
Showing 24 changed files with 1,238 additions and 547 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ public void triggerMatchingJobs(BitBucketPPRHookEvent bitbucketEvent,
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,14 +100,14 @@ public void triggerMatchingJobs(BitBucketPPRHookEvent bitbucketEvent,
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");
}
Expand All @@ -118,16 +120,16 @@ 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) {
Expand All @@ -145,10 +147,10 @@ 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
Expand All @@ -157,13 +159,13 @@ && mPJobShouldNotBeTriggered(job, bitbucketEvent, bitbucketAction)) {
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) {
Expand Down Expand Up @@ -241,7 +243,7 @@ private Optional<BitBucketPPRTrigger> getBitBucketTrigger(Job<?, ?> job) {

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)));
}

}
Loading

0 comments on commit 2ed65b5

Please sign in to comment.