修复:删除,便捷PR的评论,当评论符合正则要求时也触发构建

This commit is contained in:
yashin 2020-04-01 21:24:50 +08:00
parent 7290569a7b
commit 5bed460161
3 changed files with 30 additions and 6 deletions

View File

@ -0,0 +1,8 @@
package com.gitee.jenkins.gitee.hook.model;
/**
* @author Yashin Luo
*/
public enum NoteAction {
comment, edited, deleted
}

View File

@ -16,11 +16,19 @@ public class NoteHook extends WebHook {
private Project project; private Project project;
private PullRequestObjectAttributes pullRequest; private PullRequestObjectAttributes pullRequest;
private NoteObjectAttributes comment; private NoteObjectAttributes comment;
private NoteAction action;
public NoteAction getAction() {
return action;
}
public void setAction(NoteAction action) {
this.action = action;
}
public User getUser() { public User getUser() {
return user; return user;
} }
public void setUser(User user) { public void setUser(User user) {
this.user = user; this.user = user;
} }
@ -64,6 +72,7 @@ public class NoteHook extends WebHook {
NoteHook that = (NoteHook) o; NoteHook that = (NoteHook) o;
return new EqualsBuilder() return new EqualsBuilder()
.append(user, that.user) .append(user, that.user)
.append(action, that.action)
.append(project, that.project) .append(project, that.project)
.append(comment, that.comment) .append(comment, that.comment)
.append(pullRequest, that.pullRequest) .append(pullRequest, that.pullRequest)
@ -74,6 +83,7 @@ public class NoteHook extends WebHook {
public int hashCode() { public int hashCode() {
return new HashCodeBuilder(17, 37) return new HashCodeBuilder(17, 37)
.append(user) .append(user)
.append(action)
.append(project) .append(project)
.append(comment) .append(comment)
.append(pullRequest) .append(pullRequest)
@ -84,6 +94,7 @@ public class NoteHook extends WebHook {
public String toString() { public String toString() {
return new ToStringBuilder(this) return new ToStringBuilder(this)
.append("user", user) .append("user", user)
.append("action", action)
.append("project", project) .append("project", project)
.append("comment", comment) .append("comment", comment)
.append("pullRequest", pullRequest) .append("pullRequest", pullRequest)

View File

@ -3,9 +3,7 @@ package com.gitee.jenkins.trigger.handler.note;
import com.gitee.jenkins.cause.CauseData; import com.gitee.jenkins.cause.CauseData;
import com.gitee.jenkins.gitee.api.GiteeClient; import com.gitee.jenkins.gitee.api.GiteeClient;
import com.gitee.jenkins.gitee.api.model.PullRequest; import com.gitee.jenkins.gitee.api.model.PullRequest;
import com.gitee.jenkins.gitee.hook.model.NoteHook; import com.gitee.jenkins.gitee.hook.model.*;
import com.gitee.jenkins.gitee.hook.model.PullRequestHook;
import com.gitee.jenkins.gitee.hook.model.PullRequestObjectAttributes;
import com.gitee.jenkins.publisher.GiteeMessagePublisher; import com.gitee.jenkins.publisher.GiteeMessagePublisher;
import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException;
import com.gitee.jenkins.trigger.filter.BranchFilter; import com.gitee.jenkins.trigger.filter.BranchFilter;
@ -15,7 +13,6 @@ import hudson.model.Job;
import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitSCM;
import hudson.plugins.git.RevisionParameterAction; import hudson.plugins.git.RevisionParameterAction;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -42,7 +39,7 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
@Override @Override
public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) {
if (isValidTriggerPhrase(hook.getComment().getBody())) { if (isValidTrigger(hook)) {
// 若pr不可自动合并则评论至pr // 若pr不可自动合并则评论至pr
PullRequestObjectAttributes objectAttributes = hook.getPullRequest(); PullRequestObjectAttributes objectAttributes = hook.getPullRequest();
if (objectAttributes != null && !objectAttributes.isMergeable()) { if (objectAttributes != null && !objectAttributes.isMergeable()) {
@ -146,6 +143,14 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
} }
} }
private boolean isValidTrigger(NoteHook hook) {
return (isValidTriggerPhrase(hook.getComment().getBody()) && isValidTriggerAction(hook.getAction()));
}
private boolean isValidTriggerAction(NoteAction action) {
return action == NoteAction.comment;
}
private boolean isValidTriggerPhrase(String note) { private boolean isValidTriggerPhrase(String note) {
if (StringUtils.isEmpty(this.noteRegex)) { if (StringUtils.isEmpty(this.noteRegex)) {
return false; return false;