!8 合并Github分支

Merge pull request !8 from silverballer/20201231-github-merge
This commit is contained in:
Yashin 2021-01-04 14:00:41 +08:00 committed by Gitee
commit a11c8b9201
13 changed files with 86 additions and 17 deletions

View File

@ -208,6 +208,9 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc
variables.put("giteeTargetRepoHttpUrl", targetRepoHttpUrl); variables.put("giteeTargetRepoHttpUrl", targetRepoHttpUrl);
variables.put("giteeBefore", before); variables.put("giteeBefore", before);
variables.put("giteeAfter", after); variables.put("giteeAfter", after);
variables.put("giteeBeforeCommitSha", before);
variables.put("giteeAfterCommitSha", after);
variables.put("giteeRef", ref);
variables.put("ref", ref); variables.put("ref", ref);
variables.put("beforeSha", beforeSha); variables.put("beforeSha", beforeSha);
variables.put("isTag", isTag); variables.put("isTag", isTag);
@ -217,9 +220,12 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc
variables.put("createdAt", createdAt); variables.put("createdAt", createdAt);
variables.put("finishedAt", finishedAt); variables.put("finishedAt", finishedAt);
variables.put("duration", buildDuration); variables.put("duration", buildDuration);
variables.put("jsonBody", jsonBody);
variables.put("noteBody", noteBody);
variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase); variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase);
return variables; return variables;
} }
``` ```
# 用户支持 # 用户支持

View File

@ -14,7 +14,7 @@
</properties> </properties>
<artifactId>gitee</artifactId> <artifactId>gitee</artifactId>
<version>1.1.12-SNAPSHOT</version> <version>1.1.16-SNAPSHOT</version>
<name>Gitee Plugin</name> <name>Gitee Plugin</name>
<description>This plugin integrates Gitee to Jenkins by faking a Gitee CI Server. This plugin allows Gitee to trigger builds in Jenkins when code is committed or pull requests are opened/updated. It can also send build status back to Gitee.</description> <description>This plugin integrates Gitee to Jenkins by faking a Gitee CI Server. This plugin allows Gitee to trigger builds in Jenkins when code is committed or pull requests are opened/updated. It can also send build status back to Gitee.</description>
<url>https://wiki.jenkins.io/display/JENKINS/Gitee+Plugin</url> <url>https://wiki.jenkins.io/display/JENKINS/Gitee+Plugin</url>

View File

