!7 增加commit评论触发开关、README修改、细节修改

Merge pull request !7 from silverballer/zhangguixian-20201230-update
This commit is contained in:
Yashin 2020-12-31 17:02:50 +08:00 committed by Gitee
commit 12457b66d4
8 changed files with 85 additions and 48 deletions

View File

@ -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,19 +122,23 @@ 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 状态为不可自动合并,则不触发构建。
![触发器配置](https://images.gitee.com/uploads/images/2018/0724/120539_106f7480_58426.png "屏幕截图.png")
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/2020/1231/093554_e4c48be9_2102225.png "屏幕截图.png")
### 构建后步骤配置

View File

@ -65,6 +65,7 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
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<Job<?, ?>> {
@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<Job<?, ?>> {
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<Job<?, ?>> {
return triggerOnPush;
}
public boolean isTriggerOnCommitComment() {
return triggerOnCommitComment;
}
public boolean getTriggerOnOpenPullRequest() {
return triggerOnOpenPullRequest;
}
@ -332,6 +339,11 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
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<Job<?, ?>> {
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);
}

View File

@ -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();
}

View File

@ -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<NoteHook>
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<NoteHook>
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<NoteHook>
}
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) {

View File

@ -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<Pu
continue;
}
RevisionParameterAction revisionParameterAction = build.getAction(RevisionParameterAction.class);
if (revisionParameterAction != null) {
Config config = new Config();
config.setString("remote", hook.getRepo().getName(), "url", hook.getRepo().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);
}
}
}
}

View File

@ -6,6 +6,11 @@
<f:entry title="${%Push}" field="triggerOnPush">
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%Commit.Comment}" field="triggerOnCommitComment">
<f:checkbox default="false"/>
</f:entry>
<f:entry title="${%Opened.Pull.Request}" field="triggerOnOpenPullRequest">
<f:checkbox default="true"/>
</f:entry>

View File

@ -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

View File

@ -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