From 138434afeb4b63814428b46d842d635b150fea8a Mon Sep 17 00:00:00 2001 From: yashin Date: Thu, 16 Aug 2018 15:26:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_zh_CN.md | 50 +++++++++++++++++++ .../com/gitee/jenkins/cause/CauseData.java | 30 +++++++++-- .../jenkins/gitee/hook/model/PushHook.java | 25 ++++++++++ .../push/PushHookTriggerHandlerImpl.java | 2 + 4 files changed, 103 insertions(+), 4 deletions(-) diff --git a/README_zh_CN.md b/README_zh_CN.md index ef1832e..fa5d26d 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -17,6 +17,7 @@ - [新建码云项目WebHook](#新建码云项目WebHook) - [测试推送触发构建](#测试推送触发构建) - [测试PR触发构建](#测试PR触发构建) +- [环境变量](#环境变量) - [用户支持](#用户支持) - [参与贡献](#参与贡献) - [打包或运行测试](#打包或运行测试) @@ -163,6 +164,55 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc 1. 码云的 WebHook 管理中选择勾选了 Pull Request 的 WebHook 点击测试,观察 Jenkins 任务的构建状态 2. 在码云项目中新建一个Pull Request,观察 Jenkins 任务的构建状态 +# 环境变量 +目前支持环境变量见以下函数,其中不同的 WebHook 触发可能导致有些变量为空,具体请安装插件 [EnvInject Plugin](https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin),于构建中查看 Environment Variables + + +```java + public Map getBuildVariables() { + MapWrapper variables = new MapWrapper<>(new HashMap()); + variables.put("giteeBranch", branch); + variables.put("giteeSourceBranch", sourceBranch); + variables.put("giteeActionType", actionType.name()); + variables.put("giteeUserName", userName); + variables.put("giteeUserEmail", userEmail); + variables.put("giteeSourceRepoHomepage", sourceRepoHomepage); + variables.put("giteeSourceRepoName", sourceRepoName); + variables.put("giteeSourceNamespace", sourceNamespace); + variables.put("giteeSourceRepoURL", sourceRepoUrl); + variables.put("giteeSourceRepoSshUrl", sourceRepoSshUrl); + variables.put("giteeSourceRepoHttpUrl", sourceRepoHttpUrl); + variables.put("giteePullRequestTitle", pullRequestTitle); + variables.put("giteePullRequestDescription", pullRequestDescription); + variables.put("giteePullRequestId", pullRequestId == null ? "" : pullRequestId.toString()); + variables.put("giteePullRequestIid", pullRequestIid == null ? "" : pullRequestIid.toString()); + variables.put("giteePullRequestTargetProjectId", pullRequestTargetProjectId == null ? "" : pullRequestTargetProjectId.toString()); + variables.put("giteePullRequestLastCommit", lastCommit); + variables.put("giteePushCreated", created ? "true" : "false"); + variables.put("giteePushDeleted", deleted ? "true" : "false"); + variables.putIfNotNull("giteePullRequestState", pullRequestState); + variables.putIfNotNull("giteeMergedByUser", mergedByUser); + variables.putIfNotNull("giteePullRequestAssignee", pullRequestAssignee); + variables.put("giteeTargetBranch", targetBranch); + variables.put("giteeTargetRepoName", targetRepoName); + variables.put("giteeTargetNamespace", targetNamespace); + variables.put("giteeTargetRepoSshUrl", targetRepoSshUrl); + variables.put("giteeTargetRepoHttpUrl", targetRepoHttpUrl); + variables.put("giteeBefore", before); + variables.put("giteeAfter", after); + variables.put("ref", ref); + variables.put("beforeSha", beforeSha); + variables.put("isTag", isTag); + variables.put("sha", sha); + variables.put("status", status); + variables.put("stages", stages); + variables.put("createdAt", createdAt); + variables.put("finishedAt", finishedAt); + variables.put("duration", buildDuration); + variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase); + return variables; + } +``` # 用户支持 如在使用过程中有任何疑问,欢迎在 [Gitee Jenkins Issue](https://gitee.com/oschina/Gitee-Jenkins-Plugin/issues) 中反馈。 diff --git a/src/main/java/com/gitee/jenkins/cause/CauseData.java b/src/main/java/com/gitee/jenkins/cause/CauseData.java index 1301cda..de0e730 100644 --- a/src/main/java/com/gitee/jenkins/cause/CauseData.java +++ b/src/main/java/com/gitee/jenkins/cause/CauseData.java @@ -59,15 +59,18 @@ public final class CauseData { private final String createdAt; private final String finishedAt; private final String buildDuration; + private final boolean created; + private final boolean deleted; @GeneratePojoBuilder(withFactoryMethod = "*") CauseData(ActionType actionType, Integer sourceProjectId, Integer targetProjectId, String branch, String sourceBranch, String userName, String userEmail, String sourceRepoHomepage, String sourceRepoName, String sourceNamespace, String sourceRepoUrl, String sourceRepoSshUrl, String sourceRepoHttpUrl, String pullRequestTitle, String pullRequestDescription, Integer pullRequestId, - Integer pullRequestIid, Integer pullRequestTargetProjectId, String targetBranch, String targetRepoName, String targetNamespace, 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) { + Integer pullRequestIid, Integer pullRequestTargetProjectId, String targetBranch, String targetRepoName, String targetNamespace, + 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) { 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."); @@ -112,6 +115,8 @@ public final class CauseData { this.finishedAt = finishedAt; this.buildDuration = buildDuration; this.pathWithNamespace = pathWithNamespace; + this.created = created; + this.deleted = deleted; } public Map getBuildVariables() { @@ -133,6 +138,8 @@ public final class CauseData { variables.put("giteePullRequestIid", pullRequestIid == null ? "" : pullRequestIid.toString()); variables.put("giteePullRequestTargetProjectId", pullRequestTargetProjectId == null ? "" : pullRequestTargetProjectId.toString()); variables.put("giteePullRequestLastCommit", lastCommit); + variables.put("giteePushCreated", created ? "true" : "false"); + variables.put("giteePushDeleted", deleted ? "true" : "false"); variables.putIfNotNull("giteePullRequestState", pullRequestState); variables.putIfNotNull("giteeMergedByUser", mergedByUser); variables.putIfNotNull("giteePullRequestAssignee", pullRequestAssignee); @@ -305,6 +312,15 @@ public final class CauseData { return pullRequestAssignee; } + + public boolean getCreated() { + return created; + } + + public boolean getDeleted() { + return deleted; + } + public PullRequest getPullRequest() { if (pullRequestId == null) { return null; @@ -365,6 +381,8 @@ public final class CauseData { .append(finishedAt, causeData.getFinishedAt()) .append(buildDuration, causeData.getBuildDuration()) .append(pathWithNamespace, causeData.getPathWithNamespace()) + .append(created, causeData.getCreated()) + .append(deleted, causeData.getDeleted()) .isEquals(); } @@ -412,6 +430,8 @@ public final class CauseData { .append(finishedAt) .append(buildDuration) .append(pathWithNamespace) + .append(created) + .append(deleted) .toHashCode(); } @@ -459,6 +479,8 @@ public final class CauseData { .append("finishedAt", finishedAt) .append("duration", buildDuration) .append("pathWithNamespace", pathWithNamespace) + .append("created", created) + .append("deleted", deleted) .toString(); } diff --git a/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java b/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java index e338ac7..c9ea033 100644 --- a/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java +++ b/src/main/java/com/gitee/jenkins/gitee/hook/model/PushHook.java @@ -15,6 +15,8 @@ public class PushHook extends WebHook { private String before; private String after; + private boolean created; + private boolean deleted; private String ref; private Integer userId; private String userName; @@ -32,6 +34,23 @@ public class PushHook extends WebHook { this.before = before; } + public boolean getCreated() { + return created; + } + + public void setCreated(boolean created) { + this.created = created; + } + + public boolean getDeleted() { + return deleted; + } + + public void setDeleted(boolean deleted) { + this.deleted = deleted; + } + + public String getAfter() { return after; } @@ -127,6 +146,8 @@ public class PushHook extends WebHook { PushHook pushHook = (PushHook) o; return new EqualsBuilder() .append(before, pushHook.before) + .append(created, pushHook.created) + .append(deleted, pushHook.deleted) .append(after, pushHook.after) .append(ref, pushHook.ref) .append(userId, pushHook.userId) @@ -145,6 +166,8 @@ public class PushHook extends WebHook { .append(before) .append(after) .append(ref) + .append(created) + .append(deleted) .append(userId) .append(userName) .append(userEmail) @@ -161,6 +184,8 @@ public class PushHook extends WebHook { .append("before", before) .append("after", after) .append("ref", ref) + .append("created", created) + .append("deleted", deleted) .append("userId", userId) .append("userName", userName) .append("userEmail", userEmail) diff --git a/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java b/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java index fb0aab1..e7d725b 100644 --- a/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java +++ b/src/main/java/com/gitee/jenkins/trigger/handler/push/PushHookTriggerHandlerImpl.java @@ -96,6 +96,8 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler .withAfter(hook.getAfter()) .withRef(hook.getRef()) .withLastCommit(hook.getAfter()) + .withCreated(hook.getCreated()) + .withDeleted(hook.getDeleted()) .withTargetProjectUrl(hook.getProject().getUrl()) .build(); }