@ -59,6 +59,8 @@ public final class CauseData {
private final String createdAt; private final String createdAt;
private final String finishedAt; private final String finishedAt;
private final String buildDuration; private final String buildDuration;
private final String jsonBody;
private final String noteBody;
private final boolean created; private final boolean created;
private final boolean deleted; private final boolean deleted;
@ -70,7 +72,7 @@ public final class CauseData {
String targetRepoSshUrl, String targetRepoHttpUrl, String triggeredByUser, String before, String after, String lastCommit, String targetRepoSshUrl, String targetRepoHttpUrl, String triggeredByUser, String before, String after, String lastCommit,
String targetProjectUrl, String triggerPhrase, String pullRequestState, String mergedByUser, String pullRequestAssignee, String targetProjectUrl, String triggerPhrase, String pullRequestState, String mergedByUser, String pullRequestAssignee,
String ref, String isTag, String sha, String beforeSha, String status, String stages, String createdAt, String finishedAt, String ref, String isTag, String sha, String beforeSha, String status, String stages, String createdAt, String finishedAt,
String buildDuration, String pathWithNamespace, boolean created, boolean deleted) { String buildDuration, String pathWithNamespace, boolean created, boolean deleted, String jsonBody, String noteBody) {
this.actionType = checkNotNull(actionType, "actionType must not be null."); this.actionType = checkNotNull(actionType, "actionType must not be null.");
this.sourceProjectId = checkNotNull(sourceProjectId, "sourceProjectId must not be null."); this.sourceProjectId = checkNotNull(sourceProjectId, "sourceProjectId must not be null.");
this.targetProjectId = checkNotNull(targetProjectId, "targetProjectId must not be null."); this.targetProjectId = checkNotNull(targetProjectId, "targetProjectId must not be null.");
@ -117,6 +119,8 @@ public final class CauseData {
this.pathWithNamespace = pathWithNamespace; this.pathWithNamespace = pathWithNamespace;
this.created = created; this.created = created;
this.deleted = deleted; this.deleted = deleted;
this.jsonBody = jsonBody;
this.noteBody = noteBody;
} }
public Map<String, String> getBuildVariables() { public Map<String, String> getBuildVariables() {
@ -162,6 +166,8 @@ public final class CauseData {
variables.put("createdAt", createdAt); variables.put("createdAt", createdAt);
variables.put("finishedAt", finishedAt); variables.put("finishedAt", finishedAt);
variables.put("duration", buildDuration); variables.put("duration", buildDuration);
variables.put("jsonBody", jsonBody);
variables.put("noteBody", noteBody);
variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase); variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase);
return variables; return variables;
} }
@ -298,6 +304,10 @@ public final class CauseData {
public String getBuildDuration() { return buildDuration; } public String getBuildDuration() { return buildDuration; }
public String getJsonBody() { return jsonBody; }
public String getNoteBody() { return noteBody; }
String getShortDescription() { String getShortDescription() {
return actionType.getShortDescription(this); return actionType.getShortDescription(this);
@ -386,6 +396,8 @@ public final class CauseData {
.append(pathWithNamespace, causeData.getPathWithNamespace()) .append(pathWithNamespace, causeData.getPathWithNamespace())
.append(created, causeData.getCreated()) .append(created, causeData.getCreated())
.append(deleted, causeData.getDeleted()) .append(deleted, causeData.getDeleted())
.append(jsonBody, causeData.getJsonBody())
.append(noteBody, causeData.getNoteBody())
.isEquals(); .isEquals();
} }
@ -435,6 +447,8 @@ public final class CauseData {
.append(pathWithNamespace) .append(pathWithNamespace)
.append(created) .append(created)
.append(deleted) .append(deleted)
.append(jsonBody)
.append(noteBody)
.toHashCode(); .toHashCode();
} }
@ -484,6 +498,8 @@ public final class CauseData {
.append("pathWithNamespace", pathWithNamespace) .append("pathWithNamespace", pathWithNamespace)
.append("created", created) .append("created", created)
.append("deleted", deleted) .append("deleted", deleted)
.append("jsonBody", jsonBody)
.append("noteBody", noteBody)
.toString(); .toString();
} }

View File

@ -13,6 +13,11 @@ public abstract class WebHook {
private Repository repository; private Repository repository;
private String objectKind; private String objectKind;
private String hookName; private String hookName;
private String jsonBody;
public String getJsonBody() { return this.jsonBody; }
public void setJsonBody(String json) { this.jsonBody = json; }
public String getHookName() { public String getHookName() {
return this.hookName; return this.hookName;

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;
@ -55,6 +56,10 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
PullRequestObjectAttributes objectAttributes = hook.getPullRequest(); PullRequestObjectAttributes objectAttributes = hook.getPullRequest();
if (objectAttributes != null && !objectAttributes.isMergeable()) { if (objectAttributes != null && !objectAttributes.isMergeable()) {
LOGGER.log(Level.INFO, "This pull request can not be merge"); LOGGER.log(Level.INFO, "This pull request can not be merge");
// fixme 无法获取 publisher
// java.lang.ClassCastException: org.jenkinsci.plugins.workflow.job.WorkflowJob cannot be cast to hudson.model.AbstractProject
// at com.gitee.jenkins.publisher.GiteeMessagePublisher.getFromJob(GiteeMessagePublisher.java:65)
// at com.gitee.jenkins.trigger.handler.note.NoteHookTriggerHandlerImpl.handle(NoteHookTriggerHandlerImpl.java:47)
GiteeMessagePublisher publisher = GiteeMessagePublisher.getFromJob(job); GiteeMessagePublisher publisher = GiteeMessagePublisher.getFromJob(job);
GiteeClient client = getClient(job); GiteeClient client = getClient(job);
if (publisher != null && client != null) { if (publisher != null && client != null) {
@ -202,12 +207,23 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<NoteHook>
.withTargetProjectUrl(hook.getPullRequest().getTarget().getUrl()) .withTargetProjectUrl(hook.getPullRequest().getTarget().getUrl())
.withTriggerPhrase(hook.getComment().getBody()) .withTriggerPhrase(hook.getComment().getBody())
.withPathWithNamespace(hook.getPullRequest().getBase().getRepo().getPathWithNamespace()) .withPathWithNamespace(hook.getPullRequest().getBase().getRepo().getPathWithNamespace())
.withJsonBody(hook.getJsonBody())
.withNoteBody(hook.getComment().getBody())
.build(); .build();
} }
@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
@ -224,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

@ -127,6 +127,7 @@ class PipelineHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<Pipel
.withCreatedAt(hook.getObjectAttributes().getCreatedAt()==null?"":hook.getObjectAttributes().getCreatedAt().toString()) .withCreatedAt(hook.getObjectAttributes().getCreatedAt()==null?"":hook.getObjectAttributes().getCreatedAt().toString())
.withFinishedAt(hook.getObjectAttributes().getFinishedAt()==null?"":hook.getObjectAttributes().getFinishedAt().toString()) .withFinishedAt(hook.getObjectAttributes().getFinishedAt()==null?"":hook.getObjectAttributes().getFinishedAt().toString())
.withBuildDuration(String.valueOf(hook.getObjectAttributes().getDuration())) .withBuildDuration(String.valueOf(hook.getObjectAttributes().getDuration()))
.withJsonBody(hook.getJsonBody())
.build(); .build();
} }

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;
@ -232,12 +233,22 @@ class PullRequestHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<Pu
.withRef(hook.getPullRequest().getMergeReferenceName()) .withRef(hook.getPullRequest().getMergeReferenceName())
.withTargetProjectUrl(hook.getPullRequest().getTarget().getUrl()) .withTargetProjectUrl(hook.getPullRequest().getTarget().getUrl())
.withPathWithNamespace(hook.getRepo().getPathWithNamespace()) .withPathWithNamespace(hook.getRepo().getPathWithNamespace())
.withJsonBody(hook.getJsonBody())
.build(); .build();
} }
@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
@ -254,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();
} }

