Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cmd/spr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down Expand Up @@ -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,
},
},
{
Expand Down
7 changes: 7 additions & 0 deletions github/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ func (pr *PullRequest) String(config *config.Config) string {
return line
}

// TextString returns a plain text representation of the pull request: "<url> : <title>"
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 {
Expand Down
23 changes: 23 additions & 0 deletions github/pullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
8 changes: 7 additions & 1 deletion spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type stackediff struct {
gitcmd git.GitInterface
profiletimer profiletimer.Timer
DetailEnabled bool
TextEnabled bool

output io.Writer
input io.Reader
Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions spr/spr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Loading