Skip to content

Commit

Permalink
Ignore archived repos by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fejta committed Apr 21, 2019
1 parent a517de3 commit d82bc5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
21 changes: 16 additions & 5 deletions robots/commenter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package main

import (
"bytes"
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -62,6 +63,7 @@ func flagOptions() options {
}
flag.StringVar(&o.query, "query", "", "See https://help.github.com/articles/searching-issues-and-pull-requests/")
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.confirm, "confirm", false, "Mutate github if set")
flag.StringVar(&o.comment, "comment", "", "Append the following comment to matching issues")
Expand All @@ -86,6 +88,7 @@ type options struct {
asc bool
ceiling int
comment string
includeArchived bool
includeClosed bool
useTemplate bool
query string
Expand All @@ -112,15 +115,23 @@ func parseHTMLURL(url string) (string, string, int, error) {
return mat[1], mat[2], n, nil
}

func makeQuery(query string, includeClosed bool, minUpdated time.Duration) (string, error) {
func makeQuery(query string, includeArchived, includeClosed bool, minUpdated time.Duration) (string, error) {
parts := []string{query}
if !includeArchived {
if strings.Contains(query, "archived:true") {
return "", errors.New("archived:true requires --include-archived")
}
parts = append(parts, "archived:false")
} else if strings.Contains(query, "archived:false") {
return "", errors.New("archived:false conflicts with --include-archived")
}
if !includeClosed {
if strings.Contains(query, "is:closed") {
return "", fmt.Errorf("--query='%s' containing is:closed requires --include-closed", query)
return "", errors.New("is:closed requires --include-closed")
}
parts = append(parts, "is:open")
} else if strings.Contains(query, "is:open") {
return "", fmt.Errorf("--query='%s' should not contain is:open when using --include-closed", query)
return "", errors.New("is:open conflicts with --include-closed")
}
if minUpdated != 0 {
latest := time.Now().Add(-minUpdated)
Expand Down Expand Up @@ -168,9 +179,9 @@ func main() {
c = github.NewDryRunClient(secretAgent.GetTokenGenerator(o.token), o.graphqlEndpoint, o.endpoint.Strings()...)
}

query, err := makeQuery(o.query, o.includeClosed, o.updated)
query, err := makeQuery(o.query, o.includeArchived, o.includeClosed, o.updated)
if err != nil {
log.Fatalf("Bad query: %v", err)
log.Fatalf("Bad query %q: %v", o.query, err)
}
sort := ""
asc := false
Expand Down
25 changes: 22 additions & 3 deletions robots/commenter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestMakeQuery(t *testing.T) {
cases := []struct {
name string
query string
archived bool
closed bool
dur time.Duration
expected []string
Expand All @@ -102,16 +103,23 @@ func TestMakeQuery(t *testing.T) {
{
name: "basic query",
query: "hello world",
expected: []string{"hello world", "is:open"},
expected: []string{"hello world", "is:open", "archived:false"},
unexpected: []string{"updated:", "openhello", "worldis"},
},
{
name: "basic closed",
query: "hello world",
closed: true,
expected: []string{"hello world"},
expected: []string{"hello world", "archived:false"},
unexpected: []string{"is:open"},
},
{
name: "basic archived",
query: "hello world",
archived: true,
expected: []string{"hello world", "is:open"},
unexpected: []string{"archived:false"},
},
{
name: "basic duration",
query: "hello",
Expand All @@ -135,10 +143,21 @@ func TestMakeQuery(t *testing.T) {
query: "hello is:closed",
err: true,
},
{
name: "archived:false with include-archived errors",
query: "hello archived:false",
archived: true,
err: true,
},
{
name: "archived:true without includeArchived errors",
query: "hello archived:true",
err: true,
},
}

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

0 comments on commit d82bc5c

Please sign in to comment.