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

notify: support subscriber threshold to prevent broad spamming #18

Merged
merged 4 commits into from
Apr 29, 2022
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
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