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

robots/commenter: add --include-locked flag #23029

Merged
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
14 changes: 12 additions & 2 deletions robots/commenter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func flagOptions() options {
flag.DurationVar(&o.updated, "updated", 2*time.Hour, "Filter to issues unmodified for at least this long if set")
flag.BoolVar(&o.includeArchived, "include-archived", false, "Match archived issues if set")
flag.BoolVar(&o.includeClosed, "include-closed", false, "Match closed issues if set")
flag.BoolVar(&o.includeLocked, "include-locked", false, "Match locked issues if set")
flag.BoolVar(&o.confirm, "confirm", false, "Mutate github if set")
flag.StringVar(&o.comment, "comment", "", "Append the following comment to matching issues")
flag.BoolVar(&o.useTemplate, "template", false, templateHelp)
Expand All @@ -90,6 +91,7 @@ type options struct {
comment string
includeArchived bool
includeClosed bool
includeLocked bool
useTemplate bool
query string
sort string
Expand All @@ -115,7 +117,7 @@ func parseHTMLURL(url string) (string, string, int, error) {
return mat[1], mat[2], n, nil
}

func makeQuery(query string, includeArchived, includeClosed bool, minUpdated time.Duration) (string, error) {
func makeQuery(query string, includeArchived, includeClosed, includeLocked bool, minUpdated time.Duration) (string, error) {
// GitHub used to allow \n but changed it at some point to result in no results at all
query = strings.ReplaceAll(query, "\n", " ")
parts := []string{query}
Expand All @@ -135,6 +137,14 @@ func makeQuery(query string, includeArchived, includeClosed bool, minUpdated tim
} else if strings.Contains(query, "is:open") {
return "", errors.New("is:open conflicts with --include-closed")
}
if !includeLocked {
if strings.Contains(query, "is:locked") {
return "", errors.New("is:locked requires --include-locked")
}
parts = append(parts, "is:unlocked")
} else if strings.Contains(query, "is:unlocked") {
return "", errors.New("is:unlocked conflicts with --include-locked")
}
if minUpdated != 0 {
latest := time.Now().Add(-minUpdated)
parts = append(parts, "updated:<="+latest.Format(time.RFC3339))
Expand Down Expand Up @@ -181,7 +191,7 @@ func main() {
c = github.NewDryRunClient(secretAgent.GetTokenGenerator(o.token), secretAgent.Censor, o.graphqlEndpoint, o.endpoint.Strings()...)
}

query, err := makeQuery(o.query, o.includeArchived, o.includeClosed, o.updated)
query, err := makeQuery(o.query, o.includeArchived, o.includeClosed, o.includeLocked, o.updated)
if err != nil {
log.Fatalf("Bad query %q: %v", o.query, err)
}
Expand Down
41 changes: 30 additions & 11 deletions robots/commenter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestMakeQuery(t *testing.T) {
query string
archived bool
closed bool
locked bool
dur time.Duration
expected []string
unexpected []string
Expand All @@ -106,19 +107,26 @@ func TestMakeQuery(t *testing.T) {
expected: []string{"hello world", "is:open", "archived:false"},
unexpected: []string{"updated:", "openhello", "worldis"},
},
{
name: "basic archived",
query: "hello world",
archived: true,
expected: []string{"hello world", "is:open", "is:unlocked"},
unexpected: []string{"archived:false"},
},
{
name: "basic closed",
query: "hello world",
closed: true,
expected: []string{"hello world", "archived:false"},
expected: []string{"hello world", "archived:false", "is:unlocked"},
unexpected: []string{"is:open"},
},
{
name: "basic archived",
name: "basic locked",
query: "hello world",
archived: true,
expected: []string{"hello world", "is:open"},
unexpected: []string{"archived:false"},
locked: true,
expected: []string{"hello world", "is:open", "archived:false"},
unexpected: []string{"is:unlocked"},
},
{
name: "basic duration",
Expand All @@ -143,11 +151,6 @@ func TestMakeQuery(t *testing.T) {
closed: true,
err: true,
},
{
name: "is:closed without includeClosed",
query: "hello is:closed",
err: true,
},
{
name: "archived:false with include-archived errors",
query: "hello archived:false",
Expand All @@ -159,10 +162,26 @@ func TestMakeQuery(t *testing.T) {
query: "hello archived:true",
err: true,
},
{
name: "is:closed without includeClosed errors",
query: "hello is:closed",
err: true,
},
{
name: "is:locked without includeLocked errors",
query: "hello is:locked",
err: true,
},
{
name: "is:unlocked with includeLocked errors",
query: "hello is:unlocked",
locked: true,
err: true,
},
}

for _, tc := range cases {
actual, err := makeQuery(tc.query, tc.archived, tc.closed, tc.dur)
actual, err := makeQuery(tc.query, tc.archived, tc.closed, tc.locked, tc.dur)
if err != nil && !tc.err {
t.Errorf("%s: unexpected error: %v", tc.name, err)
} else if err == nil && tc.err {
Expand Down