Skip to content

Commit

Permalink
use merge commit for build if event is pull request fulfilled
Browse files Browse the repository at this point in the history
  • Loading branch information
julioc-p committed Oct 8, 2024
1 parent 4217fb7 commit defc1f9
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +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 io.jenkins.plugins.bitbucketpushandpullrequest.action.*;
import org.apache.commons.jelly.XMLOutput;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.Symbol;
Expand Down Expand Up @@ -66,7 +64,6 @@
import hudson.triggers.TriggerDescriptor;
import hudson.util.ListBoxModel;
import hudson.util.SequentialExecutionQueue;
import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;
import io.jenkins.plugins.bitbucketpushandpullrequest.cause.BitBucketPPRTriggerCause;
import io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRUtils;
import io.jenkins.plugins.bitbucketpushandpullrequest.event.BitBucketPPREventContext;
Expand Down Expand Up @@ -247,6 +244,8 @@ private void scheduleJob(
bitbucketAction,
new RevisionParameterAction(bitbucketAction.getLatestCommit()));

logger.info(String.format("Commit passed to git: %s", bitbucketAction.getLatestCommit()));

Check warning on line 247 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 247 is not covered by tests

QueueTaskFuture<? extends Run<?, ?>> f =
item != null ? (QueueTaskFuture) item.getFuture() : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
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;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

import hudson.model.InvisibleAction;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRHookEvent;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload;

import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_MERGED;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;

Expand All @@ -59,10 +59,13 @@ public class BitBucketPPRPullRequestAction extends BitBucketPPRActionAbstract
private final @Nonnull String repoSlug;

private final @Nonnull String pullRequestId;
private final @Nonnull BitBucketPPRHookEvent bitbucketEvent;

