GitHub merge

This commit is contained in:
Guixian Zhang 2021-01-04 13:30:50 +08:00
parent c6c5d50e41
commit fe8db03c7d
3 changed files with 44 additions and 15 deletions

View File

@ -22,7 +22,8 @@ import org.eclipse.jgit.transport.URIish;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -89,24 +90,19 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
protected abstract BuildStatusUpdate retrieveBuildStatusUpdate(H hook); protected abstract BuildStatusUpdate retrieveBuildStatusUpdate(H hook);
protected URIish retrieveUrIish(WebHook hook, GitSCM gitSCM) { protected URIish retrieveUrIish(WebHook hook, GitSCM gitSCM) {
if (hook.getRepository() == null) { if (hook.getRepository() == null || gitSCM == null) {
return null; return null;
} }
try { try {
if (gitSCM == null) { Set<URIish> set = new HashSet<>();
return new URIish(hook.getRepository().getGitHttpUrl()); set.add(new URIish(hook.getRepository().getUrl()));
} set.add(new URIish(hook.getRepository().getGitHttpUrl()));
List<URIish> uris = new ArrayList<URIish>(); set.add(new URIish(hook.getRepository().getGitSshUrl()));
uris.add(new URIish(hook.getRepository().getUrl()));
uris.add(new URIish(hook.getRepository().getGitHttpUrl()));
uris.add(new URIish(hook.getRepository().getGitSshUrl()));
// uri 需与当前项目仓库个url一致避免触发多个构建 // uri 需与当前项目仓库个url一致避免触发多个构建
for (RemoteConfig remote : gitSCM.getRepositories()) { for (RemoteConfig remote : gitSCM.getRepositories()) {
for (URIish remoteURL : remote.getURIs()) { for (URIish remoteURL : remote.getURIs()) {
for (URIish uri : uris) { if (set.contains(remoteURL)) {
if (remoteURL.equals(uri)) { return remoteURL;
return uri;
}
} }
} }
} }

View File

@ -15,6 +15,7 @@ import hudson.model.*;
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.URIish;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import java.io.IOException; import java.io.IOException;
@ -213,7 +214,16 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
@Override @Override
protected RevisionParameterAction createRevisionParameter(NoteHook hook, GitSCM gitSCM) throws NoRevisionToBuildException { protected RevisionParameterAction createRevisionParameter(NoteHook hook, GitSCM gitSCM) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook, gitSCM)); // 没有配置git源码管理
if (gitSCM == null) {
return new RevisionParameterAction(retrieveRevisionToBuild(hook));
}
URIish urIish = retrieveUrIish(hook, gitSCM);
// webhook与git源码管理仓库对不上
if (urIish == null) {
return new RevisionParameterAction(retrieveRevisionToBuild2(hook));
}
return new RevisionParameterAction(retrieveRevisionToBuild(hook), urIish);
} }
@Override @Override
@ -230,6 +240,13 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
if (hook.getPullRequest().getMergeCommitSha() != null) { if (hook.getPullRequest().getMergeCommitSha() != null) {
return hook.getPullRequest().getMergeCommitSha(); return hook.getPullRequest().getMergeCommitSha();
} }
}
return retrieveRevisionToBuild2(hook);
}
private String retrieveRevisionToBuild2(NoteHook hook) throws NoRevisionToBuildException {
if (hook.getPullRequest() != null) {
if (hook.getPullRequest().getMergeReferenceName() != null) { if (hook.getPullRequest().getMergeReferenceName() != null) {
return hook.getPullRequest().getMergeReferenceName(); return hook.getPullRequest().getMergeReferenceName();
} }

View File

@ -18,6 +18,7 @@ import hudson.model.*;
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.URIish;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import java.io.IOException; import java.io.IOException;
@ -238,7 +239,16 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<Pu
@Override @Override
protected RevisionParameterAction createRevisionParameter(PullRequestHook hook, GitSCM gitSCM) throws NoRevisionToBuildException { protected RevisionParameterAction createRevisionParameter(PullRequestHook hook, GitSCM gitSCM) throws NoRevisionToBuildException {
return new RevisionParameterAction(retrieveRevisionToBuild(hook), retrieveUrIish(hook, gitSCM)); // 没有配置git源码管理
if (gitSCM == null) {
return new RevisionParameterAction(retrieveRevisionToBuild(hook));
}
URIish urIish = retrieveUrIish(hook, gitSCM);
// webhook与git源码管理仓库对不上
if (urIish == null) {
return new RevisionParameterAction(retrieveRevisionToBuild2(hook));
}
return new RevisionParameterAction(retrieveRevisionToBuild(hook), urIish);
} }
@Override @Override
@ -255,6 +265,12 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<Pu
if (hook.getPullRequest().getMergeCommitSha() != null) { if (hook.getPullRequest().getMergeCommitSha() != null) {
return hook.getPullRequest().getMergeCommitSha(); return hook.getPullRequest().getMergeCommitSha();
} }
}
return retrieveRevisionToBuild2(hook);
}
private String retrieveRevisionToBuild2(PullRequestHook hook) throws NoRevisionToBuildException {
if (hook.getPullRequest() != null) {
if (hook.getPullRequest().getMergeReferenceName() != null) { if (hook.getPullRequest().getMergeReferenceName() != null) {
return hook.getPullRequest().getMergeReferenceName(); return hook.getPullRequest().getMergeReferenceName();
} }