From f5d07906190a7c3f6e5346a8b0e2ae6d871d00b9 Mon Sep 17 00:00:00 2001 From: Norwin Date: Sun, 29 Mar 2020 13:16:06 +0000 Subject: [PATCH] Add --state flag filter to issue & PR lists (#100) Add --state flag filter to issue & PR lists Co-authored-by: Lunny Xiao Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/100 Reviewed-by: Lunny Xiao Reviewed-by: Andrew Thornton Reviewed-by: 6543 <6543@noreply.gitea.io> --- cmd/issues.go | 29 +++++++++++++++++++++++------ cmd/pulls.go | 24 +++++++++++++++++++++--- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/cmd/issues.go b/cmd/issues.go index 5fba5a9..836a642 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -7,7 +7,6 @@ package cmd import ( "fmt" "log" - "os" "strconv" "strings" @@ -35,12 +34,18 @@ var CmdIssuesList = cli.Command{ Usage: "List issues of the repository", Description: `List issues of the repository`, Action: runIssuesList, - Flags: AllDefaultFlags, + Flags: append([]cli.Flag{ + &cli.StringFlag{ + Name: "state", + Usage: "Filter by issue state (all|open|closed)", + DefaultText: "open", + }, + }, AllDefaultFlags...), } func runIssues(ctx *cli.Context) error { - if len(os.Args) == 3 { - return runIssueDetail(ctx, os.Args[2]) + if ctx.Args().Len() == 1 { + return runIssueDetail(ctx, ctx.Args().First()) } return runIssuesList(ctx) } @@ -74,9 +79,19 @@ func runIssueDetail(ctx *cli.Context, index string) error { func runIssuesList(ctx *cli.Context) error { login, owner, repo := initCommand() + state := gitea.StateOpen + switch ctx.String("state") { + case "all": + state = gitea.StateAll + case "open": + state = gitea.StateOpen + case "closed": + state = gitea.StateClosed + } + issues, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{ Page: 0, - State: string(gitea.StateOpen), + State: string(state), }) if err != nil { @@ -85,7 +100,8 @@ func runIssuesList(ctx *cli.Context) error { headers := []string{ "Index", - "Name", + "State", + "Author", "Updated", "Title", } @@ -106,6 +122,7 @@ func runIssuesList(ctx *cli.Context) error { values, []string{ strconv.FormatInt(issue.Index, 10), + string(issue.State), name, issue.Updated.Format("2006-01-02 15:04:05"), issue.Title, diff --git a/cmd/pulls.go b/cmd/pulls.go index 50ffbe0..abcd570 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -19,15 +19,31 @@ var CmdPulls = cli.Command{ Usage: "List open pull requests", Description: `List open pull requests`, Action: runPulls, - Flags: AllDefaultFlags, + Flags: append([]cli.Flag{ + &cli.StringFlag{ + Name: "state", + Usage: "Filter by PR state (all|open|closed)", + DefaultText: "open", + }, + }, AllDefaultFlags...), } func runPulls(ctx *cli.Context) error { login, owner, repo := initCommand() + state := gitea.StateOpen + switch ctx.String("state") { + case "all": + state = gitea.StateAll + case "open": + state = gitea.StateOpen + case "closed": + state = gitea.StateClosed + } + prs, err := login.Client().ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{ Page: 0, - State: string(gitea.StateOpen), + State: string(state), }) if err != nil { @@ -36,7 +52,8 @@ func runPulls(ctx *cli.Context) error { headers := []string{ "Index", - "Name", + "State", + "Author", "Updated", "Title", } @@ -60,6 +77,7 @@ func runPulls(ctx *cli.Context) error { values, []string{ strconv.FormatInt(pr.Index, 10), + string(pr.State), name, pr.Updated.Format("2006-01-02 15:04:05"), pr.Title,