Skip to content

Commit

Permalink
Update av stack submit to open PRs in browser after entire stack is s…
Browse files Browse the repository at this point in the history
…ubmitted (#268)

When run with a stack of unpushed branches, `av stack submit` ends up opening a bunch of new browser windows for each PR one by one, which is a bit disruptive. ~Since we're pushing a single stack, only open the last-created PR once completed.~ Open all the PRs at the end once created instead.
  • Loading branch information
oleg-codaio authored May 7, 2024
1 parent 17cc574 commit dd825e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
15 changes: 13 additions & 2 deletions cmd/av/stack_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ If the --current flag is given, this command will create pull requests up to the
}

// ensure pull requests for each branch in the stack
createdPullRequestPermalinks := []string{}
ctx := context.Background()
client, err := getGitHubClient()
if err != nil {
Expand All @@ -80,13 +81,17 @@ If the --current flag is given, this command will create pull requests up to the
result, err := actions.CreatePullRequest(
ctx, repo, client, tx,
actions.CreatePullRequestOpts{
BranchName: branchName,
Draft: config.Av.PullRequest.Draft,
BranchName: branchName,
Draft: config.Av.PullRequest.Draft,
NoOpenBrowser: true,
},
)
if err != nil {
return err
}
if result.Created {
createdPullRequestPermalinks = append(createdPullRequestPermalinks, result.Branch.PullRequest.Permalink)
}
// make sure the base branch of the PR is up to date if it already exists
if !result.Created && result.Pull.BaseRefName != result.Branch.Parent.Name {
if _, err := client.UpdatePullRequest(
Expand All @@ -111,6 +116,12 @@ If the --current flag is given, this command will create pull requests up to the
}
}

if config.Av.PullRequest.OpenBrowser {
for _, createdPullRequestPermalink := range createdPullRequestPermalinks {
actions.OpenPullRequestInBrowser(createdPullRequestPermalink)
}
}

return nil
},
}
Expand Down
24 changes: 15 additions & 9 deletions internal/actions/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type CreatePullRequestOpts struct {
Force bool
// If true, open an editor for editing the title and body
Edit bool
// If true, do not open the browser after creating the PR
NoOpenBrowser bool
}

type CreatePullRequestResult struct {
Expand Down Expand Up @@ -407,21 +409,25 @@ func CreatePullRequest(
colors.UserInput(pull.Permalink), "\n",
)

if didCreatePR && config.Av.PullRequest.OpenBrowser {
if err := browser.Open(pull.Permalink); err != nil {
_, _ = fmt.Fprint(os.Stderr,
" - couldn't open browser ",
colors.UserInput(err),
" for pull request link ",
colors.UserInput(pull.Permalink),
)
}
if didCreatePR && !opts.NoOpenBrowser && config.Av.PullRequest.OpenBrowser {
OpenPullRequestInBrowser(pull.Permalink)
}

tx.SetBranch(branchMeta)
return &CreatePullRequestResult{didCreatePR, branchMeta, pull}, nil
}

func OpenPullRequestInBrowser(pullRequestLink string) {
if err := browser.Open(pullRequestLink); err != nil {
_, _ = fmt.Fprint(os.Stderr,
" - couldn't open browser ",
colors.UserInput(err),
" for pull request link ",
colors.UserInput(pullRequestLink),
)
}
}

func savePRDescriptionToTemporaryFile(saveFile string, contents string) {
if err := os.WriteFile(saveFile, []byte(contents), 0644); err != nil {
logrus.WithError(err).
Expand Down

0 comments on commit dd825e5

Please sign in to comment.