diff --git a/server/plugin/webhook.go b/server/plugin/webhook.go index 5ae92aedb..1fa51ba9f 100644 --- a/server/plugin/webhook.go +++ b/server/plugin/webhook.go @@ -383,11 +383,6 @@ func (p *Plugin) postPullRequestEvent(event *github.PullRequestEvent) { return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_pr", - } - for _, sub := range subs { if !sub.Pulls() && !sub.PullsMerged() && !sub.PullsCreated() { continue @@ -414,17 +409,19 @@ func (p *Plugin) postPullRequestEvent(event *github.PullRequestEvent) { } } + if !contained && label != "" { + continue + } + repoName := strings.ToLower(repo.GetFullName()) prNumber := event.GetPullRequest().Number + post := p.makeBotPost("", "custom_git_pr") + post.AddProp(postPropGithubRepo, repoName) post.AddProp(postPropGithubObjectID, prNumber) post.AddProp(postPropGithubObjectType, githubObjectTypeIssue) - if !contained && label != "" { - continue - } - if action == actionLabeled { if label != "" && label == eventLabel { pullRequestLabelledMessage, err := renderTemplate("pullRequestLabelled", event) @@ -509,12 +506,6 @@ func (p *Plugin) handlePRDescriptionMentionNotification(event *github.PullReques return } - post := &model.Post{ - UserId: p.BotUserID, - Message: message, - Type: "custom_git_mention", - } - for _, username := range mentionedUsernames { // Don't notify user of their own comment if username == event.GetSender().GetLogin() { @@ -540,6 +531,7 @@ func (p *Plugin) handlePRDescriptionMentionNotification(event *github.PullReques continue } + post := p.makeBotPost(message, "custom_git_mention") post.ChannelId = channel.Id if err = p.client.Post.CreatePost(post); err != nil { @@ -610,11 +602,7 @@ func (p *Plugin) postIssueEvent(event *github.IssuesEvent) { } renderedMessage = p.sanitizeDescription(renderedMessage) - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_issue", - Message: renderedMessage, - } + post := p.makeBotPost(renderedMessage, "custom_git_issue") repoName := strings.ToLower(repo.GetFullName()) issueNumber := issue.Number @@ -670,12 +658,6 @@ func (p *Plugin) postPushEvent(event *github.PushEvent) { return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_push", - Message: pushedCommitsMessage, - } - for _, sub := range subs { if !sub.Pushes() { continue @@ -685,6 +667,8 @@ func (p *Plugin) postPushEvent(event *github.PushEvent) { continue } + post := p.makeBotPost(pushedCommitsMessage, "custom_git_push") + post.ChannelId = sub.ChannelID if err = p.client.Post.CreatePost(post); err != nil { p.client.Log.Warn("Error webhook post", "post", post, "error", err.Error()) @@ -711,12 +695,6 @@ func (p *Plugin) postCreateEvent(event *github.CreateEvent) { return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_create", - Message: newCreateMessage, - } - for _, sub := range subs { if !sub.Creates() { continue @@ -726,6 +704,8 @@ func (p *Plugin) postCreateEvent(event *github.CreateEvent) { continue } + post := p.makeBotPost(newCreateMessage, "custom_git_create") + post.ChannelId = sub.ChannelID if err = p.client.Post.CreatePost(post); err != nil { p.client.Log.Warn("Error webhook post", "post", post, "error", err.Error()) @@ -754,12 +734,6 @@ func (p *Plugin) postDeleteEvent(event *github.DeleteEvent) { return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_delete", - Message: newDeleteMessage, - } - for _, sub := range subs { if !sub.Deletes() { continue @@ -769,6 +743,7 @@ func (p *Plugin) postDeleteEvent(event *github.DeleteEvent) { continue } + post := p.makeBotPost(newDeleteMessage, "custom_git_delete") post.ChannelId = sub.ChannelID if err = p.client.Post.CreatePost(post); err != nil { p.client.Log.Warn("Error webhook post", "post", post, "error", err.Error()) @@ -795,18 +770,6 @@ func (p *Plugin) postIssueCommentEvent(event *github.IssueCommentEvent) { return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_comment", - } - - repoName := strings.ToLower(repo.GetFullName()) - commentID := event.GetComment().GetID() - - post.AddProp(postPropGithubRepo, repoName) - post.AddProp(postPropGithubObjectID, commentID) - post.AddProp(postPropGithubObjectType, githubObjectTypeIssueComment) - labels := make([]string, len(event.GetIssue().Labels)) for i, v := range event.GetIssue().Labels { labels[i] = v.GetName() @@ -834,6 +797,15 @@ func (p *Plugin) postIssueCommentEvent(event *github.IssueCommentEvent) { continue } + post := p.makeBotPost("", "custom_git_comment") + + repoName := strings.ToLower(repo.GetFullName()) + commentID := event.GetComment().GetID() + + post.AddProp(postPropGithubRepo, repoName) + post.AddProp(postPropGithubObjectID, commentID) + post.AddProp(postPropGithubObjectType, githubObjectTypeIssueComment) + if event.GetAction() == actionCreated { post.Message = message } @@ -886,12 +858,6 @@ func (p *Plugin) postPullRequestReviewEvent(event *github.PullRequestReviewEvent return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_pull_review", - Message: newReviewMessage, - } - labels := make([]string, len(event.GetPullRequest().Labels)) for i, v := range event.GetPullRequest().Labels { labels[i] = v.GetName() @@ -919,6 +885,8 @@ func (p *Plugin) postPullRequestReviewEvent(event *github.PullRequestReviewEvent continue } + post := p.makeBotPost(newReviewMessage, "custom_git_pull_review") + post.ChannelId = sub.ChannelID if err = p.client.Post.CreatePost(post); err != nil { p.client.Log.Warn("Error webhook post", "post", post, "error", err.Error()) @@ -940,19 +908,6 @@ func (p *Plugin) postPullRequestReviewCommentEvent(event *github.PullRequestRevi return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_pull_review_comment", - Message: newReviewMessage, - } - - repoName := strings.ToLower(repo.GetFullName()) - commentID := event.GetComment().GetID() - - post.AddProp(postPropGithubRepo, repoName) - post.AddProp(postPropGithubObjectID, commentID) - post.AddProp(postPropGithubObjectType, githubObjectTypePRReviewComment) - labels := make([]string, len(event.GetPullRequest().Labels)) for i, v := range event.GetPullRequest().Labels { labels[i] = v.GetName() @@ -980,6 +935,15 @@ func (p *Plugin) postPullRequestReviewCommentEvent(event *github.PullRequestRevi continue } + post := p.makeBotPost(newReviewMessage, "custom_git_pull_review_comment") + + repoName := strings.ToLower(repo.GetFullName()) + commentID := event.GetComment().GetID() + + post.AddProp(postPropGithubRepo, repoName) + post.AddProp(postPropGithubObjectID, commentID) + post.AddProp(postPropGithubObjectType, githubObjectTypePRReviewComment) + post.ChannelId = sub.ChannelID if err = p.client.Post.CreatePost(post); err != nil { p.client.Log.Warn("Error webhook post", "post", post, "error", err.Error()) @@ -1008,12 +972,6 @@ func (p *Plugin) handleCommentMentionNotification(event *github.IssueCommentEven return } - post := &model.Post{ - UserId: p.BotUserID, - Message: message, - Type: "custom_git_mention", - } - assignees := event.GetIssue().Assignees for _, username := range mentionedUsernames { @@ -1054,6 +1012,8 @@ func (p *Plugin) handleCommentMentionNotification(event *github.IssueCommentEven continue } + post := p.makeBotPost(message, "custom_git_mention") + post.ChannelId = channel.Id if err = p.client.Post.CreatePost(post); err != nil { p.client.Log.Warn("Error creating mention post", "error", err.Error()) @@ -1361,12 +1321,6 @@ func (p *Plugin) postStarEvent(event *github.StarEvent) { return } - post := &model.Post{ - UserId: p.BotUserID, - Type: "custom_git_star", - Message: newStarMessage, - } - for _, sub := range subs { if !sub.Stars() { continue @@ -1376,6 +1330,8 @@ func (p *Plugin) postStarEvent(event *github.StarEvent) { continue } + post := p.makeBotPost(newStarMessage, "custom_git_star") + post.ChannelId = sub.ChannelID if err = p.client.Post.CreatePost(post); err != nil { p.client.Log.Warn("Error webhook post", "post", post, "error", err.Error()) @@ -1383,6 +1339,14 @@ func (p *Plugin) postStarEvent(event *github.StarEvent) { } } +func (p *Plugin) makeBotPost(message, postType string) *model.Post { + return &model.Post{ + UserId: p.BotUserID, + Type: postType, + Message: message, + } +} + func (p *Plugin) postReleaseEvent(event *github.ReleaseEvent) { if event.GetAction() != actionCreated && event.GetAction() != actionDeleted { return