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-352] Add feature to publish release create and delete events #762

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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`.
Copy link
Contributor

Choose a reason for hiding this comment

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

The changelog should mention that GH admins need to update their hooks.

@ayusht2810 Do you have a suggestion where we could store that notice?

Copy link
Contributor Author

@ayusht2810 ayusht2810 Apr 8, 2024

Choose a reason for hiding this comment

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

@hanzei We can manually update release notes to have that changelog or create a separate PR for this change to have a changelog for it. What is your opinion on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Release notes are essentially the changelog for plugin releases

Copy link
Contributor

Choose a reason for hiding this comment

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

We have Branch or Tag creation already. Isn't that about the same as releases? I'm actually not sure if that tag creation feature works properly

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}}
Comment on lines +441 to +442
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any other actions besides these two?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes the other events are:

  • Release created
  • Release edited
  • Release published
  • Release unpublished
  • Release deleted

{{- 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())
}
}
}
Loading