Skip to content

Commit

Permalink
#540 Pull Request 에 Comment 가 추가되는 경우 Webhook 발송 이벤트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hongwonjun authored and doortts committed Aug 3, 2020
1 parent 6b7c87b commit 84ea8f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/NotificationEvent.java
Expand Up @@ -648,6 +648,16 @@ private static void webhookRequest(EventType eventTypes, Comment comment, Boolea
}
}

private static void webhookRequest(EventType eventTypes, PullRequest pullRequest, ReviewComment reviewComment, Boolean gitPushOnly) {
List<Webhook> webhookList = Webhook.findByProject(pullRequest.toProject.id);
for (Webhook webhook : webhookList) {
if (gitPushOnly == webhook.gitPushOnly) {
// Send push event via webhook payload URLs.
webhook.sendRequestToPayloadUrl(eventTypes, UserApp.currentUser(), pullRequest, reviewComment);
}
}
}

private static void webhookRequest(Project project, List<RevCommit> commits, List<String> refNames, User sender, String title, Boolean gitPushOnly) {
List<Webhook> webhookList = Webhook.findByProject(project.id);
for (Webhook webhook : webhookList) {
Expand Down Expand Up @@ -728,6 +738,8 @@ public static void afterNewComment(User sender, PullRequest pullRequest,
}

public static NotificationEvent forNewComment(User sender, PullRequest pullRequest, ReviewComment newComment) {
webhookRequest(NEW_REVIEW_COMMENT, pullRequest, newComment, false);

NotificationEvent notiEvent = createFrom(sender, newComment);
notiEvent.title = formatReplyTitle(pullRequest);
Set<User> receivers = getMentionedUsers(newComment.getContents());
Expand Down
25 changes: 25 additions & 0 deletions app/models/Webhook.java
Expand Up @@ -220,6 +220,11 @@ public void sendRequestToPayloadUrl(EventType eventType, User sender, PullReques
sendRequest(requestBodyString);
}

public void sendRequestToPayloadUrl(EventType eventType, User sender, PullRequest eventPullRequest, ReviewComment reviewComment) {
String requestBodyString = buildRequestBody(eventType, sender, eventPullRequest, reviewComment);
sendRequest(requestBodyString);
}

public void sendRequestToPayloadUrl(EventType eventType, User sender, Comment eventComment) {
String requestBodyString = buildRequestBody(eventType, sender, eventComment);
sendRequest(requestBodyString);
Expand Down Expand Up @@ -312,6 +317,26 @@ private String buildRequestBody(EventType eventType, User sender, PullRequest ev
}
}

private String buildRequestBody(EventType eventType, User sender, PullRequest eventPullRequest, ReviewComment reviewComment) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode detailFields = mapper.createArrayNode();
ArrayNode attachments = mapper.createArrayNode();

String requestMessage = "[" + project.name + "] " + sender.name + " ";
switch (eventType) {
case NEW_REVIEW_COMMENT:
requestMessage += Messages.get(Lang.defaultLang(), "notification.type.new.simple.comment");
break;
}
requestMessage += " <" + utils.Config.getScheme() + "://" + utils.Config.getHostport("localhost:9000") + RouteUtil.getUrl(reviewComment) + "|#" + eventPullRequest.number + ": " + eventPullRequest.title + ">";

if (this.webhookType == WebhookType.SIMPLE) {
return buildTextPropertyOnlyJSON(requestMessage);
} else {
return buildJsonWithPullReqtuestDetails(eventPullRequest, detailFields, attachments, requestMessage);
}
}

private String buildRequestBody(EventType eventType, User sender, Issue eventIssue) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode detailFields = mapper.createArrayNode();
Expand Down

0 comments on commit 84ea8f4

Please sign in to comment.