Skip to content

Commit

Permalink
notify: support subscriber threshold to prevent broad spamming (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Snyder <[email protected]>
  • Loading branch information
unknwon and nicksnyder committed Apr 29, 2022
1 parent bbd530a commit cf059c5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/codenotify
/coverage.txt
.idea
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ jobs:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: sourcegraph/codenotify@v0.4
- uses: sourcegraph/codenotify@v0.5
env:
# secrets.GITHUB_TOKEN is available by default, but it won't allow CODENOTIFY to mention GitHub teams.
# If you want CODENOTIFY to be able to mention teams, then you need to create a personal access token
# (https://github.com/settings/tokens) with scopes: repo, read:org.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# # Filename in which file subscribers are defined, default is 'CODENOTIFY'
# filename: 'CODENOTIFY'
# # The threshold of notifying subscribers to prevent broad spamming, 0 to disable (default)
# subscriber-threshold: '10'
```

## CODENOTIFY files
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ inputs:
description: 'Filename in which file subscribers are defined'
required: false
default: 'CODENOTIFY'
subscriber-threshold:
description: 'The threshold of notifying subscribers to prevent broad spamming, 0 to disable'
required: false
default: '0'
runs:
using: 'docker'
image: 'Dockerfile'
Expand Down
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func cliOptions(stdout io.Writer, args []string) (*options, error) {
flags.StringVar(&opts.headRef, "headRef", "HEAD", "The head ref to use when computing the file diff.")
flags.StringVar(&opts.format, "format", "text", "The format of the output: text or markdown")
flags.StringVar(&opts.filename, "filename", "CODENOTIFY", "The filename in which file subscribers are defined")
flags.IntVar(&opts.subscriberThreshold, "subscriber-threshold", 0, "The threshold of notifying subscribers")
v := *flags.Bool("verbose", false, "Verbose messages printed to stderr")

if v {
Expand Down Expand Up @@ -370,21 +371,27 @@ func graphql(query string, variables map[string]interface{}, responseData interf
}

type options struct {
cwd string
baseRef string
headRef string
format string
filename string
author string
print func(notifs map[string][]string) error
cwd string
baseRef string
headRef string
format string
filename string
subscriberThreshold int
author string
print func(notifs map[string][]string) error
}

func markdownCommentTitle(filename string) string {
return fmt.Sprintf("<!-- codenotify:%s report -->\n", filename)
}

func (o *options) writeNotifications(w io.Writer, notifs map[string][]string) error {
subs := []string{}
if o.subscriberThreshold > 0 && len(notifs) > o.subscriberThreshold {
fmt.Fprintf(w, "Not notifying subscribers because the number of notifying subscribers (%d) has exceeded the threshold (%d).\n", len(notifs), o.subscriberThreshold)
return nil
}

subs := make([]string, 0, len(notifs))
for sub := range notifs {
subs = append(subs, sub)
}
Expand Down
13 changes: 13 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ func TestWriteNotifications(t *testing.T) {
},
err: "unsupported format: pdf",
},
{
name: "exceeded subscriber threshold",
opts: options{
subscriberThreshold: 1,
},
notifs: map[string][]string{
"@go": {"file.go", "dir/file.go"},
"@js": {"file.js", "dir/file.js"},
},
output: []string{
"Not notifying subscribers because the number of notifying subscribers (2) has exceeded the threshold (1).",
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit cf059c5

Please sign in to comment.