Skip to content

Commit 20d4c24

Browse files
committed
Refactor rejection logic to make all fields optional
1 parent afd59fd commit 20d4c24

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

main.go

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ type config struct {
9898
GenericWebhookURLs []string `yaml:"generic_webhook_urls"`
9999
}
100100

101-
// RejectionCondition contains the fields that should match a bug report for it to be rejected.
102-
// The condition matches a specific app, with optionall version and label, or the user submitted text
101+
// RejectionCondition contains the fields that can match a bug report for it to be rejected.
103102
type RejectionCondition struct {
104103
// If a payload is not a UserTextMatch and does not match this app name, the condition does not match.
105104
App string `yaml:"app"`
@@ -108,54 +107,47 @@ type RejectionCondition struct {
108107
// Optional: label that must also match in addition to the app and version. If empty, does not check label.
109108
Label string `yaml:"label"`
110109
// Alternative to matching app names, match the content of the user text
111-
UserTextMatch string `yaml:"usertext"`
110+
UserTextMatch string `yaml:"usertext"`
111+
// Send this text to the client-side to inform the user why the server rejects the rageshake
112112
UserTextMatchReason string `yaml:"reason"`
113113
}
114114

115-
// shouldReject returns true in two situations:
116-
// - if the app name AND version AND labels all match the rejection condition.
117-
// - if the text provided by the user matches a regex specified in the rejection condition
118-
//
119-
// If any one of these do not match the condition, it is not rejected.
115+
// shouldReject returns true if all the App, Version, Label and UserTextMatch attributes of a RejectionCondition match
120116
func (c RejectionCondition) shouldReject(appName, version string, labels []string, userText string) (reject bool, reason *string) {
121-
if c.App != "" {
122-
if appName != c.App {
123-
return false, nil
124-
}
125-
// version was a condition and it doesn't match => accept it
126-
if version != c.Version && c.Version != "" {
127-
return false, nil
128-
}
129-
// label was a condition and no label matches it => accept it
130-
if c.Label != "" {
131-
labelMatch := false
132-
for _, l := range labels {
133-
if l == c.Label {
134-
labelMatch = true
135-
break
136-
}
137-
}
138-
if !labelMatch {
139-
return false, nil
117+
// Reject by default and then accept as soon as one condition does not match
118+
reject = true
119+
defaultReason := "app, version, labels, user text"
120+
// Check the attribute of the RejectionCondition if it is not empty
121+
if c.App != "" && c.App != appName {
122+
reject = reject && false
123+
}
124+
if c.Version != "" && c.Version != version {
125+
reject = reject && false
126+
}
127+
if c.Label != "" {
128+
labelMatch := false
129+
for _, l := range labels {
130+
if l == c.Label {
131+
labelMatch = true
132+
break
140133
}
141134
}
142-
reason := "this application is not allowed to send rageshakes to this server"
143-
return true, &reason
135+
if !labelMatch {
136+
reject = reject && false
137+
}
144138
}
145139
if c.UserTextMatch != "" {
146140
var userTextRegexp = regexp.MustCompile(c.UserTextMatch)
147-
if userTextRegexp.MatchString(userText) {
148-
fmt.Println("Match:", c.UserTextMatch)
149-
fmt.Println("Reason:", c.UserTextMatchReason)
150-
reason := c.UserTextMatchReason
151-
if c.UserTextMatchReason == "" {
152-
reason = "user text"
153-
}
154-
return true, &reason
141+
if !userTextRegexp.MatchString(userText) {
142+
reject = reject && false
155143
}
156144
}
157-
// Nothing matches
158-
return false, nil
145+
if c.UserTextMatchReason != "" {
146+
reason = &c.UserTextMatchReason
147+
} else {
148+
reason = &defaultReason
149+
}
150+
return
159151
}
160152

161153
func (c *config) matchesRejectionCondition(p *payload) (match bool, reason *string) {

rageshake.sample.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ listings_auth_pass: secret
1313
allowed_app_names: []
1414

1515
# If any submission matches one of these rejection conditions, the submission is rejected.
16-
# You can either match an 'app' name, with optional 'version' and 'label' or the 'usertext' submitted by the user when describing their issue.
17-
#
18-
# Adding an 'app' item will reject that app altogether, effectively acting as a blocklist for app in contrast to allowed_app_names. You can optionally add 'version' and 'label' to fine-tune the rejection condition
19-
#
20-
# 'usertext' is a regex to reject user descriptions with an optional 'reason' text to send to the client
16+
# A condition is made by an union of optional fields: app, version, labels, user text. They all need to match for rejecting the rageshake
17+
# It can also contain an optional reason to explain why this server is rejecting a user's submission.
2118
rejection_conditions:
2219
- app: my-app
2320
version: "0.4.9" # if the submission has a Version which is exactly this value, reject the submission.
@@ -26,6 +23,7 @@ rejection_conditions:
2623
- app: my-app
2724
version: "0.4.9"
2825
label: "nightly" # both label and Version must match for this condition to be true
26+
reason: "this server does not accept rageshakes from nightlies"
2927
- usertext: "(\\w{4}\\s){11}\\w{4}" # reject text containing possible recovery keys
3028
reason: "it matches a recovery key and recovery keys are private"
3129

0 commit comments

Comments
 (0)