Skip to content

Conversation

@arturhoo
Copy link

@arturhoo arturhoo commented Oct 5, 2023

My workflow with private repositories usually consists of:

  1. Create a new branch locally
  2. Modify code and create commits
  3. Create a PR in the private repository using gh repo create --fill

The vast majority of times, the PR will be created in the base repository as defined by

func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams) (ghrepo.Interface, error) {

In other words, I'm pushing my local branch to the origin remote (the central, private repository) as part of the PR creation flow.

This is currently done through an interactive prompt, where the base repository is presented as the first option - I've created the muscle memory of just pressing <enter> right after running gh pr create every time.

This PR introduces a new flag --push-to-base-repo that skips this interactive prompt, when the user has write access to that repository.

Fixes #8152

--

P.S.: this seems to have been discussed in #1718 - I think this approach might tackle at least the common workflow os folks who have clones a repository which they have write access to (and don't submit PRs through forks).

@arturhoo arturhoo marked this pull request as ready for review October 5, 2023 13:14
@arturhoo arturhoo requested a review from a team as a code owner October 5, 2023 13:14
@arturhoo arturhoo requested review from andyfeller and removed request for a team October 5, 2023 13:14
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Oct 5, 2023
@cliAutomation
Copy link
Collaborator

Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message.

@arturhoo arturhoo changed the title cmd/pr/create - implement --push-to-base-repo flag pr create - implement --push-to-base-repo flag Oct 5, 2023
Copy link
Member

@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturhoo : Thank you for opening up this pull request, working to improve the GitHub CLI!

I can see how this is tangential to #1718, however I think it would best to create an issue especially for this work that the current core contributors can discuss as that issue is over 3 years old.

Comment on lines +580 to +597
if headRepo == nil && isPushEnabled && opts.PushToBaseRepo {
pushableRepos, err := repoContext.HeadRepos()
if err != nil {
return nil, err
}

for _, r := range pushableRepos {
if ghrepo.IsSame(baseRepo, r) {
headRepo = r
}
}

if headRepo == nil {
return nil, fmt.Errorf("the user might lack write access to the base repository")
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be refactored into the following section to avoid duplicating logic but leveraging (r *ResolvedRemotes) RemoteForRepo, which is doing similar logic already?

cli/context/context.go

Lines 161 to 169 in 3443a75

// RemoteForRepo finds the git remote that points to a repository
func (r *ResolvedRemotes) RemoteForRepo(repo ghrepo.Interface) (*Remote, error) {
for _, remote := range r.remotes {
if ghrepo.IsSame(remote, repo) {
return remote, nil
}
}
return nil, errors.New("not found")
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably missing something, but I don't see how we could incorporate the logic into the (r *ResolvedRemotes) RemoteForRepo method.

The proposed logic iterates over all pushable repos and determines if baseRepo can be pushed, just to fail early. Remotes, which RemoteForRepo handles, will only be used by the time we actually push on

if headRemote == nil && headRepo != nil {
headRemote, _ = ctx.RepoContext.RemoteForRepo(headRepo)
}

fl.Bool("no-maintainer-edit", false, "Disable maintainer's ability to modify pull request")
fl.StringVar(&opts.RecoverFile, "recover", "", "Recover input from a failed run of create")
fl.StringVarP(&opts.Template, "template", "T", "", "Template `file` to use as starting body text")
fl.BoolVar(&opts.PushToBaseRepo, "push-to-base-repo", false, "Push the head branch to the base repository")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to expand flags to allow people to determine this from the command line, knowing there are more options that people want, then I imagine this might be more generic flag with different values for prompt, base, ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea! I took a look at the code, but I couldn't find any other 'canonical' repository names except for base (which already has an implementation for that we're using).

For prompt, that's already the default behaviour if this flag isn't passed

// otherwise, ask the user for the head repository using info obtained from the API

Are you suggesting accepting any value here and validating it against the pushableRepos as seen on https://github.com/arturhoo/cli/compare/push-to-base-repo...arturhoo:cli:push-to-repo?expand=1 ?

@andyfeller andyfeller added enhancement a request to improve CLI core This issue is not accepting PRs from outside contributors gh-pr relating to the gh pr command labels Oct 9, 2023
@arturhoo
Copy link
Author

Hey @andyfeller - thank you for taking the time to review my suggestion. I've replied to each suggestion individually and created a new issue #8152 - I would appreciate if you could take another look before I make further changes. Thanks!

@samcoe
Copy link
Contributor

samcoe commented Oct 17, 2023

Closing, see #8152 (comment) for context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core This issue is not accepting PRs from outside contributors enhancement a request to improve CLI external pull request originating outside of the CLI core team gh-pr relating to the gh pr command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pr create: flag to push local branch to remote without prompting

4 participants