Skip to content

Commit

Permalink
Webhook: Add webhook support for Slack
Browse files Browse the repository at this point in the history
When
---
- new pull request
- pull request state changed
- pull request merged
- pull request commit changed
- issue body changed
- issue moved
- assignee changed
- new comment

And..
---
- Provide option for commit only
  • Loading branch information
hongwonjun authored and doortts committed Aug 31, 2017
1 parent faead0c commit d179aaa
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 49 deletions.
3 changes: 2 additions & 1 deletion app/controllers/ProjectApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,8 @@ public static Result newWebhook(String ownerId, String projectName) {

Webhook.create(project.id,
addWebhookForm.field("payloadUrl").value(),
addWebhookForm.field("secret").value());
addWebhookForm.field("secret").value(),
Boolean.valueOf(addWebhookForm.field("gitPushOnly").value()));

return redirect(routes.ProjectApp.webhooks(project.owner, project.name));
}
Expand Down
79 changes: 71 additions & 8 deletions app/models/NotificationEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public static NotificationEvent afterNewPullRequest(User sender, PullRequest pul
notiEvent.oldValue = null;
notiEvent.newValue = pullRequest.body;
NotificationEvent.add(notiEvent);

return notiEvent;
}

Expand Down Expand Up @@ -484,6 +485,55 @@ public String getResourceId() {
return resourceId;
}

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

private static void webhookRequest(EventType eventTypes, PullRequest pullRequest, PullRequestReviewAction reviewAction, 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, reviewAction);
}
}
}

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

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

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) {
if (gitPushOnly == webhook.gitPushOnly) {
// Send push event via webhook payload URLs.
webhook.sendRequestToPayloadUrl(commits, refNames, sender, title);
}
}
}

/**
* @see {@link models.PullRequest#merge(models.PullRequestEventMessage)}
Expand All @@ -508,6 +558,9 @@ public static NotificationEvent afterPullRequestCommitChanged(User sender, PullR
notiEvent.oldValue = null;
notiEvent.newValue = newPullRequestCommitChangedMessage(pullRequest);
NotificationEvent.add(notiEvent);

webhookRequest(PULL_REQUEST_COMMIT_CHANGED, pullRequest, false);

return notiEvent;
}

Expand All @@ -532,6 +585,8 @@ private static String newPullRequestCommitChangedMessage(PullRequest pullRequest
* @see {@link actors.PullRequestActor#processPullRequestMerging(models.PullRequestEventMessage, models.PullRequest)}
*/
public static NotificationEvent afterMerge(User sender, PullRequest pullRequest, State state) {
webhookRequest(PULL_REQUEST_MERGED, pullRequest, false);

NotificationEvent notiEvent = createFrom(sender, pullRequest);
notiEvent.title = formatReplyTitle(pullRequest);
notiEvent.receivers = state == State.MERGED ? getReceiversWithRelatedAuthors(sender, pullRequest) : getReceivers(sender, pullRequest);
Expand Down Expand Up @@ -563,14 +618,17 @@ public static NotificationEvent forNewComment(User sender, PullRequest pullReque
}

public static NotificationEvent afterNewPullRequest(PullRequest pullRequest) {
webhookRequest(NEW_PULL_REQUEST, pullRequest, false);
return afterNewPullRequest(UserApp.currentUser(), pullRequest);
}

public static NotificationEvent afterPullRequestUpdated(PullRequest pullRequest, State oldState, State newState) {
webhookRequest(PULL_REQUEST_STATE_CHANGED, pullRequest, false);
return afterPullRequestUpdated(UserApp.currentUser(), pullRequest, oldState, newState);
}

public static void afterNewComment(Comment comment) {
webhookRequest(NEW_COMMENT, comment, false);
NotificationEvent.add(forNewComment(comment, UserApp.currentUser()));
}

Expand Down Expand Up @@ -618,15 +676,15 @@ public static void afterNewCommentWithState(Comment comment, State state) {
}

public static NotificationEvent afterStateChanged(State oldState, Issue issue) {
webhookRequest(ISSUE_STATE_CHANGED, issue, false);

NotificationEvent notiEvent = createFromCurrentUser(issue);
notiEvent.title = formatReplyTitle(issue);
notiEvent.receivers = getReceivers(issue);
notiEvent.eventType = ISSUE_STATE_CHANGED;
notiEvent.oldValue = oldState != null ? oldState.state() : null;
notiEvent.newValue = issue.state.state();

NotificationEvent.add(notiEvent);

return notiEvent;
}

Expand Down Expand Up @@ -666,6 +724,8 @@ public static NotificationEvent afterStateChanged(
}

public static NotificationEvent afterAssigneeChanged(User oldAssignee, Issue issue) {
webhookRequest(ISSUE_ASSIGNEE_CHANGED, issue, false);

NotificationEvent notiEvent = createFromCurrentUser(issue);

Set<User> receivers = getReceivers(issue);
Expand All @@ -690,6 +750,7 @@ public static NotificationEvent afterAssigneeChanged(User oldAssignee, Issue iss

public static void afterNewIssue(Issue issue) {
NotificationEvent.add(forNewIssue(issue, UserApp.currentUser()));
webhookRequest(NEW_ISSUE, issue, false);
}

public static NotificationEvent forNewIssue(Issue issue, User author) {
Expand All @@ -703,6 +764,8 @@ public static NotificationEvent forNewIssue(Issue issue, User author) {
}

public static NotificationEvent afterIssueBodyChanged(String oldBody, Issue issue) {
webhookRequest(ISSUE_BODY_CHANGED, issue, false);

NotificationEvent notiEvent = createFromCurrentUser(issue);
notiEvent.title = formatReplyTitle(issue);
notiEvent.receivers = getReceiversForIssueBodyChanged(oldBody, issue);
Expand All @@ -716,6 +779,8 @@ public static NotificationEvent afterIssueBodyChanged(String oldBody, Issue issu
}

public static NotificationEvent afterIssueMoved(Project previous, Issue issue) {
webhookRequest(ISSUE_MOVED, issue, false);

NotificationEvent notiEvent = createFromCurrentUser(issue);
notiEvent.title = formatReplyTitle(issue);
notiEvent.receivers = getReceivers(issue);
Expand Down Expand Up @@ -866,15 +931,12 @@ public static void afterNewCommits(List<RevCommit> commits, List<String> refName
notiEvent.resourceId = project.asResource().getId();
NotificationEvent.add(notiEvent);

List<Webhook> webhookList = Webhook.findByProject(project.id);
for (Webhook webhook : webhookList) {
// Send push event via webhook payload URLs.
String[] eventTypes = {"push"};
webhook.sendRequestToPayloadUrl(eventTypes, commits, refNames, sender, title);
}
webhookRequest(project, commits, refNames, sender, title, true);
}

public static NotificationEvent afterReviewed(PullRequest pullRequest, PullRequestReviewAction reviewAction) {
webhookRequest(EventType.PULL_REQUEST_REVIEW_STATE_CHANGED, pullRequest, reviewAction, false);

String title = formatReplyTitle(pullRequest);
Resource resource = pullRequest.asResource();
Set<User> receivers = pullRequest.getWatchers();
Expand Down Expand Up @@ -1201,6 +1263,7 @@ public static int getNotificationsCount(User user) {
}

public static void afterCommentUpdated(Comment comment) {
webhookRequest(COMMENT_UPDATED, comment, false);
NotificationEvent.add(forUpdatedComment(comment, UserApp.currentUser()));
}
}
Loading

0 comments on commit d179aaa

Please sign in to comment.