Repository: mattermost/mattermost-plugin-github
Pull request #317 (2020-07-02 11:16:01)

send notification when issue gets reopened
#### Summary
Send notification when issue gets reopened

#### Ticket Link
Fixes https://github.com/mattermost/mattermost-plugin-github/issues/267


codecov-commenter: # [Codecov](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317?src=pr&el=h1) Report
> Merging [#317](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317?src=pr&el=desc) into [master](https://codecov.io/gh/mattermost/mattermost-plugin-github/commit/63ef1c07e952d881d9bab72d2edbda016acd1425&el=desc) will **increase** coverage by `0.05%`.
> The diff coverage is `30.00%`.

[![Impacted file tree graph](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317/graphs/tree.svg?width=650&height=150&src=pr&token=0uHZTjTrgl)](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #317      +/-   ##
==========================================
+ Coverage   18.69%   18.75%   +0.05%     
==========================================
  Files          10       10              
  Lines        2455     2464       +9     
==========================================
+ Hits          459      462       +3     
- Misses       1971     1977       +6     
  Partials       25       25              
```


| [Impacted Files](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [server/webhook.go](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317/diff?src=pr&el=tree#diff-c2VydmVyL3dlYmhvb2suZ28=) | `0.00% <0.00%> (ø)` | |
| [server/template.go](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317/diff?src=pr&el=tree#diff-c2VydmVyL3RlbXBsYXRlLmdv) | `96.11% <100.00%> (+0.05%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317?src=pr&el=footer). Last update [63ef1c0...0098b2e](https://codecov.io/gh/mattermost/mattermost-plugin-github/pull/317?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/server/plugin/template.go b/server/plugin/template.go
index 788f4e6..ddb549c 100644
--- a/server/plugin/template.go
+++ b/server/plugin/template.go
@@ -190,6 +190,10 @@ func init() {
 #### {{.GetIssue.GetTitle}}
 ##### {{template "eventRepoIssue" .}}
 #issue-labeled ` + "`{{.GetLabel.GetName}}`" + ` by {{template "user" .GetSender}}.
+`))
+
+	template.Must(masterTemplate.New("reopenedIssue").Funcs(funcMap).Parse(`
+{{template "repo" .GetRepo}} Issue {{template "issue" .GetIssue}} reopened by {{template "user" .GetSender}}.
 `))
 
 	template.Must(masterTemplate.New("pushedCommits").Funcs(funcMap).Parse(`
diff --git a/server/plugin/webhook.go b/server/plugin/webhook.go
index a47805f..88abfa1 100644
--- a/server/plugin/webhook.go
+++ b/server/plugin/webhook.go
@@ -344,41 +344,49 @@ func (p *Plugin) handlePRDescriptionMentionNotification(event *github.PullReques
 func (p *Plugin) postIssueEvent(event *github.IssuesEvent) {
 	repo := event.GetRepo()
 
-	subs := p.GetSubscribedChannelsForRepository(repo)
-	if len(subs) == 0 {
+	subscribedChannels := p.GetSubscribedChannelsForRepository(repo)
+	if len(subscribedChannels) == 0 {
 		return
 	}
 
 	action := event.GetAction()
-	if action != "opened" && action != "labeled" && action != "closed" {
-		return
-	}
+	issueTemplate := ""
+	switch action {
+	case "opened":
+		issueTemplate = "newIssue"
 
-	issue := event.GetIssue()
-	eventLabel := event.GetLabel().GetName()
-	labels := make([]string, len(issue.Labels))
-	for i, v := range issue.Labels {
-		labels[i] = v.GetName()
-	}
+	case "closed":
+		issueTemplate = "closedIssue"
 
-	newIssueMessage, err := renderTemplate("newIssue", event)
-	if err != nil {
-		mlog.Error("failed to render template", mlog.Err(err))
+	case "reopened":
+		issueTemplate = "reopenedIssue"
+
+	case "labeled":
+		issueTemplate = "issueLabelled"
+
+	default:
 		return
 	}
 
-	closedIssueMessage, err := renderTemplate("closedIssue", event)
+	renderedMessage, err := renderTemplate(issueTemplate, event)
 	if err != nil {
 		mlog.Error("failed to render template", mlog.Err(err))
 		return
 	}
-
 	post := &model.Post{
-		UserId: p.BotUserID,
-		Type:   "custom_git_issue",
+		UserId:  p.BotUserID,
+		Type:    "custom_git_issue",
+		Message: renderedMessage,
 	}
 
-	for _, sub := range subs {
+	issue := event.GetIssue()
+	eventLabel := event.GetLabel().GetName()
+	labels := make([]string, len(issue.Labels))
+	for i, v := range issue.Labels {
+		labels[i] = v.GetName()
+	}
+
+	for _, sub := range subscribedChannels {
 		if !sub.Issues() {
 			continue
 		}
@@ -401,27 +409,11 @@ func (p *Plugin) postIssueEvent(event *github.IssuesEvent) {
 		}
 
 		if action == "labeled" {
-			if label != "" && label == eventLabel {
-				issueLabelledMessage, err := renderTemplate("issueLabelled", event)
-				if err != nil {
-					mlog.Error("failed to render template", mlog.Err(err))
-					return
-				}
-
-				post.Message = issueLabelledMessage
-			} else {
+			if label == "" || label != eventLabel {
 				continue
 			}
 		}
 
-		if action == "opened" {
-			post.Message = newIssueMessage
-		}
-
-		if action == "closed" {
-			post.Message = closedIssueMessage
-		}
-
 		post.ChannelId = sub.ChannelID
 		if _, err := p.API.CreatePost(post); err != nil {
 			mlog.Error(err.Error())


Test patch:
diff --git a/server/plugin/template_test.go b/server/plugin/template_test.go
index 62da274..bdc9b5a 100644
--- a/server/plugin/template_test.go
+++ b/server/plugin/template_test.go
@@ -302,6 +302,20 @@ func TestClosedIssueTemplate(t *testing.T) {
 	require.Equal(t, expected, actual)
 }
 
+func TestReopenedIssueTemplate(t *testing.T) {
+	expected := `
+[\[mattermost-plugin-github\]](https://github.com/mattermost/mattermost-plugin-github) Issue [#1 Implement git-get-head](https://github.com/mattermost/mattermost-plugin-github/issues/1) reopened by [panda](https://github.com/panda).
+`
+
+	actual, err := renderTemplate("reopenedIssue", &github.IssuesEvent{
+		Repo:   &repo,
+		Issue:  &issue,
+		Sender: &user,
+	})
+	require.NoError(t, err)
+	require.Equal(t, expected, actual)
+}
+
 func TestIssueLabelledTemplate(t *testing.T) {
 	expected := `
 #### Implement git-get-head