diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java index b9792ef..392e0ea 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRJobProbe.java @@ -79,7 +79,7 @@ public void triggerMatchingJobs(BitBucketPPRHookEvent bitbucketEvent, String.format("Unsupported SCM type %s", bitbucketAction.getScm())); } - Function f = a -> { + Function makeUrl = a -> { try { return new URIish(a); } catch (URISyntaxException e) { @@ -87,7 +87,9 @@ public void triggerMatchingJobs(BitBucketPPRHookEvent bitbucketEvent, return null; } }; - List remotes = bitbucketAction.getScmUrls().stream().map(f) + + + List remoteScmUrls = bitbucketAction.getScmUrls().stream().map(makeUrl) .filter(Objects::nonNull).collect(Collectors.toList()); try (ACLContext ctx = ACL.as(ACL.SYSTEM)) { @@ -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"); } @@ -118,16 +120,16 @@ private void triggerScmForSingleJob(@Nonnull Job job, List 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) { @@ -145,10 +147,10 @@ private void triggerScm(@Nonnull Job job, List 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 @@ -157,13 +159,13 @@ && mPJobShouldNotBeTriggered(job, bitbucketEvent, bitbucketAction)) { return; } - Predicate p = (url) -> scmTrigger instanceof GitSCM && matchGitScm(scmTrigger, url); + Predicate 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) { @@ -241,7 +243,7 @@ private Optional 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))); } }