修复pr更新不触发构建

This commit is contained in:
yashin 2018-07-04 19:25:28 +08:00
parent 310b8996e5
commit c2a0d84136
14 changed files with 78 additions and 83 deletions

View File

@ -184,7 +184,7 @@ In the *Source Code Management* section:
1. Click *Git*
2. Enter your *Repository URL*, such as ``git@your.gitlab.server:gitlab_group/gitlab_project.git``
1. In the *Advanced* settings, set *Name* to ``origin`` and *Refspec* to ``+refs/heads/*:refs/remotes/origin/* +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*``
1. In the *Advanced* settings, set *Name* to ``origin`` and *Refspec* to ``+refs/heads/*:refs/remotes/origin/* +refs/pull/*/MERGE:refs/remotes/origin/pull/*``
3. In *Branch Specifier* enter:
1. For single-repository workflows: ``origin/${gitlabSourceBranch}``
2. For forked repository workflows: ``merge-requests/${gitlabMergeRequestIid}``

View File

@ -14,7 +14,7 @@
</properties>
<artifactId>gitee-jenkins-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<name>Gitee Jenkins Plugin</name>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Gitee+Jenkins+Plugin</url>
<packaging>hpi</packaging>

View File

@ -62,18 +62,20 @@ import static com.gitee.jenkins.trigger.handler.push.PushHookTriggerHandlerFacto
* Triggers a build when we receive a Gitee WebHook.
*
* @author Daniel Brooks
* @author Yashin Luo
*
*/
public class GiteePushTrigger extends Trigger<Job<?, ?>> {
private static final SecureRandom RANDOM = new SecureRandom();
private boolean triggerOnPush = true;
private boolean triggerOnMergeRequest = true;
private boolean triggerOnOpenMergeRequest = true;
private boolean triggerOnPipelineEvent = false;
private boolean triggerOnAcceptedMergeRequest = false;
private boolean triggerOnUpdateMergeRequest = false;
private boolean triggerOnClosedMergeRequest = false;
private boolean triggerOnApprovedMergeRequest = false;
private TriggerOpenMergeRequest triggerOpenMergeRequestOnPush;
private boolean triggerOnNoteRequest = true;
private String noteRegex = "";
private boolean ciSkip = true;
@ -106,8 +108,8 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
*/
@Deprecated
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public GiteePushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, boolean triggerOnAcceptedMergeRequest, boolean triggerOnClosedMergeRequest,
TriggerOpenMergeRequest triggerOpenMergeRequestOnPush, boolean triggerOnNoteRequest, String noteRegex,
public GiteePushTrigger(boolean triggerOnPush, boolean triggerOnOpenMergeRequest, boolean triggerOnUpdateMergeRequest, boolean triggerOnAcceptedMergeRequest, boolean triggerOnClosedMergeRequest,
boolean triggerOnNoteRequest, String noteRegex,
boolean skipWorkInProgressMergeRequest, boolean ciSkip,
boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addCiMessage, boolean addVoteOnMergeRequest,
boolean acceptMergeRequestOnSuccess, BranchFilterType branchFilterType,
@ -115,12 +117,12 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
MergeRequestLabelFilterConfig mergeRequestLabelFilterConfig, String secretToken, boolean triggerOnPipelineEvent,
boolean triggerOnApprovedMergeRequest, String pendingBuildName, boolean cancelPendingBuildsOnUpdate) {
this.triggerOnPush = triggerOnPush;
this.triggerOnMergeRequest = triggerOnMergeRequest;
this.triggerOnOpenMergeRequest = triggerOnOpenMergeRequest;
this.triggerOnUpdateMergeRequest = triggerOnUpdateMergeRequest;
this.triggerOnAcceptedMergeRequest = triggerOnAcceptedMergeRequest;
this.triggerOnClosedMergeRequest = triggerOnClosedMergeRequest;
this.triggerOnNoteRequest = triggerOnNoteRequest;
this.noteRegex = noteRegex;
this.triggerOpenMergeRequestOnPush = triggerOpenMergeRequestOnPush;
this.triggerOnPipelineEvent = triggerOnPipelineEvent;
this.ciSkip = ciSkip;
this.skipWorkInProgressMergeRequest = skipWorkInProgressMergeRequest;
@ -195,8 +197,12 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
return triggerOnPush;
}
public boolean getTriggerOnMergeRequest() {
return triggerOnMergeRequest;
public boolean getTriggerOnOpenMergeRequest() {
return triggerOnOpenMergeRequest;
}
public boolean getTriggerOnUpdateMergeRequest() {
return triggerOnUpdateMergeRequest;
}
public boolean isTriggerOnAcceptedMergeRequest() {
@ -221,10 +227,6 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
return this.noteRegex == null ? "" : this.noteRegex;
}
public TriggerOpenMergeRequest getTriggerOpenMergeRequestOnPush() {
return triggerOpenMergeRequestOnPush;
}
public boolean getSetBuildDescription() {
return setBuildDescription;
}
@ -280,8 +282,8 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
}
@DataBoundSetter
public void setTriggerOnMergeRequest(boolean triggerOnMergeRequest) {
this.triggerOnMergeRequest = triggerOnMergeRequest;
public void setTriggerOnOpenMergeRequest(boolean triggerOnOpenMergeRequest) {
this.triggerOnOpenMergeRequest = triggerOnOpenMergeRequest;
}
@DataBoundSetter
@ -374,6 +376,10 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
this.acceptMergeRequestOnSuccess = acceptMergeRequestOnSuccess;
}
@DataBoundSetter
public void setTriggerOnUpdateMergeRequest(boolean triggerOnUpdateMergeRequest) {
this.triggerOnUpdateMergeRequest = triggerOnUpdateMergeRequest;
}
@DataBoundSetter
public void setTriggerOnPipelineEvent(boolean triggerOnPipelineEvent) {
this.triggerOnPipelineEvent = triggerOnPipelineEvent;
@ -440,11 +446,11 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
}
private void initializeTriggerHandler() {
mergeRequestHookTriggerHandler = newMergeRequestHookTriggerHandler(triggerOnMergeRequest,
triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest, triggerOpenMergeRequestOnPush,
mergeRequestHookTriggerHandler = newMergeRequestHookTriggerHandler(triggerOnOpenMergeRequest,
triggerOnUpdateMergeRequest, triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest,
skipWorkInProgressMergeRequest, triggerOnApprovedMergeRequest, cancelPendingBuildsOnUpdate);
noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnNoteRequest, noteRegex);
pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, triggerOpenMergeRequestOnPush, skipWorkInProgressMergeRequest);
pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, skipWorkInProgressMergeRequest);
pipelineTriggerHandler = newPipelineHookTriggerHandler(triggerOnPipelineEvent);
}

