From 57afc2e69fd8fcbd32592f4e145f3fe20d3a8e69 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 13 Nov 2019 13:55:24 -0800 Subject: [PATCH] Add assignee --- api/queries_issue.go | 27 +++++++++++++++++++++------ command/issue.go | 18 ++++++++++++++---- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/api/queries_issue.go b/api/queries_issue.go index 96d0e1b2b..0437c7ec7 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -147,7 +147,7 @@ func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPa return &payload, nil } -func IssueList(client *Client, ghRepo Repo, state string) ([]Issue, error) { +func IssueList(client *Client, ghRepo Repo, state string, labels []string, assigneeString string) ([]Issue, error) { var states []string switch state { case "open", "": @@ -160,14 +160,27 @@ func IssueList(client *Client, ghRepo Repo, state string) ([]Issue, error) { return nil, fmt.Errorf("invalid state: %s", state) } + // If you don't want to filter by lables, graphql requires you need + // to send nil instead of an empty array. + if len(labels) == 0 { + labels = nil + } + + var assignee interface{} + if len(assigneeString) > 0 { + assignee = assigneeString + } else { + assignee = nil + } + query := ` fragment issue on Issue { number title } - query($owner: String!, $repo: String!, $per_page: Int = 10, $states: [IssueState!] = OPEN) { + query($owner: String!, $repo: String!, $per_page: Int = 10, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String) { repository(owner: $owner, name: $repo) { - issues(first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}, states: $states) { + issues(first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) { edges { node { ...issue @@ -181,9 +194,11 @@ func IssueList(client *Client, ghRepo Repo, state string) ([]Issue, error) { owner := ghRepo.RepoOwner() repo := ghRepo.RepoName() variables := map[string]interface{}{ - "owner": owner, - "repo": repo, - "states": states, + "owner": owner, + "repo": repo, + "states": states, + "labels": labels, + "assignee": assignee, } var resp struct { diff --git a/command/issue.go b/command/issue.go index ac7ccbf97..acce47dd7 100644 --- a/command/issue.go +++ b/command/issue.go @@ -37,9 +37,9 @@ func init() { Short: "List open issues", RunE: issueList, } - issueListCmd.Flags().StringP("assignee", "a", "", "filter by assignee") - issueListCmd.Flags().StringP("label", "l", "", "Filter by assignee") - issueListCmd.Flags().StringP("state", "s", "", "Filter by state") + issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") + issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by labels ") + issueListCmd.Flags().StringP("state", "s", "", "Filter by state (open, closed or all)") issueCmd.AddCommand((issueListCmd)) } @@ -71,7 +71,17 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - issues, err := api.IssueList(apiClient, baseRepo, state) + labels, err := cmd.Flags().GetStringSlice("label") + if err != nil { + return err + } + + assignee, err := cmd.Flags().GetString("assignee") + if err != nil { + return err + } + + issues, err := api.IssueList(apiClient, baseRepo, state, labels, assignee) if err != nil { return err }