From 5e5eec28d36e9f89763f9178263eeae6719ce352 Mon Sep 17 00:00:00 2001 From: Guixian Zhang Date: Mon, 28 Dec 2020 17:48:50 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=94=AF=E6=8C=81=20[ci-build]=20=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E8=A7=A6=E5=8F=91=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jenkins/gitee/hook/model/PushHook.java | 4 -- .../jenkins/trigger/GiteePushTrigger.java | 47 +++++++++++++++---- .../filter/BuildInstructionFilter.java | 16 +++++++ .../filter/BuildInstructionFilterType.java | 46 ++++++++++++++++++ .../AbstractWebHookTriggerHandler.java | 7 +-- .../handler/WebHookTriggerHandler.java | 4 +- .../note/NopNoteHookTriggerHandler.java | 3 +- .../note/NoteHookTriggerHandlerImpl.java | 11 ++--- .../NopPipelineHookTriggerHandler.java | 3 +- .../PipelineHookTriggerHandlerImpl.java | 7 +-- .../NopPullRequestHookTriggerHandler.java | 3 +- .../PullRequestHookTriggerHandlerImpl.java | 11 ++--- .../push/NopPushHookTriggerHandler.java | 3 +- .../push/PushHookTriggerHandlerImpl.java | 15 +++--- .../push/PushHookTriggerHandlerList.java | 5 +- .../trigger/GiteePushTrigger/config.jelly | 24 +++++++++- .../GiteePushTrigger/config.properties | 4 ++ .../GiteePushTrigger/config_zh_CN.properties | 4 ++ src/main/webapp/help/help-ci-build.html | 4 ++ src/main/webapp/help/help-ci-build_zh_CN.html | 8 ++++ 20 files changed, 183 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilter.java create mode 100644 src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilterType.java create mode 100644 src/main/webapp/help/help-ci-build.html create mode 100644 src/main/webapp/help/help-ci-build_zh_CN.html diff --git a/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java b/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java index c9ea033..ed3189d 100644 --- a/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java +++ b/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java @@ -111,10 +111,6 @@ public class PushHook extends WebHook { this.project = project; } - public void setRepository(Project project) { - this.project = project; - } - public List getCommits() { return commits; } diff --git a/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java b/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java index e67713d..1a369e1 100644 --- a/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java +++ b/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java @@ -74,7 +74,8 @@ public class GiteePushTrigger extends Trigger> { private boolean triggerOnTestedPullRequest = false; private boolean triggerOnNoteRequest = true; private String noteRegex = ""; - private boolean ciSkip = true; + private transient boolean ciSkip = true; + private BuildInstructionFilterType buildInstructionFilterType; private boolean skipWorkInProgressPullRequest; private boolean ciSkipFroTestNotRequired; private boolean skipLastCommitHasBeenBuild; @@ -113,6 +114,7 @@ public class GiteePushTrigger extends Trigger> { boolean triggerOnClosedPullRequest, boolean triggerOnNoteRequest, String noteRegex, boolean skipWorkInProgressPullRequest, boolean ciSkip, + BuildInstructionFilterType buildInstructionFilterType, boolean setBuildDescription, boolean addNoteOnPullRequest, boolean addCiMessage, boolean addVoteOnPullRequest, boolean acceptPullRequestOnSuccess, BranchFilterType branchFilterType, String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex, @@ -127,6 +129,7 @@ public class GiteePushTrigger extends Trigger> { this.noteRegex = noteRegex; this.triggerOnPipelineEvent = triggerOnPipelineEvent; this.ciSkip = ciSkip; + this.buildInstructionFilterType = buildInstructionFilterType; this.skipWorkInProgressPullRequest = skipWorkInProgressPullRequest; this.setBuildDescription = setBuildDescription; this.addNoteOnPullRequest = addNoteOnPullRequest; @@ -193,6 +196,24 @@ public class GiteePushTrigger extends Trigger> { oldConfig.jobsMigrated2 = true; oldConfig.save(); } + + // 兼容构建指令升级 + if (!oldConfig.jobsMigrated3) { + for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) { + GiteePushTrigger trigger = project.getTrigger(GiteePushTrigger.class); + if (trigger != null) { + if (trigger.getCiSkip()) { + trigger.setBuildInstructionFilterType(BuildInstructionFilterType.CI_SKIP); + } else { + trigger.setBuildInstructionFilterType(BuildInstructionFilterType.NONE); + } + project.save(); + } + } + oldConfig.jobsMigrated3 = true; + oldConfig.save(); + } + } public boolean getAddNoteOnPullRequest() { return addNoteOnPullRequest; } @@ -219,8 +240,8 @@ public class GiteePushTrigger extends Trigger> { public boolean isTriggerOnApprovedPullRequest() { return triggerOnApprovedPullRequest; - } - + } + public boolean isTriggerOnClosedPullRequest() { return triggerOnClosedPullRequest; } @@ -243,6 +264,10 @@ public class GiteePushTrigger extends Trigger> { return ciSkip; } + public BuildInstructionFilterType getBuildInstructionFilterType() { + return buildInstructionFilterType; + } + public boolean getCiSkipFroTestNotRequired() { return ciSkipFroTestNotRequired; } @@ -299,7 +324,7 @@ public class GiteePushTrigger extends Trigger> { public void setTriggerOnPush(boolean triggerOnPush) { this.triggerOnPush = triggerOnPush; } - + @DataBoundSetter public void setTriggerOnApprovedPullRequest(boolean triggerOnApprovedPullRequest) { this.triggerOnApprovedPullRequest = triggerOnApprovedPullRequest; @@ -340,6 +365,11 @@ public class GiteePushTrigger extends Trigger> { this.ciSkip = ciSkip; } + @DataBoundSetter + public void setBuildInstructionFilterType(BuildInstructionFilterType buildInstructionFilterType) { + this.buildInstructionFilterType = buildInstructionFilterType; + } + @DataBoundSetter public void setCiSkipFroTestNotRequired(boolean ciSkipFroTestNotRequired) { this.ciSkipFroTestNotRequired = ciSkipFroTestNotRequired; @@ -448,7 +478,7 @@ public class GiteePushTrigger extends Trigger> { if (pushHookTriggerHandler == null) { initializeTriggerHandler(); } - pushHookTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); + pushHookTriggerHandler.handle(job, hook, buildInstructionFilterType, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); } // executes when the Trigger receives a pull request @@ -462,7 +492,7 @@ public class GiteePushTrigger extends Trigger> { if (pullRequestHookTriggerHandler == null) { initializeTriggerHandler(); } - pullRequestHookTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); + pullRequestHookTriggerHandler.handle(job, hook, buildInstructionFilterType, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); } // executes when the Trigger receives a note request @@ -476,7 +506,7 @@ public class GiteePushTrigger extends Trigger> { if (noteHookTriggerHandler == null) { initializeTriggerHandler(); } - noteHookTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); + noteHookTriggerHandler.handle(job, hook, buildInstructionFilterType, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); } // executes when the Trigger receives a pipeline event @@ -484,7 +514,7 @@ public class GiteePushTrigger extends Trigger> { if (pipelineTriggerHandler == null) { initializeTriggerHandler(); } - pipelineTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); + pipelineTriggerHandler.handle(job, hook, buildInstructionFilterType, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); } private void initializeTriggerHandler() { @@ -540,6 +570,7 @@ public class GiteePushTrigger extends Trigger> { private transient final SequentialExecutionQueue queue = new SequentialExecutionQueue(Jenkins.MasterComputer.threadPoolForRemoting); private boolean jobsMigrated = false; private boolean jobsMigrated2 = false; + private boolean jobsMigrated3 = false; private String GiteeApiToken; private String giteeHostUrl = ""; private boolean ignoreCertificateErrors = false; diff --git a/src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilter.java b/src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilter.java new file mode 100644 index 0000000..69c7e2e --- /dev/null +++ b/src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilter.java @@ -0,0 +1,16 @@ +package com.gitee.jenkins.trigger.filter; + +/** + * @author zhanggx + */ +public interface BuildInstructionFilter { + + /** + * 是否触发构建 + * + * @param body + * @return + */ + boolean isBuildAllow(String body); + +} diff --git a/src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilterType.java b/src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilterType.java new file mode 100644 index 0000000..e13ed51 --- /dev/null +++ b/src/main/java/com/gitee/jenkins/trigger/filter/BuildInstructionFilterType.java @@ -0,0 +1,46 @@ +package com.gitee.jenkins.trigger.filter; + +/** + * @author zhanggx + */ +public enum BuildInstructionFilterType implements BuildInstructionFilter { + + /** + * 无操作 + */ + NONE("") { + @Override + public boolean isBuildAllow(String body) { + return true; + } + }, + /** + * 包含 [ci-skip] 时跳过构建 + */ + CI_SKIP("[ci-skip]") { + @Override + public boolean isBuildAllow(String body) { + return body == null ? true : !body.contains(getBody()); + } + }, + /** + * 包含 [ci-build] 时触发构建 + */ + CI_BUILD("[ci-build]") { + @Override + public boolean isBuildAllow(String body) { + return body == null ? false : body.contains(getBody()); + } + }; + + private String body; + + BuildInstructionFilterType(String body) { + this.body = body; + } + + public String getBody() { + return body; + } + +} diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java b/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java index 096628a..b17bee2 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java @@ -5,6 +5,7 @@ import com.gitee.jenkins.cause.GiteeWebHookCause; import com.gitee.jenkins.gitee.hook.model.WebHook; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import com.gitee.jenkins.util.LoggerUtil; import hudson.model.Action; @@ -34,8 +35,8 @@ public abstract class AbstractWebHookTriggerHandler implement protected PendingBuildsHandler pendingBuildsHandler = new PendingBuildsHandler(); @Override - public void handle(Job job, H hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { - if (ciSkip && isCiSkip(hook)) { + public void handle(Job job, H hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + if (isCiSkip(hook, buildInstructionFilter)) { LOGGER.log(Level.INFO, "Skipping due to ci-skip."); return; } @@ -57,7 +58,7 @@ public abstract class AbstractWebHookTriggerHandler implement protected abstract String getTriggerType(); - protected abstract boolean isCiSkip(H hook); + protected abstract boolean isCiSkip(H hook, BuildInstructionFilter buildInstructionFilter); protected abstract boolean isCommitSkip(Job job, H hook); protected Action[] createActions(Job job, H hook) { diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/WebHookTriggerHandler.java b/src/main/java/com/gitee/jenkins/trigger/handler/WebHookTriggerHandler.java index 97a7aed..c2f1944 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/WebHookTriggerHandler.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/WebHookTriggerHandler.java @@ -2,6 +2,7 @@ package com.gitee.jenkins.trigger.handler; import com.gitee.jenkins.gitee.hook.model.WebHook; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import hudson.model.Job; @@ -10,5 +11,6 @@ import hudson.model.Job; */ public interface WebHookTriggerHandler { - void handle(Job job, H hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter); + void handle(Job job, H hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter); + } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/note/NopNoteHookTriggerHandler.java b/src/main/java/com/gitee/jenkins/trigger/handler/note/NopNoteHookTriggerHandler.java index ff5aad3..1c6ca01 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/note/NopNoteHookTriggerHandler.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/note/NopNoteHookTriggerHandler.java @@ -2,6 +2,7 @@ package com.gitee.jenkins.trigger.handler.note; import com.gitee.jenkins.gitee.hook.model.NoteHook; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import hudson.model.Job; @@ -10,7 +11,7 @@ import hudson.model.Job; */ class NopNoteHookTriggerHandler implements NoteHookTriggerHandler { @Override - public void handle(Job job, NoteHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, NoteHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { // nothing to do } } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java index a57f4e6..dd84a85 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java @@ -7,6 +7,7 @@ import com.gitee.jenkins.gitee.hook.model.*; import com.gitee.jenkins.publisher.GiteeMessagePublisher; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; import hudson.model.Job; @@ -38,7 +39,7 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler } @Override - public void handle(Job job, NoteHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, NoteHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { if (isValidTrigger(hook)) { // 若pr不可自动合并则评论至pr PullRequestObjectAttributes objectAttributes = hook.getPullRequest(); @@ -61,15 +62,13 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler } - super.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); + super.handle(job, hook, buildInstructionFilter, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); } } @Override - protected boolean isCiSkip(NoteHook hook) { - return hook.getPullRequest() != null - && hook.getPullRequest().getBody() != null - && hook.getPullRequest().getBody().contains("[ci-skip]"); + protected boolean isCiSkip(NoteHook hook, BuildInstructionFilter buildInstructionFilter) { + return hook.getPullRequest() == null ? false : !buildInstructionFilter.isBuildAllow(hook.getPullRequest().getBody()); } @Override diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/NopPipelineHookTriggerHandler.java b/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/NopPipelineHookTriggerHandler.java index 4ba290c..77eb3ba 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/NopPipelineHookTriggerHandler.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/NopPipelineHookTriggerHandler.java @@ -2,6 +2,7 @@ package com.gitee.jenkins.trigger.handler.pipeline; import com.gitee.jenkins.gitee.hook.model.PipelineHook; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import hudson.model.Job; @@ -11,7 +12,7 @@ import hudson.model.Job; class NopPipelineHookTriggerHandler implements PipelineHookTriggerHandler { @Override - public void handle(Job job, PipelineHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, PipelineHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { } } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java index 69bb084..0563ba6 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java @@ -8,6 +8,7 @@ import com.gitee.jenkins.gitee.hook.model.PipelineEventObjectAttributes; import com.gitee.jenkins.gitee.hook.model.PipelineHook; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; import com.gitee.jenkins.util.BuildUtil; @@ -44,7 +45,7 @@ class PipelineHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler job, PipelineHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, PipelineHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { PipelineEventObjectAttributes objectAttributes = hook.getObjectAttributes(); try { if (job instanceof AbstractProject) { @@ -58,7 +59,7 @@ class PipelineHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler job, PullRequestHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, PullRequestHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { // nothing to do } } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java index 54271e2..fb26f29 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java @@ -9,6 +9,7 @@ import com.gitee.jenkins.gitee.hook.model.PullRequestHook; import com.gitee.jenkins.publisher.GiteeMessagePublisher; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; import com.gitee.jenkins.util.BuildUtil; @@ -58,7 +59,7 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler job, PullRequestHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, PullRequestHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { PullRequestObjectAttributes objectAttributes = hook.getPullRequest(); try { @@ -93,7 +94,7 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler job, PushHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, PushHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { // nothing to do } } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java index 52cf6ae..8c14073 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java @@ -6,6 +6,7 @@ import com.gitee.jenkins.gitee.hook.model.Commit; import com.gitee.jenkins.gitee.hook.model.PushHook; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; import hudson.model.Job; @@ -31,19 +32,19 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler private static final String NO_COMMIT = "0000000000000000000000000000000000000000"; @Override - public void handle(Job job, PushHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, PushHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { if (isNoRemoveBranchPush(hook)) { - super.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); + super.handle(job, hook, buildInstructionFilter, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); } } @Override - protected boolean isCiSkip(PushHook hook) { + protected boolean isCiSkip(PushHook hook, BuildInstructionFilter buildInstructionFilter) { List commits = hook.getCommits(); - return commits != null && - !commits.isEmpty() && - commits.get(commits.size() - 1).getMessage() != null && - commits.get(commits.size() - 1).getMessage().contains("[ci-skip]"); + if (commits != null && !commits.isEmpty()) { + return !buildInstructionFilter.isBuildAllow(commits.get(commits.size() - 1).getMessage()); + } + return false; } @Override diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerList.java b/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerList.java index f9e13aa..2ecf9d2 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerList.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerList.java @@ -2,6 +2,7 @@ package com.gitee.jenkins.trigger.handler.push; import com.gitee.jenkins.gitee.hook.model.PushHook; import com.gitee.jenkins.trigger.filter.BranchFilter; +import com.gitee.jenkins.trigger.filter.BuildInstructionFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import hudson.model.Job; @@ -19,9 +20,9 @@ class PushHookTriggerHandlerList implements PushHookTriggerHandler { } @Override - public void handle(Job job, PushHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { + public void handle(Job job, PushHook hook, BuildInstructionFilter buildInstructionFilter, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, PullRequestLabelFilter pullRequestLabelFilter) { for (PushHookTriggerHandler handler : handlers) { - handler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); + handler.handle(job, hook, buildInstructionFilter, skipLastCommitHasBeenBuild, branchFilter, pullRequestLabelFilter); } } } diff --git a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly index 60fa584..e95b1e0 100644 --- a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly +++ b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly @@ -40,9 +40,29 @@ - - + + + + + + +
+ diff --git a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties index 6917f8b..933ed95 100644 --- a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties +++ b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties @@ -3,6 +3,10 @@ Generate=Generate Clear=Clear Push=Push Events Enable.CI.Skip=Enable [ci-skip] +Build.Instruction.Filter=Build instruction filter +Build.Instruction.Filter.None=None +Build.Instruction.Filter.CiSkip=[ci-skip] skip build +Build.Instruction.Filter.CiBuild=[ci-build] trigger build Enable.CI.SkipFroTestNotRequired=Skip ci when test not required Enabled.Gitee.Triggers=Enabled Gitee triggers Approved.Pull.Request=Approved Pull Requests diff --git a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties index 686a850..b4319e5 100644 --- a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties +++ b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties @@ -4,6 +4,10 @@ Clear=\u6E05\u9664 Push=\u63A8\u9001\u4EE3\u7801 Enable.CI.Skip=\u652F\u6301 [ci-skip] \u6307\u4EE4\u8FC7\u6EE4\u6784\u5EFA Enable.CI.SkipFroTestNotRequired=PR \u4E0D\u8981\u6C42\u5FC5\u987B\u6D4B\u8BD5\u65F6\u8FC7\u6EE4\u6784\u5EFA +Build.Instruction.Filter=\u6784\u5EFA\u6307\u4EE4\u8FC7\u6EE4 +Build.Instruction.Filter.None=\u65E0 +Build.Instruction.Filter.CiSkip=[ci-skip] \u6307\u4EE4\u8DF3\u8FC7\u6784\u5EFA +Build.Instruction.Filter.CiBuild=[ci-build] \u6307\u4EE4\u89E6\u53D1\u6784\u5EFA Enabled.Gitee.Triggers=Gitee \u89E6\u53D1\u6784\u5EFA\u7B56\u7565 Approved.Pull.Request=\u5BA1\u67E5\u901A\u8FC7 Pull Requests Tested.Pull.Request=\u6D4B\u8BD5\u901A\u8FC7 Pull Requests diff --git a/src/main/webapp/help/help-ci-build.html b/src/main/webapp/help/help-ci-build.html new file mode 100644 index 0000000..cd02972 --- /dev/null +++ b/src/main/webapp/help/help-ci-build.html @@ -0,0 +1,4 @@ +
+ For Push hook: build if the commit message contains [ci-build]. + For Pull Request hook: build if the pull request body contains [ci-build]. +
diff --git a/src/main/webapp/help/help-ci-build_zh_CN.html b/src/main/webapp/help/help-ci-build_zh_CN.html new file mode 100644 index 0000000..b2891af --- /dev/null +++ b/src/main/webapp/help/help-ci-build_zh_CN.html @@ -0,0 +1,8 @@ +
+

+ 代码推送触发: 若推送的最后一个提交的描述信息包含 [ci-build],则触发构建。 +

+

+ PR 操作触发: 若 PR 的内容描述信息包含 [ci-build],则触发构建。 +

+
From 30d81f13f2eda26cada3765aacd5d77887348a43 Mon Sep 17 00:00:00 2001 From: Guixian Zhang Date: Mon, 28 Dec 2020 18:11:42 +0800 Subject: [PATCH 2/4] =?UTF-8?q?README=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_zh_CN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README_zh_CN.md b/README_zh_CN.md index 73ca4d7..2d63c25 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -29,7 +29,7 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc ## 目前支持特性: - 推送代码到码云时,由配置的 WebHook 触发 Jenkins 任务构建。 - 提交 Pull Request 到码云项目时,由配置的 WebHook 触发 Jenkins 任务构建,支持PR动作:新建,更新,接受,关闭,审查通过,测试通过。 -- 支持 [ci-skip] 指令过滤。 +- 支持 [ci-skip] 指令过滤 或者 [ci-build] 指令触发构建。 - 过滤已经构建的 Commit 版本,若是分支 Push,则相同分支Push才过滤,若是 PR,则是同一个PR才过滤。 - 按分支名过滤触发器。 - 正则表达式过滤可触发的分支。 From f21f90e061721b2e0f0c0ff2250c2d925afbffc4 Mon Sep 17 00:00:00 2001 From: silverballer <66757495@qq.com> Date: Wed, 30 Dec 2020 16:19:17 +0800 Subject: [PATCH 3/4] =?UTF-8?q?!3=20=E6=94=AF=E6=8C=81=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=97=B6=EF=BC=8C=E5=8F=96=E6=B6=88=E7=9B=B8?= =?UTF-8?q?=E5=90=8CPR=E6=9C=AA=E5=AE=8C=E6=88=90=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=EF=BC=8C=E8=BF=9B=E8=A1=8C=E5=BD=93=E5=89=8D=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=20=E5=AF=B9=E4=BA=8EPR=E8=A7=A6=E5=8F=91=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E7=9A=84=E5=9C=BA=E6=99=AF=EF=BC=8C=E5=90=8C=E4=B8=80=E4=B8=AA?= =?UTF-8?q?PR=E8=BF=9E=E7=BB=AD=E8=A7=A6=E5=8F=91=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=97=B6=EF=BC=8C=E6=94=AF=E6=8C=81=E5=8B=BE?= =?UTF-8?q?=E9=80=89=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=BB=A5=E5=88=B0=E8=BE=BE?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E4=B9=8B=E5=89=8D=E6=9E=84=E5=BB=BA=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E4=BB=BB=E5=8A=A1=EF=BC=8C=E8=BF=9B=E8=A1=8C=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E6=9E=84=E5=BB=BA=E4=BB=BB=E5=8A=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_zh_CN.md | 1 + .../jenkins/trigger/GiteePushTrigger.java | 20 ++++++-- .../AbstractWebHookTriggerHandler.java | 3 ++ .../note/NoteHookTriggerHandlerFactory.java | 4 +- .../note/NoteHookTriggerHandlerImpl.java | 49 +++++++++++++++++- .../PullRequestHookTriggerHandlerFactory.java | 22 ++++---- .../PullRequestHookTriggerHandlerImpl.java | 51 +++++++++++++++++-- .../trigger/GiteePushTrigger/config.jelly | 5 ++ .../GiteePushTrigger/config.properties | 1 + .../GiteePushTrigger/config_zh_CN.properties | 1 + ...el-same-pull-request-incomplete-build.html | 4 ++ ...e-pull-request-incomplete-build_zh_CN.html | 3 ++ 12 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 src/main/webapp/help/help-cancel-same-pull-request-incomplete-build.html create mode 100644 src/main/webapp/help/help-cancel-same-pull-request-incomplete-build_zh_CN.html diff --git a/README_zh_CN.md b/README_zh_CN.md index 73ca4d7..8447110 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -39,6 +39,7 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc - 对于 PR 相关的所有事件,若 PR 代码冲突不可自动合并,则不触发构建;且若配置了评论到PR的功能,则评论到 PR 提示冲突。 - PR 评论可通过 WebHook 触发构建(可用于 PR 触发构建失败是便于从码云平台评论重新触发构建) - 支持配置 PR 不要求必须测试时过滤触发构建。(可用于不需测试则不构建部署测试环境) +- 支持相同 PR 触发构建时,取消进行中的未完成构建,进行当前构建(相同 PR 构建不排队,多个不同 PR 构建仍需排队) ## 计划中特性 1. PR 审查并测试通过触发构建(可用户触发部署,且可配合自动合并 PR 的特性完善工作流。) diff --git a/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java b/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java index e67713d..9c5b4e4 100644 --- a/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java +++ b/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java @@ -92,6 +92,7 @@ public class GiteePushTrigger extends Trigger> { private volatile Secret secretToken; private String pendingBuildName; private boolean cancelPendingBuildsOnUpdate; + private boolean cancelIncompleteBuildOnSamePullRequest; private transient BranchFilter branchFilter; private transient PushHookTriggerHandler pushHookTriggerHandler; @@ -117,7 +118,8 @@ public class GiteePushTrigger extends Trigger> { boolean acceptPullRequestOnSuccess, BranchFilterType branchFilterType, String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex, PullRequestLabelFilterConfig pullRequestLabelFilterConfig, String secretToken, boolean triggerOnPipelineEvent, - boolean triggerOnApprovedPullRequest, String pendingBuildName, boolean cancelPendingBuildsOnUpdate) { + boolean triggerOnApprovedPullRequest, String pendingBuildName, boolean cancelPendingBuildsOnUpdate, + boolean cancelIncompleteBuildOnSamePullRequest) { this.triggerOnPush = triggerOnPush; this.triggerOnOpenPullRequest = triggerOnOpenPullRequest; this.triggerOnUpdatePullRequest = triggerOnUpdatePullRequest; @@ -142,6 +144,7 @@ public class GiteePushTrigger extends Trigger> { this.triggerOnApprovedPullRequest = triggerOnApprovedPullRequest; this.pendingBuildName = pendingBuildName; this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate; + this.cancelIncompleteBuildOnSamePullRequest = cancelIncompleteBuildOnSamePullRequest; initializeTriggerHandler(); initializeBranchFilter(); @@ -295,6 +298,10 @@ public class GiteePushTrigger extends Trigger> { return this.cancelPendingBuildsOnUpdate; } + public boolean isCancelIncompleteBuildOnSamePullRequest() { + return cancelIncompleteBuildOnSamePullRequest; + } + @DataBoundSetter public void setTriggerOnPush(boolean triggerOnPush) { this.triggerOnPush = triggerOnPush; @@ -437,6 +444,11 @@ public class GiteePushTrigger extends Trigger> { this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate; } + @DataBoundSetter + public void setCancelIncompleteBuildOnSamePullRequest(boolean cancelIncompleteBuildOnSamePullRequest) { + this.cancelIncompleteBuildOnSamePullRequest = cancelIncompleteBuildOnSamePullRequest; + } + // executes when the Trigger receives a push request public void onPost(final PushHook hook) { if (branchFilter == null) { @@ -490,8 +502,10 @@ public class GiteePushTrigger extends Trigger> { private void initializeTriggerHandler() { pullRequestHookTriggerHandler = newPullRequestHookTriggerHandler(triggerOnOpenPullRequest, triggerOnUpdatePullRequest, triggerOnAcceptedPullRequest, triggerOnClosedPullRequest, - skipWorkInProgressPullRequest, triggerOnApprovedPullRequest, triggerOnTestedPullRequest, cancelPendingBuildsOnUpdate, ciSkipFroTestNotRequired); - noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnNoteRequest, noteRegex, ciSkipFroTestNotRequired); + skipWorkInProgressPullRequest, triggerOnApprovedPullRequest, triggerOnTestedPullRequest, cancelPendingBuildsOnUpdate, ciSkipFroTestNotRequired, + cancelIncompleteBuildOnSamePullRequest + ); + noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnNoteRequest, noteRegex, ciSkipFroTestNotRequired, cancelIncompleteBuildOnSamePullRequest); pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, skipWorkInProgressPullRequest); pipelineTriggerHandler = newPipelineHookTriggerHandler(triggerOnPipelineEvent); } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java b/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java index 096628a..e428f62 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/AbstractWebHookTriggerHandler.java @@ -49,6 +49,7 @@ public abstract class AbstractWebHookTriggerHandler implement if (branchFilter.isBranchAllowed(targetBranch)) { LOGGER.log(Level.INFO, "{0} triggered for {1}.", LoggerUtil.toArray(job.getFullName(), getTriggerType())); cancelPendingBuildsIfNecessary(job, hook); + cancelIncompleteBuildIfNecessary(job, hook); scheduleBuild(job, createActions(job, hook)); } else { LOGGER.log(Level.INFO, "branch {0} is not allowed", targetBranch); @@ -76,6 +77,8 @@ public abstract class AbstractWebHookTriggerHandler implement protected void cancelPendingBuildsIfNecessary(Job job, H hook) {} + protected void cancelIncompleteBuildIfNecessary(Job job, H hook) {} + protected abstract CauseData retrieveCauseData(H hook); protected abstract String getTargetBranch(H hook); diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerFactory.java b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerFactory.java index bf392dc..a75a829 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerFactory.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerFactory.java @@ -7,9 +7,9 @@ public final class NoteHookTriggerHandlerFactory { private NoteHookTriggerHandlerFactory() {} - public static NoteHookTriggerHandler newNoteHookTriggerHandler(boolean triggerOnNoteRequest, String noteRegex, boolean ciSkipFroTestNotRequired) { + public static NoteHookTriggerHandler newNoteHookTriggerHandler(boolean triggerOnNoteRequest, String noteRegex, boolean ciSkipFroTestNotRequired, boolean cancelIncompleteBuildOnSamePullRequest) { if (triggerOnNoteRequest) { - return new NoteHookTriggerHandlerImpl(noteRegex, ciSkipFroTestNotRequired); + return new NoteHookTriggerHandlerImpl(noteRegex, ciSkipFroTestNotRequired, cancelIncompleteBuildOnSamePullRequest); } else { return new NopNoteHookTriggerHandler(); } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java index a57f4e6..7f5f7c5 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java @@ -9,10 +9,18 @@ import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.filter.BranchFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; +import hudson.model.AbstractBuild; import hudson.model.Job; +import hudson.model.Run; import hudson.plugins.git.GitSCM; import hudson.plugins.git.RevisionParameterAction; import org.apache.commons.lang.StringUtils; +import org.eclipse.jgit.lib.Config; +import org.eclipse.jgit.transport.RemoteConfig; + +import javax.servlet.ServletException; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; @@ -31,10 +39,12 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler private final String noteRegex; private final boolean ciSkipFroTestNotRequired; + private final boolean cancelIncompleteBuildOnSamePullRequest; - NoteHookTriggerHandlerImpl(String noteRegex, boolean ciSkipFroTestNotRequired) { + NoteHookTriggerHandlerImpl(String noteRegex, boolean ciSkipFroTestNotRequired, boolean cancelIncompleteBuildOnSamePullRequest) { this.noteRegex = noteRegex; this.ciSkipFroTestNotRequired = ciSkipFroTestNotRequired; + this.cancelIncompleteBuildOnSamePullRequest = cancelIncompleteBuildOnSamePullRequest; } @Override @@ -76,6 +86,43 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler protected boolean isCommitSkip(Job project, NoteHook hook) { return false; } + + @Override + protected void cancelIncompleteBuildIfNecessary(Job job, NoteHook hook) { + if (!cancelIncompleteBuildOnSamePullRequest || hook.getPullRequest() == null) { + return; + } + + for (Run build : job.getBuilds()) { + if (!job.isBuilding()) { + break; + } + + if (!build.isBuilding()) { + continue; + } + + RevisionParameterAction revisionParameterAction = build.getAction(RevisionParameterAction.class); + if (revisionParameterAction != null) { + Config config = new Config(); + config.setString("remote", hook.getRepository().getName(), "url", hook.getRepository().getGitHttpUrl()); + try { + if (revisionParameterAction.canOriginateFrom(RemoteConfig.getAllRemoteConfigs(config)) + && revisionParameterAction.commit.equals(hook.getPullRequest().getMergeReferenceName())) { + if (build.isBuilding()) { + ((AbstractBuild) build).doStop(); + LOGGER.log(Level.WARNING, "Abort incomplete build"); + } + } + } catch (URISyntaxException e) { + LOGGER.log(Level.WARNING, "Parsing repo url error", e); + } catch (ServletException | IOException e) { + LOGGER.log(Level.WARNING, "Unable to abort incomplete build", e); + } + } + } + } + @Override protected String getTargetBranch(NoteHook hook) { return hook.getPullRequest() == null ? null : hook.getPullRequest().getTargetBranch(); diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerFactory.java b/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerFactory.java index 55b72d1..2a2e1d8 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerFactory.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerFactory.java @@ -15,14 +15,15 @@ public final class PullRequestHookTriggerHandlerFactory { private PullRequestHookTriggerHandlerFactory() {} public static PullRequestHookTriggerHandler newPullRequestHookTriggerHandler(boolean triggerOnOpenPullRequest, - String triggerOnUpdatePullRequest, - boolean triggerOnAcceptedPullRequest, - boolean triggerOnClosedPullRequest, - boolean skipWorkInProgressPullRequest, - boolean triggerOnApprovedPullRequest, - boolean triggerOnTestedPullRequest, - boolean cancelPendingBuildsOnUpdate, - boolean ciSkipFroTestNotRequired) { + String triggerOnUpdatePullRequest, + boolean triggerOnAcceptedPullRequest, + boolean triggerOnClosedPullRequest, + boolean skipWorkInProgressPullRequest, + boolean triggerOnApprovedPullRequest, + boolean triggerOnTestedPullRequest, + boolean cancelPendingBuildsOnUpdate, + boolean ciSkipFroTestNotRequired, + boolean cancelIncompleteBuildOnSamePullRequest) { if (triggerOnOpenPullRequest || !("0".equals(triggerOnUpdatePullRequest) || "false".equals(triggerOnUpdatePullRequest)) || triggerOnAcceptedPullRequest @@ -46,7 +47,8 @@ public final class PullRequestHookTriggerHandlerFactory { retrieveAllowedActionDesces(triggerOnUpdatePullRequest), skipWorkInProgressPullRequest, cancelPendingBuildsOnUpdate, - ciSkipFroTestNotRequired); + ciSkipFroTestNotRequired, + cancelIncompleteBuildOnSamePullRequest); } else { return new NopPullRequestHookTriggerHandler(); } @@ -135,7 +137,7 @@ public final class PullRequestHookTriggerHandlerFactory { if (triggerOnClosedPullRequest) { result.add(State.closed); } - + return result; } } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java index 54271e2..b7c41e8 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/pull/PullRequestHookTriggerHandlerImpl.java @@ -12,12 +12,18 @@ import com.gitee.jenkins.trigger.filter.BranchFilter; import com.gitee.jenkins.trigger.filter.PullRequestLabelFilter; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; import com.gitee.jenkins.util.BuildUtil; +import hudson.model.AbstractBuild; import hudson.model.Job; import hudson.model.Run; import hudson.plugins.git.GitSCM; import hudson.plugins.git.RevisionParameterAction; import org.apache.commons.lang.StringUtils; +import org.eclipse.jgit.lib.Config; +import org.eclipse.jgit.transport.RemoteConfig; +import javax.servlet.ServletException; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.Collection; import java.util.ArrayList; import java.util.List; @@ -43,18 +49,20 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler allowedActions; private final Collection allowedActionDesces; private final boolean cancelPendingBuildsOnUpdate; + private final boolean cancelIncompleteBuildOnSamePullRequest; - PullRequestHookTriggerHandlerImpl(Collection allowedStates, boolean skipWorkInProgressPullRequest, boolean cancelPendingBuildsOnUpdate, boolean ciSkipFroTestNotRequired) { - this(allowedStates, EnumSet.allOf(Action.class), EnumSet.allOf(ActionDesc.class), skipWorkInProgressPullRequest, cancelPendingBuildsOnUpdate, ciSkipFroTestNotRequired); + PullRequestHookTriggerHandlerImpl(Collection allowedStates, boolean skipWorkInProgressPullRequest, boolean cancelPendingBuildsOnUpdate, boolean ciSkipFroTestNotRequired, boolean cancelIncompleteBuildOnSamePullRequest) { + this(allowedStates, EnumSet.allOf(Action.class), EnumSet.allOf(ActionDesc.class), skipWorkInProgressPullRequest, cancelPendingBuildsOnUpdate, ciSkipFroTestNotRequired, cancelIncompleteBuildOnSamePullRequest); } - PullRequestHookTriggerHandlerImpl(Collection allowedStates, Collection allowedActions, Collection allowedActionDesces, boolean skipWorkInProgressPullRequest, boolean cancelPendingBuildsOnUpdate, boolean ciSkipFroTestNotRequired) { + PullRequestHookTriggerHandlerImpl(Collection allowedStates, Collection allowedActions, Collection allowedActionDesces, boolean skipWorkInProgressPullRequest, boolean cancelPendingBuildsOnUpdate, boolean ciSkipFroTestNotRequired, boolean cancelIncompleteBuildOnSamePullRequest) { this.allowedStates = allowedStates; this.allowedActions = allowedActions; this.allowedActionDesces = allowedActionDesces; this.skipWorkInProgressPullRequest = skipWorkInProgressPullRequest; this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate; this.ciSkipFroTestNotRequired = ciSkipFroTestNotRequired; + this.cancelIncompleteBuildOnSamePullRequest = cancelIncompleteBuildOnSamePullRequest; } @Override @@ -138,6 +146,43 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler job, PullRequestHook hook) { + if (!cancelIncompleteBuildOnSamePullRequest) { + return; + } + + for (Run build : job.getBuilds()) { + if (!job.isBuilding()) { + break; + } + + if (!build.isBuilding()) { + continue; + } + + RevisionParameterAction revisionParameterAction = build.getAction(RevisionParameterAction.class); + if (revisionParameterAction != null) { + Config config = new Config(); + config.setString("remote", hook.getRepo().getName(), "url", hook.getRepo().getGitHttpUrl()); + try { + if (revisionParameterAction.canOriginateFrom(RemoteConfig.getAllRemoteConfigs(config)) + && revisionParameterAction.commit.equals(hook.getPullRequest().getMergeReferenceName())) { + if (build.isBuilding()) { + ((AbstractBuild) build).doStop(); + LOGGER.log(Level.WARNING, "Abort incomplete build"); + } + } + } catch (URISyntaxException e) { + LOGGER.log(Level.WARNING, "Parsing repo url error", e); + } catch (ServletException | IOException e) { + LOGGER.log(Level.WARNING, "Unable to abort incomplete build", e); + } + } + } + + } + @Override protected String getTargetBranch(PullRequestHook hook) { return hook.getPullRequest() == null ? null : hook.getPullRequest().getTargetBranch(); diff --git a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly index 60fa584..599b533 100644 --- a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly +++ b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.jelly @@ -50,6 +50,11 @@ + + + + + diff --git a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties index 6917f8b..573d425 100644 --- a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties +++ b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties @@ -33,6 +33,7 @@ Filter.merge.request.by.label=Filter pull request by label Exclude=Exclude Include=Include Ignore.Last.Commit.Has.Build=Ignore last commit has been build +Cancel.Same.Pull.Request.Incomplete.Build=Cancel incomplete build on same Pull Requests Comments=Comment Pull Requests Comment.Regex=Comment (regex) for triggering a build Retry.Text=Jenkins please retry a build diff --git a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties index 686a850..1ecc38b 100644 --- a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties +++ b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config_zh_CN.properties @@ -33,6 +33,7 @@ Filter.merge.request.by.label=\u6839\u636E\u6807\u7B7E\u8FC7\u6EE4PR Exclude=\u6392\u9664 Include=\u5305\u62EC Ignore.Last.Commit.Has.Build=\u8FC7\u6EE4\u5DF2\u7ECF\u6784\u5EFA\u7684 Commit \u7248\u672C +Cancel.Same.Pull.Request.Incomplete.Build=\u53D6\u6D88\u76F8\u540C Pull Requests \u672A\u5B8C\u6210\u6784\u5EFA Comments=\u8BC4\u8BBA Pull Requests Comment.Regex=\u8BC4\u8BBA\u5185\u5BB9\u7684\u6B63\u5219\u8868\u8FBE\u5F0F Retry.Text=Jenkins please retry a build diff --git a/src/main/webapp/help/help-cancel-same-pull-request-incomplete-build.html b/src/main/webapp/help/help-cancel-same-pull-request-incomplete-build.html new file mode 100644 index 0000000..522056a --- /dev/null +++ b/src/main/webapp/help/help-cancel-same-pull-request-incomplete-build.html @@ -0,0 +1,4 @@ +
+ If there is an building build for the same PR, + cancel the building build and trigger the current build +
diff --git a/src/main/webapp/help/help-cancel-same-pull-request-incomplete-build_zh_CN.html b/src/main/webapp/help/help-cancel-same-pull-request-incomplete-build_zh_CN.html new file mode 100644 index 0000000..f116096 --- /dev/null +++ b/src/main/webapp/help/help-cancel-same-pull-request-incomplete-build_zh_CN.html @@ -0,0 +1,3 @@ +
+ 如果相同 PR 存在进行中的构建,则取消进行中构建,触发当前构建 +
From 896007e1931d19768b64d47254d0d770e44a89f2 Mon Sep 17 00:00:00 2001 From: silverballer <66757495@qq.com> Date: Wed, 30 Dec 2020 16:20:58 +0800 Subject: [PATCH 4/4] =?UTF-8?q?!4=20=E6=94=AF=E6=8C=81commit=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E8=A7=A6=E5=8F=91=20*=20=E6=94=AF=E6=8C=81commit?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gitee/jenkins/cause/CauseData.java | 5 ++ .../jenkins/gitee/hook/model/NoteHook.java | 5 ++ .../hook/model/NoteObjectAttributes.java | 72 ++++++++++++------- .../note/NoteHookTriggerHandlerImpl.java | 39 +++++++++- .../gitee/jenkins/cause/Messages.properties | 1 + .../jenkins/cause/Messages_zh_CN.properties | 1 + 6 files changed, 97 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/gitee/jenkins/cause/CauseData.java b/src/main/java/com/gitee/jenkins/cause/CauseData.java index 3964f53..7ed465a 100644 --- a/src/main/java/com/gitee/jenkins/cause/CauseData.java +++ b/src/main/java/com/gitee/jenkins/cause/CauseData.java @@ -531,6 +531,11 @@ public final class CauseData { data.getTargetProjectUrl()); } } + }, COMMIT_COMMENT { + @Override + String getShortDescription(CauseData data) { + return Messages.GiteeWebHookCause_ShortDescription_Commit_comment(data.getTriggeredByUser()); + } }, PIPELINE { @Override String getShortDescription(CauseData data) { diff --git a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java index c5288bc..75571a5 100644 --- a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java +++ b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java @@ -58,6 +58,11 @@ public class NoteHook extends WebHook { } public String getWebHookDescription() { + // 兼容commit评论 + if (pullRequest == null) { + return getHookName() + " commit sha = " + comment.getCommitId(); + } + return getHookName() + " iid = " + pullRequest.getNumber() + " merge commit sha = " + pullRequest.getMergeCommitSha(); } diff --git a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java index 6a8e7b5..9ac2cf1 100644 --- a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java +++ b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java @@ -20,6 +20,8 @@ public class NoteObjectAttributes { private Date createdAt; private Date updatedAt; private String htmlUrl; + private String commitId; + private User user; public Integer getId() { return id; @@ -77,6 +79,22 @@ public class NoteObjectAttributes { this.htmlUrl = htmlUrl; } + public String getCommitId() { + return commitId; + } + + public void setCommitId(String commitId) { + this.commitId = commitId; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -87,39 +105,45 @@ public class NoteObjectAttributes { } NoteObjectAttributes that = (NoteObjectAttributes) o; return new EqualsBuilder() - .append(id, that.id) - .append(body, that.body) - .append(projectId, that.projectId) - .append(authorId, that.authorId) - .append(createdAt, that.createdAt) - .append(updatedAt, that.updatedAt) - .append(htmlUrl, that.htmlUrl) - .isEquals(); + .append(id, that.id) + .append(body, that.body) + .append(projectId, that.projectId) + .append(authorId, that.authorId) + .append(createdAt, that.createdAt) + .append(updatedAt, that.updatedAt) + .append(htmlUrl, that.htmlUrl) + .append(commitId, that.commitId) + .append(user, that.user) + .isEquals(); } @Override public int hashCode() { return new HashCodeBuilder(17, 37) - .append(id) - .append(body) - .append(projectId) - .append(authorId) - .append(createdAt) - .append(updatedAt) - .append(htmlUrl) - .toHashCode(); + .append(id) + .append(body) + .append(projectId) + .append(authorId) + .append(createdAt) + .append(updatedAt) + .append(htmlUrl) + .append(commitId) + .append(user) + .toHashCode(); } @Override public String toString() { return new ToStringBuilder(this) - .append("id", id) - .append("body", body) - .append("projectId", projectId) - .append("authorId", authorId) - .append("createdAt", createdAt) - .append("updatedAt", updatedAt) - .append("htmlUrl", htmlUrl) - .toString(); + .append("id", id) + .append("body", body) + .append("projectId", projectId) + .append("authorId", authorId) + .append("createdAt", createdAt) + .append("updatedAt", updatedAt) + .append("htmlUrl", htmlUrl) + .append("commitId", commitId) + .append("user", user) + .toString(); } } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java index 7f5f7c5..56a72fb 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java @@ -135,6 +135,35 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler @Override protected CauseData retrieveCauseData(NoteHook hook) { + // 兼容来自commit的评论 + if (hook.getPullRequest() == null) { + return causeData() + .withActionType(CauseData.ActionType.COMMIT_COMMENT) + .withUserName(hook.getComment().getUser().getUsername()) + .withUserEmail(hook.getComment().getUser().getEmail()) + .withPullRequestTitle("") + .withBranch("") + .withSourceBranch("") + .withSourceProjectId(hook.getProject().getId()) + .withSourceRepoHomepage(hook.getProject().getHomepage()) + .withSourceRepoName(hook.getProject().getName()) + .withSourceNamespace(hook.getProject().getNamespace()) + .withSourceRepoUrl(hook.getProject().getUrl()) + .withSourceRepoSshUrl(hook.getProject().getSshUrl()) + .withSourceRepoHttpUrl(hook.getProject().getGitHttpUrl()) + .withTargetBranch("") + .withTargetProjectId(hook.getProject().getId()) + .withTargetRepoName(hook.getProject().getName()) + .withTargetNamespace(hook.getProject().getNamespace()) + .withTargetRepoSshUrl(hook.getProject().getSshUrl()) + .withTargetRepoHttpUrl(hook.getProject().getGitHttpUrl()) + .withTriggeredByUser(hook.getComment().getUser().getName()) + .withTriggerPhrase(hook.getComment().getBody()) + .withSha(hook.getComment().getCommitId()) + .withPathWithNamespace(hook.getProject().getPathWithNamespace()) + .build(); + } + return causeData() .withActionType(CauseData.ActionType.NOTE) .withSourceProjectId(hook.getPullRequest().getSourceProjectId()) @@ -188,9 +217,15 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler if (hook.getPullRequest() != null && hook.getPullRequest().getMergeReferenceName() != null) { return hook.getPullRequest().getMergeReferenceName(); - } else { - throw new NoRevisionToBuildException(); } + + // 兼容来自commit的评论 + if (hook.getComment() != null + && StringUtils.isNotBlank(hook.getComment().getCommitId())) { + return hook.getComment().getCommitId(); + } + + throw new NoRevisionToBuildException(); } private boolean isValidTrigger(NoteHook hook) { diff --git a/src/main/resources/com/gitee/jenkins/cause/Messages.properties b/src/main/resources/com/gitee/jenkins/cause/Messages.properties index 178bbe8..a1311ab 100644 --- a/src/main/resources/com/gitee/jenkins/cause/Messages.properties +++ b/src/main/resources/com/gitee/jenkins/cause/Messages.properties @@ -4,5 +4,6 @@ GiteeWebHookCause.ShortDescription.PullRequestHook_html=Triggered by Gitee Pull Request #{1}: {2} => {3} GiteeWebHookCause.ShortDescription.NoteHook_plain=Triggered by {0} Gitee Pull Request #{1}: {2} => {3} +GiteeWebHookCause.ShortDescription.Commit_comment=Started by Gitee comment by {0} GiteeWebHookCause.ShortDescription.PipelineHook_noStatus=Started by Gitee Pipeline event GiteeWebHookCause.ShortDescription.PipelineHook=Started by Gitee Pipeline event {0} diff --git a/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties b/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties index 15097a5..b86ca36 100644 --- a/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties +++ b/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties @@ -4,5 +4,6 @@ GiteeWebHookCause.ShortDescription.PullRequestHook_html=\u7531 Gitee Pull Request #{1}: {2} => {3} \u89E6\u53D1 GiteeWebHookCause.ShortDescription.NoteHook_plain=\u7531 {0} Gitee Pull Request #{1}: {2} => {3} \u89E6\u53D1 +GiteeWebHookCause.ShortDescription.Commit_comment=Gitee \u7528\u6237 {0} \u8BC4\u8BBA\u63D0\u4EA4\u89E6\u53D1\u6784\u5EFA GiteeWebHookCause.ShortDescription.PipelineHook_noStatus=\u7531 Gitee Pipeline \u89E6\u53D1 GiteeWebHookCause.ShortDescription.PipelineHook=\u7531 Gitee Pipeline {0} \u89E6\u53D1