Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-405]: Fixed the pull review request events not working #768

Merged
merged 6 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions server/plugin/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ Assignees: {{range $i, $el := .Assignees -}} {{- if $i}}, {{end}}{{template "use

template.Must(masterTemplate.New("pullRequestReviewEvent").Funcs(funcMap).Parse(`
{{template "repo" .GetRepo}} {{template "user" .GetSender}}
{{- if eq .GetReview.GetState "APPROVED"}} approved
{{- else if eq .GetReview.GetState "COMMENTED"}} commented on
{{- else if eq .GetReview.GetState "CHANGES_REQUESTED"}} requested changes on
Comment on lines -311 to -313
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did GitHub document the changes anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hanzei I was getting the state in small case and in the code as well they have been changed to lower case in the using toLower. Let me try to find the GitHub documentation for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hanzei Can't find a documentation but here is a screenshot of the webhook payload
Screenshot from 2024-06-21 12-33-38

{{- if eq .GetReview.GetState "approved"}} approved
{{- else if eq .GetReview.GetState "commented"}} commented on
{{- else if eq .GetReview.GetState "changes_requested"}} requested changes on
{{- end }} {{template "pullRequest" .GetPullRequest}}:

{{.Review.GetBody | replaceAllGitHubUsernames}}
Expand All @@ -319,7 +319,6 @@ Assignees: {{range $i, $el := .Assignees -}} {{- if $i}}, {{end}}{{template "use
template.Must(masterTemplate.New("newReviewComment").Funcs(funcMap).Parse(`
{{template "repo" .GetRepo}} New review comment by {{template "user" .GetSender}} on {{template "pullRequest" .GetPullRequest}}:

{{.GetComment.GetDiffHunk}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a screenshot for what this looks like?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old UI
Screenshot from 2024-06-21 12-46-20

New UI
new

{{.GetComment.GetBody | trimBody | replaceAllGitHubUsernames}}
`))

Expand Down
16 changes: 6 additions & 10 deletions server/plugin/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ Excited to see git-get-head land!
PullRequest: &pullRequest,
Sender: &user,
Review: &github.PullRequestReview{
State: sToP("APPROVED"),
State: sToP("approved"),
Body: sToP("Excited to see git-get-head land!"),
},
})
Expand All @@ -944,7 +944,7 @@ Excited to see git-get-head land!
PullRequest: &pullRequest,
Sender: &user,
Review: &github.PullRequestReview{
State: sToP("COMMENTED"),
State: sToP("commented"),
Body: sToP("Excited to see git-get-head land!"),
},
})
Expand All @@ -964,7 +964,7 @@ Excited to see git-get-head land!
PullRequest: &pullRequest,
Sender: &user,
Review: &github.PullRequestReview{
State: sToP("CHANGES_REQUESTED"),
State: sToP("changes_requested"),
Body: sToP("Excited to see git-get-head land!"),
},
})
Expand All @@ -985,7 +985,7 @@ Excited to see git-get-head land!
PullRequest: &pullRequest,
Sender: &user,
Review: &github.PullRequestReview{
State: sToP("APPROVED"),
State: sToP("approved"),
Body: sToP("Excited to see git-get-head land!\n" + gitHubMentions),
},
})
Expand All @@ -999,16 +999,14 @@ func TestPullRequestReviewCommentEventTemplate(t *testing.T) {
expected := `
[\[mattermost-plugin-github\]](https://github.com/mattermost/mattermost-plugin-github) New review comment by [panda](https://github.com/panda) on [#42 Leverage git-get-head](https://github.com/mattermost/mattermost-plugin-github/pull/42):

HUNK
Should this be here?
`

actual, err := renderTemplate("newReviewComment", &github.PullRequestReviewCommentEvent{
Repo: &repo,
PullRequest: &pullRequest,
Comment: &github.PullRequestComment{
Body: sToP("Should this be here?"),
DiffHunk: sToP("HUNK"),
Body: sToP("Should this be here?"),
},
Sender: &user,
})
Expand All @@ -1020,7 +1018,6 @@ Should this be here?
expected := `
[\[mattermost-plugin-github\]](https://github.com/mattermost/mattermost-plugin-github) New review comment by @pandabot on [#42 Leverage git-get-head](https://github.com/mattermost/mattermost-plugin-github/pull/42):

HUNK
Should this be here?
` + usernameMentions + `
`
Expand All @@ -1029,8 +1026,7 @@ Should this be here?
Repo: &repo,
PullRequest: &pullRequest,
Comment: &github.PullRequestComment{
Body: sToP("Should this be here?\n" + gitHubMentions),
DiffHunk: sToP("HUNK"),
Body: sToP("Should this be here?\n" + gitHubMentions),
},
Sender: &user,
})
Expand Down
14 changes: 7 additions & 7 deletions server/plugin/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,10 @@ func (p *Plugin) postPullRequestReviewEvent(event *github.PullRequestReviewEvent
return
}

switch strings.ToUpper(event.GetReview().GetState()) {
case "APPROVED":
case "COMMENTED":
case "CHANGES_REQUESTED":
switch event.GetReview().GetState() {
case "approved":
case "commented":
case "changes_requested":
default:
p.client.Log.Debug("Unhandled review state", "state", event.GetReview().GetState())
return
Expand Down Expand Up @@ -935,7 +935,7 @@ func (p *Plugin) postPullRequestReviewCommentEvent(event *github.PullRequestRevi
continue
}

post := p.makeBotPost(newReviewMessage, "custom_git_pull_review_comment")
post := p.makeBotPost(newReviewMessage, "custom_git_pr_comment")

repoName := strings.ToLower(repo.GetFullName())
commentID := event.GetComment().GetID()
Expand All @@ -945,8 +945,8 @@ func (p *Plugin) postPullRequestReviewCommentEvent(event *github.PullRequestRevi
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())
if _, appErr := p.API.CreatePost(post); appErr != nil {
p.client.Log.Warn("Error webhook post", "post", post, "error", appErr.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.API should not be used. The returned model.AppError makes things way too hard. p.client is they way to go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hanzei Please refer to this comment: #768 (comment)

Copy link
Contributor

@mickmister mickmister Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the p.client.Post method has essentially a bug in it, that has caused issues in this plugin because it mutates the post that is passed in. Is it really a "bug"? Maybe not, but I think we should avoid using it since it has caused issues in production that we then need to fix

2/5 The benefits of using error over AppError in this case are not worth it imo. I think keeping it as p.client.Post will make potential issues come back, just as they did in this PR. This can be changed in a different PR though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kshitij-Katiyar I'm thinking we should keep the changes in this PR to a minimum so let's keep this as-is

}
}
}
Expand Down
Loading
Loading