解决旧项目多个分支的情况下webhook一次请求触发多个构建

This commit is contained in:
yashin 2018-07-05 16:39:33 +08:00
parent 489c55e76f
commit a73c373a33
6 changed files with 24 additions and 7 deletions

View File

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

View File

@ -16,10 +16,12 @@ import hudson.scm.SCM;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -76,11 +78,24 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
protected abstract BuildStatusUpdate retrieveBuildStatusUpdate(H hook);
protected URIish retrieveUrIish(WebHook hook) {
protected URIish retrieveUrIish(WebHook hook, GitSCM gitSCM) {
List<URIish> uris = new ArrayList<URIish>();
try {
if (hook.getRepository() != null) {
return new URIish(hook.getRepository().getUrl());
uris.add(new URIish(hook.getRepository().getGitSshUrl()));
uris.add(new URIish(hook.getRepository().getGitHttpUrl()));
}
// uri 需与当前项目仓库个url一致避免触发多个构建
for (RemoteConfig remote : gitSCM.getRepositories()) {
for (URIish remoteURL : remote.getURIs()) {
for (URIish uri : uris) {
if (remoteURL.equals(uri)) {
return uri;
}
}
}
}
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "could not parse URL");
}

View File

@ -17,6 +17,8 @@ import hudson.model.Run;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.RevisionParameterAction;
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.ArrayList;
@ -150,7 +152,7 @@ class MergeRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<M
@Override
protected RevisionParameterAction createRevisionParameter(MergeRequestHook hook, GitSCM gitSCM) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook, gitSCM));
}
@Override

View File

@ -89,7 +89,7 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
@Override
protected RevisionParameterAction createRevisionParameter(NoteHook hook, GitSCM gitSCM) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook, gitSCM));
}
@Override

View File

@ -124,7 +124,7 @@ class PipelineHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<Pipel
@Override
protected RevisionParameterAction createRevisionParameter(PipelineHook hook, GitSCM gitSCM) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook));
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook, gitSCM));
}
@Override

View File

@ -92,7 +92,7 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
@Override
protected RevisionParameterAction createRevisionParameter(PushHook hook, GitSCM gitSCM) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook, gitSCM), retrieveUrIish(hook));
return new RevisionParameterAction(retrieveRevisionToBuild(hook, gitSCM), retrieveUrIish(hook, gitSCM));
}
@Override