From ed4ede6fad5fc640f1a8240b3835680710c4d9b4 Mon Sep 17 00:00:00 2001 From: yashin Date: Fri, 13 Nov 2020 18:00:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0json=20body=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_zh_CN.md | 6 ++++++ .../com/gitee/jenkins/cause/CauseData.java | 18 +++++++++++++++++- .../jenkins/gitee/hook/model/WebHook.java | 5 +++++ .../note/NoteHookTriggerHandlerImpl.java | 6 ++++++ .../PipelineHookTriggerHandlerImpl.java | 1 + .../PullRequestHookTriggerHandlerImpl.java | 1 + .../push/PushHookTriggerHandlerImpl.java | 1 + .../jenkins/webhook/build/NoteBuildAction.java | 1 + .../webhook/build/PipelineBuildAction.java | 1 + .../webhook/build/PullRequestBuildAction.java | 1 + .../jenkins/webhook/build/PushBuildAction.java | 1 + 11 files changed, 41 insertions(+), 1 deletion(-) diff --git a/README_zh_CN.md b/README_zh_CN.md index e9488cd..7b7aefe 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -202,6 +202,9 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc variables.put("giteeTargetRepoHttpUrl", targetRepoHttpUrl); variables.put("giteeBefore", before); variables.put("giteeAfter", after); + variables.put("giteeBeforeCommitSha", before); + variables.put("giteeAfterCommitSha", after); + variables.put("giteeRef", ref); variables.put("ref", ref); variables.put("beforeSha", beforeSha); variables.put("isTag", isTag); @@ -211,9 +214,12 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc variables.put("createdAt", createdAt); variables.put("finishedAt", finishedAt); variables.put("duration", buildDuration); + variables.put("jsonBody", jsonBody); + variables.put("noteBody", noteBody); variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase); return variables; } + ``` # 用户支持 diff --git a/src/main/java/com/gitee/jenkins/cause/CauseData.java b/src/main/java/com/gitee/jenkins/cause/CauseData.java index 3964f53..7b2bb8d 100644 --- a/src/main/java/com/gitee/jenkins/cause/CauseData.java +++ b/src/main/java/com/gitee/jenkins/cause/CauseData.java @@ -59,6 +59,8 @@ public final class CauseData { private final String createdAt; private final String finishedAt; private final String buildDuration; + private final String jsonBody; + private final String noteBody; private final boolean created; 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 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 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.sourceProjectId = checkNotNull(sourceProjectId, "sourceProjectId 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.created = created; this.deleted = deleted; + this.jsonBody = jsonBody; + this.noteBody = noteBody; } public Map getBuildVariables() { @@ -162,6 +166,8 @@ public final class CauseData { variables.put("createdAt", createdAt); variables.put("finishedAt", finishedAt); variables.put("duration", buildDuration); + variables.put("jsonBody", jsonBody); + variables.put("noteBody", noteBody); variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase); return variables; } @@ -298,6 +304,10 @@ public final class CauseData { public String getBuildDuration() { return buildDuration; } + public String getJsonBody() { return jsonBody; } + + public String getNoteBody() { return noteBody; } + String getShortDescription() { return actionType.getShortDescription(this); @@ -386,6 +396,8 @@ public final class CauseData { .append(pathWithNamespace, causeData.getPathWithNamespace()) .append(created, causeData.getCreated()) .append(deleted, causeData.getDeleted()) + .append(jsonBody, causeData.getJsonBody()) + .append(noteBody, causeData.getNoteBody()) .isEquals(); } @@ -435,6 +447,8 @@ public final class CauseData { .append(pathWithNamespace) .append(created) .append(deleted) + .append(jsonBody) + .append(noteBody) .toHashCode(); } @@ -484,6 +498,8 @@ public final class CauseData { .append("pathWithNamespace", pathWithNamespace) .append("created", created) .append("deleted", deleted) + .append("jsonBody", jsonBody) + .append("noteBody", noteBody) .toString(); } diff --git a/src/main/java/com/gitee/jenkins/gitee/hook/model/WebHook.java b/src/main/java/com/gitee/jenkins/gitee/hook/model/WebHook.java index dbe07c5..60abcde 100644 --- a/src/main/java/com/gitee/jenkins/gitee/hook/model/WebHook.java +++ b/src/main/java/com/gitee/jenkins/gitee/hook/model/WebHook.java @@ -13,6 +13,11 @@ public abstract class WebHook { private Repository repository; private String objectKind; private String hookName; + private String jsonBody; + + public String getJsonBody() { return this.jsonBody; } + + public void setJsonBody(String json) { this.jsonBody = json; } public String getHookName() { return this.hookName; diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java index 44cb549..cce73f0 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java @@ -44,6 +44,10 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler PullRequestObjectAttributes objectAttributes = hook.getPullRequest(); if (objectAttributes != null && !objectAttributes.isMergeable()) { 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); GiteeClient client = getClient(job); if (publisher != null && client != null) { @@ -120,6 +124,8 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler .withTargetProjectUrl(hook.getPullRequest().getTarget().getUrl()) .withTriggerPhrase(hook.getComment().getBody()) .withPathWithNamespace(hook.getPullRequest().getBase().getRepo().getPathWithNamespace()) + .withJsonBody(hook.getJsonBody()) + .withNoteBody(hook.getComment().getBody()) .build(); } diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java index b78f069..e03ee1e 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java @@ -126,6 +126,7 @@ class PipelineHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler .withCreated(hook.getCreated()) .withDeleted(hook.getDeleted()) .withTargetProjectUrl(hook.getProject().getUrl()) + .withJsonBody(hook.getJsonBody()) .build(); } diff --git a/src/main/java/com/gitee/jenkins/webhook/build/NoteBuildAction.java b/src/main/java/com/gitee/jenkins/webhook/build/NoteBuildAction.java index dcfc252..c706360 100644 --- a/src/main/java/com/gitee/jenkins/webhook/build/NoteBuildAction.java +++ b/src/main/java/com/gitee/jenkins/webhook/build/NoteBuildAction.java @@ -30,6 +30,7 @@ public class NoteBuildAction extends BuildWebHookAction { LOGGER.log(Level.FINE, "Note: {0}", toPrettyPrint(json)); this.project = project; this.noteHook = JsonUtil.read(json, NoteHook.class); + this.noteHook.setJsonBody(json); this.secretToken = secretToken; } diff --git a/src/main/java/com/gitee/jenkins/webhook/build/PipelineBuildAction.java b/src/main/java/com/gitee/jenkins/webhook/build/PipelineBuildAction.java index d161f43..8d67b24 100644 --- a/src/main/java/com/gitee/jenkins/webhook/build/PipelineBuildAction.java +++ b/src/main/java/com/gitee/jenkins/webhook/build/PipelineBuildAction.java @@ -31,6 +31,7 @@ public class PipelineBuildAction extends BuildWebHookAction { LOGGER.log(Level.FINE, "Pipeline event: {0}", toPrettyPrint(json)); this.project = project; this.pipelineBuildHook = JsonUtil.read(json, PipelineHook.class); + this.pipelineBuildHook.setJsonBody(json); this.secretToken = secretToken; } diff --git a/src/main/java/com/gitee/jenkins/webhook/build/PullRequestBuildAction.java b/src/main/java/com/gitee/jenkins/webhook/build/PullRequestBuildAction.java index aa88e7b..15d379c 100644 --- a/src/main/java/com/gitee/jenkins/webhook/build/PullRequestBuildAction.java +++ b/src/main/java/com/gitee/jenkins/webhook/build/PullRequestBuildAction.java @@ -30,6 +30,7 @@ public class PullRequestBuildAction extends BuildWebHookAction { LOGGER.log(Level.FINE, "PullRequest: {0}", toPrettyPrint(json)); this.project = project; this.pullRequestHook = JsonUtil.read(json, PullRequestHook.class); + this.pullRequestHook.setJsonBody(json); this.secretToken = secretToken; } diff --git a/src/main/java/com/gitee/jenkins/webhook/build/PushBuildAction.java b/src/main/java/com/gitee/jenkins/webhook/build/PushBuildAction.java index a3a0de3..f47f053 100644 --- a/src/main/java/com/gitee/jenkins/webhook/build/PushBuildAction.java +++ b/src/main/java/com/gitee/jenkins/webhook/build/PushBuildAction.java @@ -37,6 +37,7 @@ public class PushBuildAction extends BuildWebHookAction { LOGGER.log(Level.FINE, "Push: {0}", toPrettyPrint(json)); this.project = project; this.pushHook = JsonUtil.read(json, PushHook.class); + this.pushHook.setJsonBody(json); this.secretToken = secretToken; }