View File

@ -101,6 +101,7 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
.withCreated(hook.getCreated()) .withCreated(hook.getCreated())
.withDeleted(hook.getDeleted()) .withDeleted(hook.getDeleted())
.withTargetProjectUrl(hook.getProject().getUrl()) .withTargetProjectUrl(hook.getProject().getUrl())
.withJsonBody(hook.getJsonBody())
.build(); .build();
} }

View File

@ -30,6 +30,7 @@ public class NoteBuildAction extends BuildWebHookAction {
LOGGER.log(Level.FINE, "Note: {0}", toPrettyPrint(json)); LOGGER.log(Level.FINE, "Note: {0}", toPrettyPrint(json));
this.project = project; this.project = project;
this.noteHook = JsonUtil.read(json, NoteHook.class); this.noteHook = JsonUtil.read(json, NoteHook.class);
this.noteHook.setJsonBody(json);
this.secretToken = secretToken; this.secretToken = secretToken;
} }

View File

@ -31,6 +31,7 @@ public class PipelineBuildAction extends BuildWebHookAction {
LOGGER.log(Level.FINE, "Pipeline event: {0}", toPrettyPrint(json)); LOGGER.log(Level.FINE, "Pipeline event: {0}", toPrettyPrint(json));
this.project = project; this.project = project;
this.pipelineBuildHook = JsonUtil.read(json, PipelineHook.class); this.pipelineBuildHook = JsonUtil.read(json, PipelineHook.class);
this.pipelineBuildHook.setJsonBody(json);
this.secretToken = secretToken; this.secretToken = secretToken;
} }

View File

@ -30,6 +30,7 @@ public class PullRequestBuildAction extends BuildWebHookAction {
LOGGER.log(Level.FINE, "PullRequest: {0}", toPrettyPrint(json)); LOGGER.log(Level.FINE, "PullRequest: {0}", toPrettyPrint(json));
this.project = project; this.project = project;
this.pullRequestHook = JsonUtil.read(json, PullRequestHook.class); this.pullRequestHook = JsonUtil.read(json, PullRequestHook.class);
this.pullRequestHook.setJsonBody(json);
this.secretToken = secretToken; this.secretToken = secretToken;
} }

View File

@ -37,6 +37,7 @@ public class PushBuildAction extends BuildWebHookAction {
LOGGER.log(Level.FINE, "Push: {0}", toPrettyPrint(json)); LOGGER.log(Level.FINE, "Push: {0}", toPrettyPrint(json));
this.project = project; this.project = project;
this.pushHook = JsonUtil.read(json, PushHook.class); this.pushHook = JsonUtil.read(json, PushHook.class);
this.pushHook.setJsonBody(json);
this.secretToken = secretToken; this.secretToken = secretToken;
} }