修复push无法过滤已构建的版本

This commit is contained in:
yashin 2018-07-24 18:17:20 +08:00
parent 9d5fe887e6
commit db047e0f4a
14 changed files with 98 additions and 66 deletions

View File

@ -80,7 +80,7 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
private String noteRegex = ""; private String noteRegex = "";
private boolean ciSkip = true; private boolean ciSkip = true;
private boolean skipWorkInProgressMergeRequest; private boolean skipWorkInProgressMergeRequest;
private boolean skipLastCommitHasBuild; private boolean skipLastCommitHasBeenBuild;
private boolean setBuildDescription = true; private boolean setBuildDescription = true;
private transient boolean addNoteOnMergeRequest; private transient boolean addNoteOnMergeRequest;
private transient boolean addCiMessage; private transient boolean addCiMessage;
@ -240,12 +240,16 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
return ciSkip; return ciSkip;
} }
public boolean getSkipLastCommitHasBeenBuild() {
return skipLastCommitHasBeenBuild;
}
public boolean isSkipWorkInProgressMergeRequest() { public boolean isSkipWorkInProgressMergeRequest() {
return skipWorkInProgressMergeRequest; return skipWorkInProgressMergeRequest;
} }
public boolean isSkipLastCommitHasBuild() { public boolean isSkipLastCommitHasBuild() {
return skipLastCommitHasBuild; return skipLastCommitHasBeenBuild;
} }
public BranchFilterType getBranchFilterType() { public BranchFilterType getBranchFilterType() {
@ -331,8 +335,8 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
} }
@DataBoundSetter @DataBoundSetter
public void setSkipLastCommitHasBuild(boolean skipLastCommitHasBuild) { public void setSkipLastCommitHasBeenBuild(boolean skipLastCommitHasBeenBuild) {
this.skipLastCommitHasBuild = skipLastCommitHasBuild; this.skipLastCommitHasBeenBuild = skipLastCommitHasBeenBuild;
} }
@ -427,7 +431,7 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
if (pushHookTriggerHandler == null) { if (pushHookTriggerHandler == null) {
initializeTriggerHandler(); initializeTriggerHandler();
} }
pushHookTriggerHandler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); pushHookTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
// executes when the Trigger receives a merge request // executes when the Trigger receives a merge request
@ -441,7 +445,7 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
if (mergeRequestHookTriggerHandler == null) { if (mergeRequestHookTriggerHandler == null) {
initializeTriggerHandler(); initializeTriggerHandler();
} }
mergeRequestHookTriggerHandler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); mergeRequestHookTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
// executes when the Trigger receives a note request // executes when the Trigger receives a note request
@ -455,7 +459,7 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
if (noteHookTriggerHandler == null) { if (noteHookTriggerHandler == null) {
initializeTriggerHandler(); initializeTriggerHandler();
} }
noteHookTriggerHandler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); noteHookTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
// executes when the Trigger receives a pipeline event // executes when the Trigger receives a pipeline event
@ -463,13 +467,13 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
if (pipelineTriggerHandler == null) { if (pipelineTriggerHandler == null) {
initializeTriggerHandler(); initializeTriggerHandler();
} }
pipelineTriggerHandler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); pipelineTriggerHandler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
private void initializeTriggerHandler() { private void initializeTriggerHandler() {
mergeRequestHookTriggerHandler = newMergeRequestHookTriggerHandler(triggerOnOpenMergeRequest, mergeRequestHookTriggerHandler = newMergeRequestHookTriggerHandler(triggerOnOpenMergeRequest,
triggerOnUpdateMergeRequest, triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest, triggerOnUpdateMergeRequest, triggerOnAcceptedMergeRequest, triggerOnClosedMergeRequest,
skipWorkInProgressMergeRequest, skipLastCommitHasBuild, triggerOnApprovedMergeRequest, triggerOnTestedMergeRequest, cancelPendingBuildsOnUpdate); skipWorkInProgressMergeRequest, triggerOnApprovedMergeRequest, triggerOnTestedMergeRequest, cancelPendingBuildsOnUpdate);
noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnNoteRequest, noteRegex); noteHookTriggerHandler = newNoteHookTriggerHandler(triggerOnNoteRequest, noteRegex);
pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, skipWorkInProgressMergeRequest); pushHookTriggerHandler = newPushHookTriggerHandler(triggerOnPush, skipWorkInProgressMergeRequest);
pipelineTriggerHandler = newPipelineHookTriggerHandler(triggerOnPipelineEvent); pipelineTriggerHandler = newPipelineHookTriggerHandler(triggerOnPipelineEvent);
@ -580,7 +584,6 @@ public class GiteePushTrigger extends Trigger<Job<?, ?>> {
return super.configure(req, formData); return super.configure(req, formData);
} }
public void doGenerateSecretToken(@AncestorInPath final Job<?, ?> project, StaplerResponse response) { public void doGenerateSecretToken(@AncestorInPath final Job<?, ?> project, StaplerResponse response) {
byte[] random = new byte[16]; // 16x8=128bit worth of randomness, since we use md5 digest as the API token byte[] random = new byte[16]; // 16x8=128bit worth of randomness, since we use md5 digest as the API token
RANDOM.nextBytes(random); RANDOM.nextBytes(random);

View File

@ -34,12 +34,17 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
protected PendingBuildsHandler pendingBuildsHandler = new PendingBuildsHandler(); protected PendingBuildsHandler pendingBuildsHandler = new PendingBuildsHandler();
@Override @Override
public void handle(Job<?, ?> job, H hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, H hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
if (ciSkip && isCiSkip(hook)) { if (ciSkip && isCiSkip(hook)) {
LOGGER.log(Level.INFO, "Skipping due to ci-skip."); LOGGER.log(Level.INFO, "Skipping due to ci-skip.");
return; return;
} }
if (skipLastCommitHasBeenBuild && isCommitSkip(job, hook)) {
LOGGER.log(Level.INFO, "Skipping due to ignore last commit has been build.");
return;
}
String targetBranch = getTargetBranch(hook); String targetBranch = getTargetBranch(hook);
if (branchFilter.isBranchAllowed(targetBranch)) { if (branchFilter.isBranchAllowed(targetBranch)) {
LOGGER.log(Level.INFO, "{0} triggered for {1}.", LoggerUtil.toArray(job.getFullName(), getTriggerType())); LOGGER.log(Level.INFO, "{0} triggered for {1}.", LoggerUtil.toArray(job.getFullName(), getTriggerType()));
@ -53,6 +58,7 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
protected abstract String getTriggerType(); protected abstract String getTriggerType();
protected abstract boolean isCiSkip(H hook); protected abstract boolean isCiSkip(H hook);
protected abstract boolean isCommitSkip(Job<?, ?> job, H hook);
protected Action[] createActions(Job<?, ?> job, H hook) { protected Action[] createActions(Job<?, ?> job, H hook) {
ArrayList<Action> actions = new ArrayList<>(); ArrayList<Action> actions = new ArrayList<>();

View File

@ -10,5 +10,5 @@ import hudson.model.Job;
*/ */
public interface WebHookTriggerHandler<H extends WebHook> { public interface WebHookTriggerHandler<H extends WebHook> {
void handle(Job<?, ?> job, H hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter); void handle(Job<?, ?> job, H hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter);
} }

View File

@ -20,7 +20,6 @@ public final class MergeRequestHookTriggerHandlerFactory {
boolean triggerOnAcceptedMergeRequest, boolean triggerOnAcceptedMergeRequest,
boolean triggerOnClosedMergeRequest, boolean triggerOnClosedMergeRequest,
boolean skipWorkInProgressMergeRequest, boolean skipWorkInProgressMergeRequest,
boolean skipLastCommitHasBuild,
boolean triggerOnApprovedMergeRequest, boolean triggerOnApprovedMergeRequest,
boolean triggerOnTestedMergeRequest, boolean triggerOnTestedMergeRequest,
boolean cancelPendingBuildsOnUpdate) { boolean cancelPendingBuildsOnUpdate) {
@ -45,7 +44,6 @@ public final class MergeRequestHookTriggerHandlerFactory {
triggerOnApprovedMergeRequest, triggerOnApprovedMergeRequest,
triggerOnTestedMergeRequest), triggerOnTestedMergeRequest),
skipWorkInProgressMergeRequest, skipWorkInProgressMergeRequest,
skipLastCommitHasBuild,
cancelPendingBuildsOnUpdate); cancelPendingBuildsOnUpdate);
} else { } else {
return new NopMergeRequestHookTriggerHandler(); return new NopMergeRequestHookTriggerHandler();

View File

@ -17,8 +17,6 @@ import hudson.model.Run;
import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitSCM;
import hudson.plugins.git.RevisionParameterAction; import hudson.plugins.git.RevisionParameterAction;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import java.util.Collection; import java.util.Collection;
import java.util.ArrayList; import java.util.ArrayList;
@ -29,7 +27,6 @@ import java.util.logging.Logger;
import static com.gitee.jenkins.cause.CauseDataBuilder.causeData; import static com.gitee.jenkins.cause.CauseDataBuilder.causeData;
import static com.gitee.jenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate; import static com.gitee.jenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate;
import static com.gitee.jenkins.util.LoggerUtil.toArray;
/** /**
* @author Robin Müller * @author Robin Müller
@ -41,32 +38,28 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
private final Collection<State> allowedStates; private final Collection<State> allowedStates;
private final boolean skipWorkInProgressMergeRequest; private final boolean skipWorkInProgressMergeRequest;
private final boolean skipLastCommitHasBuild;
private final Collection<Action> allowedActions; private final Collection<Action> allowedActions;
private final boolean cancelPendingBuildsOnUpdate; private final boolean cancelPendingBuildsOnUpdate;
MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, boolean skipWorkInProgressMergeRequest, boolean skipLastCommitHasBuild, boolean cancelPendingBuildsOnUpdate) { MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, boolean skipWorkInProgressMergeRequest, boolean cancelPendingBuildsOnUpdate) {
this(allowedStates, EnumSet.allOf(Action.class), skipWorkInProgressMergeRequest, skipLastCommitHasBuild, cancelPendingBuildsOnUpdate); this(allowedStates, EnumSet.allOf(Action.class), skipWorkInProgressMergeRequest, cancelPendingBuildsOnUpdate);
} }
MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, Collection<Action> allowedActions, boolean skipWorkInProgressMergeRequest, boolean skipLastCommitHasBuild, boolean cancelPendingBuildsOnUpdate) { MergeRequestHookTriggerHandlerImpl(Collection<State> allowedStates, Collection<Action> allowedActions, boolean skipWorkInProgressMergeRequest, boolean cancelPendingBuildsOnUpdate) {
this.allowedStates = allowedStates; this.allowedStates = allowedStates;
this.allowedActions = allowedActions; this.allowedActions = allowedActions;
this.skipWorkInProgressMergeRequest = skipWorkInProgressMergeRequest; this.skipWorkInProgressMergeRequest = skipWorkInProgressMergeRequest;
this.skipLastCommitHasBuild = skipLastCommitHasBuild;
this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate; this.cancelPendingBuildsOnUpdate = cancelPendingBuildsOnUpdate;
} }
@Override @Override
public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
MergeRequestObjectAttributes objectAttributes = hook.getPullRequest(); MergeRequestObjectAttributes objectAttributes = hook.getPullRequest();
try { try {
LOGGER.log(Level.INFO, "request hook state=" + hook.getState() + ", action = " + hook.getAction() + " pr iid = " + objectAttributes.getNumber() + " hook name = " + hook.getHookName()); LOGGER.log(Level.INFO, "request hook state=" + hook.getState() + ", action = " + hook.getAction() + " pr iid = " + objectAttributes.getNumber() + " hook name = " + hook.getHookName());
if (isAllowedByConfig(hook) if (isAllowedByConfig(hook)
&& isLastCommitNotYetBuild(job, hook)
&& isNotSkipWorkInProgressMergeRequest(objectAttributes)) { && isNotSkipWorkInProgressMergeRequest(objectAttributes)) {
List<String> labelsNames = new ArrayList<>(); List<String> labelsNames = new ArrayList<>();
if (hook.getLabels() != null) { if (hook.getLabels() != null) {
for (MergeRequestLabel label : hook.getLabels()) { for (MergeRequestLabel label : hook.getLabels()) {
@ -75,7 +68,7 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
} }
if (mergeRequestLabelFilter.isMergeRequestAllowed(labelsNames)) { if (mergeRequestLabelFilter.isMergeRequestAllowed(labelsNames)) {
super.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); super.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
} }
else { else {
@ -95,6 +88,20 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
&& hook.getPullRequest().getBody().contains("[ci-skip]"); && hook.getPullRequest().getBody().contains("[ci-skip]");
} }
@Override
protected boolean isCommitSkip(Job<?, ?> project, MergeRequestHook hook) {
MergeRequestObjectAttributes objectAttributes = hook.getPullRequest();
if (objectAttributes != null && objectAttributes.getMergeCommitSha() != null) {
Run<?, ?> mergeBuild = BuildUtil.getBuildBySHA1IncludingMergeBuilds(project, objectAttributes.getMergeCommitSha());
if (mergeBuild != null && StringUtils.equals(getTargetBranchFromBuild(mergeBuild), objectAttributes.getTargetBranch())) {
LOGGER.log(Level.INFO, "Last commit in Merge Request has already been built in build #" + mergeBuild.getNumber());
return true;
}
}
return false;
}
@Override @Override
protected void cancelPendingBuildsIfNecessary(Job<?, ?> job, MergeRequestHook hook) { protected void cancelPendingBuildsIfNecessary(Job<?, ?> job, MergeRequestHook hook) {
if (!this.cancelPendingBuildsOnUpdate) { if (!this.cancelPendingBuildsOnUpdate) {
@ -175,23 +182,6 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
} }
} }
private boolean isLastCommitNotYetBuild(Job<?, ?> project, MergeRequestHook hook) {
if (!skipLastCommitHasBuild) {
return true;
}
MergeRequestObjectAttributes objectAttributes = hook.getPullRequest();
if (objectAttributes != null && objectAttributes.getMergeCommitSha() != null) {
Run<?, ?> mergeBuild = BuildUtil.getBuildBySHA1IncludingMergeBuilds(project, objectAttributes.getMergeCommitSha());
if (mergeBuild != null && StringUtils.equals(getTargetBranchFromBuild(mergeBuild), objectAttributes.getTargetBranch())) {
LOGGER.log(Level.INFO, "Last commit in Merge Request has already been built in build #" + mergeBuild.getNumber());
return false;
}
}
return true;
}
private String getTargetBranchFromBuild(Run<?, ?> mergeBuild) { private String getTargetBranchFromBuild(Run<?, ?> mergeBuild) {
GiteeWebHookCause cause = mergeBuild.getCause(GiteeWebHookCause.class); GiteeWebHookCause cause = mergeBuild.getCause(GiteeWebHookCause.class);
return cause == null ? null : cause.getData().getTargetBranch(); return cause == null ? null : cause.getData().getTargetBranch();

View File

@ -10,7 +10,7 @@ import hudson.model.Job;
*/ */
class NopMergeRequestHookTriggerHandler implements MergeRequestHookTriggerHandler { class NopMergeRequestHookTriggerHandler implements MergeRequestHookTriggerHandler {
@Override @Override
public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, MergeRequestHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
// nothing to do // nothing to do
} }
} }

View File

@ -10,7 +10,7 @@ import hudson.model.Job;
*/ */
class NopNoteHookTriggerHandler implements NoteHookTriggerHandler { class NopNoteHookTriggerHandler implements NoteHookTriggerHandler {
@Override @Override
public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
// nothing to do // nothing to do
} }
} }

View File

@ -2,15 +2,19 @@ package com.gitee.jenkins.trigger.handler.note;
import com.gitee.jenkins.cause.CauseData; import com.gitee.jenkins.cause.CauseData;
import com.gitee.jenkins.gitee.hook.model.NoteHook; import com.gitee.jenkins.gitee.hook.model.NoteHook;
import com.gitee.jenkins.gitee.hook.model.PushHook;
import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException;
import com.gitee.jenkins.trigger.filter.BranchFilter; import com.gitee.jenkins.trigger.filter.BranchFilter;
import com.gitee.jenkins.trigger.filter.MergeRequestLabelFilter; import com.gitee.jenkins.trigger.filter.MergeRequestLabelFilter;
import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler;
import com.gitee.jenkins.util.BuildUtil;
import hudson.model.Job; import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitSCM;
import hudson.plugins.git.RevisionParameterAction; import hudson.plugins.git.RevisionParameterAction;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -19,6 +23,7 @@ import static com.gitee.jenkins.trigger.handler.builder.generated.BuildStatusUpd
/** /**
* @author Nikolay Ustinov * @author Nikolay Ustinov
* @author Yashin Luo
*/ */
class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook> implements NoteHookTriggerHandler { class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook> implements NoteHookTriggerHandler {
@ -31,9 +36,9 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
} }
@Override @Override
public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, NoteHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
if (isValidTriggerPhrase(hook.getObjectAttributes().getNote())) { if (isValidTriggerPhrase(hook.getObjectAttributes().getNote())) {
super.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); super.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
} }
@ -44,6 +49,10 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
&& hook.getMergeRequest().getBody().contains("[ci-skip]"); && hook.getMergeRequest().getBody().contains("[ci-skip]");
} }
@Override
protected boolean isCommitSkip(Job<?, ?> project, NoteHook hook) {
return false;
}
@Override @Override
protected String getTargetBranch(NoteHook hook) { protected String getTargetBranch(NoteHook hook) {
return hook.getMergeRequest() == null ? null : hook.getMergeRequest().getTargetBranch(); return hook.getMergeRequest() == null ? null : hook.getMergeRequest().getTargetBranch();

View File

@ -11,7 +11,7 @@ import hudson.model.Job;
class NopPipelineHookTriggerHandler implements PipelineHookTriggerHandler { class NopPipelineHookTriggerHandler implements PipelineHookTriggerHandler {
@Override @Override
public void handle(Job<?, ?> job, PipelineHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, PipelineHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
} }
} }

View File

@ -28,6 +28,8 @@ import static com.gitee.jenkins.trigger.handler.builder.generated.BuildStatusUpd
/** /**
* @author Milena Zachow * @author Milena Zachow
* @author Yashin Luo
*
*/ */
// fixme // fixme
@ -42,7 +44,7 @@ class PipelineHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<Pipel
} }
@Override @Override
public void handle(Job<?, ?> job, PipelineHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, PipelineHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
PipelineEventObjectAttributes objectAttributes = hook.getObjectAttributes(); PipelineEventObjectAttributes objectAttributes = hook.getObjectAttributes();
try { try {
if (job instanceof AbstractProject<?, ?>) { if (job instanceof AbstractProject<?, ?>) {
@ -80,6 +82,11 @@ class PipelineHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<Pipel
return false; return false;
} }
@Override
protected boolean isCommitSkip(Job<?, ?> project, PipelineHook hook) {
return false;
}
@Override @Override
protected String getTargetBranch(PipelineHook hook) { protected String getTargetBranch(PipelineHook hook) {
return hook.getObjectAttributes().getRef() == null ? null : hook.getObjectAttributes().getRef().replaceFirst("^refs/heads/", ""); return hook.getObjectAttributes().getRef() == null ? null : hook.getObjectAttributes().getRef().replaceFirst("^refs/heads/", "");

View File

@ -10,7 +10,7 @@ import hudson.model.Job;
*/ */
class NopPushHookTriggerHandler implements PushHookTriggerHandler { class NopPushHookTriggerHandler implements PushHookTriggerHandler {
@Override @Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
// nothing to do // nothing to do
} }
} }

View File

@ -1,6 +1,7 @@
package com.gitee.jenkins.trigger.handler.push; package com.gitee.jenkins.trigger.handler.push;
import com.gitee.jenkins.cause.CauseData; import com.gitee.jenkins.cause.CauseData;
import com.gitee.jenkins.cause.GiteeWebHookCause;
import com.gitee.jenkins.gitee.hook.model.Commit; import com.gitee.jenkins.gitee.hook.model.Commit;
import com.gitee.jenkins.gitee.hook.model.PushHook; import com.gitee.jenkins.gitee.hook.model.PushHook;
import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException; import com.gitee.jenkins.trigger.exception.NoRevisionToBuildException;
@ -8,26 +9,31 @@ import com.gitee.jenkins.trigger.filter.BranchFilter;
import com.gitee.jenkins.trigger.filter.MergeRequestLabelFilter; import com.gitee.jenkins.trigger.filter.MergeRequestLabelFilter;
import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler; import com.gitee.jenkins.trigger.handler.AbstractWebHookTriggerHandler;
import hudson.model.Job; import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitSCM;
import hudson.plugins.git.RevisionParameterAction; import hudson.plugins.git.RevisionParameterAction;
import org.eclipse.jgit.util.StringUtils; import com.gitee.jenkins.util.BuildUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils;
import static com.gitee.jenkins.cause.CauseDataBuilder.causeData; import static com.gitee.jenkins.cause.CauseDataBuilder.causeData;
import static com.gitee.jenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate; import static com.gitee.jenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate;
/** /**
* @author Robin Müller * @author Robin MüllerPushHookTriggerHandlerImpl
* @author Yashin Luo
*/ */
class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook> implements PushHookTriggerHandler { class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook> implements PushHookTriggerHandler {
private static final Logger LOGGER = Logger.getLogger(PushHookTriggerHandlerImpl.class.getName());
private static final String NO_COMMIT = "0000000000000000000000000000000000000000"; private static final String NO_COMMIT = "0000000000000000000000000000000000000000";
@Override @Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
if (isNoRemoveBranchPush(hook)) { if (isNoRemoveBranchPush(hook)) {
super.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); super.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
} }
@ -40,6 +46,19 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
commits.get(commits.size() - 1).getMessage().contains("[ci-skip]"); commits.get(commits.size() - 1).getMessage().contains("[ci-skip]");
} }
@Override
protected boolean isCommitSkip(Job<?, ?> project, PushHook hook) {
String sha = hook.getAfter();
if (hook != null && sha != null) {
Run<?, ?> pushBuild = BuildUtil.getBuildBySHA1IncludingMergeBuilds(project, sha);
if (pushBuild != null && StringUtils.equals(getRefFromBuild(pushBuild), hook.getRef())) {
LOGGER.log(Level.INFO, "Last commit in push has already been built sha=" + sha);
return true;
}
}
return false;
}
@Override @Override
protected CauseData retrieveCauseData(PushHook hook) { protected CauseData retrieveCauseData(PushHook hook) {
// fixme 判断是否push tagGitee 钩子未有相关数据 // fixme 判断是否push tagGitee 钩子未有相关数据
@ -75,6 +94,7 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
.withTriggeredByUser(retrievePushedBy(hook)) .withTriggeredByUser(retrievePushedBy(hook))
.withBefore(hook.getBefore()) .withBefore(hook.getBefore())
.withAfter(hook.getAfter()) .withAfter(hook.getAfter())
.withRef(hook.getRef())
.withLastCommit(hook.getAfter()) .withLastCommit(hook.getAfter())
.withTargetProjectUrl(hook.getProject().getUrl()) .withTargetProjectUrl(hook.getProject().getUrl())
.build(); .build();
@ -107,7 +127,7 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
private String retrievePushedBy(final PushHook hook) { private String retrievePushedBy(final PushHook hook) {
final String userName = hook.getUserName(); final String userName = hook.getUserName();
if (!StringUtils.isEmptyOrNull(userName)) { if (StringUtils.isNotBlank(userName)) {
return userName; return userName;
} }
@ -139,4 +159,9 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
private boolean isNoRemoveBranchPush(PushHook hook) { private boolean isNoRemoveBranchPush(PushHook hook) {
return hook.getAfter() != null && !hook.getAfter().equals(NO_COMMIT); return hook.getAfter() != null && !hook.getAfter().equals(NO_COMMIT);
} }
private String getRefFromBuild(Run<?, ?> pushBuild) {
GiteeWebHookCause cause = pushBuild.getCause(GiteeWebHookCause.class);
return cause == null ? null : cause.getData().getRef();
}
} }

View File

@ -19,9 +19,9 @@ class PushHookTriggerHandlerList implements PushHookTriggerHandler {
} }
@Override @Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) { public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, boolean skipLastCommitHasBeenBuild, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
for (PushHookTriggerHandler handler : handlers) { for (PushHookTriggerHandler handler : handlers) {
handler.handle(job, hook, ciSkip, branchFilter, mergeRequestLabelFilter); handler.handle(job, hook, ciSkip, skipLastCommitHasBeenBuild, branchFilter, mergeRequestLabelFilter);
} }
} }
} }

View File

@ -30,15 +30,9 @@
<f:checkbox default="true"/> <f:checkbox default="true"/>
</f:entry> </f:entry>
<f:entry title="${%Ignore.Last.Commit.Has.Build}" field="skipLastCommitHasBuild" help="/plugin/gitee/help/help-skip-last-commit.html"> <f:entry title="${%Ignore.Last.Commit.Has.Build}" field="skipLastCommitHasBeenBuild" help="/plugin/gitee/help/help-skip-last-commit.html">
<f:checkbox default="false"/>
</f:entry>
<!--
<f:entry title="${%Ignore.WIP.Merge.Requests}" field="skipWorkInProgressMergeRequest" help="/plugin/gitee/help/help-skip-last-commit.html">
<f:checkbox default="true"/> <f:checkbox default="true"/>
</f:entry> </f:entry>
-->
<f:entry title="${%Allowed.branches}"> <f:entry title="${%Allowed.branches}">
<table> <table>
<!--<f:section title="">--> <!--<f:section title="">-->