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 4 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
30 changes: 17 additions & 13 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 @@ -908,6 +908,19 @@ func (p *Plugin) postPullRequestReviewCommentEvent(event *github.PullRequestRevi
return
}

post := &model.Post{
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
UserId: p.BotUserID,
Type: "custom_git_pr_comment",
Copy link
Contributor

Choose a reason for hiding this comment

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

While we are not using the custom type currently, I think there is value in keeping the type the same for each post.

What is the reason behind changing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept getting the error that the length of the type should be less than 2, that's why I have reduced it.
@hanzei

Message: newReviewMessage,
}

repoName := strings.ToLower(repo.GetFullName())
commentID := event.GetComment().GetID()
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -935,15 +948,6 @@ 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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Referencing https://github.com/mattermost/mattermost-plugin-github/pull/768/files#r1649049447 the p.client.Post.CreatePost(post) here mutates the post instead of returning the created post, which has had side effects in this plugin. I think we should instead use p.API.CreatePost(post) to avoid the risk of introducing the issue on unrelated code changes cc @hanzei

p.client.Log.Warn("Error webhook post", "post", post, "error", err.Error())
Expand Down
Loading
Loading