Skip to content

Commit

Permalink
abstract class and central propagation url overriden by the trigger c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
julioc-p committed Sep 12, 2024
1 parent dfbb5bc commit 325bef8
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -81,6 +82,8 @@
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem;

import static org.apache.commons.lang3.ObjectUtils.isEmpty;

/**
* @author cdelmonte
*/
Expand Down Expand Up @@ -198,22 +201,11 @@ private void checkLocalPropagationUrl(BitBucketPPRAction bitBucketPPRAction) {
if (bitBucketPPRAction == null) {

Check warning on line 201 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 201 is only partially covered, one branch is missing
return;

Check warning on line 202 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 202 is not covered by tests
}
if (bitBucketPPRAction instanceof BitBucketPPRServerRepositoryAction
&& propagationUrl != null
&& !propagationUrl.isEmpty()) {
// cast
BitBucketPPRServerRepositoryAction bitBucketPPRServerRepositoryAction =
(BitBucketPPRServerRepositoryAction) bitBucketPPRAction;
bitBucketPPRServerRepositoryAction.setBaseUrl(propagationUrl);
}

if (bitBucketPPRAction instanceof BitBucketPPRPullRequestServerAction
&& propagationUrl != null
&& !propagationUrl.isEmpty()) {
// cast
BitBucketPPRPullRequestServerAction bitBucketPPRPullRequestServerAction =
(BitBucketPPRPullRequestServerAction) bitBucketPPRAction;
bitBucketPPRPullRequestServerAction.setBaseUrl(propagationUrl);
assert bitBucketPPRAction instanceof BitBucketPPRActionAbstract;

Check warning on line 204 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 204 is only partially covered, one branch is missing
if (!isEmpty(propagationUrl)) {

Check warning on line 205 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/BitBucketPPRTrigger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 205 is only partially covered, one branch is missing
BitBucketPPRActionAbstract bitBucketPPRActionAbstract =
(BitBucketPPRActionAbstract) bitBucketPPRAction;
bitBucketPPRActionAbstract.setPropagationUrl(propagationUrl);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package io.jenkins.plugins.bitbucketpushandpullrequest.action;

import java.net.MalformedURLException;
import java.util.List;

import hudson.model.Action;
Expand All @@ -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;
}

Expand Down Expand Up @@ -125,11 +126,11 @@ public default String getLatestCommit() {
return null;
}

public default String getCommitLink() {
public default String getCommitLink() throws MalformedURLException {
return null;
}

public default List<String> getCommitLinks() {
public default List<String> getCommitLinks() throws MalformedURLException {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)) {

Check warning on line 12 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRActionAbstract.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 12 is only partially covered, one branch is missing
this.setPropagationUrl(getGlobalConfig().propagationUrl);

Check warning on line 13 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRActionAbstract.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 13 is not covered by tests
}
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,8 +36,10 @@
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 {
public class BitBucketPPRPullRequestAction extends BitBucketPPRActionAbstract
implements BitBucketPPRAction {
private static final Logger logger =
Logger.getLogger(BitBucketPPRPullRequestAction.class.getName());
public static final String APPROVE = "/approve";
Expand Down Expand Up @@ -224,8 +228,9 @@ public String getLatestCommit() {

@Override
public String getCommitLink() {
return String.join(
"/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, COMMIT)
String baseCommitLink =
isEmpty(this.getPropagationUrl()) ? BITBUCKET_API_BASE_URL : this.getPropagationUrl();
return String.join("/", baseCommitLink, BITBUCKET_REPOSITORIES, workspace, repoSlug, COMMIT)
+ '/'
+ this.getLatestCommit();

Check warning on line 235 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 167-235 are not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
import java.util.logging.Logger;

import static java.util.Objects.isNull;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;

public class BitBucketPPRPullRequestServerAction extends InvisibleAction
public class BitBucketPPRPullRequestServerAction extends BitBucketPPRActionAbstract
implements BitBucketPPRAction {

private static final Logger logger =
Expand Down Expand Up @@ -177,7 +178,7 @@ public String getRepositoryId() {
}

@Override
public String getLinkApprove() {
public String getLinkApprove() throws MalformedURLException {
String projectKey =
payload.getServerPullRequest().getFromRef().getRepository().getProject().getKey();
String repoSlug = payload.getServerPullRequest().getFromRef().getRepository().getSlug();
Expand All @@ -194,7 +195,7 @@ public String getLinkApprove() {
}

@Override
public String getLinkDecline() {
public String getLinkDecline() throws MalformedURLException {
// returns:
// {baseUrl}/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/approve

Expand All @@ -219,16 +220,22 @@ 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();

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
import io.jenkins.plugins.bitbucketpushandpullrequest.model.cloud.BitBucketPPRChange;
import java.util.stream.Collectors;

public class BitBucketPPRRepositoryAction extends InvisibleAction implements BitBucketPPRAction {
import static org.apache.commons.lang3.ObjectUtils.isEmpty;

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";
Expand Down Expand Up @@ -142,12 +145,14 @@ public String getLatestCommit() {

@Override
public List<String> getCommitLinks() {
String baseCommitLink =
isEmpty(this.getPropagationUrl()) ? BITBUCKET_API_BASE_URL : this.getPropagationUrl();

Check warning on line 149 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRRepositoryAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 149 is only partially covered, one branch is missing
return payload.getPush().getChanges().stream()
.map(
c ->
String.join(
"/",
BITBUCKET_API_BASE_URL,
baseCommitLink,
BITBUCKET_REPOSITORIES,
workspace,
repoSlug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class BitBucketPPRServerRepositoryAction extends InvisibleAction
import static org.apache.commons.lang3.ObjectUtils.isEmpty;

public class BitBucketPPRServerRepositoryAction extends BitBucketPPRActionAbstract
implements BitBucketPPRAction {
private static final Logger logger = Logger.getLogger(BitBucketPPRAction.class.getName());
private static final BitBucketPPRPluginConfig globalConfig =
Expand Down Expand Up @@ -68,14 +70,6 @@ public BitBucketPPRServerRepositoryAction(BitBucketPPRPayload payload) {
}
}

if (!globalConfig.getPropagationUrl().isEmpty()) {
try {
this.baseUrl = new URL(globalConfig.getPropagationUrl());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

for (BitBucketPPRServerChange change : payload.getServerChanges()) {
if (change.getRefId() != null) {
this.targetBranchName = change.getRef().getDisplayId();
Expand Down Expand Up @@ -172,16 +166,17 @@ public String getLatestCommit() {
}

@Override
public List<String> getCommitLinks() {
public List<String> getCommitLinks() throws MalformedURLException {
// returns:
// /rest/build-status/1.0/commits/{commitId}

String baseUrl = getBaseUrl();
URL baseCommitLink =
isEmpty(this.getPropagationUrl()) ? baseUrl : new URL(this.getPropagationUrl());

Check warning on line 174 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 174 is only partially covered, one branch is missing

List<BitBucketPPRServerChange> changes = payload.getServerChanges();
List<String> 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());

Check warning on line 179 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 179 is not covered by tests
}

return links;
Expand All @@ -190,13 +185,4 @@ public List<String> getCommitLinks() {
private String getBaseUrl() {
return baseUrl.getProtocol() + "://" + baseUrl.getHost() + ":" + baseUrl.getPort();
}

public String setBaseUrl(String url) {
try {
this.baseUrl = new URL(url);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void setBuildStatusOnFinished() throws MalformedURLException {
}

@Override
public void setBuildStatusInProgress() {
public void setBuildStatusInProgress() throws MalformedURLException {
BitBucketPPRAction bitbucketAction = context.getAction();
String url = bitbucketAction.getCommitLink() + "/statuses/build";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,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;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public void setApprovedOrDeclined() {
}

@Override
public void setBuildStatusOnFinished() {
public void setBuildStatusOnFinished() throws MalformedURLException {
BitBucketPPRAction bitbucketAction = context.getAction();
String url = bitbucketAction.getCommitLink();
Result result = context.getRun().getResult();
Expand All @@ -92,7 +92,7 @@ public void setBuildStatusOnFinished() {
}

@Override
public void setBuildStatusInProgress() {
public void setBuildStatusInProgress() throws MalformedURLException {
BitBucketPPRAction bitbucketAction = context.getAction();
String url = bitbucketAction.getCommitLink();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,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 =
Expand All @@ -61,7 +61,7 @@ public void setBuildStatusOnFinished() {
}

@Override
public void setBuildStatusInProgress() {
public void setBuildStatusInProgress() throws MalformedURLException {
BitBucketPPRAction bitbucketAction = context.getAction();

Map<String, String> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,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";
Expand All @@ -60,7 +60,7 @@ public void setBuildStatusOnFinished() {
}

@Override
public void setBuildStatusInProgress() {
public void setBuildStatusInProgress() throws MalformedURLException {
BitBucketPPRAction bitbucketAction = context.getAction();
Map<String, String> map = new HashMap<>();
map.put("key", computeBitBucketBuildKey(context));
Expand Down

0 comments on commit 325bef8

Please sign in to comment.