From 896007e1931d19768b64d47254d0d770e44a89f2 Mon Sep 17 00:00:00 2001 From: silverballer <66757495@qq.com> Date: Wed, 30 Dec 2020 16:20:58 +0800 Subject: [PATCH] =?UTF-8?q?!4=20=E6=94=AF=E6=8C=81commit=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=20*=20=E6=94=AF=E6=8C=81commit=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gitee/jenkins/cause/CauseData.java | 5 ++ .../jenkins/gitee/hook/model/NoteHook.java | 5 ++ .../hook/model/NoteObjectAttributes.java | 72 ++++++++++++------- .../note/NoteHookTriggerHandlerImpl.java | 39 +++++++++- .../gitee/jenkins/cause/Messages.properties | 1 + .../jenkins/cause/Messages_zh_CN.properties | 1 + 6 files changed, 97 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/gitee/jenkins/cause/CauseData.java b/src/main/java/com/gitee/jenkins/cause/CauseData.java index 3964f53..7ed465a 100644 --- a/src/main/java/com/gitee/jenkins/cause/CauseData.java +++ b/src/main/java/com/gitee/jenkins/cause/CauseData.java @@ -531,6 +531,11 @@ public final class CauseData { data.getTargetProjectUrl()); } } + }, COMMIT_COMMENT { + @Override + String getShortDescription(CauseData data) { + return Messages.GiteeWebHookCause_ShortDescription_Commit_comment(data.getTriggeredByUser()); + } }, PIPELINE { @Override String getShortDescription(CauseData data) { diff --git a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java index c5288bc..75571a5 100644 --- a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java +++ b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteHook.java @@ -58,6 +58,11 @@ public class NoteHook extends WebHook { } public String getWebHookDescription() { + // 兼容commit评论 + if (pullRequest == null) { + return getHookName() + " commit sha = " + comment.getCommitId(); + } + return getHookName() + " iid = " + pullRequest.getNumber() + " merge commit sha = " + pullRequest.getMergeCommitSha(); } diff --git a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java index 6a8e7b5..9ac2cf1 100644 --- a/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java +++ b/src/main/java/com/gitee/jenkins/gitee/hook/model/NoteObjectAttributes.java @@ -20,6 +20,8 @@ public class NoteObjectAttributes { private Date createdAt; private Date updatedAt; private String htmlUrl; + private String commitId; + private User user; public Integer getId() { return id; @@ -77,6 +79,22 @@ public class NoteObjectAttributes { this.htmlUrl = htmlUrl; } + public String getCommitId() { + return commitId; + } + + public void setCommitId(String commitId) { + this.commitId = commitId; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -87,39 +105,45 @@ public class NoteObjectAttributes { } NoteObjectAttributes that = (NoteObjectAttributes) o; return new EqualsBuilder() - .append(id, that.id) - .append(body, that.body) - .append(projectId, that.projectId) - .append(authorId, that.authorId) - .append(createdAt, that.createdAt) - .append(updatedAt, that.updatedAt) - .append(htmlUrl, that.htmlUrl) - .isEquals(); + .append(id, that.id) + .append(body, that.body) + .append(projectId, that.projectId) + .append(authorId, that.authorId) + .append(createdAt, that.createdAt) + .append(updatedAt, that.updatedAt) + .append(htmlUrl, that.htmlUrl) + .append(commitId, that.commitId) + .append(user, that.user) + .isEquals(); } @Override public int hashCode() { return new HashCodeBuilder(17, 37) - .append(id) - .append(body) - .append(projectId) - .append(authorId) - .append(createdAt) - .append(updatedAt) - .append(htmlUrl) - .toHashCode(); + .append(id) + .append(body) + .append(projectId) + .append(authorId) + .append(createdAt) + .append(updatedAt) + .append(htmlUrl) + .append(commitId) + .append(user) + .toHashCode(); } @Override public String toString() { return new ToStringBuilder(this) - .append("id", id) - .append("body", body) - .append("projectId", projectId) - .append("authorId", authorId) - .append("createdAt", createdAt) - .append("updatedAt", updatedAt) - .append("htmlUrl", htmlUrl) - .toString(); + .append("id", id) + .append("body", body) + .append("projectId", projectId) + .append("authorId", authorId) + .append("createdAt", createdAt) + .append("updatedAt", updatedAt) + .append("htmlUrl", htmlUrl) + .append("commitId", commitId) + .append("user", user) + .toString(); } } 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 7f5f7c5..56a72fb 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 @@ -135,6 +135,35 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler @Override protected CauseData retrieveCauseData(NoteHook hook) { + // 兼容来自commit的评论 + if (hook.getPullRequest() == null) { + return causeData() + .withActionType(CauseData.ActionType.COMMIT_COMMENT) + .withUserName(hook.getComment().getUser().getUsername()) + .withUserEmail(hook.getComment().getUser().getEmail()) + .withPullRequestTitle("") + .withBranch("") + .withSourceBranch("") + .withSourceProjectId(hook.getProject().getId()) + .withSourceRepoHomepage(hook.getProject().getHomepage()) + .withSourceRepoName(hook.getProject().getName()) + .withSourceNamespace(hook.getProject().getNamespace()) + .withSourceRepoUrl(hook.getProject().getUrl()) + .withSourceRepoSshUrl(hook.getProject().getSshUrl()) + .withSourceRepoHttpUrl(hook.getProject().getGitHttpUrl()) + .withTargetBranch("") + .withTargetProjectId(hook.getProject().getId()) + .withTargetRepoName(hook.getProject().getName()) + .withTargetNamespace(hook.getProject().getNamespace()) + .withTargetRepoSshUrl(hook.getProject().getSshUrl()) + .withTargetRepoHttpUrl(hook.getProject().getGitHttpUrl()) + .withTriggeredByUser(hook.getComment().getUser().getName()) + .withTriggerPhrase(hook.getComment().getBody()) + .withSha(hook.getComment().getCommitId()) + .withPathWithNamespace(hook.getProject().getPathWithNamespace()) + .build(); + } + return causeData() .withActionType(CauseData.ActionType.NOTE) .withSourceProjectId(hook.getPullRequest().getSourceProjectId()) @@ -188,9 +217,15 @@ class NoteHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler if (hook.getPullRequest() != null && hook.getPullRequest().getMergeReferenceName() != null) { return hook.getPullRequest().getMergeReferenceName(); - } else { - throw new NoRevisionToBuildException(); } + + // 兼容来自commit的评论 + if (hook.getComment() != null + && StringUtils.isNotBlank(hook.getComment().getCommitId())) { + return hook.getComment().getCommitId(); + } + + throw new NoRevisionToBuildException(); } private boolean isValidTrigger(NoteHook hook) { diff --git a/src/main/resources/com/gitee/jenkins/cause/Messages.properties b/src/main/resources/com/gitee/jenkins/cause/Messages.properties index 178bbe8..a1311ab 100644 --- a/src/main/resources/com/gitee/jenkins/cause/Messages.properties +++ b/src/main/resources/com/gitee/jenkins/cause/Messages.properties @@ -4,5 +4,6 @@ GiteeWebHookCause.ShortDescription.PullRequestHook_html=Triggered by Gitee Pull Request #{1}: {2} => {3} GiteeWebHookCause.ShortDescription.NoteHook_plain=Triggered by {0} Gitee Pull Request #{1}: {2} => {3} +GiteeWebHookCause.ShortDescription.Commit_comment=Started by Gitee comment by {0} GiteeWebHookCause.ShortDescription.PipelineHook_noStatus=Started by Gitee Pipeline event GiteeWebHookCause.ShortDescription.PipelineHook=Started by Gitee Pipeline event {0} diff --git a/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties b/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties index 15097a5..b86ca36 100644 --- a/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties +++ b/src/main/resources/com/gitee/jenkins/cause/Messages_zh_CN.properties @@ -4,5 +4,6 @@ GiteeWebHookCause.ShortDescription.PullRequestHook_html=\u7531 Gitee Pull Request #{1}: {2} => {3} \u89E6\u53D1 GiteeWebHookCause.ShortDescription.NoteHook_plain=\u7531 {0} Gitee Pull Request #{1}: {2} => {3} \u89E6\u53D1 +GiteeWebHookCause.ShortDescription.Commit_comment=Gitee \u7528\u6237 {0} \u8BC4\u8BBA\u63D0\u4EA4\u89E6\u53D1\u6784\u5EFA GiteeWebHookCause.ShortDescription.PipelineHook_noStatus=\u7531 Gitee Pipeline \u89E6\u53D1 GiteeWebHookCause.ShortDescription.PipelineHook=\u7531 Gitee Pipeline {0} \u89E6\u53D1