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..a57d5fb 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java @@ -36,7 +36,8 @@ import static java.util.Objects.nonNull; public class BitBucketPPRPullRequestAction extends InvisibleAction implements BitBucketPPRAction { - private static final Logger logger = Logger.getLogger(BitBucketPPRPullRequestAction.class.getName()); + 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 +62,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 +86,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 +94,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 +160,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 +180,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 +224,10 @@ public String getLatestCommit() { @Override public String getCommitLink() { - return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, - COMMIT) + '/' + this.getLatestCommit(); + return String.join( + "/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, COMMIT) + + '/' + + this.getLatestCommit(); } @Override 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..af015fa 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRRepositoryAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRRepositoryAction.java @@ -35,7 +35,6 @@ import io.jenkins.plugins.bitbucketpushandpullrequest.model.cloud.BitBucketPPRChange; import java.util.stream.Collectors; - public class BitBucketPPRRepositoryAction extends InvisibleAction implements BitBucketPPRAction { private static final Logger logger = Logger.getLogger(BitBucketPPRAction.class.getName()); public static final String COMMIT = "commit"; @@ -66,16 +65,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 +128,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 +142,18 @@ 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()); + 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()); } @Override 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 9a9769d..1928ef8 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/config/BitBucketPPRPluginConfig.java @@ -113,6 +113,7 @@ public boolean getUseJobNameAsBuildKey() { @DataBoundSetter public void setUseJobNameAsBuildKey(@CheckForNull boolean useJobNameAsBuildKey) { this.useJobNameAsBuildKey = useJobNameAsBuildKey; + save(); } @DataBoundSetter 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..03b96e8 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(); 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..74c3430 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; @@ -61,18 +62,18 @@ 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 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..f16f8e4 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; @@ -55,7 +56,7 @@ 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)); } 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..e7dc319 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; 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/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);