Skip to content

Commit

Permalink
[MM-352] Add feature to publish release create and delete events (#762)
Browse files Browse the repository at this point in the history
* [MM-352] Add feature to publish release create and delete events

* [MM-352] Update readme for updated webhook events

* [MM-352] Update logic of creating post for the release events

* [MM-352] Update indentation in template

* [MM-352] Add release event in command help text
  • Loading branch information
ayusht2810 authored May 14, 2024
1 parent 936e5c3 commit 2056a77
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 3 additions & 1 deletion server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
featureIssueComments = "issue_comments"
featurePullReviews = "pull_reviews"
featureStars = "stars"
featureReleases = "releases"
)

const (
Expand All @@ -44,6 +45,7 @@ var validFeatures = map[string]bool{
featureIssueComments: true,
featurePullReviews: true,
featureStars: true,
featureReleases: true,
}

type Features string
Expand Down Expand Up @@ -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:\"<labelname>\". 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:\"<labelname>\". 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{
Expand Down
4 changes: 4 additions & 0 deletions server/plugin/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
12 changes: 12 additions & 0 deletions server/plugin/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:<labelname>` - 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" +
Expand All @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions server/plugin/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions server/plugin/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}
}

0 comments on commit 2056a77

Please sign in to comment.