From 8e7ba907b195bf03f8ccb0ecb4d0595a740c93be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Mon, 9 Mar 2020 18:50:23 -0600 Subject: [PATCH] Add filter issues by author --- api/queries_issue.go | 12 +++++++++--- api/queries_issue_test.go | 2 +- command/issue.go | 8 +++++++- command/issue_test.go | 4 +++- test/fixtures/issueList.json | 9 +++++++++ 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/api/queries_issue.go b/api/queries_issue.go index 2c755b1d3..e0de92841 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -55,6 +55,9 @@ const fragments = ` } totalCount } + author { + login + } } ` @@ -171,7 +174,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) return &payload, nil } -func IssueList(client *Client, repo ghrepo.Interface, state string, labels []string, assigneeString string, limit int) ([]Issue, error) { +func IssueList(client *Client, repo ghrepo.Interface, state string, labels []string, assigneeString string, limit int, authorString string) ([]Issue, error) { var states []string switch state { case "open", "": @@ -185,10 +188,10 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str } query := fragments + ` - query($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String) { + query($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String, $author: String) { repository(owner: $owner, name: $repo) { hasIssuesEnabled - issues(first: $limit, after: $endCursor, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) { + issues(first: $limit, after: $endCursor, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee, createdBy: $author}) { nodes { ...issue } @@ -212,6 +215,9 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str if assigneeString != "" { variables["assignee"] = assigneeString } + if authorString != "" { + variables["author"] = authorString + } var response struct { Repository struct { diff --git a/api/queries_issue_test.go b/api/queries_issue_test.go index bde6b8696..c9db7ead6 100644 --- a/api/queries_issue_test.go +++ b/api/queries_issue_test.go @@ -38,7 +38,7 @@ func TestIssueList(t *testing.T) { } } } `)) - _, err := IssueList(client, ghrepo.FromFullName("OWNER/REPO"), "open", []string{}, "", 251) + _, err := IssueList(client, ghrepo.FromFullName("OWNER/REPO"), "open", []string{}, "", 251, "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/command/issue.go b/command/issue.go index 4fc6d5ab2..8d83e392b 100644 --- a/command/issue.go +++ b/command/issue.go @@ -37,6 +37,7 @@ func init() { issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label") issueListCmd.Flags().StringP("state", "s", "", "Filter by state: {open|closed|all}") issueListCmd.Flags().IntP("limit", "L", 30, "Maximum number of issues to fetch") + issueListCmd.Flags().StringP("author", "A", "", "Filter by author") issueViewCmd.Flags().BoolP("preview", "p", false, "Display preview of issue content") } @@ -109,9 +110,14 @@ func issueList(cmd *cobra.Command, args []string) error { return err } + author, err := cmd.Flags().GetString("author") + if err != nil { + return err + } + fmt.Fprintf(colorableErr(cmd), "\nIssues for %s\n\n", ghrepo.FullName(baseRepo)) - issues, err := api.IssueList(apiClient, baseRepo, state, labels, assignee, limit) + issues, err := api.IssueList(apiClient, baseRepo, state, labels, assignee, limit, author) if err != nil { return err } diff --git a/command/issue_test.go b/command/issue_test.go index f5f8854f2..a4de959e1 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -136,7 +136,7 @@ func TestIssueList_withFlags(t *testing.T) { } } } `)) - output, err := RunCommand(issueListCmd, "issue list -a probablyCher -l web,bug -s open") + output, err := RunCommand(issueListCmd, "issue list -a probablyCher -l web,bug -s open -A foo") if err != nil { t.Errorf("error running command `issue list`: %v", err) } @@ -154,6 +154,7 @@ No issues match your search Assignee string Labels []string States []string + Author string } }{} json.Unmarshal(bodyBytes, &reqBody) @@ -161,6 +162,7 @@ No issues match your search eq(t, reqBody.Variables.Assignee, "probablyCher") eq(t, reqBody.Variables.Labels, []string{"web", "bug"}) eq(t, reqBody.Variables.States, []string{"OPEN"}) + eq(t, reqBody.Variables.Author, "foo") } func TestIssueList_nullAssigneeLabels(t *testing.T) { diff --git a/test/fixtures/issueList.json b/test/fixtures/issueList.json index 40537f12d..7357d3ca4 100644 --- a/test/fixtures/issueList.json +++ b/test/fixtures/issueList.json @@ -15,6 +15,9 @@ } ], "totalCount": 1 + }, + "author": { + "login": "foo" } }, { @@ -28,6 +31,9 @@ } ], "totalCount": 1 + }, + "author": { + "login": "bar" } }, { @@ -41,6 +47,9 @@ } ], "totalCount": 1 + }, + "author": { + "login": "bar" } } ]