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))); } } diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java index a072792..2f58d31 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java @@ -1,17 +1,17 @@ /******************************************************************************* * The MIT License - * + * * Copyright (C) 2021, CloudBees, Inc. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, @@ -27,10 +27,15 @@ import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.logging.Logger; import javax.annotation.CheckForNull; + +import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRActionAbstract; +import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRPullRequestServerAction; +import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRServerRepositoryAction; import org.apache.commons.jelly.XMLOutput; import org.eclipse.jgit.transport.URIish; import org.jenkinsci.Symbol; @@ -77,10 +82,10 @@ import jenkins.model.ParameterizedJobMixIn; import jenkins.triggers.SCMTriggerItem; +import static org.apache.commons.lang3.ObjectUtils.isEmpty; + /** - * * @author cdelmonte - * */ public class BitBucketPPRTrigger extends Trigger> { @@ -89,6 +94,9 @@ public class BitBucketPPRTrigger extends Trigger> { private List triggers; public static final boolean ALLOW_HOOKURL_OVERRIDE = true; public String credentialsId; + public String propagationUrl; + + // list of classes @DataBoundConstructor public BitBucketPPRTrigger(List triggers) { @@ -100,18 +108,33 @@ public void setCredentialsId(@CheckForNull String credentialsId) { this.credentialsId = credentialsId; } + @DataBoundSetter + public void setPropagationUrl(String propagationUrl) { + + if (propagationUrl != null && !propagationUrl.isEmpty()) { + this.propagationUrl = propagationUrl; + } else { + this.propagationUrl = ""; + } + } + /** * Called when a POST is made. - * + * * @param bitbucketEvent * @param bitbucketAction * @param scmTrigger * @param observable * @throws Exception */ - public void onPost(BitBucketPPRHookEvent bitbucketEvent, BitBucketPPRAction bitbucketAction, - SCM scmTrigger, BitBucketPPRObservable observable) throws Exception { + public void onPost( + BitBucketPPRHookEvent bitbucketEvent, + BitBucketPPRAction bitbucketAction, + SCM scmTrigger, + BitBucketPPRObservable observable) + throws Exception { logger.finest(String.format("Called onPost Method for action %s", bitbucketAction)); + checkLocalPropagationUrl(bitbucketAction); if (job == null) { logger.warning("Error: the job is null"); @@ -125,38 +148,46 @@ public void onPost(BitBucketPPRHookEvent bitbucketEvent, BitBucketPPRAction bitb if (matchingFilters != null && !matchingFilters.isEmpty()) { BitBucketPPRPollingRunnable bitbucketPollingRunnable = - new BitBucketPPRPollingRunnable(job, getLogFile(), new BitBucketPPRPollResultListener() { - - @Override - public void onPollSuccess(PollingResult pollingResult) { - matchingFilters.stream().forEach(filter -> { - try { - BitBucketPPRTriggerCause cause = - filter.getCause(getLogFile(), bitbucketAction, bitbucketEvent); - if (shouldScheduleJob(filter, pollingResult, bitbucketAction)) { - scheduleJob(cause, bitbucketAction, scmTrigger, observable, filter); - } - } catch (Throwable e) { - logger.warning(String.format( - "During the polling process an exception was thrown: %s.", e.getMessage())); + new BitBucketPPRPollingRunnable( + job, + getLogFile(), + new BitBucketPPRPollResultListener() { + + @Override + public void onPollSuccess(PollingResult pollingResult) { + matchingFilters.stream() + .forEach( + filter -> { + try { + BitBucketPPRTriggerCause cause = + filter.getCause(getLogFile(), bitbucketAction, bitbucketEvent); + if (shouldScheduleJob(filter, pollingResult, bitbucketAction)) { + scheduleJob(cause, bitbucketAction, scmTrigger, observable, filter); + } + } catch (Throwable e) { + logger.warning( + String.format( + "During the polling process an exception was thrown: %s.", + e.getMessage())); + e.printStackTrace(); + } + }); + } + + @Override + public void onPollError(Throwable e) { + logger.warning(String.format("Called onPollError: %s.", e.getMessage())); e.printStackTrace(); } }); - } - - @Override - public void onPollError(Throwable e) { - logger.warning(String.format("Called onPollError: %s.", e.getMessage())); - e.printStackTrace(); - } - }); try { getDescriptor().queue.execute(bitbucketPollingRunnable); } catch (Throwable e) { - logger.warning(String.format( - "Error: cannot add the BB PPR polling runnable to the Jenkins' SequentialExecutionQueue queue: %s", - e.getMessage())); + logger.warning( + String.format( + "Error: cannot add the BB PPR polling runnable to the Jenkins' SequentialExecutionQueue queue: %s", + e.getMessage())); e.printStackTrace(); } @@ -165,19 +196,40 @@ public void onPollError(Throwable e) { } } - private boolean shouldScheduleJob(BitBucketPPRTriggerFilter filter, PollingResult pollingResult, + private void checkLocalPropagationUrl(BitBucketPPRAction bitBucketPPRAction) { + + if (bitBucketPPRAction == null) { + return; + } + + if (!isEmpty(propagationUrl)) { + BitBucketPPRActionAbstract bitBucketPPRActionAbstract = + (BitBucketPPRActionAbstract) bitBucketPPRAction; + bitBucketPPRActionAbstract.setPropagationUrl(propagationUrl); + } + } + + private boolean shouldScheduleJob( + BitBucketPPRTriggerFilter filter, + PollingResult pollingResult, BitBucketPPRAction bitbucketAction) { - logger.finest(String.format( - "Should schedule job: %s and (polling result has changes: %s or trigger also if there aren't changes: %s)", - filter.shouldScheduleJob(bitbucketAction), pollingResult.hasChanges(), - filter.shouldTriggerAlsoIfNothingChanged())); + logger.finest( + String.format( + "Should schedule job: %s and (polling result has changes: %s or trigger also if there aren't changes: %s)", + filter.shouldScheduleJob(bitbucketAction), + pollingResult.hasChanges(), + filter.shouldTriggerAlsoIfNothingChanged())); return filter.shouldScheduleJob(bitbucketAction) && (pollingResult.hasChanges() || filter.shouldTriggerAlsoIfNothingChanged()); } - private void scheduleJob(BitBucketPPRTriggerCause cause, BitBucketPPRAction bitbucketAction, - SCM scmTrigger, BitBucketPPRObservable observable, BitBucketPPRTriggerFilter filter) + private void scheduleJob( + BitBucketPPRTriggerCause cause, + BitBucketPPRAction bitbucketAction, + SCM scmTrigger, + BitBucketPPRObservable observable, + BitBucketPPRTriggerFilter filter) throws URISyntaxException { // Jenkins will take all instances of QueueAction from this job and will try to compare these @@ -187,30 +239,38 @@ private void scheduleJob(BitBucketPPRTriggerCause cause, BitBucketPPRAction bitb // So we need RevisionParameterAction to distinguish THIS job from all other pending jobs in // queue - Queue.Item item = ParameterizedJobMixIn.scheduleBuild2(job, 5, new CauseAction(cause), - bitbucketAction, new RevisionParameterAction(bitbucketAction.getLatestCommit(), - new URIish(bitbucketAction.getScmUrls().get(0)))); + Queue.Item item = + ParameterizedJobMixIn.scheduleBuild2( + job, + 5, + new CauseAction(cause), + bitbucketAction, + new RevisionParameterAction( + bitbucketAction.getLatestCommit(), + new URIish(bitbucketAction.getScmUrls().get(0)))); QueueTaskFuture> f = item != null ? (QueueTaskFuture) item.getFuture() : null; - if (f == null) - return; + if (f == null) return; try { Run startedBuild = (Run) f.waitForStart(); logger.info(String.format("Triggering %s # %d", job.getName(), startedBuild.getNumber())); - observable.notifyObservers(BitBucketPPREventFactory.createEvent( - BitBucketPPREventType.BUILD_STARTED, - new BitBucketPPREventContext(this, bitbucketAction, scmTrigger, startedBuild, filter))); + observable.notifyObservers( + BitBucketPPREventFactory.createEvent( + BitBucketPPREventType.BUILD_STARTED, + new BitBucketPPREventContext( + this, bitbucketAction, scmTrigger, startedBuild, filter))); Run run = (Run) f.get(); if (f.isDone()) { observable.notifyObservers( - BitBucketPPREventFactory.createEvent(BitBucketPPREventType.BUILD_FINISHED, + BitBucketPPREventFactory.createEvent( + BitBucketPPREventType.BUILD_FINISHED, new BitBucketPPREventContext(this, bitbucketAction, scmTrigger, run, filter))); } @@ -223,8 +283,7 @@ bitbucketAction, new RevisionParameterAction(bitbucketAction.getLatestCommit(), } File getLogFile() { - if (job == null) - return null; + if (job == null) return null; return new File(job.getRootDir(), BITBUCKET_POLLING_LOG); } @@ -242,9 +301,7 @@ public List getTriggers() { return triggers; } - /** - * Action object for {@link BitBucketPPRProject}. Used to display the polling log. - */ + /** Action object for {@link BitBucketPPRProject}. Used to display the polling log. */ public class BitBucketPPRWebHookPollingAction implements hudson.model.Action { public Job getOwner() { return job; @@ -269,15 +326,14 @@ public String getLog() throws Exception { return Util.loadFile(getLogFile(), StandardCharsets.UTF_8); } - /** - * Writes the annotated log to the given output. - */ + /** Writes the annotated log to the given output. */ @edu.umd.cs.findbugs.annotations.SuppressFBWarnings( - value="RV_RETURN_VALUE_IGNORED", - justification="I know what I'm doing") + value = "RV_RETURN_VALUE_IGNORED", + justification = "I know what I'm doing") public void writeLogTo(XMLOutput out) throws Exception { - new AnnotatedLargeText(getLogFile(), - Charset.defaultCharset(), true, this).writeHtmlTo(0, out.asWriter()); + new AnnotatedLargeText( + getLogFile(), Charset.defaultCharset(), true, this) + .writeHtmlTo(0, out.asWriter()); } } @@ -287,23 +343,31 @@ public static class DescriptorImpl extends TriggerDescriptor { private final SequentialExecutionQueue queue = new SequentialExecutionQueue(Jenkins.MasterComputer.threadPoolForRemoting); - public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item context, - @QueryParameter String remote, @QueryParameter String credentialsId) { + public ListBoxModel doFillCredentialsIdItems( + @AncestorInPath Item context, + @QueryParameter String remote, + @QueryParameter String credentialsId) { if (context == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER) || context != null && !context.hasPermission(Item.EXTENDED_READ)) { return new StandardListBoxModel().includeCurrentValue(credentialsId); } - return new StandardListBoxModel().includeEmptyValue() - .includeMatchingAs(ACL.SYSTEM, context, StandardCredentials.class, - Collections.emptyList(), CredentialsMatchers.always()) + return new StandardListBoxModel() + .includeEmptyValue() + .includeMatchingAs( + ACL.SYSTEM, + context, + StandardCredentials.class, + Collections.emptyList(), + CredentialsMatchers.always()) .includeCurrentValue(credentialsId); } @Override public boolean isApplicable(Item item) { - return item instanceof Job && SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(item) != null + return item instanceof Job + && SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(item) != null && item instanceof ParameterizedJobMixIn.ParameterizedJob; } @@ -317,6 +381,5 @@ public List getTriggerDescriptors() { // fancy return Jenkins.get().getDescriptorList(BitBucketPPRTriggerFilter.class); } - } } diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java index 1685765..bc06aa9 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java @@ -21,6 +21,7 @@ package io.jenkins.plugins.bitbucketpushandpullrequest.action; +import java.net.MalformedURLException; import java.util.List; import hudson.model.Action; @@ -40,11 +41,11 @@ public default String getLinkSelf() { return null; } - public default String getLinkApprove() { + public default String getLinkApprove() throws MalformedURLException { return null; } - public default String getLinkDecline() { + public default String getLinkDecline() throws MalformedURLException { return null; } @@ -75,6 +76,7 @@ public default String getType() { public default String getRepositoryName() { return null; } + // TODO: do we really neeed it? public default List getScmUrls() { return null; @@ -84,7 +86,7 @@ public default String getPullRequestId() { return null; } - public default String getRepositoryId(){ + public default String getRepositoryId() { return null; } @@ -96,27 +98,27 @@ public default String getProjectUrl() { return null; } - public default String getPullRequestApiUrl(){ + public default String getPullRequestApiUrl() { return null; } - public default String getPullRequestUrl(){ + public default String getPullRequestUrl() { return null; } - public default String getTitle(){ + public default String getTitle() { return null; } - public default String getDescription(){ + public default String getDescription() { return null; } - public default String getComment(){ + public default String getComment() { return null; } - public default String getServerComment(){ + public default String getServerComment() { return null; } @@ -124,11 +126,11 @@ public default String getLatestCommit() { return null; } - public default String getCommitLink() { + public default String getCommitLink() throws MalformedURLException { return null; } - public default List getCommitLinks() { + public default List getCommitLinks() throws MalformedURLException { return null; } @@ -139,4 +141,4 @@ public default String getLatestCommitFromRef() { public default String getLatestCommitToRef() { return null; } -} \ No newline at end of file +} diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRActionAbstract.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRActionAbstract.java new file mode 100644 index 0000000..b59e32a --- /dev/null +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRActionAbstract.java @@ -0,0 +1,31 @@ +package io.jenkins.plugins.bitbucketpushandpullrequest.action; + +import hudson.model.InvisibleAction; +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; + +import static org.apache.commons.lang3.ObjectUtils.isEmpty; + +public class BitBucketPPRActionAbstract extends InvisibleAction { + private String propagationUrl = ""; + + public BitBucketPPRActionAbstract() { + if (!isEmpty(getGlobalConfig().propagationUrl)) { + this.setPropagationUrl(getGlobalConfig().propagationUrl); + } + } + + private static final BitBucketPPRPluginConfig globalConfig = + BitBucketPPRPluginConfig.getInstance(); + + public BitBucketPPRPluginConfig getGlobalConfig() { + return globalConfig; + } + + public void setPropagationUrl(String propagationUrl) { + this.propagationUrl = propagationUrl; + } + + public String getPropagationUrl() { + return propagationUrl; + } +} diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java index e02a3af..28beab9 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java @@ -24,6 +24,8 @@ import io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRUtils; import io.jenkins.plugins.bitbucketpushandpullrequest.exception.BitBucketPPRRepositoryNotParsedException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -34,9 +36,12 @@ import hudson.model.InvisibleAction; import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; import static java.util.Objects.nonNull; +import static org.apache.commons.lang3.ObjectUtils.isEmpty; -public class BitBucketPPRPullRequestAction extends InvisibleAction implements BitBucketPPRAction { - private static final Logger logger = Logger.getLogger(BitBucketPPRPullRequestAction.class.getName()); +public class BitBucketPPRPullRequestAction extends BitBucketPPRActionAbstract + implements BitBucketPPRAction { + private static final Logger logger = + Logger.getLogger(BitBucketPPRPullRequestAction.class.getName()); public static final String APPROVE = "/approve"; public static final String DECLINE = "/decline"; public static final String STATUSES = "/statuses"; @@ -61,8 +66,9 @@ public BitBucketPPRPullRequestAction(@Nonnull BitBucketPPRPayload payload) { Map workspaceRepo; try { - workspaceRepo = BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl( - payload.getRepository().getLinks().getHtml().getHref()); + workspaceRepo = + BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl( + payload.getRepository().getLinks().getHtml().getHref()); } catch (BitBucketPPRRepositoryNotParsedException e) { throw new RuntimeException(e); } @@ -84,6 +90,7 @@ public String getTargetBranch() { public String getLatestCommitFromRef() { return payload.getPullRequest().getSource().getCommit().getHash(); } + @Override public String getLatestCommitToRef() { return payload.getPullRequest().getDestination().getCommit().getHash(); @@ -91,14 +98,20 @@ public String getLatestCommitToRef() { @Override public String getPullRequestApiUrl() { - return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, - PULL_REQUEST_API, pullRequestId); + return String.join( + "/", + BITBUCKET_API_BASE_URL, + BITBUCKET_REPOSITORIES, + workspace, + repoSlug, + PULL_REQUEST_API, + pullRequestId); } @Override public String getPullRequestUrl() { - return String.join("/", BITBUCKET_HTTP_BASE_URL, workspace, repoSlug, PULL_REQUEST_HTTP, - pullRequestId); + return String.join( + "/", BITBUCKET_HTTP_BASE_URL, workspace, repoSlug, PULL_REQUEST_HTTP, pullRequestId); } @Override @@ -151,7 +164,8 @@ public String getPullRequestId() { @Override public String getComment() { - if (payload.getComment() == null || payload.getComment().getContent() == null + if (payload.getComment() == null + || payload.getComment().getContent() == null || payload.getComment().getContent().getRaw() == null) { return ""; } @@ -170,20 +184,41 @@ public String getLinkSelf() { @Override public String getLinkApprove() { - return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, - PULL_REQUEST_API, pullRequestId) + APPROVE; + return String.join( + "/", + BITBUCKET_API_BASE_URL, + BITBUCKET_REPOSITORIES, + workspace, + repoSlug, + PULL_REQUEST_API, + pullRequestId) + + APPROVE; } @Override public String getLinkDecline() { - return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, - PULL_REQUEST_API, pullRequestId) + DECLINE; + return String.join( + "/", + BITBUCKET_API_BASE_URL, + BITBUCKET_REPOSITORIES, + workspace, + repoSlug, + PULL_REQUEST_API, + pullRequestId) + + DECLINE; } @Override public String getLinkStatuses() { - return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, - PULL_REQUEST_API, pullRequestId) + STATUSES; + return String.join( + "/", + BITBUCKET_API_BASE_URL, + BITBUCKET_REPOSITORIES, + workspace, + repoSlug, + PULL_REQUEST_API, + pullRequestId) + + STATUSES; } @Override @@ -193,8 +228,11 @@ public String getLatestCommit() { @Override public String getCommitLink() { - return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, - COMMIT) + '/' + this.getLatestCommit(); + String baseCommitLink = + isEmpty(this.getPropagationUrl()) ? BITBUCKET_API_BASE_URL : this.getPropagationUrl(); + return String.join("/", baseCommitLink, BITBUCKET_REPOSITORIES, workspace, repoSlug, COMMIT) + + '/' + + this.getLatestCommit(); } @Override diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerAction.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerAction.java index 4546da9..7bc59de 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerAction.java @@ -21,49 +21,59 @@ package io.jenkins.plugins.bitbucketpushandpullrequest.action; -import static java.util.Objects.isNull; -import static java.util.Objects.nonNull; - import hudson.model.InvisibleAction; +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; import io.jenkins.plugins.bitbucketpushandpullrequest.exception.BitBucketPPRPayloadPropertyNotFoundException; import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerClone; + +import javax.annotation.Nonnull; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; -import javax.annotation.Nonnull; -public class BitBucketPPRPullRequestServerAction extends InvisibleAction implements - BitBucketPPRAction { +import static java.util.Objects.isNull; +import static org.apache.commons.lang3.ObjectUtils.isEmpty; + +public class BitBucketPPRPullRequestServerAction extends BitBucketPPRActionAbstract + implements BitBucketPPRAction { - private static final Logger logger = Logger.getLogger( - BitBucketPPRPullRequestServerAction.class.getName()); + private static final Logger logger = + Logger.getLogger(BitBucketPPRPullRequestServerAction.class.getName()); private final @Nonnull BitBucketPPRPayload payload; private URL baseUrl; private List scmUrls = new ArrayList<>(2); private String repositoryUuid; + private static final BitBucketPPRPluginConfig globalConfig = + BitBucketPPRPluginConfig.getInstance(); public BitBucketPPRPullRequestServerAction(@Nonnull BitBucketPPRPayload payload) throws BitBucketPPRPayloadPropertyNotFoundException { this.payload = payload; - if (isNull(payload.getServerPullRequest().getToRef()) || isNull(payload - .getServerPullRequest().getToRef().getRepository()) || isNull(payload - .getServerPullRequest().getToRef().getRepository().getLinks()) || isNull(payload - .getServerPullRequest().getToRef().getRepository().getLinks().getCloneProperty())) { + if (isNull(payload.getServerPullRequest().getToRef()) + || isNull(payload.getServerPullRequest().getToRef().getRepository()) + || isNull(payload.getServerPullRequest().getToRef().getRepository().getLinks()) + || isNull( + payload + .getServerPullRequest() + .getToRef() + .getRepository() + .getLinks() + .getCloneProperty())) { throw new BitBucketPPRPayloadPropertyNotFoundException( "A property (toRef -> repository -> links -> clone ) was not found in the JSON payload."); } - List clones = payload.getServerPullRequest().getToRef() - .getRepository().getLinks() - .getCloneProperty(); + List clones = + payload.getServerPullRequest().getToRef().getRepository().getLinks().getCloneProperty(); - if (clones.isEmpty()) throw new BitBucketPPRPayloadPropertyNotFoundException( - "Number of clone urls in JSON payload is zero."); + if (clones.isEmpty()) + throw new BitBucketPPRPayloadPropertyNotFoundException( + "Number of clone urls in JSON payload is zero."); for (BitBucketPPRServerClone clone : clones) { if (clone.getName().equalsIgnoreCase("http") || clone.getName().equalsIgnoreCase("https")) { @@ -78,6 +88,14 @@ public BitBucketPPRPullRequestServerAction(@Nonnull BitBucketPPRPayload payload) } } + if (!globalConfig.getPropagationUrl().isEmpty()) { + try { + this.baseUrl = new URL(globalConfig.getPropagationUrl()); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + logger.fine("BitBucketPPRPullRequestServerAction was called."); } @@ -160,30 +178,40 @@ public String getRepositoryId() { } @Override - public String getLinkApprove() { - String projectKey = payload.getServerPullRequest().getFromRef().getRepository().getProject() - .getKey(); + public String getLinkApprove() throws MalformedURLException { + String projectKey = + payload.getServerPullRequest().getFromRef().getRepository().getProject().getKey(); String repoSlug = payload.getServerPullRequest().getFromRef().getRepository().getSlug(); Long pullrequestId = payload.getServerPullRequest().getId(); - return getBaseUrl() + "/rest/api/1.0/projects/" + projectKey.trim() + "/repos/" - + repoSlug.trim() + "/pull-requests/" - + Long.toString(pullrequestId).trim() + "/approve"; + return getBaseUrl() + + "/rest/api/1.0/projects/" + + projectKey.trim() + + "/repos/" + + repoSlug.trim() + + "/pull-requests/" + + Long.toString(pullrequestId).trim() + + "/approve"; } @Override - public String getLinkDecline() { + public String getLinkDecline() throws MalformedURLException { // returns: // {baseUrl}/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/approve - String projectKey = payload.getServerPullRequest().getFromRef().getRepository().getProject() - .getKey(); + String projectKey = + payload.getServerPullRequest().getFromRef().getRepository().getProject().getKey(); String repoSlug = payload.getServerPullRequest().getFromRef().getRepository().getSlug(); Long pullrequestId = payload.getServerPullRequest().getId(); - return getBaseUrl() + "/rest/api/1.0/projects/" + projectKey.trim() + "/repos/" - + repoSlug.trim() + "/pull-requests/" - + Long.toString(pullrequestId).trim() + "/decline"; + return getBaseUrl() + + "/rest/api/1.0/projects/" + + projectKey.trim() + + "/repos/" + + repoSlug.trim() + + "/pull-requests/" + + Long.toString(pullrequestId).trim() + + "/decline"; } @Override @@ -192,7 +220,7 @@ public String getLatestCommit() { } @Override - public String getCommitLink() { + public String getCommitLink() throws MalformedURLException { // returns: // /rest/build-status/1.0/commits/{commitId} String commitId = payload.getServerPullRequest().getFromRef().getLatestCommit(); @@ -200,8 +228,14 @@ public String getCommitLink() { return getBaseUrl() + "/rest/build-status/1.0/commits/" + commitId; } - private String getBaseUrl() { - return baseUrl.getProtocol() + "://" + baseUrl.getHost() + ":" + baseUrl.getPort(); + private String getBaseUrl() throws MalformedURLException { + URL baseCommitLink = + isEmpty(this.getPropagationUrl()) ? baseUrl : new URL(this.getPropagationUrl()); + return baseCommitLink.getProtocol() + + "://" + + baseCommitLink.getHost() + + ":" + + baseCommitLink.getPort(); } @Override @@ -209,4 +243,12 @@ public String toString() { return "BitBucketPPRPullRequestServerAction"; } + public String setBaseUrl(String url) { + try { + this.baseUrl = new URL(url); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + return url; + } } diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRRepositoryAction.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRRepositoryAction.java index 43f8ab1..82e62db 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRRepositoryAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRRepositoryAction.java @@ -35,8 +35,10 @@ import io.jenkins.plugins.bitbucketpushandpullrequest.model.cloud.BitBucketPPRChange; import java.util.stream.Collectors; +import static org.apache.commons.lang3.ObjectUtils.isEmpty; -public class BitBucketPPRRepositoryAction extends InvisibleAction implements BitBucketPPRAction { +public class BitBucketPPRRepositoryAction extends BitBucketPPRActionAbstract + implements BitBucketPPRAction { private static final Logger logger = Logger.getLogger(BitBucketPPRAction.class.getName()); public static final String COMMIT = "commit"; private static final String BITBUCKET_API_BASE_URL = "https://api.bitbucket.org/2.0"; @@ -66,16 +68,17 @@ public BitBucketPPRRepositoryAction(@Nonnull BitBucketPPRPayload payload) { Map workspaceRepo; try { - workspaceRepo = BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl( - payload.getRepository().getLinks().getHtml().getHref()); + workspaceRepo = + BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl( + payload.getRepository().getLinks().getHtml().getHref()); } catch (BitBucketPPRRepositoryNotParsedException e) { throw new RuntimeException(e); } this.repoSlug = workspaceRepo.get(BitBucketPPRUtils.BB_REPOSITORY); this.workspace = workspaceRepo.get(BitBucketPPRUtils.BB_WORKSPACE); - logger.log(Level.INFO, - () -> "Received commit hook notification for branch: " + this.targetBranchName); + logger.log( + Level.INFO, () -> "Received commit hook notification for branch: " + this.targetBranchName); logger.log(Level.INFO, () -> "Received commit hook type: " + this.type); } @@ -128,7 +131,8 @@ public String getRepositoryId() { @Override public String getLatestCommit() { - // According to constructor `targetBranchName`, `type` and `repositoryUuid` will be set to first non-null change + // According to constructor `targetBranchName`, `type` and `repositoryUuid` will be set to first + // non-null change // So lets hope it is not very destructive move to set latestCommit from first change. for (BitBucketPPRChange change : payload.getPush().getChanges()) { if (change.getNewChange() != null) { @@ -141,9 +145,20 @@ public String getLatestCommit() { @Override public List getCommitLinks() { - return payload.getPush().getChanges().stream().map(c -> String.join("/", - BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, COMMIT, - c.getNewChange().getTarget().getHash())).collect(Collectors.toList()); + String baseCommitLink = + isEmpty(this.getPropagationUrl()) ? BITBUCKET_API_BASE_URL : this.getPropagationUrl(); + return payload.getPush().getChanges().stream() + .map( + c -> + String.join( + "/", + baseCommitLink, + BITBUCKET_REPOSITORIES, + workspace, + repoSlug, + COMMIT, + c.getNewChange().getTarget().getHash())) + .collect(Collectors.toList()); } @Override diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java index ef49340..1a69318 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java @@ -21,26 +21,27 @@ package io.jenkins.plugins.bitbucketpushandpullrequest.action; +import hudson.model.InvisibleAction; +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerChange; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerClone; + +import javax.annotation.Nonnull; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; - -import hudson.model.InvisibleAction; -import io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRUtils; -import io.jenkins.plugins.bitbucketpushandpullrequest.exception.BitBucketPPRRepositoryNotParsedException; -import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; -import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerChange; -import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerClone; +import static org.apache.commons.lang3.ObjectUtils.isEmpty; -public class BitBucketPPRServerRepositoryAction extends InvisibleAction implements BitBucketPPRAction { +public class BitBucketPPRServerRepositoryAction extends BitBucketPPRActionAbstract + implements BitBucketPPRAction { private static final Logger logger = Logger.getLogger(BitBucketPPRAction.class.getName()); + private static final BitBucketPPRPluginConfig globalConfig = + BitBucketPPRPluginConfig.getInstance(); private final @Nonnull BitBucketPPRPayload payload; private URL baseUrl; @@ -53,7 +54,8 @@ public BitBucketPPRServerRepositoryAction(BitBucketPPRPayload payload) { this.payload = payload; // TODO: do we need link clones or link self is enough?? - List clones = payload.getServerRepository().getLinks().getCloneProperty(); + List clones = + payload.getServerRepository().getLinks().getCloneProperty(); for (BitBucketPPRServerClone clone : clones) { if (clone.getName().equalsIgnoreCase("http") || clone.getName().equalsIgnoreCase("https")) { @@ -77,8 +79,11 @@ public BitBucketPPRServerRepositoryAction(BitBucketPPRPayload payload) { } } - logger.log(Level.INFO, - () -> "Received commit hook notification from server for destination branch: " + this.targetBranchName); + logger.log( + Level.INFO, + () -> + "Received commit hook notification from server for destination branch: " + + this.targetBranchName); logger.log(Level.INFO, () -> "Received commit hook type from server: " + this.type); } @@ -149,7 +154,8 @@ public String toString() { @Override public String getLatestCommit() { - // According to constructor `targetBranchName`, `type` and `targetBranchRefId` will be set to first non-null change + // According to constructor `targetBranchName`, `type` and `targetBranchRefId` will be set to + // first non-null change // So lets hope it is not very destructive move to set latestCommit from first change. for (BitBucketPPRServerChange change : payload.getServerChanges()) { if (change.getRefId() != null) { @@ -160,16 +166,17 @@ public String getLatestCommit() { } @Override - public List getCommitLinks() { + public List getCommitLinks() throws MalformedURLException { // returns: // /rest/build-status/1.0/commits/{commitId} - String baseUrl = getBaseUrl(); + URL baseCommitLink = + isEmpty(this.getPropagationUrl()) ? baseUrl : new URL(this.getPropagationUrl()); List changes = payload.getServerChanges(); List links = new ArrayList<>(); for (BitBucketPPRServerChange change : changes) { - links.add(baseUrl + "/rest/build-status/1.0/commits/" + change.getToHash()); + links.add(baseCommitLink + "/rest/build-status/1.0/commits/" + change.getToHash()); } return links; diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java index d72050c..1928ef8 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java @@ -1,13 +1,5 @@ package io.jenkins.plugins.bitbucketpushandpullrequest.config; -import static org.apache.commons.lang3.StringUtils.isEmpty; -import java.util.Collections; -import java.util.logging.Logger; -import javax.annotation.CheckForNull; -import org.kohsuke.stapler.AncestorInPath; -import org.kohsuke.stapler.DataBoundSetter; -import org.kohsuke.stapler.QueryParameter; -import org.kohsuke.stapler.StaplerRequest; import com.cloudbees.plugins.credentials.CredentialsMatchers; import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.cloudbees.plugins.credentials.common.StandardListBoxModel; @@ -16,11 +8,23 @@ import hudson.ExtensionList; import hudson.model.Item; import hudson.security.ACL; +import hudson.util.FormValidation; import hudson.util.ListBoxModel; import jenkins.model.GlobalConfiguration; import jenkins.model.Jenkins; import net.sf.json.JSONObject; +import org.kohsuke.stapler.AncestorInPath; +import org.kohsuke.stapler.DataBoundSetter; +import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.StaplerRequest; +import javax.annotation.CheckForNull; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.logging.Logger; + +import static org.apache.commons.lang3.StringUtils.isEmpty; @Extension public class BitBucketPPRPluginConfig extends GlobalConfiguration { @@ -33,11 +37,13 @@ public class BitBucketPPRPluginConfig extends GlobalConfiguration { public boolean notifyBitBucket; public boolean useJobNameAsBuildKey; - + public String credentialsId; public String singleJob; + public String propagationUrl; + public BitBucketPPRPluginConfig() { logger.fine("Read bitbucket push and pull request plugin global configuration."); this.notifyBitBucket = true; @@ -50,7 +56,7 @@ public static BitBucketPPRPluginConfig getInstance() { @DataBoundSetter public void setHookUrl(String hookUrl) { - if (isEmpty(hookUrl)) { + if (hookUrl == null || isEmpty(hookUrl)) { this.hookUrl = ""; } else { this.hookUrl = hookUrl; @@ -58,6 +64,27 @@ public void setHookUrl(String hookUrl) { save(); } + @DataBoundSetter + public void setPropagationUrl(String propagationUrl) { + + this.propagationUrl = propagationUrl; + + save(); + } + + public FormValidation doCheckPropagationUrl(@QueryParameter String value) { + if (value == null || value.isEmpty()) { + return FormValidation.ok(); + } + + try { + new URL(value); // This will throw MalformedURLException if the URL is not valid + return FormValidation.ok(); + } catch (MalformedURLException e) { + return FormValidation.error("This is not a valid URL. Please enter a correct URL."); + } + } + public boolean isHookUrlSet() { return !isEmpty(hookUrl); } @@ -70,6 +97,10 @@ public boolean getNotifyBitBucket() { return notifyBitBucket; } + public String getPropagationUrl() { + return propagationUrl; + } + @DataBoundSetter public void setNotifyBitBucket(@CheckForNull boolean notifyBitBucket) { this.notifyBitBucket = notifyBitBucket; @@ -82,6 +113,7 @@ public boolean getUseJobNameAsBuildKey() { @DataBoundSetter public void setUseJobNameAsBuildKey(@CheckForNull boolean useJobNameAsBuildKey) { this.useJobNameAsBuildKey = useJobNameAsBuildKey; + save(); } @DataBoundSetter @@ -89,7 +121,9 @@ public void setCredentialsId(@CheckForNull String credentialsId) { this.credentialsId = credentialsId; } - public String getCredentialsId() { return credentialsId; } + public String getCredentialsId() { + return credentialsId; + } @DataBoundSetter public void setSingleJob(String singleJob) { @@ -121,18 +155,24 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc return true; } - public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item context, - @QueryParameter String remote, @QueryParameter String credentialsId) { + public ListBoxModel doFillCredentialsIdItems( + @AncestorInPath Item context, + @QueryParameter String remote, + @QueryParameter String credentialsId) { if (context == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER) || context != null && !context.hasPermission(Item.EXTENDED_READ)) { return new StandardListBoxModel().includeCurrentValue(credentialsId); } - return new StandardListBoxModel().includeEmptyValue() - .includeMatchingAs(ACL.SYSTEM, Jenkins.getInstance(), StandardCredentials.class, - Collections.emptyList(), CredentialsMatchers.always()) + return new StandardListBoxModel() + .includeEmptyValue() + .includeMatchingAs( + ACL.SYSTEM, + Jenkins.getInstance(), + StandardCredentials.class, + Collections.emptyList(), + CredentialsMatchers.always()) .includeCurrentValue(credentialsId); - } } diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRHandlerTemplate.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRHandlerTemplate.java index 4449132..461e543 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRHandlerTemplate.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRHandlerTemplate.java @@ -1,17 +1,17 @@ /******************************************************************************* * The MIT License - * + * * Copyright (C) 2021, CloudBees, Inc. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, @@ -20,6 +20,7 @@ ******************************************************************************/ package io.jenkins.plugins.bitbucketpushandpullrequest.observer; +import java.net.MalformedURLException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -34,7 +35,6 @@ import io.jenkins.plugins.bitbucketpushandpullrequest.event.BitBucketPPREventContext; import io.jenkins.plugins.bitbucketpushandpullrequest.event.BitBucketPPREventType; - public abstract class BitBucketPPRHandlerTemplate { static final Logger logger = Logger.getLogger(BitBucketPPRHandlerTemplate.class.getName()); @@ -61,13 +61,13 @@ public void run(BitBucketPPREventType eventType) throws Exception { } // @todo: do we need it also for pushs? - public void setApprovedOrDeclined() { + public void setApprovedOrDeclined() throws MalformedURLException { return; } - public abstract void setBuildStatusOnFinished(); + public abstract void setBuildStatusOnFinished() throws MalformedURLException; - public abstract void setBuildStatusInProgress(); + public abstract void setBuildStatusInProgress() throws MalformedURLException; protected BitBucketPPRPluginConfig getGlobalConfig() { return BitBucketPPRPluginConfig.getInstance(); @@ -96,8 +96,8 @@ protected void callClient(@Nonnull String url, @Nonnull Map payl } } - protected void callClient(@Nonnull Verb verb, @Nonnull String url, - @Nonnull Map payload) { + protected void callClient( + @Nonnull Verb verb, @Nonnull String url, @Nonnull Map payload) { ObjectMapper objectMapper = new ObjectMapper(); try { diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestCloudObserver.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestCloudObserver.java index efbb32b..0f6952e 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestCloudObserver.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestCloudObserver.java @@ -1,17 +1,17 @@ /******************************************************************************* * The MIT License - * + * * Copyright (C) 2021, CloudBees, Inc. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, @@ -20,6 +20,7 @@ ******************************************************************************/ package io.jenkins.plugins.bitbucketpushandpullrequest.observer; +import java.net.MalformedURLException; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; @@ -47,7 +48,7 @@ public void getNotification(BitBucketPPREvent event) { } @Override - public void setApprovedOrDeclined() { + public void setApprovedOrDeclined() throws MalformedURLException { if (!(context.getFilter().shouldSendApprove() || context.getFilter().shouldSendDecline())) { return; } @@ -75,7 +76,7 @@ public void setApprovedOrDeclined() { } @Override - public void setBuildStatusOnFinished() { + public void setBuildStatusOnFinished() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); String url = bitbucketAction.getCommitLink() + "/statuses/build"; Result result = context.getRun().getResult(); @@ -91,7 +92,7 @@ public void setBuildStatusOnFinished() { } @Override - public void setBuildStatusInProgress() { + public void setBuildStatusInProgress() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); String url = bitbucketAction.getCommitLink() + "/statuses/build"; diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestServerObserver.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestServerObserver.java index cd714b6..1cca018 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestServerObserver.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPullRequestServerObserver.java @@ -1,17 +1,17 @@ /******************************************************************************* * The MIT License - * + * * Copyright (C) 2021, CloudBees, Inc. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, @@ -20,6 +20,7 @@ ******************************************************************************/ package io.jenkins.plugins.bitbucketpushandpullrequest.observer; +import java.net.MalformedURLException; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; @@ -47,7 +48,7 @@ public void getNotification(BitBucketPPREvent event) { } @Override - public void setApprovedOrDeclined() { + public void setApprovedOrDeclined() throws MalformedURLException { if (!(context.getFilter().shouldSendApprove() || context.getFilter().shouldSendDecline())) { return; } @@ -61,22 +62,22 @@ public void setApprovedOrDeclined() { if (context.getFilter().shouldSendApprove()) { url = bitbucketAction.getLinkApprove(); Verb verb = Verb.POST; - + if (result == Result.FAILURE) { verb = Verb.DELETE; } - + callClient(verb, url, map); } if (result == Result.FAILURE && context.getFilter().shouldSendDecline()) { url = bitbucketAction.getLinkDecline(); callClient(Verb.POST, url, map); - } + } } @Override - public void setBuildStatusOnFinished() { + public void setBuildStatusOnFinished() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); String url = bitbucketAction.getCommitLink(); Result result = context.getRun().getResult(); @@ -91,7 +92,7 @@ public void setBuildStatusOnFinished() { } @Override - public void setBuildStatusInProgress() { + public void setBuildStatusInProgress() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); String url = bitbucketAction.getCommitLink(); diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushCloudObserver.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushCloudObserver.java index e1b534b..c387633 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushCloudObserver.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushCloudObserver.java @@ -1,17 +1,17 @@ /******************************************************************************* * The MIT License - * + * * Copyright (C) 2021, CloudBees, Inc. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, @@ -20,6 +20,7 @@ ******************************************************************************/ package io.jenkins.plugins.bitbucketpushandpullrequest.observer; +import java.net.MalformedURLException; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; @@ -45,7 +46,7 @@ public void getNotification(BitBucketPPREvent event) { } @Override - public void setBuildStatusOnFinished() { + public void setBuildStatusOnFinished() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); Result result = context.getRun().getResult(); String state = @@ -55,12 +56,12 @@ public void setBuildStatusOnFinished() { map.put("key", computeBitBucketBuildKey(context)); map.put("url", context.getAbsoluteUrl()); map.put("state", state); - + bitbucketAction.getCommitLinks().forEach(l -> callClient(l.concat("/statuses/build"), map)); } @Override - public void setBuildStatusInProgress() { + public void setBuildStatusInProgress() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); Map map = new HashMap<>(); diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushServerObserver.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushServerObserver.java index 6eb1e56..9f27078 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushServerObserver.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRPushServerObserver.java @@ -1,17 +1,17 @@ /******************************************************************************* * The MIT License - * + * * Copyright (C) 2021, CloudBees, Inc. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, @@ -20,6 +20,7 @@ ******************************************************************************/ package io.jenkins.plugins.bitbucketpushandpullrequest.observer; +import java.net.MalformedURLException; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; @@ -45,7 +46,7 @@ public void getNotification(BitBucketPPREvent event) { } @Override - public void setBuildStatusOnFinished() { + public void setBuildStatusOnFinished() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); Result result = context.getRun().getResult(); String state = result == Result.SUCCESS ? "SUCCESSFUL" : "FAILED"; @@ -59,7 +60,7 @@ public void setBuildStatusOnFinished() { } @Override - public void setBuildStatusInProgress() { + public void setBuildStatusInProgress() throws MalformedURLException { BitBucketPPRAction bitbucketAction = context.getAction(); Map map = new HashMap<>(); map.put("key", computeBitBucketBuildKey(context)); diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/receiver/BitBucketPPRHookReceiver.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/receiver/BitBucketPPRHookReceiver.java index df64818..4147caa 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/receiver/BitBucketPPRHookReceiver.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/receiver/BitBucketPPRHookReceiver.java @@ -1,17 +1,17 @@ /******************************************************************************* * The MIT License - * + * * Copyright (C) 2022, CloudBees, Inc. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, @@ -58,9 +58,8 @@ /** * BitbucketHookReceiver processes HTTP POST requests sent to $JENKINS_URL/bitbucket-hook/ - * + * * @author cdelmonte - * */ @Extension public class BitBucketPPRHookReceiver extends CrumbExclusion implements UnprotectedRootAction { @@ -70,34 +69,40 @@ public class BitBucketPPRHookReceiver extends CrumbExclusion implements Unprotec public void doIndex(@Nonnull StaplerRequest request, @Nonnull StaplerResponse response) throws IOException { + // log request URL + logger.log(Level.INFO, "Request URL: {0}", request.getRequestURI()); + logger.log(Level.INFO, "Internal URL: {0}", getUrlName()); if (request.getRequestURI().toLowerCase().contains("/" + getUrlName() + "/") && request.getMethod().equalsIgnoreCase("POST")) { logger.log(Level.INFO, "Received POST request over Bitbucket hook"); System.out.println(">>> Received POST request over Bitbucket hook"); - try { - BitBucketPPRHookEvent bitbucketEvent = getBitbucketEvent(request); + try { + BitBucketPPRHookEvent bitbucketEvent = getBitbucketEvent(request); BitBucketPPRPayload payload = getPayload(getInputStream(request), bitbucketEvent); - BitBucketPPRObservable observable = BitBucketPPRObserverFactory.createObservable(bitbucketEvent); - + BitBucketPPRObservable observable = + BitBucketPPRObserverFactory.createObservable(bitbucketEvent); + writeSuccessResponse(response); - - BitBucketPPRPayloadProcessorFactory.createProcessor(bitbucketEvent).processPayload(payload, observable); - } catch (IOException | InputStreamException | JsonSyntaxException | OperationNotSupportedException e) { + + BitBucketPPRPayloadProcessorFactory.createProcessor(bitbucketEvent) + .processPayload(payload, observable); + } catch (IOException + | InputStreamException + | JsonSyntaxException + | OperationNotSupportedException e) { System.out.println(">>> Exception: " + e.getMessage()); writeFailResponse(response); - } catch ( - BitBucketPPRPayloadPropertyNotFoundException e) { + } catch (BitBucketPPRPayloadPropertyNotFoundException e) { logger.info( - "Payload Property doesn't exists. It could be that the " + - "Bitbucket Client / Server version is not currently supported by the plugin. " + "Payload Property doesn't exists. It could be that the " + + "Bitbucket Client / Server version is not currently supported by the plugin. " + e.getMessage()); } } } - private void writeSuccessResponse(@Nonnull StaplerResponse response) - throws IOException { + private void writeSuccessResponse(@Nonnull StaplerResponse response) throws IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); response.setStatus(HttpServletResponse.SC_OK); @@ -107,8 +112,7 @@ private void writeSuccessResponse(@Nonnull StaplerResponse response) out.close(); } - private void writeFailResponse(@Nonnull StaplerResponse response) - throws IOException { + private void writeFailResponse(@Nonnull StaplerResponse response) throws IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); @@ -128,20 +132,23 @@ String getInputStream(@Nonnull StaplerRequest request) throws IOException, Input return decodeInputStream(inputStream, request.getContentType()); } - BitBucketPPRPayload getPayload(@Nonnull final String inputStream, - @Nonnull BitBucketPPRHookEvent bitbucketEvent) + BitBucketPPRPayload getPayload( + @Nonnull final String inputStream, @Nonnull BitBucketPPRHookEvent bitbucketEvent) throws JsonSyntaxException, OperationNotSupportedException { - BitBucketPPRPayload pl = new Gson().fromJson(inputStream, - BitBucketPPRPayloadFactory.getInstance(bitbucketEvent).getClass()); + BitBucketPPRPayload pl = + new Gson() + .fromJson( + inputStream, BitBucketPPRPayloadFactory.getInstance(bitbucketEvent).getClass()); logger.log(Level.FINEST, "the payload is: {0}", pl); return pl; } - static String decodeInputStream(@Nonnull final String inputStream, - @Nonnull final String contentType) throws UnsupportedEncodingException { + static String decodeInputStream( + @Nonnull final String inputStream, @Nonnull final String contentType) + throws UnsupportedEncodingException { String input = inputStream; - if (StringUtils.startsWithIgnoreCase(contentType, - BitBucketPPRConst.APPLICATION_X_WWW_FORM_URLENCODED)) { + if (StringUtils.startsWithIgnoreCase( + contentType, BitBucketPPRConst.APPLICATION_X_WWW_FORM_URLENCODED)) { input = URLDecoder.decode(input, StandardCharsets.UTF_8); } if (StringUtils.startsWithIgnoreCase(input, BitBucketPPRConst.PAYLOAD_PFX)) { @@ -155,7 +162,8 @@ BitBucketPPRHookEvent getBitbucketEvent(@Nonnull StaplerRequest request) String xEventHeader = request.getHeader(BitBucketPPRConst.X_EVENT_KEY); // @todo: DEPRECATED_X_HEADER_REPO_POST deprecated. It will be removed in version 3.0.0 - return StringUtils.isNotBlank(xEventHeader) ? new BitBucketPPRHookEvent(xEventHeader) + return StringUtils.isNotBlank(xEventHeader) + ? new BitBucketPPRHookEvent(xEventHeader) : new BitBucketPPRHookEvent(BitBucketPPRConst.DEPRECATED_X_HEADER_REPO_POST); } @@ -175,8 +183,9 @@ public String getUrlName() { } @Override - public boolean process(HttpServletRequest request, HttpServletResponse response, - FilterChain chain) throws IOException, ServletException { + public boolean process( + HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws IOException, ServletException { String path = request.getPathInfo(); diff --git a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger/config.jelly b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger/config.jelly index 47ba265..3a5d67e 100644 --- a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger/config.jelly +++ b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger/config.jelly @@ -9,4 +9,7 @@ + + + \ No newline at end of file diff --git a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly index 9246173..ff8eb45 100644 --- a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly +++ b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/config.jelly @@ -3,20 +3,23 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/help-propagationUrlTitle.html b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/help-propagationUrlTitle.html new file mode 100644 index 0000000..b8e4329 --- /dev/null +++ b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig/help-propagationUrlTitle.html @@ -0,0 +1,4 @@ +
+

Optional endpoint used to propagate statuses.

+

It is mandatory in the case that your bitbucket repository supports only ssh cloning

+
\ No newline at end of file diff --git a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTriggerTest.java b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTriggerTest.java new file mode 100644 index 0000000..fb0a007 --- /dev/null +++ b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTriggerTest.java @@ -0,0 +1,59 @@ +package io.jenkins.plugins.bitbucketpushandpullrequest; + +import hudson.scm.SCM; +import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRPullRequestServerAction; +import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRServerRepositoryAction; +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; +import io.jenkins.plugins.bitbucketpushandpullrequest.filter.BitBucketPPRTriggerFilter; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRHookEvent; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerClone; +import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObservable; +import org.junit.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class BitBucketPPRTriggerTest { + + @Test + public void testTriggerUrlOverridesBaseUrl() { + try (MockedStatic config = Mockito.mockStatic( + BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + when(c.getPropagationUrl()).thenReturn("https://example.org/scm/some-namespace/some-repo.git"); + + BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS); + List clones = new ArrayList<>(); + BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class); + when(mockServerClone.getName()).thenReturn("ssh"); + when(mockServerClone.getHref()).thenReturn("ssh://git@example.org/some-namespace/some-repo.git"); + clones.add(mockServerClone); + when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones); + BitBucketPPRPullRequestServerAction bitBucketPPRServerRepositoryAction = new BitBucketPPRPullRequestServerAction(payloadMock); + BitBucketPPRTriggerFilter bitBucketPPRTriggerFilter = mock(BitBucketPPRTriggerFilter.class); + BitBucketPPRTrigger bitBucketPPRTrigger = new BitBucketPPRTrigger(List.of(bitBucketPPRTriggerFilter)); + bitBucketPPRTrigger.setPropagationUrl("https://example2.org/scm/some-namespace/some-repo2.git"); + BitBucketPPRHookEvent bitBucketHookEvent = mock(BitBucketPPRHookEvent.class); + + SCM scmTrigger = mock(SCM.class); + BitBucketPPRObservable observable = mock(BitBucketPPRObservable.class); + bitBucketPPRTrigger.onPost(bitBucketHookEvent, bitBucketPPRServerRepositoryAction, scmTrigger, observable); + assertEquals("https://example2.org:-1", bitBucketPPRServerRepositoryAction.getCommitLink().split("/rest/build-status/1.0/commits/")[0]); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerActionTest.java b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerActionTest.java new file mode 100644 index 0000000..7f5a484 --- /dev/null +++ b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerActionTest.java @@ -0,0 +1,46 @@ +package io.jenkins.plugins.bitbucketpushandpullrequest.action; + +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; +import io.jenkins.plugins.bitbucketpushandpullrequest.exception.BitBucketPPRPayloadPropertyNotFoundException; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerClone; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.*; + +public class BitBucketPPRPullRequestServerActionTest { + @Test + public void testBaseUrlSet() { + try (MockedStatic config = Mockito.mockStatic( + BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + when(c.getPropagationUrl()).thenReturn("https://example.org/scm/some-namespace/some-repo.git"); + + BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS); + List clones = new ArrayList<>(); + BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class); + when(mockServerClone.getName()).thenReturn("ssh"); + when(mockServerClone.getHref()).thenReturn("ssh://git@example.org/some-namespace/some-repo.git"); + clones.add(mockServerClone); + when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones); + BitBucketPPRPullRequestServerAction bitBucketPPRPullRequestServerAction; + try { + bitBucketPPRPullRequestServerAction = new BitBucketPPRPullRequestServerAction(payloadMock); + assertDoesNotThrow(() -> { + bitBucketPPRPullRequestServerAction.getCommitLink(); + }); + } catch (BitBucketPPRPayloadPropertyNotFoundException e) { + e.printStackTrace(); + } + } + } + + +} diff --git a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryActionTest.java b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryActionTest.java new file mode 100644 index 0000000..6a481c7 --- /dev/null +++ b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryActionTest.java @@ -0,0 +1,49 @@ +package io.jenkins.plugins.bitbucketpushandpullrequest.action; + + +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; +import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerClone; +import org.junit.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class BitBucketPPRServerRepositoryActionTest { + + + @Test + public void testBaseUrlSet() { + try (MockedStatic config = Mockito.mockStatic( + BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + when(c.getPropagationUrl()).thenReturn("https://example.org/scm/some-namespace/some-repo.git"); + + BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS); + List clones = new ArrayList<>(); + BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class); + when(mockServerClone.getName()).thenReturn("ssh"); + when(mockServerClone.getHref()).thenReturn("ssh://git@example.org/some-namespace/some-repo.git"); + clones.add(mockServerClone); + when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones); + BitBucketPPRServerRepositoryAction bitBucketPPRServerRepositoryAction = new BitBucketPPRServerRepositoryAction(payloadMock); + + assertDoesNotThrow(() -> { + bitBucketPPRServerRepositoryAction.getCommitLinks(); + }); + } + + + } + + +} diff --git a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/environment/BitBucketPPREnvironmentContributorTest.java b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/environment/BitBucketPPREnvironmentContributorTest.java index 25de4c8..524c6d8 100644 --- a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/environment/BitBucketPPREnvironmentContributorTest.java +++ b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/environment/BitBucketPPREnvironmentContributorTest.java @@ -39,10 +39,13 @@ import com.google.gson.Gson; import com.google.gson.stream.JsonReader; +import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import hudson.EnvVars; @@ -78,182 +81,359 @@ public void buildEnvVarsSpy() { @Test public void buildEnvironmentForCloudRepoPushTest() { - BitBucketPPRPayload payload = getCloudPayload("./cloud/repo_push.json"); - - BitBucketPPRRepositoryCause cause = mock(BitBucketPPRRepositoryCause.class); - when(cause.getRepositoryPayLoad()).thenReturn(new BitBucketPPRRepositoryAction(payload)); - when(cause.getHookEvent()).thenReturn("X-EVENT"); - - // do - runEnvironmentContributorForCause(cause); - - // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.REPOSITORY_LINK, - "https://bitbucket.org/some-repository/some-repo")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_REPOSITORY_URL, - "https://bitbucket.org/some-repository/some-repo")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_REPOSITORY_UUID, - "{6b5a1057-07ff-47c1-a65e-6c136cce4hj4}")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-van-me-nickname")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + BitBucketPPRPayload payload = getCloudPayload("./cloud/repo_push.json"); + + BitBucketPPRRepositoryCause cause = mock(BitBucketPPRRepositoryCause.class); + BitBucketPPRRepositoryAction bitBucketPPRRepositoryAction = + new BitBucketPPRRepositoryAction(payload); + when(cause.getRepositoryPayLoad()).thenReturn(bitBucketPPRRepositoryAction); + when(cause.getHookEvent()).thenReturn("X-EVENT"); + + // do + runEnvironmentContributorForCause(cause); + + // assert + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.REPOSITORY_LINK, + "https://bitbucket.org/some-repository/some-repo")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_REPOSITORY_URL, + "https://bitbucket.org/some-repository/some-repo")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_REPOSITORY_UUID, + "{6b5a1057-07ff-47c1-a65e-6c136cce4hj4}")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-van-me-nickname")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + } } @Test public void buildEnvironmentForCloudPullRequestCreatedTest() { - BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_created.json"); - - BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestAction(payload)); - when(cause.getHookEvent()).thenReturn("X-EVENT"); - - // do - runEnvironmentContributorForCause(cause); - - // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "I have to push the pram a lot X.")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "Some description for PR")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_created.json"); + + BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); + BitBucketPPRPullRequestAction bitBucketPPRPullRequestAction = + new BitBucketPPRPullRequestAction(payload); + when(cause.getPullRequestPayLoad()).thenReturn(bitBucketPPRPullRequestAction); + when(cause.getHookEvent()).thenReturn("X-EVENT"); + + // do + runEnvironmentContributorForCause(cause); + + // assert + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, + "I have to push the pram a lot X.")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, + "Some description for PR")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + } } @Test public void buildEnvironmentForCloudPullRequestMergedTest() { - BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_fulfilled.json"); - - BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestAction(payload)); - when(cause.getHookEvent()).thenReturn("X-EVENT"); - - // do - runEnvironmentContributorForCause(cause); - - // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "I have to push the pram a lot X.")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "Some description for PR")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_fulfilled.json"); + + BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); + BitBucketPPRPullRequestAction bitBucketPPRPullRequestAction = + new BitBucketPPRPullRequestAction(payload); + when(cause.getPullRequestPayLoad()).thenReturn(bitBucketPPRPullRequestAction); + when(cause.getHookEvent()).thenReturn("X-EVENT"); + + // do + runEnvironmentContributorForCause(cause); + + // assert + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, + "I have to push the pram a lot X.")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, + "Some description for PR")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + } } @Test public void buildEnvironmentForCloudPullRequestDeclinedTest() { - BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_rejected.json"); - - BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestAction(payload)); - when(cause.getHookEvent()).thenReturn("X-EVENT"); - - // do - runEnvironmentContributorForCause(cause); - - // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "I have to push the pram a lot X.")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "Some description for PR")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_rejected.json"); + + BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); + BitBucketPPRPullRequestAction bitBucketPPRPullRequestAction = + new BitBucketPPRPullRequestAction(payload); + when(cause.getPullRequestPayLoad()).thenReturn(bitBucketPPRPullRequestAction); + when(cause.getHookEvent()).thenReturn("X-EVENT"); + + // do + runEnvironmentContributorForCause(cause); + + // assert + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, + "I have to push the pram a lot X.")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, + "Some description for PR")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + } } @Test public void buildEnvironmentForCloudPullRequestUpdatedTest() { - BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_updated.json"); - - BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestAction(payload)); - when(cause.getHookEvent()).thenReturn("X-EVENT"); - - // do - runEnvironmentContributorForCause(cause); - - // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "I have to push the pram a lot X.")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "Some description for PR")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_updated.json"); + + BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); + BitBucketPPRPullRequestAction bitBucketPPRPullRequestAction = + new BitBucketPPRPullRequestAction(payload); + when(cause.getPullRequestPayLoad()).thenReturn(bitBucketPPRPullRequestAction); + when(cause.getHookEvent()).thenReturn("X-EVENT"); + + // do + runEnvironmentContributorForCause(cause); + + // assert + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, + "I have to push the pram a lot X.")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, + "Some description for PR")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + } } @Test public void buildEnvironmentForCloudPullRequestApprovedTest() { - BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_approved.json"); - - BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestAction(payload)); - when(cause.getHookEvent()).thenReturn("X-EVENT"); - - // do - runEnvironmentContributorForCause(cause); - - // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "I have to push the pram a lot X.")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "Some description for PR")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + + BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_approved.json"); + + BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); + BitBucketPPRPullRequestAction bitBucketPPRPullRequestAction = + new BitBucketPPRPullRequestAction(payload); + when(cause.getPullRequestPayLoad()).thenReturn(bitBucketPPRPullRequestAction); + when(cause.getHookEvent()).thenReturn("X-EVENT"); + + // do + runEnvironmentContributorForCause(cause); + + // assert + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "develop")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, + "I have to push the pram a lot X.")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, + "Some description for PR")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + } } @Test public void buildEnvironmentForCloudPullRequestCommentCreatedTest() { - BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_comment_created.json"); - - BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestAction(payload)); - when(cause.getHookEvent()).thenReturn("X-EVENT"); - - // do - runEnvironmentContributorForCause(cause); - - // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "destination-branch")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "I have to push the pram a lot X.")); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "Some description for PR")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + BitBucketPPRPayload payload = getCloudPayload("./cloud/pr_comment_created.json"); + + BitBucketPPRPullRequestCause cause = mock(BitBucketPPRPullRequestCause.class); + BitBucketPPRPullRequestAction bitBucketPPRPullRequestAction = + new BitBucketPPRPullRequestAction(payload); + when(cause.getPullRequestPayLoad()).thenReturn(bitBucketPPRPullRequestAction); + when(cause.getHookEvent()).thenReturn("X-EVENT"); + + // do + runEnvironmentContributorForCause(cause); + + // assert + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "feature/do-not-merge")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "destination-branch")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "https://bitbucket.org/some-repo-namespace/some-repo/pull-requests/198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "198")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-nickname")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, + "I have to push the pram a lot X.")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, + "Some description for PR")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); + } } @Ignore @@ -262,22 +442,34 @@ public void buildEnvironmentForServerPullRequestOpenedTest() throws Exception { BitBucketPPRServerPayload payload = getServerPayload("./server/pr_opened.json"); BitBucketPPRPullRequestServerCause cause = mock(BitBucketPPRPullRequestServerCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestServerAction(payload)); + when(cause.getPullRequestPayLoad()) + .thenReturn(new BitBucketPPRPullRequestServerAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do runEnvironmentContributorForCause(cause); // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "61")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "http://example.org/projects/ABC/repos/some-repo/pullrequests/61")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "61")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "http://example.org/projects/ABC/repos/some-repo/pullrequests/61")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-name")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -287,22 +479,34 @@ public void buildEnvironmentForServerPullRequestModifiedTest() throws Exception BitBucketPPRServerPayload payload = getServerPayload("./server/pr_modified.json"); BitBucketPPRPullRequestServerCause cause = mock(BitBucketPPRPullRequestServerCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestServerAction(payload)); + when(cause.getPullRequestPayLoad()) + .thenReturn(new BitBucketPPRPullRequestServerAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do runEnvironmentContributorForCause(cause); // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "http://example.org/projects/ABC/repos/some-repo/pull-requests/13")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "13")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "http://example.org/projects/ABC/repos/some-repo/pull-requests/13")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "13")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-name")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -312,22 +516,34 @@ public void buildEnvironmentForServerSourcePullRequestUpdatedTest() throws Excep BitBucketPPRServerPayload payload = getServerPayload("./server/pr_from_ref_updated.json"); BitBucketPPRPullRequestServerCause cause = mock(BitBucketPPRPullRequestServerCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestServerAction(payload)); + when(cause.getPullRequestPayLoad()) + .thenReturn(new BitBucketPPRPullRequestServerAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do runEnvironmentContributorForCause(cause); // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "http://example.org/projects/ABC/repos/some-repo/pull-requests/61")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "1")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "http://example.org/projects/ABC/repos/some-repo/pull-requests/61")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "1")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-name")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -337,22 +553,35 @@ public void buildEnvironmentForServerPullRequestApprovedTest() throws Exception BitBucketPPRServerPayload payload = getServerPayload("./server/pr_reviewer_approved.json"); BitBucketPPRPullRequestServerCause cause = mock(BitBucketPPRPullRequestServerCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestServerAction(payload)); + when(cause.getPullRequestPayLoad()) + .thenReturn(new BitBucketPPRPullRequestServerAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do runEnvironmentContributorForCause(cause); // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "http://example.org/projects/ABC/repos/some-repo/pull-requests/12")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "12")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "some-reviewer")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "http://example.org/projects/ABC/repos/some-repo/pull-requests/12")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "12")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "some-reviewer")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -362,22 +591,34 @@ public void buildEnvironmentForServerPullRequestMergedTest() throws Exception { BitBucketPPRServerPayload payload = getServerPayload("./server/pr_merged.json"); BitBucketPPRPullRequestServerCause cause = mock(BitBucketPPRPullRequestServerCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestServerAction(payload)); + when(cause.getPullRequestPayLoad()) + .thenReturn(new BitBucketPPRPullRequestServerAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do runEnvironmentContributorForCause(cause); // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "http://example.org/projects/ABC/repos/some-repo/pull-requests/61")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "61")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "develop")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "http://example.org/projects/ABC/repos/some-repo/pull-requests/61")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "61")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-name")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "test")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -387,22 +628,36 @@ public void buildEnvironmentForServerPullRequestDeclinedTest() throws Exception BitBucketPPRServerPayload payload = getServerPayload("./server/pr_declined.json"); BitBucketPPRPullRequestServerCause cause = mock(BitBucketPPRPullRequestServerCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestServerAction(payload)); + when(cause.getPullRequestPayLoad()) + .thenReturn(new BitBucketPPRPullRequestServerAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do runEnvironmentContributorForCause(cause); // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "bugfix/tst-2")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "bugfix/tst-2")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, "http://bitbucket:7990/projects/PPRPLUG/repos/hellophp/pull-requests/7")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "7")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "7")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-name")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "dummy change")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "dummy change")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -412,7 +667,8 @@ public void buildEnvironmentForServerRepoPushTest() { BitBucketPPRServerPayload payload = getServerPayload("./server/repo_refs_changed.json"); BitBucketPPRServerRepositoryCause cause = mock(BitBucketPPRServerRepositoryCause.class); - when(cause.getServerRepositoryPayLoad()).thenReturn(new BitBucketPPRServerRepositoryAction(payload)); + when(cause.getServerRepositoryPayLoad()) + .thenReturn(new BitBucketPPRServerRepositoryAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do @@ -422,7 +678,9 @@ public void buildEnvironmentForServerRepoPushTest() { assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.REPOSITORY_NAME, "some-repo")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "me-name")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_REPOSITORY_ID, "99")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -432,24 +690,40 @@ public void buildEnvironmentForServerCommentCreatedTest() throws Exception { BitBucketPPRServerPayload payload = getServerPayload("./server/pr_comment_created.json"); BitBucketPPRPullRequestServerCause cause = mock(BitBucketPPRPullRequestServerCause.class); - when(cause.getPullRequestPayLoad()).thenReturn(new BitBucketPPRPullRequestServerAction(payload)); + when(cause.getPullRequestPayLoad()) + .thenReturn(new BitBucketPPRPullRequestServerAction(payload)); when(cause.getHookEvent()).thenReturn("X-EVENT"); // do runEnvironmentContributorForCause(cause); // assert - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "test-pr2")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, - "https://bitbucket.company.com/bitbucket/users/username/repos/test-repo/pull-requests/2")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "2")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_SOURCE_BRANCH, "test-pr2")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_TARGET_BRANCH, "master")); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_LINK, + "https://bitbucket.company.com/bitbucket/users/username/repos/test-repo/pull-requests/2")); + assertThat( + envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_ID, "2")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_ACTOR, "username")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "Test pr2")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); - assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); - assertThat(envVars, - hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_COMMENT_TEXT, "Comment content")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_TITLE, "Test pr2")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_DESCRIPTION, "")); + assertThat( + envVars, + hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_PAYLOAD, payload.toString())); + assertThat( + envVars, + hasEntry( + BitBucketPPREnvironmentContributor.BITBUCKET_PULL_REQUEST_COMMENT_TEXT, + "Comment content")); assertThat(envVars, hasEntry(BitBucketPPREnvironmentContributor.BITBUCKET_X_EVENT, "X-EVENT")); } @@ -462,10 +736,14 @@ public void getBitbucketEventKeyPrOpenedTest() throws Exception { BitBucketPPRAction bitbucketAction = mock(BitBucketPPRAction.class); BitBucketPPRHookEvent bitBucketHookEvent = new BitBucketPPRHookEvent(hookEventAction); - BitBucketPPRPullRequestCreatedActionFilter actionFilter = new BitBucketPPRPullRequestCreatedActionFilter(); - BitBucketPPRTriggerCause cause = actionFilter.getCause(pollingLog, bitbucketAction, bitBucketHookEvent); - - assertEquals("Bitbuckethook event and hockEvent property of cause object are the same.", cause.getHookEvent(), + BitBucketPPRPullRequestCreatedActionFilter actionFilter = + new BitBucketPPRPullRequestCreatedActionFilter(); + BitBucketPPRTriggerCause cause = + actionFilter.getCause(pollingLog, bitbucketAction, bitBucketHookEvent); + + assertEquals( + "Bitbuckethook event and hockEvent property of cause object are the same.", + cause.getHookEvent(), hookEventAction); } @@ -481,11 +759,14 @@ public void getBitbucketEventKeyrepoRefsChangedTest() throws Exception { // method params: boolean triggerAlsoIfTagPush, boolean // triggerAlsoIfNothingChanged, String allowedBranches - BitBucketPPRRepositoryPushActionFilter actionFilter = new BitBucketPPRRepositoryPushActionFilter(false, false, - null); - BitBucketPPRTriggerCause cause = actionFilter.getCause(pollingLog, bitbucketAction, bitBucketHookEvent); - - assertEquals("Bitbuckethook event and hockEvent property of cause object are the same.", cause.getHookEvent(), + BitBucketPPRRepositoryPushActionFilter actionFilter = + new BitBucketPPRRepositoryPushActionFilter(false, false, null); + BitBucketPPRTriggerCause cause = + actionFilter.getCause(pollingLog, bitbucketAction, bitBucketHookEvent); + + assertEquals( + "Bitbuckethook event and hockEvent property of cause object are the same.", + cause.getHookEvent(), hookEventAction); } diff --git a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRCloudObserverTest.java b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRCloudObserverTest.java index 1f36b35..0645829 100644 --- a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRCloudObserverTest.java +++ b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/observer/BitBucketPPRCloudObserverTest.java @@ -1,7 +1,7 @@ package io.jenkins.plugins.bitbucketpushandpullrequest.observer; - import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; import java.io.InputStream; import java.io.InputStreamReader; @@ -20,6 +20,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -32,7 +33,6 @@ import io.jenkins.plugins.bitbucketpushandpullrequest.model.cloud.BitBucketPPRCloudPayload; import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; - @RunWith(MockitoJUnitRunner.class) public class BitBucketPPRCloudObserverTest { public BitBucketPPRPayload payload; @@ -63,36 +63,40 @@ public void readPayload() { @Test public void testPushCloudObserver() throws Throwable { - BitBucketPPRAction action = new BitBucketPPRRepositoryAction(payload); - List links = new ArrayList<>(); - links.add( - "https://api.bitbucket.org/2.0/repositories/some-repository/some-repo/commit/09c4367c5bdbef7d7a28ba4cc2638488c2088d6b"); - - assertEquals(links, action.getCommitLinks()); - - BitBucketPPRPushCloudObserver spyObserver = Mockito.spy(BitBucketPPRPushCloudObserver.class); - BitBucketPPREvent event = Mockito.mock(BitBucketPPREvent.class); - BitBucketPPREventContext context = Mockito.mock(BitBucketPPREventContext.class); - BitBucketPPRPluginConfig config = Mockito.mock(BitBucketPPRPluginConfig.class); - - Mockito.when(context.getAbsoluteUrl()).thenReturn("https://someURL"); - Mockito.when(context.getBuildNumber()).thenReturn(12); - Mockito.when(context.getAction()).thenReturn(action); - Mockito.when(event.getContext()).thenReturn(context); - Mockito.doReturn(config).when(spyObserver).getGlobalConfig(); - - String url = - "https://api.bitbucket.org/2.0/repositories/some-repository/some-repo/commit/09c4367c5bdbef7d7a28ba4cc2638488c2088d6b/statuses/build"; - Map map = new HashMap<>(); - map.put("key", spyObserver.computeBitBucketBuildKey(context)); - map.put("state", "INPROGRESS"); - map.put("url", context.getAbsoluteUrl()); - - spyObserver.getNotification(event); - spyObserver.setBuildStatusInProgress(); - Mockito.verify(spyObserver).setBuildStatusInProgress(); - - Mockito.verify(spyObserver).callClient(url, map); + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { + BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); + config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + BitBucketPPRAction action = new BitBucketPPRRepositoryAction(payload); + List links = new ArrayList<>(); + links.add( + "https://api.bitbucket.org/2.0/repositories/some-repository/some-repo/commit/09c4367c5bdbef7d7a28ba4cc2638488c2088d6b"); + + assertEquals(links, action.getCommitLinks()); + + BitBucketPPRPushCloudObserver spyObserver = Mockito.spy(BitBucketPPRPushCloudObserver.class); + BitBucketPPREvent event = Mockito.mock(BitBucketPPREvent.class); + BitBucketPPREventContext context = Mockito.mock(BitBucketPPREventContext.class); + + Mockito.when(context.getAbsoluteUrl()).thenReturn("https://someURL"); + Mockito.when(context.getBuildNumber()).thenReturn(12); + Mockito.when(context.getAction()).thenReturn(action); + Mockito.when(event.getContext()).thenReturn(context); + Mockito.doReturn(c).when(spyObserver).getGlobalConfig(); + + String url = + "https://api.bitbucket.org/2.0/repositories/some-repository/some-repo/commit/09c4367c5bdbef7d7a28ba4cc2638488c2088d6b/statuses/build"; + Map map = new HashMap<>(); + map.put("key", spyObserver.computeBitBucketBuildKey(context)); + map.put("state", "INPROGRESS"); + map.put("url", context.getAbsoluteUrl()); + + spyObserver.getNotification(event); + spyObserver.setBuildStatusInProgress(); + Mockito.verify(spyObserver).setBuildStatusInProgress(); + + Mockito.verify(spyObserver).callClient(url, map); + } } @Test @@ -142,7 +146,6 @@ public void testComputeBitBucketBuildKeyForFinishedBuild() { Mockito.when(context.getBuildNumber()).thenReturn(buildNumber); Mockito.doReturn(config).when(spyObserver).getGlobalConfig(); - // When it's configured to not use the job name Mockito.when(config.getUseJobNameAsBuildKey()).thenReturn(false); diff --git a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/processor/BitBucketPPRPullRequestServerPayloadProcessorTest.java b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/processor/BitBucketPPRPullRequestServerPayloadProcessorTest.java index c834138..d1ef687 100644 --- a/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/processor/BitBucketPPRPullRequestServerPayloadProcessorTest.java +++ b/src/test/java/io/jenkins/plugins/bitbucketpushandpullrequest/processor/BitBucketPPRPullRequestServerPayloadProcessorTest.java @@ -21,25 +21,16 @@ package io.jenkins.plugins.bitbucketpushandpullrequest.processor; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - import com.google.gson.Gson; import com.google.gson.stream.JsonReader; import io.jenkins.plugins.bitbucketpushandpullrequest.BitBucketPPRJobProbe; import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction; import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig; -import io.jenkins.plugins.bitbucketpushandpullrequest.exception.BitBucketPPRPayloadPropertyNotFoundException; import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRHookEvent; import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload; import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerPayload; import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObservable; import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObserverFactory; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; @@ -49,22 +40,24 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class BitBucketPPRPullRequestServerPayloadProcessorTest { - @Captor - private ArgumentCaptor eventCaptor; + @Captor private ArgumentCaptor eventCaptor; - @Captor - private ArgumentCaptor actionCaptor; + @Captor private ArgumentCaptor actionCaptor; - @Captor - private ArgumentCaptor observableCaptor; + @Captor private ArgumentCaptor observableCaptor; BitBucketPPRPullRequestServerPayloadProcessor pullRequestPayloadProcessor; - @Test public void testProcessPayload() throws Exception { JsonReader reader = null; @@ -79,10 +72,11 @@ public void testProcessPayload() throws Exception { e.printStackTrace(); } - try (MockedStatic config = Mockito.mockStatic( - BitBucketPPRPluginConfig.class)) { + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); + when(c.getPropagationUrl()).thenReturn(""); BitBucketPPRJobProbe probe = mock(BitBucketPPRJobProbe.class); @@ -99,8 +93,9 @@ public void testProcessPayload() throws Exception { BitBucketPPRObserverFactory.createObservable(bitbucketEvent); pullRequestPayloadProcessor.processPayload(payload, observable); - verify(probe).triggerMatchingJobs(eventCaptor.capture(), actionCaptor.capture(), - observableCaptor.capture()); + verify(probe) + .triggerMatchingJobs( + eventCaptor.capture(), actionCaptor.capture(), observableCaptor.capture()); assertEquals(bitbucketEvent, eventCaptor.getValue()); assertEquals(payload, actionCaptor.getValue().getPayload()); @@ -122,8 +117,8 @@ public void testProcessPayloadException() throws Exception { e.printStackTrace(); } - try (MockedStatic config = Mockito.mockStatic( - BitBucketPPRPluginConfig.class)) { + try (MockedStatic config = + Mockito.mockStatic(BitBucketPPRPluginConfig.class)) { BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class); config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c); @@ -141,9 +136,11 @@ public void testProcessPayloadException() throws Exception { BitBucketPPRObservable observable = BitBucketPPRObserverFactory.createObservable(bitbucketEvent); - Assertions.assertThrows(Exception.class, () -> { - pullRequestPayloadProcessor.processPayload(payload, observable); - }); + Assertions.assertThrows( + Exception.class, + () -> { + pullRequestPayloadProcessor.processPayload(payload, observable); + }); } } -} \ No newline at end of file +}