Skip to content

Commit

Permalink
[GH-717] Implement subscription notification for pull request "reopen…
Browse files Browse the repository at this point in the history
…ed" event (#718)

* [MI-3808] Fix issue: Subscription notification not working for pull request "reopened" event.

* [MI-3808] Added test case for new template

* [MI-3808] Review fix
  • Loading branch information
raghavaggarwal2308 authored and mickmister committed Dec 12, 2023
1 parent 34e32c4 commit 7c47f89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions server/plugin/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ Assignees: {{range $i, $el := .Assignees -}} {{- if $i}}, {{end}}{{template "use
{{- if .GetPullRequest.GetMerged }} merged
{{- else }} closed
{{- end }} by {{template "user" .GetSender}}.
`))

template.Must(masterTemplate.New("reopenedPR").Funcs(funcMap).Parse(`
{{template "repo" .GetRepo}} Pull request {{template "pullRequest" .GetPullRequest}} was reopened by {{template "user" .GetSender}}.
`))

template.Must(masterTemplate.New("pullRequestLabelled").Funcs(funcMap).Parse(`
Expand Down
16 changes: 16 additions & 0 deletions server/plugin/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,22 @@ func TestClosedPRMessageTemplate(t *testing.T) {
})
}

func TestReopenedPRMessageTemplate(t *testing.T) {
t.Run("reopened", func(t *testing.T) {
expected := `
[\[mattermost-plugin-github\]](https://github.com/mattermost/mattermost-plugin-github) Pull request [#42 Leverage git-get-head](https://github.com/mattermost/mattermost-plugin-github/pull/42) was reopened by [panda](https://github.com/panda).
`

actual, err := renderTemplate("reopenedPR", &github.PullRequestEvent{
Repo: &repo,
PullRequest: &pullRequest,
Sender: &user,
})
require.NoError(t, err)
require.Equal(t, expected, actual)
})
}

func TestPullRequestLabelledTemplate(t *testing.T) {
expected := `
#### Leverage git-get-head
Expand Down
17 changes: 16 additions & 1 deletion server/plugin/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ func (p *Plugin) postPullRequestEvent(event *github.PullRequestEvent) {
}

action := event.GetAction()
if action != actionOpened && action != actionLabeled && action != actionClosed {
switch action {
case actionOpened,
actionReopened,
actionLabeled,
actionClosed:
default:
return
}

Expand Down Expand Up @@ -387,6 +392,16 @@ func (p *Plugin) postPullRequestEvent(event *github.PullRequestEvent) {
post.Message = p.sanitizeDescription(newPRMessage)
}

if action == actionReopened {
reopenedPRMessage, err := renderTemplate("reopenedPR", event)
if err != nil {
p.client.Log.Warn("Failed to render template", "error", err.Error())
return
}

post.Message = p.sanitizeDescription(reopenedPRMessage)
}

if action == actionClosed {
post.Message = closedPRMessage
}
Expand Down

0 comments on commit 7c47f89

Please sign in to comment.