public BitBucketPPRPullRequestAction(@Nonnull BitBucketPPRPayload payload) {
public BitBucketPPRPullRequestAction(
@Nonnull BitBucketPPRPayload payload, @Nonnull BitBucketPPRHookEvent event) {
this.payload = payload;
this.pullRequestId = payload.getPullRequest().getId();
this.bitbucketEvent = event;

Map<String, String> workspaceRepo;
try {
Expand Down Expand Up @@ -223,6 +226,9 @@ public String getLinkStatuses() {

@Override
public String getLatestCommit() {
if (PULL_REQUEST_MERGED.equalsIgnoreCase(this.bitbucketEvent.getAction())) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 229 is only partially covered, one branch is missing
return payload.getPullRequest().getMergeCommit().getHash();
}
return payload.getPullRequest().getSource().getCommit().getHash();
}

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

package io.jenkins.plugins.bitbucketpushandpullrequest.action;

import hudson.model.InvisibleAction;
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.BitBucketPPRServerClone;

Expand All @@ -34,6 +34,7 @@
import java.util.List;
import java.util.logging.Logger;

import static io.jenkins.plugins.bitbucketpushandpullrequest.common.BitBucketPPRConst.PULL_REQUEST_MERGED;
import static java.util.Objects.isNull;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;

Expand All @@ -49,10 +50,13 @@ public class BitBucketPPRPullRequestServerAction extends BitBucketPPRActionAbstr
private String repositoryUuid;
private static final BitBucketPPRPluginConfig globalConfig =
BitBucketPPRPluginConfig.getInstance();
private final BitBucketPPRHookEvent bitbucketEvent;

public BitBucketPPRPullRequestServerAction(@Nonnull BitBucketPPRPayload payload)
public BitBucketPPRPullRequestServerAction(
@Nonnull BitBucketPPRPayload payload, BitBucketPPRHookEvent bitbucketEvent)
throws BitBucketPPRPayloadPropertyNotFoundException {
this.payload = payload;
this.bitbucketEvent = bitbucketEvent;

if (isNull(payload.getServerPullRequest().getToRef())
|| isNull(payload.getServerPullRequest().getToRef().getRepository())
Expand Down Expand Up @@ -216,14 +220,17 @@ public String getLinkDecline() throws MalformedURLException {

@Override
public String getLatestCommit() {
if (PULL_REQUEST_MERGED.equalsIgnoreCase(this.bitbucketEvent.getAction())) {
return payload.getPullRequest().getMergeCommit().getHash();
}
return payload.getServerPullRequest().getFromRef().getLatestCommit();
}

@Override
public String getCommitLink() throws MalformedURLException {
// returns:
// /rest/build-status/1.0/commits/{commitId}
String commitId = payload.getServerPullRequest().getFromRef().getLatestCommit();
String commitId = this.getLatestCommit();

return getBaseUrl() + "/rest/build-status/1.0/commits/" + commitId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import hudson.model.InvisibleAction;

import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.cloud.BitBucketPPRChange;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2018, 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,
Expand All @@ -23,7 +23,6 @@
import com.google.gson.annotations.SerializedName;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload;


public class BitBucketPPRCloudPayload implements BitBucketPPRPayload {
private static final long serialVersionUID = -3467640601880230847L;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2018, 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,
Expand All @@ -26,7 +26,6 @@
import java.util.List;
import com.google.gson.annotations.SerializedName;


public class BitBucketPPRPullRequest implements Serializable {
private static final long serialVersionUID = -530740975503014281L;
private String id;
Expand All @@ -36,6 +35,7 @@ public class BitBucketPPRPullRequest implements Serializable {
private BitBucketPPRActor author;
private @SerializedName("created_on") Date createdOn;
private @SerializedName("updated_on") Date updatedOn;
private @SerializedName("merge_commit") BitBucketPPRCommit mergeCommit;
private BitBucketPPRSource source;
private BitBucketPPRDestination destination;
private List<BitBucketPPRParticipant> participants = new ArrayList<>();
Expand Down Expand Up @@ -75,6 +75,14 @@ public void setState(final String state) {
this.state = state;
}

public BitBucketPPRCommit getMergeCommit() {
return mergeCommit;
}

public void setMergeCommit(final BitBucketPPRCommit mergeCommit) {
this.mergeCommit = mergeCommit;
}

public BitBucketPPRActor getAuthor() {
return author;
}
Expand Down Expand Up @@ -149,9 +157,32 @@ public void setReason(final String reason) {

@Override
public String toString() {
return "BitBucketPPRPullRequest [id=" + id + ", title=" + title + ", description=" + description
+ ", state=" + state + ", author=" + author + ", createdOn=" + createdOn + ", updatedOn="
+ updatedOn + ", source=" + source + ", destination=" + destination + ", participants="
+ participants + ", type=" + type + ", reason=" + reason + ", links=" + links + "]";
return "BitBucketPPRPullRequest [id="

Check warning on line 160 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/model/cloud/BitBucketPPRPullRequest.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 79-160 are not covered by tests
+ id
+ ", title="
+ title
+ ", description="
+ description
+ ", state="
+ state
+ ", author="
+ author
+ ", createdOn="
+ createdOn
+ ", updatedOn="
+ updatedOn
+ ", source="
+ source
+ ", destination="
+ destination
+ ", participants="
+ participants
+ ", type="
+ type
+ ", reason="
+ reason
+ ", links="
+ links
+ "]";
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2020, 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,
Expand All @@ -27,18 +27,18 @@
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload;
import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObservable;


public abstract class BitBucketPPRPayloadProcessor {
protected final BitBucketPPRJobProbe jobProbe;
protected final BitBucketPPRHookEvent bitbucketEvent;


public BitBucketPPRPayloadProcessor(@Nonnull final BitBucketPPRJobProbe jobProbe,
public BitBucketPPRPayloadProcessor(
@Nonnull final BitBucketPPRJobProbe jobProbe,
@Nonnull final BitBucketPPRHookEvent bitbucketEvent) {
this.jobProbe = jobProbe;
this.bitbucketEvent = bitbucketEvent;
}

public abstract void processPayload(@Nonnull BitBucketPPRPayload payload, BitBucketPPRObservable observable)
public abstract void processPayload(
@Nonnull BitBucketPPRPayload payload, BitBucketPPRObservable observable)
throws BitBucketPPRPayloadPropertyNotFoundException;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2020, 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,
Expand All @@ -28,21 +28,21 @@
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload;
import io.jenkins.plugins.bitbucketpushandpullrequest.observer.BitBucketPPRObservable;


public class BitBucketPPRPullRequestCloudPayloadProcessor extends BitBucketPPRPayloadProcessor {
public BitBucketPPRPullRequestCloudPayloadProcessor(@Nonnull BitBucketPPRJobProbe jobProbe,
@Nonnull BitBucketPPRHookEvent bitbucketEvent) {
public BitBucketPPRPullRequestCloudPayloadProcessor(
@Nonnull BitBucketPPRJobProbe jobProbe, @Nonnull BitBucketPPRHookEvent bitbucketEvent) {
super(jobProbe, bitbucketEvent);
}

private BitBucketPPRAction buildActionForJobs(@Nonnull BitBucketPPRPayload payload) {
return new BitBucketPPRPullRequestAction(payload);
private BitBucketPPRAction buildActionForJobs(
@Nonnull BitBucketPPRPayload payload, BitBucketPPRHookEvent event) {
return new BitBucketPPRPullRequestAction(payload, event);
}

@Override
public void processPayload(@Nonnull BitBucketPPRPayload payload,
BitBucketPPRObservable observable) {
BitBucketPPRAction action = buildActionForJobs(payload);
public void processPayload(
@Nonnull BitBucketPPRPayload payload, BitBucketPPRObservable observable) {
BitBucketPPRAction action = buildActionForJobs(payload, bitbucketEvent);
jobProbe.triggerMatchingJobs(bitbucketEvent, action, observable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/


package io.jenkins.plugins.bitbucketpushandpullrequest.processor;

import static java.util.Objects.nonNull;
Expand All @@ -34,28 +33,29 @@
import java.util.logging.Logger;
import javax.annotation.Nonnull;


public class BitBucketPPRPullRequestServerPayloadProcessor extends BitBucketPPRPayloadProcessor {

private static final Logger logger =
Logger.getLogger(BitBucketPPRPullRequestServerPayloadProcessor.class.getName());

public BitBucketPPRPullRequestServerPayloadProcessor(@Nonnull BitBucketPPRJobProbe jobProbe,
@Nonnull BitBucketPPRHookEvent bitbucketEvent) {
public BitBucketPPRPullRequestServerPayloadProcessor(
@Nonnull BitBucketPPRJobProbe jobProbe, @Nonnull BitBucketPPRHookEvent bitbucketEvent) {
super(jobProbe, bitbucketEvent);
logger.fine(() -> "Processing " + bitbucketEvent.toString());

}

private BitBucketPPRAction buildActionForJobs(@Nonnull BitBucketPPRPayload payload)
private BitBucketPPRAction buildActionForJobs(
@Nonnull BitBucketPPRPayload payload, @Nonnull BitBucketPPRHookEvent bitbucketEvent)
throws BitBucketPPRPayloadPropertyNotFoundException {
return new BitBucketPPRPullRequestServerAction(payload);
return new BitBucketPPRPullRequestServerAction(payload, bitbucketEvent);
}

@Override
public void processPayload(@Nonnull BitBucketPPRPayload payload,
BitBucketPPRObservable observable) throws BitBucketPPRPayloadPropertyNotFoundException {
public void processPayload(
@Nonnull BitBucketPPRPayload payload, BitBucketPPRObservable observable)
throws BitBucketPPRPayloadPropertyNotFoundException {

jobProbe.triggerMatchingJobs(bitbucketEvent, buildActionForJobs(payload), observable);
jobProbe.triggerMatchingJobs(
bitbucketEvent, buildActionForJobs(payload, bitbucketEvent), observable);
}
}
Loading

0 comments on commit defc1f9

Please sign in to comment.