Skip to content

Commit

Permalink
Add support for the "pr:comment:added" bitbucket server event (#122)
Browse files Browse the repository at this point in the history
fix 121

Co-authored-by: rabadin <rvbadin@gmail.com>
  • Loading branch information
rabadin and rabadin authored Aug 16, 2020
1 parent f7aef4f commit 0f62752
Show file tree
Hide file tree
Showing 19 changed files with 699 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public default String getComment(){
return null;
}

public default String getServerComment(){
return null;
}

public default String getCommitLink() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public String getRepositoryName() {
return payload.getServerRepository().getName();
}

@Override
public String getServerComment() {
return payload.getServerComment().getText();
}

@Override
public List<String> getScmUrls() {
return scmUrls;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* 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,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/


package io.jenkins.plugins.bitbucketpushandpullrequest.cause.pullrequest.server;

import java.io.File;
import java.io.IOException;
import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;


public class BitBucketPPRPullRequestServerCommentCreatedCause extends BitBucketPPRPullRequestServerCause {
public BitBucketPPRPullRequestServerCommentCreatedCause(File pollingLog, BitBucketPPRAction bitbucketAction)
throws IOException {
super(pollingLog, bitbucketAction);
}

@Override
public String getShortDescription() {
String pusher = bitbucketAction.getUser() != null ? bitbucketAction.getUser() : "";
return "Started by user " + pusher + ": Bitbucket PPR: comment created for pull request";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class BitBucketPPREnvironmentContributor extends EnvironmentContributor {
static final String BITBUCKET_REPOSITORY_ID = "BITBUCKET_REPOSITORY_ID";
static final String BITBUCKET_REPOSITORY_URL = "BITBUCKET_REPOSITORY_URL";
static final String BITBUCKET_SOURCE_BRANCH = "BITBUCKET_SOURCE_BRANCH";
static final String BITBUCKET_PULL_REQUEST_COMMENT_TEXT = "BITBUCKET_PULL_REQUEST_COMMENT_TEXT";
static final String REPOSITORY_LINK = "REPOSITORY_LINK";
static final String REPOSITORY_NAME = "REPOSITORY_NAME";
static final String BITBUCKET_ACTOR = "BITBUCKET_ACTOR";
Expand Down Expand Up @@ -171,6 +172,9 @@ private static void setEnvVarsForServerPullRequest(EnvVars envVars,

String payload = action.getPayload().toString();
putEnvVar(envVars, BITBUCKET_PAYLOAD, payload);

String pullRequestCommentText = action.getServerComment();
putEnvVar(envVars, BITBUCKET_PULL_REQUEST_COMMENT_TEXT, pullRequestCommentText);
}

private static void putEnvVar(EnvVars envs, String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.cloud.BitBucketPPRPullRequestTriggerFilter;
import io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.cloud.BitBucketPPRPullRequestUpdatedActionFilter;
import io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.server.BitBucketPPRPullRequestServerApprovedActionFilter;
import io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.server.BitBucketPPRPullRequestServerCommentCreatedActionFilter;
import io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.server.BitBucketPPRPullRequestServerCreatedActionFilter;
import io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.server.BitBucketPPRPullRequestServerMergedActionFilter;
import io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.server.BitBucketPPRPullRequestServerSourceUpdatedActionFilter;
Expand Down Expand Up @@ -405,4 +406,31 @@ public void pullRequestServerMergedAction(String allowedBranches, boolean isToAp
new BitBucketPPRPullRequestServerTriggerFilter(pullRequestServerMegedActionFilter);
triggers.add(pullRequestServerTriggerFilter);
}

public void pullRequestServerCommentCreatedAction() {
BitBucketPPRPullRequestServerCommentCreatedActionFilter pullRequestServerCommentCreatedActionFilter =
new BitBucketPPRPullRequestServerCommentCreatedActionFilter();
BitBucketPPRPullRequestServerTriggerFilter pullRequestServerTriggerFilter =
new BitBucketPPRPullRequestServerTriggerFilter(pullRequestServerCommentCreatedActionFilter);
triggers.add(pullRequestServerTriggerFilter);
}

public void pullRequestServerCommentCreatedAction(String allowedBranches) {
BitBucketPPRPullRequestServerCommentCreatedActionFilter pullRequestServerCommentCreatedActionFilter =
new BitBucketPPRPullRequestServerCommentCreatedActionFilter();
pullRequestServerCommentCreatedActionFilter.setAllowedBranches(allowedBranches);
BitBucketPPRPullRequestServerTriggerFilter pullRequestServerTriggerFilter =
new BitBucketPPRPullRequestServerTriggerFilter(pullRequestServerCommentCreatedActionFilter);
triggers.add(pullRequestServerTriggerFilter);
}

public void pullRequestServerCommentCreatedAction(String allowedBranches, String commentFilter) {
BitBucketPPRPullRequestServerCommentCreatedActionFilter pullRequestServerCommentCreatedActionFilter =
new BitBucketPPRPullRequestServerCommentCreatedActionFilter();
pullRequestServerCommentCreatedActionFilter.setAllowedBranches(allowedBranches);
pullRequestServerCommentCreatedActionFilter.setCommentFilter(commentFilter);
BitBucketPPRPullRequestServerTriggerFilter pullRequestServerTriggerFilter =
new BitBucketPPRPullRequestServerTriggerFilter(pullRequestServerCommentCreatedActionFilter);
triggers.add(pullRequestServerTriggerFilter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*******************************************************************************
* 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,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/

package io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.server;

import java.io.File;
import java.io.IOException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import hudson.EnvVars;
import hudson.Extension;
import io.jenkins.plugins.bitbucketpushandpullrequest.action.BitBucketPPRAction;
import io.jenkins.plugins.bitbucketpushandpullrequest.cause.BitBucketPPRTriggerCause;
import io.jenkins.plugins.bitbucketpushandpullrequest.cause.pullrequest.server.BitBucketPPRPullRequestServerCommentCreatedCause;
import io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRUtils;

public class BitBucketPPRPullRequestServerCommentCreatedActionFilter
extends BitBucketPPRPullRequestServerActionFilter {

public String allowedBranches;
public String commentFilter;

@DataBoundConstructor
public BitBucketPPRPullRequestServerCommentCreatedActionFilter() {}

@DataBoundSetter
public void setAllowedBranches(String allowedBranches) {
if (allowedBranches == null) {
this.allowedBranches = "";
} else {
this.allowedBranches = allowedBranches;
}
}

@DataBoundSetter
public void setCommentFilter(String commentFilter) {
if (commentFilter == null) {
this.commentFilter = "";
} else {
this.commentFilter = commentFilter;
}
}

@Override
public boolean shouldTriggerBuild(BitBucketPPRAction bitbucketAction) {
return matches(allowedBranches, bitbucketAction.getTargetBranch(), null) &&
hasInComment(bitbucketAction.getServerComment(), null);
}

@Override
public BitBucketPPRTriggerCause getCause(File pollingLog, BitBucketPPRAction pullRequestAction)
throws IOException {
return new BitBucketPPRPullRequestServerCommentCreatedCause(pollingLog, pullRequestAction);
}

@Override
public boolean shouldSendApprove() {
return false;
}

public boolean hasInComment(String comment, EnvVars vars) {
return BitBucketPPRUtils.matchWithRegex(comment, commentFilter, vars);
}

@Extension
public static class ActionFilterDescriptorImpl extends BitBucketPPRPullRequestServerActionDescriptor {

@Override
public String getDisplayName() {
return "Comment Created";
}
}

@Override
public String toString() {
return "BitBucketPPRPullRequestServerCommentCreatedActionFilter [getDescriptor()=" + getDescriptor()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package io.jenkins.plugins.bitbucketpushandpullrequest.filter.pullrequest.server;

import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_APPROVED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_COMMENT_CREATED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_CREATED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_MERGED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_UPDATED;
Expand Down Expand Up @@ -52,7 +53,9 @@ public boolean matchesAction(final BitBucketPPRHookEvent bitbucketEvent,
&& triggerFilter
.getActionFilter() instanceof BitBucketPPRPullRequestServerCreatedActionFilter)
|| (PULL_REQUEST_SERVER_MERGED.equalsIgnoreCase(bitbucketEvent.getAction()) && triggerFilter
.getActionFilter() instanceof BitBucketPPRPullRequestServerMergedActionFilter);
.getActionFilter() instanceof BitBucketPPRPullRequestServerMergedActionFilter)
|| (PULL_REQUEST_SERVER_COMMENT_CREATED.equals(bitbucketEvent.getAction()) && triggerFilter
.getActionFilter() instanceof BitBucketPPRPullRequestServerCommentCreatedActionFilter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_MERGED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_UPDATED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_SOURCE_UPDATED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_SERVER_COMMENT_CREATED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_UPDATED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_COMMENT_CREATED;
import static io.jenkins.plugins.bitbucketpushandpullrequest.util.BitBucketPPRConstsUtils.PULL_REQUEST_COMMENT_UPDATED;
Expand All @@ -47,14 +48,15 @@ public class BitBucketPPRHookEvent {
private String event;
private String action;


public BitBucketPPRHookEvent(String eventAction) throws OperationNotSupportedException {
String[] eventActionPair = eventAction.split(":");

event = eventActionPair[0];

if (eventActionPair.length == 3 && eventActionPair[1].equalsIgnoreCase("reviewer")) {
action = eventActionPair[2];
} else if (eventActionPair.length == 3 && eventActionPair[1].equalsIgnoreCase("comment")) {
action = eventActionPair[1] + ":" + eventActionPair[2];
} else {
action = eventActionPair[1];
}
Expand Down Expand Up @@ -85,7 +87,8 @@ private void checkOperationSupportedException(String event, String action)
|| PULL_REQUEST_SERVER_UPDATED.equalsIgnoreCase(action)
|| PULL_REQUEST_SERVER_SOURCE_UPDATED.equalsIgnoreCase(action)
|| PULL_REQUEST_SERVER_APPROVED.equalsIgnoreCase(action)
|| PULL_REQUEST_SERVER_MERGED.equalsIgnoreCase(action))) {
|| PULL_REQUEST_SERVER_MERGED.equalsIgnoreCase(action)
|| PULL_REQUEST_SERVER_COMMENT_CREATED.equalsIgnoreCase(action))) {
error = true;
}
} else if (DIAGNOSTICS.equalsIgnoreCase(event)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,27 @@
public class BitBucketPPRServerComment implements Serializable{
private static final long serialVersionUID = -4778766502864544307L;

String id;
String text;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

@Override
public String toString() {
return "BitBucketPPRServerComment [text=" + text + ", id=" + id + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class BitBucketPPRServerPayload implements BitBucketPPRPayload {
private BitBucketPPRServerPullRequest pullRequest;
private BitBucketPPRServerRepository repository;
private final List<BitBucketPPRServerChange> changes = new ArrayList<>();
private BitBucketPPRServerComment comment;

@SuppressFBWarnings
@Override
Expand All @@ -36,6 +37,11 @@ public List<BitBucketPPRServerChange> getServerChanges() {
return new ArrayList<>(changes);
}

@Override
public BitBucketPPRServerComment getServerComment() {
return comment;
}

@Override
public String toString() {
return new com.google.gson.Gson().toJson(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class BitBucketPPRConstsUtils {
public static final String PULL_REQUEST_SERVER_UPDATED = "modified";
public static final String PULL_REQUEST_SERVER_SOURCE_UPDATED = "from_ref_updated";
public static final String PULL_REQUEST_SERVER_MERGED = "merged";
public static final String PULL_REQUEST_SERVER_COMMENT_CREATED = "comment:added";

public static final String PULL_REQUEST_REVIEWER = "REVIEWER";
public static final String PULL_REQUEST_PARTICIPANT = "PARTICIPANT";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<p>Specify a java regex filter on the text's comment. If left blank, all comments will be examined for changes and built.</p>
<p>Specify a java regex filter on the comment's text. If left blank, all comments will be examined for changes and built.</p>
<p>For more infos about regex in java, consult the official java documentation</p>
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<p>Specify a java regex filter on the text's comment. If left blank, all comments will be examined for changes and built.</p>
<p>Specify a java regex filter on the comment's text. If left blank, all comments will be examined for changes and built.</p>
<p>For more infos about regex in java, consult the official java documentation</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form">
<f:entry title="Allowed Branches" field="allowed">
<f:textbox field="allowedBranches" />
</f:entry>
<f:entry title="Filter Comment" field="filter">
<f:textbox field="commentFilter" />
</f:entry>
</j:jelly>
Loading

0 comments on commit 0f62752

Please sign in to comment.