View File

@ -1,8 +0,0 @@
package com.gitee.jenkins.trigger;
/**
* @author Robin Müller
*/
public enum TriggerOpenMergeRequest {
never, source, both
}

View File

@ -2,7 +2,6 @@ package com.gitee.jenkins.trigger.handler.merge;
import com.gitee.jenkins.gitee.hook.model.Action;
import com.gitee.jenkins.gitee.hook.model.State;
import com.gitee.jenkins.trigger.TriggerOpenMergeRequest;
import java.util.ArrayList;
import java.util.EnumSet;
@ -16,48 +15,65 @@ public final class MergeRequestHookTriggerHandlerFactory {
private MergeRequestHookTriggerHandlerFactory() {}
public static MergeRequestHookTriggerHandler newMergeRequestHookTriggerHandler(boolean triggerOnMergeRequest,
public static MergeRequestHookTriggerHandler newMergeRequestHookTriggerHandler(boolean triggerOnOpenMergeRequest,
boolean triggerOnUpdateMergeRequest,
boolean triggerOnAcceptedMergeRequest,
boolean triggerOnClosedMergeRequest,
TriggerOpenMergeRequest triggerOpenMergeRequest,
boolean skipWorkInProgressMergeRequest,
boolean triggerOnApprovedMergeRequest,
boolean cancelPendingBuildsOnUpdate) {
if (triggerOnMergeRequest || triggerOnAcceptedMergeRequest || triggerOnClosedMergeRequest || triggerOpenMergeRequest != TriggerOpenMergeRequest.never || triggerOnApprovedMergeRequest) {
return new MergeRequestHookTriggerHandlerImpl(retrieveAllowedStates(triggerOnMergeRequest, triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest, triggerOpenMergeRequest),
retrieveAllowedActions(triggerOnApprovedMergeRequest),
if (triggerOnOpenMergeRequest || triggerOnUpdateMergeRequest || triggerOnAcceptedMergeRequest || triggerOnClosedMergeRequest || triggerOnApprovedMergeRequest) {
return new MergeRequestHookTriggerHandlerImpl(retrieveAllowedStates(triggerOnOpenMergeRequest, triggerOnUpdateMergeRequest, triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest),
retrieveAllowedActions(triggerOnOpenMergeRequest, triggerOnUpdateMergeRequest, triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest),
skipWorkInProgressMergeRequest, cancelPendingBuildsOnUpdate);
} else {
return new NopMergeRequestHookTriggerHandler();
}
}
private static Set<Action> retrieveAllowedActions(boolean triggerOnApprovedMergeRequest) {
Set<Action> allowedActions = EnumSet.of(Action.open, Action.update, Action.close, Action.merge);
if (triggerOnApprovedMergeRequest)
allowedActions.add(Action.approved);
private static List<Action> retrieveAllowedActions(boolean triggerOnOpenMergeRequest,
boolean triggerOnUpdateMergeRequest,
boolean triggerOnAcceptedMergeRequest,
boolean triggerOnClosedMergeRequest) {
List<Action> allowedActions =new ArrayList<>();
if (triggerOnOpenMergeRequest) {
allowedActions.add(Action.open);
}
if (triggerOnUpdateMergeRequest) {
allowedActions.add(Action.update);
}
if (triggerOnAcceptedMergeRequest) {
allowedActions.add(Action.merge);
}
if (triggerOnClosedMergeRequest) {
allowedActions.add(Action.close);
}
return allowedActions;
}
private static List<State> retrieveAllowedStates(boolean triggerOnMergeRequest,
boolean triggerOnAcceptedMergeRequest,
boolean triggerOnClosedMergeRequest,
TriggerOpenMergeRequest triggerOpenMergeRequest) {
private static List<State> retrieveAllowedStates(boolean triggerOnOpenMergeRequest,
boolean triggerOnUpdateMergeRequest,
boolean triggerOnAcceptedMergeRequest,
boolean triggerOnClosedMergeRequest) {
List<State> result = new ArrayList<>();
if (triggerOnMergeRequest) {
if (triggerOnOpenMergeRequest || triggerOnUpdateMergeRequest) {
result.add(State.opened);
result.add(State.open);
result.add(State.reopened);
result.add(State.updated);
}
if (triggerOnAcceptedMergeRequest) {
result.add(State.merged);
}
if (triggerOnClosedMergeRequest) {
result.add(State.closed);
}
if (triggerOpenMergeRequest != TriggerOpenMergeRequest.never) {
result.add(State.updated);
}
return result;
}

View File

@ -31,6 +31,7 @@ import static com.gitee.jenkins.util.LoggerUtil.toArray;
/**
* @author Robin Müller
* @author Yashin Luo
*/
class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<MergeRequestHook> implements MergeRequestHookTriggerHandler {
@ -57,6 +58,7 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
MergeRequestObjectAttributes objectAttributes = hook.getObjectAttributes();
try {
LOGGER.log(Level.INFO, "request hook state=" + hook.getState() + ", action = " + hook.getAction() + " pr iid = " + objectAttributes.getIid() + " hook name = " + hook.getHookName());
if (isAllowedByConfig(hook)
&& isLastCommitNotYetBuild(job, hook)
&& isNotSkipWorkInProgressMergeRequest(objectAttributes)) {
@ -141,7 +143,7 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
.withTargetRepoHttpUrl(hook.getObjectAttributes().getTarget().getHttpUrl())
.withTriggeredByUser(hook.getObjectAttributes().getHead().getUser().getName())
.withLastCommit(hook.getObjectAttributes().getMergeCommitSha())
.withTargetProjectUrl(hook.getObjectAttributes().getTarget().getWebUrl())
.withTargetProjectUrl(hook.getObjectAttributes().getTarget().getUrl())
.withPathWithNamespace(hook.getProject().getPathWithNamespace())
.build();
}

View File

@ -82,7 +82,7 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
.withTargetRepoHttpUrl(hook.getMergeRequest().getTarget().getHttpUrl())
.withTriggeredByUser(hook.getMergeRequest().getHead().getUser().getName())
.withLastCommit(hook.getMergeRequest().getMergeCommitSha())
.withTargetProjectUrl(hook.getMergeRequest().getTarget().getWebUrl())
.withTargetProjectUrl(hook.getMergeRequest().getTarget().getUrl())
.withTriggerPhrase(hook.getObjectAttributes().getNote())
.build();
}

View File

@ -1,7 +1,5 @@
package com.gitee.jenkins.trigger.handler.push;
import com.gitee.jenkins.trigger.TriggerOpenMergeRequest;
import java.util.ArrayList;
import java.util.List;
@ -12,19 +10,15 @@ public final class PushHookTriggerHandlerFactory {
private PushHookTriggerHandlerFactory() {}
public static PushHookTriggerHandler newPushHookTriggerHandler(boolean triggerOnPush,
TriggerOpenMergeRequest triggerOpenMergeRequestOnPush,
boolean skipWorkInProgressMergeRequest) {
if (triggerOnPush || triggerOpenMergeRequestOnPush == TriggerOpenMergeRequest.both) {
return new PushHookTriggerHandlerList(retrieveHandlers(triggerOnPush, triggerOpenMergeRequestOnPush, skipWorkInProgressMergeRequest));
public static PushHookTriggerHandler newPushHookTriggerHandler(boolean triggerOnPush, boolean skipWorkInProgressMergeRequest) {
if (triggerOnPush) {
return new PushHookTriggerHandlerList(retrieveHandlers(triggerOnPush, skipWorkInProgressMergeRequest));
} else {
return new NopPushHookTriggerHandler();
}
}
private static List<PushHookTriggerHandler> retrieveHandlers(boolean triggerOnPush,
TriggerOpenMergeRequest triggerOpenMergeRequestOnPush,
boolean skipWorkInProgressMergeRequest) {
private static List<PushHookTriggerHandler> retrieveHandlers(boolean triggerOnPush, boolean skipWorkInProgressMergeRequest) {
List<PushHookTriggerHandler> result = new ArrayList<>();
if (triggerOnPush) {
result.add(new PushHookTriggerHandlerImpl());

View File

@ -76,7 +76,7 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
.withBefore(hook.getBefore())
.withAfter(hook.getAfter())
.withLastCommit(hook.getAfter())
.withTargetProjectUrl(hook.getProject().getWebUrl())
.withTargetProjectUrl(hook.getProject().getUrl())
.build();
}

View File

@ -6,7 +6,10 @@
<f:entry title="${%Push}" field="triggerOnPush">
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%Updated.Pull.Request}" field="triggerOnMergeRequest">
<f:entry title="${%Opened.Pull.Request}" field="triggerOnOpenMergeRequest">
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%Updated.Pull.Request}" field="triggerOnUpdateMergeRequest">
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%Accepted.Pull.Request}" field="triggerOnAcceptedMergeRequest">
@ -15,9 +18,6 @@
<f:entry title="${%Closed.Pull.Request}" field="triggerOnClosedMergeRequest">
<f:checkbox default="false"/>
</f:entry>
<f:entry title="${%Approved.Pull.Requests}" field="triggerOnApprovedMergeRequest">
<f:checkbox default="true"/>
</f:entry>
</table>
</f:entry>
<f:advanced>
@ -54,18 +54,6 @@
<f:textbox name="targetBranchRegex" field="targetBranchRegex"/>
</f:entry>
</f:radioBlock>
<!--</f:section> -->
<f:optionalBlock title="${%Filter.merge.request.by.label}" field="mergeRequestLabelFilterConfig"
checked="${instance.mergeRequestLabelFilterConfig != null}">
<f:entry title="${%Include}">
<f:textbox field="includeMergeRequestLabels" name="include" value="${instance.mergeRequestLabelFilterConfig.include}"
autoCompleteDelimChar=","/>
</f:entry>
<f:entry title="${%Exclude}">
<f:textbox field="excludeMergeRequestLabels" name="exclude" value="${instance.mergeRequestLabelFilterConfig.exclude}"
autoCompleteDelimChar=","/>
</f:entry>
</f:optionalBlock>
</table>
</f:entry>
<f:entry title="${%Secret.Token}" help="/plugin/gitee-plugin/help/help-secretToken.html">

View File

@ -1,16 +1,16 @@
Secret.Token=Gitee WebHook \u5BC6\u7801
Generate=\u751F\u6210
Clear=\u6E05\u9664
Push=\u63A8\u9001
Enable.CI.Skip=\u5141\u8BB8 [ci-skip]
Enabled.Gitee.Triggers=\u5F00\u542F Gitee \u89E6\u53D1\u5668
Push=\u63A8\u9001\u4EE3\u7801
Enable.CI.Skip=\u5F00\u542F [ci-skip]
Enabled.Gitee.Triggers=Gitee \u89E6\u53D1\u6784\u5EFA\u7B56\u7565
Approved.Pull.Requests=Pull Requests \u5BA1\u67E5\u901A\u8FC7
Closed.Pull.Request=\u5173\u95ED Pull Request
Accepted.Pull.Request=\u63A5\u53D7 Pull Request
Opened.Pull.Request=\u65B0\u5EFA Pull Request
Updated.Pull.Request=\u66F4\u65B0 Pull Request
Ignore.WIP.Merge.Requests=\u8FC7\u6EE4\u6B63\u5728\u8FDB\u884C\u6784\u5EFA\u7684Pull Request
Set.Build.Desctiption=\u4E3A Pull Request \u6216 Gitee \u63A8\u9001\u8BBE\u7F6E\u6784\u5EFA\u63CF\u8FF0\u3002
Set.Build.Desctiption=\u8BBE\u7F6E\u6784\u5EFA\u63CF\u8FF0
CommitNotifier.Success=\u6784\u5EFA {0} \u6210\u529F\u4E8E {1}
CommitNotifier.Unstable=\u6784\u5EFA {0} \u53D1\u73B0 {1} \u4E0D\u7A33\u5B9A
CommitNotifier.Failed=\u6784\u5EFA {0} \u5931\u8D25\u4E8E {1}

View File

@ -90,7 +90,7 @@ public class GiteeIT {
public void buildOnMergeRequest() throws IOException, InterruptedException, GitAPIException {
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject("test");
GiteePushTrigger trigger = giteePushTrigger().withTriggerOnMergeRequest(true).withBranchFilterType(BranchFilterType.All).build();
GiteePushTrigger trigger = giteePushTrigger().withTriggerOnOpenMergeRequest(true).withBranchFilterType(BranchFilterType.All).build();
project.addTrigger(trigger);
trigger.start(project, true);
project.getBuildersList().add(new TestBuilder() {

View File

@ -127,7 +127,7 @@ public class PendingBuildsHandlerTest {
private GiteePushTrigger giteePushTrigger() {
GiteePushTrigger giteePushTrigger = new GiteePushTrigger();
giteePushTrigger.setTriggerOnPush(true);
giteePushTrigger.setTriggerOnMergeRequest(true);
giteePushTrigger.setTriggerOnOpenMergeRequest(true);
giteePushTrigger.setPendingBuildName(GITEE_BUILD_NAME);
giteePushTrigger.setBranchFilterType(BranchFilterType.NameBasedFilter);
giteePushTrigger.setBranchFilterName("");

View File

@ -2,7 +2,6 @@ package com.gitee.jenkins.webhook.build;
import com.gitee.jenkins.trigger.GiteePushTrigger;
import com.gitee.jenkins.gitee.hook.model.PushHook;
import com.gitee.jenkins.trigger.TriggerOpenMergeRequest;
import hudson.model.FreeStyleProject;
import org.apache.commons.io.IOUtils;
import org.junit.ClassRule;
@ -57,7 +56,6 @@ public class PushBuildActionTest {
public void build() throws IOException {
try {
FreeStyleProject testProject = jenkins.createFreeStyleProject();
when(trigger.getTriggerOpenMergeRequestOnPush()).thenReturn(TriggerOpenMergeRequest.never);
testProject.addTrigger(trigger);
exception.expect(HttpResponses.HttpResponseException.class);
@ -73,7 +71,6 @@ public class PushBuildActionTest {
@Test
public void invalidToken() throws IOException {
FreeStyleProject testProject = jenkins.createFreeStyleProject();
when(trigger.getTriggerOpenMergeRequestOnPush()).thenReturn(TriggerOpenMergeRequest.never);
when(trigger.getSecretToken()).thenReturn("secret");
testProject.addTrigger(trigger);