Skip to content

Commit

Permalink
Fix PR subscription error "You cannot update an existing Post" (#755)
Browse files Browse the repository at this point in the history
* avoid reusing the same post struct for multiple webhook posts

* apply fix to other subscription loops

* PR feedback

* fix merge issue

* lint
  • Loading branch information
mickmister authored Jun 4, 2024
1 parent e958064 commit 796d591
Showing 1 changed file with 45 additions and 81 deletions.
126 changes: 45 additions & 81 deletions server/plugin/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -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()
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -1376,13 +1330,23 @@ 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())
}
}
}

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
Expand Down

0 comments on commit 796d591

Please sign in to comment.