diff --git a/cmd/spr/main.go b/cmd/spr/main.go index 1854d4a5..b32e34c3 100644 --- a/cmd/spr/main.go +++ b/cmd/spr/main.go @@ -98,6 +98,13 @@ func main() { Usage: "Show detailed status bits output", } + textFlag := &cli.BoolFlag{ + Name: "text", + Aliases: []string{"t"}, + Value: false, + Usage: "Show plain text output (URL : title)", + } + cli.AppHelpTemplate = `NAME: {{.Name}} - {{.Usage}} @@ -170,12 +177,16 @@ VERSION: fork of {{.Version}} Name: "status", Aliases: []string{"s", "st"}, Usage: "Show status of open pull requests", - Action: func(c *cli.Context) error { - stackedpr.StatusPullRequests(ctx) - return nil - }, + Action: func(c *cli.Context) error { + if c.IsSet("text") { + stackedpr.TextEnabled = true + } + stackedpr.StatusPullRequests(ctx) + return nil + }, Flags: []cli.Flag{ detailFlag, + textFlag, }, }, { diff --git a/github/pullrequest.go b/github/pullrequest.go index fe473df2..0367d3b9 100644 --- a/github/pullrequest.go +++ b/github/pullrequest.go @@ -237,6 +237,13 @@ func (pr *PullRequest) String(config *config.Config) string { return line } +// TextString returns a plain text representation of the pull request: " : " +func (pr *PullRequest) TextString(config *config.Config) string { + prURL := fmt.Sprintf("https://%s/%s/%s/pull/%d", + config.Repo.GitHubHost, config.Repo.GitHubRepoOwner, config.Repo.GitHubRepoName, pr.Number) + return fmt.Sprintf("%s : %s", prURL, pr.Title) +} + func (cs checkStatus) String(config *config.Config) string { icons := statusBitIcons(config) if config.Repo.RequireChecks { diff --git a/github/pullrequest_test.go b/github/pullrequest_test.go index 8e1c5744..f607d780 100644 --- a/github/pullrequest_test.go +++ b/github/pullrequest_test.go @@ -241,3 +241,26 @@ func TestString(t *testing.T) { assert.Equal(t, test.expect, test.pr.String(test.cfg), fmt.Sprintf("case %d failed", i)) } } + +func TestTextString(t *testing.T) { + cfg := &config.Config{ + Repo: &config.RepoConfig{ + GitHubHost: "github.com", + GitHubRepoOwner: "testowner", + GitHubRepoName: "testrepo", + }, + User: &config.UserConfig{}, + } + + pr := &PullRequest{ + Number: 42, + Title: "Add new feature", + } + assert.Equal(t, "https://github.com/testowner/testrepo/pull/42 : Add new feature", pr.TextString(cfg)) + + pr2 := &PullRequest{ + Number: 7, + Title: "Fix bug in parser", + } + assert.Equal(t, "https://github.com/testowner/testrepo/pull/7 : Fix bug in parser", pr2.TextString(cfg)) +} diff --git a/spr/spr.go b/spr/spr.go index 4ee50bea..59d45ec8 100644 --- a/spr/spr.go +++ b/spr/spr.go @@ -42,6 +42,7 @@ type stackediff struct { gitcmd git.GitInterface profiletimer profiletimer.Timer DetailEnabled bool + TextEnabled bool output io.Writer input io.Reader @@ -517,7 +518,12 @@ func (sd *stackediff) StatusPullRequests(ctx context.Context) { sd.profiletimer.Step("StatusPullRequests::Start") githubInfo := sd.github.GetInfo(ctx, sd.gitcmd) - if len(githubInfo.PullRequests) == 0 { + if sd.TextEnabled { + for i := len(githubInfo.PullRequests) - 1; i >= 0; i-- { + pr := githubInfo.PullRequests[i] + fmt.Fprintf(sd.output, "%s\n", pr.TextString(sd.config)) + } + } else if len(githubInfo.PullRequests) == 0 { fmt.Fprintf(sd.output, "pull request stack is empty\n") } else { if sd.DetailEnabled { diff --git a/spr/spr_test.go b/spr/spr_test.go index 0379ee70..74a484ee 100644 --- a/spr/spr_test.go +++ b/spr/spr_test.go @@ -1257,3 +1257,43 @@ func TestEditCommitAbortNotEditing(t *testing.T) { s.EditCommitAbort(ctx) require.Equal(t, "No edit session in progress.\n", output.String()) } + +func TestStatusPullRequestsTextMode(t *testing.T) { + s, _, githubmock, _, output := makeTestObjects(t, true) + assert := require.New(t) + ctx := context.Background() + s.TextEnabled = true + + s.config.Repo.GitHubHost = "github.com" + s.config.Repo.GitHubRepoOwner = "testowner" + s.config.Repo.GitHubRepoName = "testrepo" + + // Text mode with empty stack should print nothing + githubmock.ExpectGetInfo() + s.StatusPullRequests(ctx) + assert.Equal("", output.String()) + githubmock.ExpectationsMet() + output.Reset() + + // Text mode with PRs should print "<url> : <title>" for each PR + githubmock.Info.PullRequests = []*github.PullRequest{ + { + Number: 1, + Title: "first PR", + Commit: git.Commit{CommitID: "00000001"}, + }, + { + Number: 2, + Title: "second PR", + Commit: git.Commit{CommitID: "00000002"}, + }, + } + githubmock.ExpectGetInfo() + s.StatusPullRequests(ctx) + lines := strings.Split(strings.TrimRight(output.String(), "\n"), "\n") + assert.Equal(2, len(lines)) + // PRs are printed in reverse order (top of stack first) + assert.Equal("https://github.com/testowner/testrepo/pull/2 : second PR", lines[0]) + assert.Equal("https://github.com/testowner/testrepo/pull/1 : first PR", lines[1]) + githubmock.ExpectationsMet() +}