From 78b74b656399df01c69472597c24eaf898a0f5ef Mon Sep 17 00:00:00 2001 From: Guixian Zhang Date: Wed, 30 Dec 2020 19:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0commit=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=BC=80=E5=85=B3=20=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=8F=96=E6=B6=88=E9=80=BB=E8=BE=91=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20README=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 | 14 ++++-- .../jenkins/trigger/GiteePushTrigger.java | 14 +++++- .../note/NoteHookTriggerHandlerFactory.java | 6 +-- .../note/NoteHookTriggerHandlerImpl.java | 49 +++++++++++-------- .../PullRequestHookTriggerHandlerImpl.java | 40 ++++++++------- .../trigger/GiteePushTrigger/config.jelly | 5 ++ .../GiteePushTrigger/config.properties | 1 + .../GiteePushTrigger/config_zh_CN.properties | 1 + 8 files changed, 84 insertions(+), 46 deletions(-) diff --git a/README_zh_CN.md b/README_zh_CN.md index 377d127..39792e2 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -28,6 +28,7 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc ## 目前支持特性: - 推送代码到码云时,由配置的 WebHook 触发 Jenkins 任务构建。 +- 评论提交记录触发提交记录对应版本 Jenkins 任务构建 - 提交 Pull Request 到码云项目时,由配置的 WebHook 触发 Jenkins 任务构建,支持PR动作:新建,更新,接受,关闭,审查通过,测试通过。 - 支持 [ci-skip] 指令过滤 或者 [ci-build] 指令触发构建。 - 过滤已经构建的 Commit 版本,若是分支 Push,则相同分支Push才过滤,若是 PR,则是同一个PR才过滤。 @@ -121,17 +122,22 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc 1. ``Enabled Gitee triggers`` 勾选您所需要的构建触发规则,如 `Push Event`, `Opened Merge Request Events`,勾选的事件会接受WebHook,触发构建。目前支持触发事件有: - Push Events :推送代码事件 + - Commit Comment Events :评论提交记录事件 - Opened Merge Request Events :提交 PR 事件 - Updated Merge Request Events :更新 PR 事件 - Accepted Merge Request Events :接受/合并 PR 事件 - Closed Merge Request Events :关闭 PR 事件 - Approved Pull Requests : 审查通过 PR 事件 - Tested Pull Requests :测试通过 PR 事件 -2. `Enable [ci-skip]` 该选项可以开启支持 `[ci-skip]` 指令,只要commit message 中包含 `[ci-skip]`,当前commit 即可跳过构建触发。 +2. `Build Instruction Filter` : + - `None` : 无过滤 + - `[ci-skip] skip build` :commit message 或者 PR 说明包含 `[ci-skip]` 时,跳过构建触发。 + - `[ci-build] trigger build` :commit message 或者 PR 说明包含 `[ci-build]` 时,触发构建。 3. `Ignore last commit has build` 该选项可以跳过已经构建过的 Commit 版本。 -4. `Allowed branches` 可以配置允许构建的分支,目前支持分支名和正则表达式的方式进行过滤。 -5. `Secret Token for Gitee WebHook` 该选项可以配置 WebHook 的密码,该密码需要与码云 WebHook配置的密码一致方可触发构建。 -6. 注意:若 PR 状态为不可自动合并,则不触发构建。 +4. `Cancel incomplete build on same Pull Requests` 该选项在 PR 触发构建时,会判断是否存在相同 PR 且未完成的构建,有则取消未完成构建,再进行当前构建。 +5. `Allowed branches` 可以配置允许构建的分支,目前支持分支名和正则表达式的方式进行过滤。 +6. `Secret Token for Gitee WebHook` 该选项可以配置 WebHook 的密码,该密码需要与码云 WebHook配置的密码一致方可触发构建。 +7. 注意:若 PR 状态为不可自动合并,则不触发构建。 ![触发器配置](https://images.gitee.com/uploads/images/2018/0724/120539_106f7480_58426.png "屏幕截图.png") diff --git a/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java b/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java index e6fcc1a..0266431 100644 --- a/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java +++ b/src/main/java/com/gitee/jenkins/trigger/GiteePushTrigger.java @@ -65,6 +65,7 @@ public class GiteePushTrigger extends Trigger> { private static final SecureRandom RANDOM = new SecureRandom(); private boolean triggerOnPush = true; + private boolean triggerOnCommitComment = false; private boolean triggerOnOpenPullRequest = true; private boolean triggerOnPipelineEvent = false; private boolean triggerOnAcceptedPullRequest = false; @@ -109,6 +110,7 @@ public class GiteePushTrigger extends Trigger> { @Deprecated @GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*") public GiteePushTrigger(boolean triggerOnPush, + boolean triggerOnCommitComment, boolean triggerOnOpenPullRequest, String triggerOnUpdatePullRequest, boolean triggerOnAcceptedPullRequest, @@ -123,6 +125,7 @@ public class GiteePushTrigger extends Trigger> { boolean triggerOnApprovedPullRequest, String pendingBuildName, boolean cancelPendingBuildsOnUpdate, boolean cancelIncompleteBuildOnSamePullRequest) { this.triggerOnPush = triggerOnPush; + this.triggerOnCommitComment = triggerOnCommitComment; this.triggerOnOpenPullRequest = triggerOnOpenPullRequest; this.triggerOnUpdatePullRequest = triggerOnUpdatePullRequest; this.triggerOnAcceptedPullRequest = triggerOnAcceptedPullRequest; @@ -225,6 +228,10 @@ public class GiteePushTrigger extends Trigger> { return triggerOnPush; } + public boolean isTriggerOnCommitComment() { + return triggerOnCommitComment; + } + public boolean getTriggerOnOpenPullRequest() { return triggerOnOpenPullRequest; } @@ -332,6 +339,11 @@ public class GiteePushTrigger extends Trigger> { this.triggerOnPush = triggerOnPush; } + @DataBoundSetter + public void setTriggerOnCommitComment(boolean triggerOnCommitComment) { + this.triggerOnCommitComment = triggerOnCommitComment; + } + @DataBoundSetter public void setTriggerOnApprovedPullRequest(boolean triggerOnApprovedPullRequest) { this.triggerOnApprovedPullRequest = triggerOnApprovedPullRequest; @@ -535,7 +547,7 @@ public class GiteePushTrigger extends Trigger> { skipWorkInProgressPullRequest, triggerOnApprovedPullRequest, triggerOnTestedPullRequest, cancelPendingBuildsOnUpdate, ciSkipFroTestNotRequired, cancelIncompleteBuildOnSamePullRequest ); - noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnNoteRequest, noteRegex, ciSkipFroTestNotRequired, cancelIncompleteBuildOnSamePullRequest); + noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnCommitComment, triggerOnNoteRequest, noteRegex, ciSkipFroTestNotRequired, cancelIncompleteBuildOnSamePullRequest); pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, skipWorkInProgressPullRequest); pipelineTriggerHandler = newPipelineHookTriggerHandler(triggerOnPipelineEvent); } 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 a75a829..d6560b5 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, boolean cancelIncompleteBuildOnSamePullRequest) { - if (triggerOnNoteRequest) { - return new NoteHookTriggerHandlerImpl(noteRegex, ciSkipFroTestNotRequired, cancelIncompleteBuildOnSamePullRequest); + public static NoteHookTriggerHandler newNoteHookTriggerHandler(boolean triggerOnCommitComment, boolean triggerOnNoteRequest, String noteRegex, boolean ciSkipFroTestNotRequired, boolean cancelIncompleteBuildOnSamePullRequest) { + if (triggerOnCommitComment || triggerOnNoteRequest) { + return new NoteHookTriggerHandlerImpl(triggerOnCommitComment, triggerOnNoteRequest, 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 2399973..9cd3f9b 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 @@ -1,6 +1,7 @@ package com.gitee.jenkins.trigger.handler.note; import com.gitee.jenkins.cause.CauseData; +import com.gitee.jenkins.cause.GiteeWebHookCause; import com.gitee.jenkins.gitee.api.GiteeClient; import com.gitee.jenkins.gitee.api.model.PullRequest; import com.gitee.jenkins.gitee.hook.model.*; @@ -10,18 +11,13 @@ 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.AbstractBuild; -import hudson.model.Job; -import hudson.model.Run; +import hudson.model.*; 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; @@ -38,11 +34,15 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler private static final Logger LOGGER = Logger.getLogger(NoteHookTriggerHandlerImpl.class.getName()); + private final boolean triggerOnCommitComment; + private final boolean triggerOnNoteRequest; private final String noteRegex; private final boolean ciSkipFroTestNotRequired; private final boolean cancelIncompleteBuildOnSamePullRequest; - NoteHookTriggerHandlerImpl(String noteRegex, boolean ciSkipFroTestNotRequired, boolean cancelIncompleteBuildOnSamePullRequest) { + NoteHookTriggerHandlerImpl(boolean triggerOnCommitComment, boolean triggerOnNoteRequest, String noteRegex, boolean ciSkipFroTestNotRequired, boolean cancelIncompleteBuildOnSamePullRequest) { + this.triggerOnCommitComment = triggerOnCommitComment; + this.triggerOnNoteRequest = triggerOnNoteRequest; this.noteRegex = noteRegex; this.ciSkipFroTestNotRequired = ciSkipFroTestNotRequired; this.cancelIncompleteBuildOnSamePullRequest = cancelIncompleteBuildOnSamePullRequest; @@ -101,20 +101,27 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler continue; } - RevisionParameterAction revisionParameterAction = build.getAction(RevisionParameterAction.class); - if (revisionParameterAction != null) { - Config config = new Config(); - config.setString("remote", hook.getRepository().getName(), "url", hook.getRepository().getGitHttpUrl()); + CauseAction causeAction = build.getAction(CauseAction.class); + GiteeWebHookCause giteeWebHookCause = null; + for (Cause cause : causeAction.getCauses()) { + if (cause instanceof GiteeWebHookCause) { + giteeWebHookCause = (GiteeWebHookCause) cause; + break; + } + } + + if (giteeWebHookCause == null) { + continue; + } + CauseData causeData = giteeWebHookCause.getData(); + if (causeData.getSourceRepoHttpUrl().equals(hook.getPullRequest().getSource().getGitHttpUrl()) + && causeData.getTargetRepoHttpUrl().equals(hook.getPullRequest().getTarget().getGitHttpUrl()) + && causeData.getRef().equals(hook.getPullRequest().getMergeReferenceName())) { 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"); - } + 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); } @@ -232,7 +239,9 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler } private boolean isValidTrigger(NoteHook hook) { - return (isValidTriggerPhrase(hook.getComment().getBody()) && isValidTriggerAction(hook.getAction())); + // commit评论pullRequest为null + return ((triggerOnCommitComment && hook.getPullRequest() == null) || (triggerOnNoteRequest && hook.getPullRequest() != null)) + && (isValidTriggerPhrase(hook.getComment().getBody()) && isValidTriggerAction(hook.getAction())); } private boolean isValidTriggerAction(NoteAction action) { 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 4718f66..5d62876 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 @@ -5,6 +5,7 @@ import com.gitee.jenkins.cause.GiteeWebHookCause; import com.gitee.jenkins.gitee.api.GiteeClient; import com.gitee.jenkins.gitee.api.model.PullRequest; import com.gitee.jenkins.gitee.hook.model.*; +import com.gitee.jenkins.gitee.hook.model.Action; import com.gitee.jenkins.gitee.hook.model.PullRequestHook; import com.gitee.jenkins.publisher.GiteeMessagePublisher; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; @@ -13,18 +14,13 @@ 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; -import hudson.model.AbstractBuild; -import hudson.model.Job; -import hudson.model.Run; +import hudson.model.*; 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; @@ -160,24 +156,32 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler + + + + + 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 9666fac..e447966 100644 --- a/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties +++ b/src/main/resources/com/gitee/jenkins/trigger/GiteePushTrigger/config.properties @@ -2,6 +2,7 @@ Secret.Token=Secret Token for Gitee WebHook Generate=Generate Clear=Clear Push=Push Events +Commit.Comment=Commit Comment Events Enable.CI.Skip=Enable [ci-skip] Build.Instruction.Filter=Build instruction filter Build.Instruction.Filter.None=None 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 cb89769..b4ca987 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 @@ -2,6 +2,7 @@ Secret.Token=Gitee WebHook \u5BC6\u7801 Generate=\u751F\u6210 Clear=\u6E05\u9664 Push=\u63A8\u9001\u4EE3\u7801 +Commit.Comment=\u8BC4\u8BBA\u63D0\u4EA4\u8BB0\u5F55 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