diff --git a/README.md b/README.md index 9885bd637..6f38feb8b 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ As a system admin, you must create a webhook for each organization you want to r - **Content Type:** `application/json` - **Secret:** the webhook secret you copied previously. 6. Select **Let me select individual events** for "Which events would you like to trigger this webhook?". -7. Select the following events: `Branch or Tag creation`, `Branch or Tag deletion`, `Issue comments`, `Issues`, `Pull requests`, `Pull request review`, `Pull request review comments`, `Pushes`, `Stars`. +7. Select the following events: `Branch or Tag creation`, `Branch or Tag deletion`, `Issue comments`, `Issues`, `Pull requests`, `Pull request review`, `Pull request review comments`, `Pushes`, `Stars`, `Releases`. 7. Hit **Add Webhook** to save it. If you have multiple organizations, repeat the process starting from step 3 to create a webhook for each organization. @@ -150,7 +150,7 @@ When you’ve tested the plugin and confirmed it’s working, notify your team s /github subscriptions add mattermost/mattermost-server --features issues,pulls,issue_comments,label:"Help Wanted" ``` - The following flags are supported: - - `--features`: comma-delimited list of one or more of: issues, pulls, pulls_merged, pulls_created, pushes, creates, deletes, issue_creations, issue_comments, pull_reviews, label:"labelname". Defaults to pulls,issues,creates,deletes. + - `--features`: comma-delimited list of one or more of: issues, pulls, pulls_merged, pulls_created, pushes, creates, deletes, issue_creations, issue_comments, pull_reviews, releases, label:"labelname". Defaults to pulls,issues,creates,deletes. - `--exclude-org-member`: events triggered by organization members will not be delivered. It will be locked to the organization provided in the plugin configuration and it will only work for users whose membership is public. Note that organization members and collaborators are not the same. - `--render-style`: notifications will be delivered in the specified style (for example, the body of a pull request will not be displayed). Supported values are `collapsed`, `skip-body` or `default` (same as omitting the flag). diff --git a/server/plugin/command.go b/server/plugin/command.go index 7fb317c0a..7996948bb 100644 --- a/server/plugin/command.go +++ b/server/plugin/command.go @@ -26,6 +26,7 @@ const ( featureIssueComments = "issue_comments" featurePullReviews = "pull_reviews" featureStars = "stars" + featureReleases = "releases" ) const ( @@ -44,6 +45,7 @@ var validFeatures = map[string]bool{ featureIssueComments: true, featurePullReviews: true, featureStars: true, + featureReleases: true, } type Features string @@ -894,7 +896,7 @@ func getAutocompleteData(config *Configuration) *model.AutocompleteData { subscriptionsAdd := model.NewAutocompleteData("add", "[owner/repo] [features] [flags]", "Subscribe the current channel to receive notifications about opened pull requests and issues for an organization or repository. [features] and [flags] are optional arguments") subscriptionsAdd.AddTextArgument("Owner/repo to subscribe to", "[owner/repo]", "") - subscriptionsAdd.AddNamedTextArgument("features", "Comma-delimited list of one or more of: issues, pulls, pulls_merged, pulls_created, pushes, creates, deletes, issue_creations, issue_comments, pull_reviews, label:\"\". Defaults to pulls,issues,creates,deletes", "", `/[^,-\s]+(,[^,-\s]+)*/`, false) + subscriptionsAdd.AddNamedTextArgument("features", "Comma-delimited list of one or more of: issues, pulls, pulls_merged, pulls_created, pushes, creates, deletes, issue_creations, issue_comments, pull_reviews, releases, label:\"\". Defaults to pulls,issues,creates,deletes", "", `/[^,-\s]+(,[^,-\s]+)*/`, false) if config.GitHubOrg != "" { subscriptionsAdd.AddNamedStaticListArgument("exclude-org-member", "Events triggered by organization members will not be delivered (the organization config should be set, otherwise this flag has not effect)", false, []model.AutocompleteListItem{ diff --git a/server/plugin/subscriptions.go b/server/plugin/subscriptions.go index d37c0e755..8b68f78a3 100644 --- a/server/plugin/subscriptions.go +++ b/server/plugin/subscriptions.go @@ -123,6 +123,10 @@ func (s *Subscription) Stars() bool { return strings.Contains(s.Features.String(), featureStars) } +func (s *Subscription) Release() bool { + return strings.Contains(s.Features.String(), featureReleases) +} + func (s *Subscription) Label() string { if !strings.Contains(s.Features.String(), "label:") { return "" diff --git a/server/plugin/template.go b/server/plugin/template.go index 82b47fd15..805e69e26 100644 --- a/server/plugin/template.go +++ b/server/plugin/template.go @@ -158,6 +158,11 @@ func init() { `[#{{.GetNumber}} {{.GetTitle}}]({{.GetHTMLURL}})`, )) + // The release links to the corresponding release. + template.Must(masterTemplate.New("release").Parse( + `[{{.GetTagName}}]({{.GetHTMLURL}})`, + )) + // The eventRepoIssue links to the corresponding issue. Note that, for some events, the // issue *is* a pull request, and so we still use .GetIssue and this template accordingly. template.Must(masterTemplate.New("eventRepoIssue").Parse( @@ -408,6 +413,7 @@ Assignees: {{range $i, $el := .Assignees -}} {{- if $i}}, {{end}}{{template "use " * `issue_comments` - includes new issue comments\n" + " * `issue_creations` - includes new issues only \n" + " * `pull_reviews` - includes pull request reviews\n" + + " * `releases` - includes release created and deleted\n" + " * `label:` - limit pull request and issue events to only this label. Must include `pulls` or `issues` in feature list when using a label.\n" + " * Defaults to `pulls,issues,creates,deletes`\n\n" + " * `--exclude-org-member` - events triggered by organization members will not be delivered (the GitHub organization config should be set, otherwise this flag has not effect)\n" + @@ -429,6 +435,12 @@ Assignees: {{range $i, $el := .Assignees -}} {{- if $i}}, {{end}}{{template "use {{- else }} unstarred {{- end }} by {{template "user" .GetSender}} It now has **{{.GetRepo.GetStargazersCount}}** stars.`)) + + template.Must(masterTemplate.New("newReleaseEvent").Funcs(funcMap).Parse(` +{{template "repo" .GetRepo}} {{template "user" .GetSender}} +{{- if eq .GetAction "created" }} created a release {{template "release" .GetRelease}} +{{- else if eq .GetAction "deleted" }} deleted a release {{template "release" .GetRelease}} +{{- end -}}`)) } func registerGitHubToUsernameMappingCallback(callback func(string) string) { diff --git a/server/plugin/template_test.go b/server/plugin/template_test.go index 929dd9a6b..5498a7c23 100644 --- a/server/plugin/template_test.go +++ b/server/plugin/template_test.go @@ -1407,6 +1407,42 @@ func TestPullRequestReviewNotification(t *testing.T) { })) } +func TestReleaseNotification(t *testing.T) { + t.Run("created", func(t *testing.T) { + expected := ` +[\[mattermost-plugin-github\]](https://github.com/mattermost/mattermost-plugin-github) [panda](https://github.com/panda) created a release [v0.0.1](https://github.com/mattermost/mattermost-plugin-github/releases/tag/v0.0.1)` + + actual, err := renderTemplate("newReleaseEvent", &github.ReleaseEvent{ + Repo: &repo, + Sender: &user, + Action: sToP(actionCreated), + Release: &github.RepositoryRelease{ + TagName: sToP("v0.0.1"), + HTMLURL: sToP("https://github.com/mattermost/mattermost-plugin-github/releases/tag/v0.0.1"), + }, + }) + require.NoError(t, err) + require.Equal(t, expected, actual) + }) + + t.Run("deleted", func(t *testing.T) { + expected := ` +[\[mattermost-plugin-github\]](https://github.com/mattermost/mattermost-plugin-github) [panda](https://github.com/panda) deleted a release [v0.0.1](https://github.com/mattermost/mattermost-plugin-github/releases/tag/v0.0.1)` + + actual, err := renderTemplate("newReleaseEvent", &github.ReleaseEvent{ + Repo: &repo, + Sender: &user, + Action: sToP(actionDeleted), + Release: &github.RepositoryRelease{ + TagName: sToP("v0.0.1"), + HTMLURL: sToP("https://github.com/mattermost/mattermost-plugin-github/releases/tag/v0.0.1"), + }, + }) + require.NoError(t, err) + require.Equal(t, expected, actual) + }) +} + func TestGitHubUsernameRegex(t *testing.T) { stringAndMatchMap := map[string]string{ // Contain valid usernames diff --git a/server/plugin/webhook.go b/server/plugin/webhook.go index bc2a05d2f..af707f655 100644 --- a/server/plugin/webhook.go +++ b/server/plugin/webhook.go @@ -282,6 +282,11 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) { handler = func() { p.postStarEvent(event) } + case *github.ReleaseEvent: + repo = event.GetRepo() + handler = func() { + p.postReleaseEvent(event) + } } if handler == nil { @@ -1377,3 +1382,39 @@ func (p *Plugin) postStarEvent(event *github.StarEvent) { } } } + +func (p *Plugin) postReleaseEvent(event *github.ReleaseEvent) { + if event.GetAction() != actionCreated && event.GetAction() != actionDeleted { + return + } + + repo := event.GetRepo() + subs := p.GetSubscribedChannelsForRepository(repo) + + if len(subs) == 0 { + return + } + + newReleaseMessage, err := renderTemplate("newReleaseEvent", event) + if err != nil { + p.client.Log.Warn("Failed to render template", "Error", err.Error()) + return + } + + for _, sub := range subs { + if !sub.Release() { + continue + } + + post := &model.Post{ + UserId: p.BotUserID, + Type: "custom_git_release", + Message: newReleaseMessage, + ChannelId: sub.ChannelID, + } + + if err = p.client.Post.CreatePost(post); err != nil { + p.client.Log.Warn("Error webhook post", "Post", post, "Error", err.Error()) + } + } +}