total match count in title

This commit is contained in:
UmairShahzad 2020-02-20 01:54:51 +05:00
parent 94ef4bb377
commit bb0fe46db6
7 changed files with 78 additions and 63 deletions

View file

@ -171,7 +171,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) (*IssuesAndTotalCount, error) {
var states []string
switch state {
case "open", "":
@ -189,7 +189,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
repository(owner: $owner, name: $repo) {
hasIssuesEnabled
issues(first: $limit, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) {
nodes {
totalCount
nodes {
...issue
}
}
@ -213,7 +214,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
var resp struct {
Repository struct {
Issues struct {
Nodes []Issue
Nodes []Issue
TotalCount int
}
HasIssuesEnabled bool
}
@ -228,7 +230,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo))
}
return resp.Repository.Issues.Nodes, nil
res := IssuesAndTotalCount{Issues: resp.Repository.Issues.Nodes, TotalCount: resp.Repository.Issues.TotalCount}
return &res, nil
}
func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, error) {

View file

@ -432,7 +432,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
return &result.CreatePullRequest.PullRequest, nil
}
func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]PullRequest, error) {
func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) {
type prBlock struct {
Edges []struct {
Node PullRequest
@ -441,6 +441,8 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
HasNextPage bool
EndCursor string
}
TotalCount int
IssueCount int
}
type response struct {
Repository struct {
@ -483,23 +485,25 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
first: $limit,
after: $endCursor,
orderBy: {field: CREATED_AT, direction: DESC}
) {
edges {
node {
...pr
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
) {
totalCount
edges {
node {
...pr
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`
prs := []PullRequest{}
pageLimit := min(limit, 100)
variables := map[string]interface{}{}
res := PullRequestAndTotalCount{}
// If assignee was specified, use the `search` API rather than
// `Repository.pullRequests`, but this mode doesn't support multiple labels
@ -511,6 +515,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
$endCursor: String,
) {
search(query: $q, type: ISSUE, first: $limit, after: $endCursor) {
issueCount
edges {
node {
...pr
@ -564,8 +569,10 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
return nil, err
}
prData := data.Repository.PullRequests
res.TotalCount = prData.TotalCount
if _, ok := variables["q"]; ok {
prData = data.Search
res.TotalCount = prData.IssueCount
}
for _, edge := range prData.Edges {
@ -583,8 +590,8 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
done:
break
}
return prs, nil
res.PullRequests = prs
return &res, nil
}
func min(a, b int) int {

View file

@ -16,7 +16,6 @@ import (
"github.com/cli/cli/pkg/githubtemplate"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func init() {
@ -108,31 +107,15 @@ func issueList(cmd *cobra.Command, args []string) error {
return err
}
issues, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit)
issuesData, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit)
if err != nil {
return err
}
issues := issuesData.Issues
issueCount := issuesData.TotalCount
title := func (msg string) string {
return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo))
}
if len(issues) == 0 {
colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open issues" message is actually an issue
msg := "There are no open issues"
userSetFlags := false
cmd.Flags().Visit(func(f *pflag.Flag) {
userSetFlags = true
})
if userSetFlags {
msg = "No issues match your search"
}
fmt.Fprintf(colorErr, title(msg))
return nil
}
fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(issues), "issue")))
title := utils.GetTitle(cmd, "issue", limit, issueCount, baseRepo)
fmt.Fprintf(colorableErr(cmd), title)
out := cmd.OutOrStdout()
table := utils.NewTablePrinter(out)

View file

@ -13,7 +13,6 @@ import (
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func init() {
@ -135,7 +134,6 @@ func prList(cmd *cobra.Command, args []string) error {
return err
}
limit, err := cmd.Flags().GetInt("limit")
if err != nil {
return err
@ -186,32 +184,17 @@ func prList(cmd *cobra.Command, args []string) error {
params["assignee"] = assignee
}
prs, err := api.PullRequestList(apiClient, params, limit)
prsData, err := api.PullRequestList(apiClient, params, limit)
if err != nil {
return err
}
title := func (msg string) string {
return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo))
}
prs := prsData.PullRequests
prCount := prsData.TotalCount
if len(prs) == 0 {
colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open prs" message is acually a pr
msg := "There are no open pull requests"
title := utils.GetTitle(cmd, "pull request", limit, prCount, baseRepo)
fmt.Fprintf(colorableErr(cmd), title)
userSetFlags := false
cmd.Flags().Visit(func(f *pflag.Flag) {
userSetFlags = true
})
if userSetFlags {
msg = "No pull requests match your search"
}
fmt.Fprintf(colorErr, title(msg))
return nil
}
fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(prs), "pull request")))
table := utils.NewTablePrinter(cmd.OutOrStdout())
for _, pr := range prs {
prNum := strconv.Itoa(pr.Number)

View file

@ -3,6 +3,7 @@
"repository": {
"hasIssuesEnabled": true,
"issues": {
"totalCount": 3,
"nodes": [
{
"number": 1,

View file

@ -2,6 +2,7 @@
"data": {
"repository": {
"pullRequests": {
"totalCount": 3,
"edges": [
{
"node": {

View file

@ -5,7 +5,10 @@ import (
"fmt"
"time"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/browser"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
md "github.com/vilmibm/go-termd"
)
@ -77,3 +80,37 @@ func FuzzyAgo(ago time.Duration) string {
return fmtDuration(int(ago.Hours()/24/365), "year")
}
func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo *ghrepo.Interface) string {
userSetFlagCounter := 0
limitSet := false
cmd.Flags().Visit(func(f *pflag.Flag) {
userSetFlagCounter += 1
if f.Name == "limit" {
limitSet = true
}
})
title := "\n%s in %s\n\n"
if matchCount == 0 {
msg := fmt.Sprintf("There are no open %ss", cmdType)
if userSetFlagCounter > 0 {
msg = fmt.Sprintf("No %ss match your search", cmdType)
}
return fmt.Sprintf(title, msg, ghrepo.FullName(*baseRepo))
}
if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) {
title = "\n%s match your search in %s\n\n"
}
out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(*baseRepo))
if limit < matchCount {
out = out + fmt.Sprintln(Gray(fmt.Sprintf("Showing %d/%d results\n", limit, matchCount)))
}
return out
}