From d2de3094a05260851a47a268007e6b3e7eab529f Mon Sep 17 00:00:00 2001 From: Michael Kaiser Date: Tue, 3 Oct 2017 12:28:17 +0200 Subject: [PATCH] Add limit flag for issue and release commands --- commands/issue.go | 13 ++++++++++++- commands/release.go | 13 ++++++++++++- features/issue.feature | 31 +++++++++++++++++++++++++++++++ features/release.feature | 30 ++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/commands/issue.go b/commands/issue.go index 3aee8c81..84f6b60d 100644 --- a/commands/issue.go +++ b/commands/issue.go @@ -17,7 +17,7 @@ var ( cmdIssue = &Command{ Run: listIssues, Usage: ` -issue [-a ] [-c ] [-@ ] [-s ] [-f ] [-M ] [-l ] [-d ] [-o [-^]] +issue [-a ] [-c ] [-@ ] [-s ] [-f ] [-M ] [-l ] [-d ] [-o [-^]] [-L ] issue create [-oc] [-m |-F ] [-a ] [-M ] [-l ] `, Long: `Manage GitHub issues for the current project. @@ -131,6 +131,9 @@ With no arguments, show a list of open issues. -^ --sort-ascending Sort by ascending dates instead of descending. + -L, --limit + Display only the first issues. + --include-pulls Include pull requests as well as issues. `, @@ -165,6 +168,8 @@ With no arguments, show a list of open issues. flagIssueAssignees, flagIssueLabels listFlag + + flagIssueLimit int ) func init() { @@ -188,6 +193,7 @@ func init() { cmdIssue.Flag.StringVarP(&flagIssueSort, "sort", "o", "created", "SORT_KEY") cmdIssue.Flag.BoolVarP(&flagIssueSortAscending, "sort-ascending", "^", false, "SORT_KEY") cmdIssue.Flag.BoolVarP(&flagIssueIncludePulls, "include-pulls", "", false, "INCLUDE_PULLS") + cmdIssue.Flag.IntVarP(&flagIssueLimit, "limit", "L", -1, "LIMIT") cmdIssue.Use(cmdCreateIssue) CmdRunner.Use(cmdIssue) @@ -244,12 +250,17 @@ func listIssues(cmd *Command, args *Args) { } colorize := ui.IsTerminal(os.Stdout) + c := 0 for _, issue := range issues { if !flagIssueIncludePulls && issue.PullRequest != nil { continue } ui.Printf(formatIssue(issue, flagIssueFormat, colorize)) + c++ + if c == flagIssueLimit { + break + } } } diff --git a/commands/release.go b/commands/release.go index 80645179..2e1f9664 100644 --- a/commands/release.go +++ b/commands/release.go @@ -17,7 +17,7 @@ var ( cmdRelease = &Command{ Run: listReleases, Usage: ` -release [--include-drafts] [--exclude-prereleases] +release [--include-drafts] [--exclude-prereleases] [-L ] release show release create [-dpoc] [-a ] [-m |-F ] [-t ] release edit [] @@ -56,6 +56,9 @@ With '--exclude-prereleases', exclude non-stable releases from the listing. Delete the release and associated assets for the specified . ## Options: + -L, --limit + Display only the first releases. + -d, --draft Create a draft release. @@ -135,11 +138,14 @@ hub(1), git-tag(1) flagReleaseCommitish string flagReleaseAssets stringSliceValue + + flagReleaseLimit int ) func init() { cmdRelease.Flag.BoolVarP(&flagReleaseIncludeDrafts, "include-drafts", "d", false, "DRAFTS") cmdRelease.Flag.BoolVarP(&flagReleaseExcludePrereleases, "exclude-prereleases", "p", false, "PRERELEASE") + cmdRelease.Flag.IntVarP(&flagReleaseLimit, "limit", "L", -1, "LIMIT") cmdShowRelease.Flag.BoolVarP(&flagReleaseShowDownloads, "show-downloads", "d", false, "DRAFTS") @@ -184,10 +190,15 @@ func listReleases(cmd *Command, args *Args) { releases, err := gh.FetchReleases(project) utils.Check(err) + c := 0 for _, release := range releases { if (!release.Draft || flagReleaseIncludeDrafts) && (!release.Prerelease || !flagReleaseExcludePrereleases) { ui.Println(release.TagName) + c++ + if c == flagReleaseLimit { + break + } } } } diff --git a/features/issue.feature b/features/issue.feature index 7bc57ea5..625c2a73 100644 --- a/features/issue.feature +++ b/features/issue.feature @@ -38,6 +38,37 @@ Feature: hub issue #13 Second issue\n """ + Scenario: List limited number of issues + Given the GitHub API server: + """ + get('/repos/github/hub/issues') { + + json [ + { :number => 102, + :title => "First issue", + :state => "open", + :user => { :login => "octocat" }, + }, + { :number => 13, + :title => "Second issue", + :state => "open", + :user => { :login => "octocat" }, + }, + { :number => 999, + :title => "Third issue", + :state => "open", + :user => { :login => "octocat" }, + }, + ] + } + """ + When I successfully run `hub issue -L 2` + Then the output should contain exactly: + """ + #102 First issue + #13 Second issue\n + """ + Scenario: Fetch issues and pull requests Given the GitHub API server: """ diff --git a/features/release.feature b/features/release.feature index 93131f09..eb4ff3a5 100644 --- a/features/release.feature +++ b/features/release.feature @@ -147,6 +147,36 @@ Feature: hub release v1.0.0\n """ + Scenario: List limited number of releases + Given the GitHub API server: + """ + get('/repos/mislav/will_paginate/releases') { + json [ + { tag_name: 'v1.2.0', + name: 'will_paginate 1.2.0', + draft: false, + prerelease: false, + }, + { tag_name: 'v1.2.0-pre', + name: 'will_paginate 1.2.0-pre', + draft: false, + prerelease: true, + }, + { tag_name: 'v1.0.2', + name: 'will_paginate 1.0.2', + draft: false, + prerelease: false, + }, + ] + } + """ + When I successfully run `hub release -L 2` + Then the output should contain exactly: + """ + v1.2.0 + v1.2.0-pre\n + """ + Scenario: Repository not found when listing releases Given the GitHub API server: """ -- GitLab