Skip to content

Commit

Permalink
Use gh to automerge and cleanup branches on bump* PRs.
Browse files Browse the repository at this point in the history
This should allow reducing the mass PR creation flow by a few clicks.

Fixes #14538.
  • Loading branch information
MikeMcQuaid committed Jul 28, 2023
1 parent e700bc7 commit 6bd8456
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Library/Homebrew/dev-cmd/bump-cask-pr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def bump_cask_pr_args
description: "Print the pull request URL instead of opening in a browser."
switch "--no-fork",
description: "Don't try to fork the repository."
switch "--automerge",
env: :bump_automerge,
description: "Open pull requests with automerge and branch cleanup enabled."
flag "--version=",
description: "Specify the new <version> for the cask."
flag "--version-arm=",
Expand All @@ -58,6 +61,7 @@ def bump_cask_pr_args
conflicts "--no-audit", "--online"
conflicts "--version=", "--version-arm="
conflicts "--version=", "--version-intel="
conflicts "--"

named_args :cask, number: 1, without_api: true
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/dev-cmd/bump-formula-pr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def bump_formula_pr_args
description: "Print the pull request URL instead of opening in a browser."
switch "--no-fork",
description: "Don't try to fork the repository."
switch "--automerge",
env: :bump_automerge,
description: "Open pull requests with automerge and branch cleanup enabled."
comma_array "--mirror",
description: "Use the specified <URL> as a mirror URL. If <URL> is a comma-separated list " \
"of URLs, multiple mirrors will be added."
Expand Down
9 changes: 7 additions & 2 deletions Library/Homebrew/dev-cmd/bump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def bump_args
description: "Check only casks."
switch "--open-pr",
description: "Open a pull request for the new version if none have been opened yet."
switch "--automerge",
env: :bump_automerge,
description: "Open pull requests with automerge and branch cleanup enabled."
flag "--limit=",
description: "Limit number of package results returned."
flag "--start-with=",
Expand Down Expand Up @@ -288,7 +291,9 @@ def retrieve_and_display_info_and_open_pr(formula_or_cask, name, repositories, a
return if open_pull_requests
return if closed_pull_requests

system HOMEBREW_BREW_FILE, "bump-#{type}-pr", "--no-browse",
"--message=Created by `brew bump`", "--version=#{new_version}", name
bump_args = ["--no-browse", "--message=Created by `brew bump`", "--version=#{new_version}"]

Check warning on line 294 in Library/Homebrew/dev-cmd/bump.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump.rb#L294

Added line #L294 was not covered by tests
bump_args << "--automerge" if args.automerge?

system HOMEBREW_BREW_FILE, "bump-#{type}-pr", *bump_args, name

Check warning on line 297 in Library/Homebrew/dev-cmd/bump.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump.rb#L297

Added line #L297 was not covered by tests
end
end
5 changes: 5 additions & 0 deletions Library/Homebrew/env_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ module EnvConfig
description: "Use this as the browser when opening project homepages.",
default_text: "`$BROWSER` or the OS's default browser.",
},
HOMEBREW_BUMP_AUTOMERGE: {
description: "If set, `brew bump*` commands will attempt to enable automerge and branch cleanup. " \
"This will only work if you have `gh` installed and write access on the relevant repository.",
boolean: true,
},
HOMEBREW_CACHE: {
description: "Use this directory as the download cache.",
default_text: "macOS: `$HOME/Library/Caches/Homebrew`, " \
Expand Down
20 changes: 12 additions & 8 deletions Library/Homebrew/utils/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -682,17 +682,21 @@ def self.create_bump_pr(info, args:)
EOS
end

begin
url = create_pull_request(tap_remote_repo, commit_message,
"#{username}:#{branch}", remote_branch, pr_message)["html_url"]
if args.no_browse?
puts url
else
exec_browser url
end
url = begin
create_pull_request(tap_remote_repo, commit_message,

Check warning on line 686 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L686

Added line #L686 was not covered by tests
"#{username}:#{branch}", remote_branch, pr_message)["html_url"]
rescue *API::ERRORS => e
odie "Unable to open pull request: #{e.message}!"
end

# Attempt to enable automerge on the created PR if requested.
GitHub::API.run_gh_cli(args: ["pr", "merge", "--auto", "--merge", "--delete-branch", url]) if args.automerge?

if args.no_browse?

Check warning on line 695 in Library/Homebrew/utils/github.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L695

Added line #L695 was not covered by tests
puts url
else
exec_browser url
end
end
end
end
Expand Down
21 changes: 14 additions & 7 deletions Library/Homebrew/utils/github/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,25 @@ def initialize(github_message, errors)
JSON::ParserError,
].freeze

sig {
params(args: T::Array[String], print_stdout: T::Boolean,

Check warning on line 127 in Library/Homebrew/utils/github/api.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github/api.rb#L127

Added line #L127 was not covered by tests
print_stderr: T::Boolean).returns(SystemCommand::Result)
}
def self.run_gh_cli(args:, print_stdout: true, print_stderr: true)
# Avoid `Formula["gh"].opt_bin` so this method works even with `HOMEBREW_DISABLE_LOAD_FORMULA`.
env = { "PATH" => PATH.new(HOMEBREW_PREFIX/"opt/gh/bin", ENV.fetch("PATH")) }
system_command "gh", args: args, env: env, print_stdout: print_stdout, print_stderr: print_stderr

Check warning on line 133 in Library/Homebrew/utils/github/api.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github/api.rb#L132-L133

Added lines #L132 - L133 were not covered by tests
end

# Gets the token from the GitHub CLI for github.com.
sig { returns(T.nilable(String)) }
def self.github_cli_token
# Avoid `Formula["gh"].opt_bin` so this method works even with `HOMEBREW_DISABLE_LOAD_FORMULA`.
env = { "PATH" => PATH.new(HOMEBREW_PREFIX/"opt/gh/bin", ENV.fetch("PATH")) }
gh_out, _, result = system_command "gh",
args: ["auth", "token", "--hostname", "github.com"],
env: env,
print_stderr: false
result = run_gh_cli args: ["auth", "token", "--hostname", "github.com"],

Check warning on line 139 in Library/Homebrew/utils/github/api.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github/api.rb#L139

Added line #L139 was not covered by tests
print_stdout: false,
print_stderr: false
return unless result.success?

gh_out.chomp
result.stdout.chomp

Check warning on line 144 in Library/Homebrew/utils/github/api.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github/api.rb#L144

Added line #L144 was not covered by tests
end

# Gets the password field from `git-credential-osxkeychain` for github.com,
Expand Down
3 changes: 3 additions & 0 deletions completions/bash/brew
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ _brew_bump() {
case "${cur}" in
-*)
__brewcomp "
--automerge
--cask
--debug
--formula
Expand All @@ -461,6 +462,7 @@ _brew_bump_cask_pr() {
case "${cur}" in
-*)
__brewcomp "
--automerge
--commit
--debug
--dry-run
Expand Down Expand Up @@ -494,6 +496,7 @@ _brew_bump_formula_pr() {
case "${cur}" in
-*)
__brewcomp "
--automerge
--commit
--debug
--dry-run
Expand Down
3 changes: 3 additions & 0 deletions completions/fish/brew.fish
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ __fish_brew_complete_arg 'bottle' -a '(__fish_brew_suggest_formulae_installed)'


__fish_brew_complete_cmd 'bump' 'Display out-of-date brew formulae and the latest version available'
__fish_brew_complete_arg 'bump' -l automerge -d 'Open pull requests with automerge and branch cleanup enabled'
__fish_brew_complete_arg 'bump' -l cask -d 'Check only casks'
__fish_brew_complete_arg 'bump' -l debug -d 'Display any debugging information'
__fish_brew_complete_arg 'bump' -l formula -d 'Check only formulae'
Expand All @@ -395,6 +396,7 @@ __fish_brew_complete_arg 'bump; and not __fish_seen_argument -l formula -l formu


__fish_brew_complete_cmd 'bump-cask-pr' 'Create a pull request to update cask with a new version'
__fish_brew_complete_arg 'bump-cask-pr' -l automerge -d 'Open pull requests with automerge and branch cleanup enabled'
__fish_brew_complete_arg 'bump-cask-pr' -l commit -d 'When passed with `--write-only`, generate a new commit after writing changes to the cask file'
__fish_brew_complete_arg 'bump-cask-pr' -l debug -d 'Display any debugging information'
__fish_brew_complete_arg 'bump-cask-pr' -l dry-run -d 'Print what would be done rather than doing it'
Expand All @@ -419,6 +421,7 @@ __fish_brew_complete_arg 'bump-cask-pr' -a '(__fish_brew_suggest_casks_all)'


__fish_brew_complete_cmd 'bump-formula-pr' 'Create a pull request to update formula with a new URL or a new tag'
__fish_brew_complete_arg 'bump-formula-pr' -l automerge -d 'Open pull requests with automerge and branch cleanup enabled'
__fish_brew_complete_arg 'bump-formula-pr' -l commit -d 'When passed with `--write-only`, generate a new commit after writing changes to the formula file'
__fish_brew_complete_arg 'bump-formula-pr' -l debug -d 'Display any debugging information'
__fish_brew_complete_arg 'bump-formula-pr' -l dry-run -d 'Print what would be done rather than doing it'
Expand Down
3 changes: 3 additions & 0 deletions completions/zsh/_brew
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ _brew_bottle() {
# brew bump
_brew_bump() {
_arguments \
'--automerge[Open pull requests with automerge and branch cleanup enabled]' \
'--debug[Display any debugging information]' \
'--full-name[Print formulae/casks with fully-qualified names]' \
'--help[Show this message]' \
Expand All @@ -512,6 +513,7 @@ _brew_bump() {
# brew bump-cask-pr
_brew_bump_cask_pr() {
_arguments \
'--automerge[Open pull requests with automerge and branch cleanup enabled]' \
'--commit[When passed with `--write-only`, generate a new commit after writing changes to the cask file]' \
'--debug[Display any debugging information]' \
'(--write)--dry-run[Print what would be done rather than doing it]' \
Expand Down Expand Up @@ -539,6 +541,7 @@ _brew_bump_cask_pr() {
# brew bump-formula-pr
_brew_bump_formula_pr() {
_arguments \
'--automerge[Open pull requests with automerge and branch cleanup enabled]' \
'--commit[When passed with `--write-only`, generate a new commit after writing changes to the formula file]' \
'--debug[Display any debugging information]' \
'(--write-only)--dry-run[Print what would be done rather than doing it]' \
Expand Down
9 changes: 9 additions & 0 deletions docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,8 @@ formulae, also displays whether a pull request has been opened with the URL.
Check only casks.
* `--open-pr`:
Open a pull request for the new version if none have been opened yet.
* `--automerge`:
Open pull requests with automerge and branch cleanup enabled.
* `--limit`:
Limit number of package results returned.
* `--start-with`:
Expand Down Expand Up @@ -1046,6 +1048,8 @@ supplied by the user.
Print the pull request URL instead of opening in a browser.
* `--no-fork`:
Don't try to fork the repository.
* `--automerge`:
Open pull requests with automerge and branch cleanup enabled.
* `--version`:
Specify the new *`version`* for the cask.
* `--version-arm`:
Expand Down Expand Up @@ -1098,6 +1102,8 @@ nor vice versa. It must use whichever style specification the formula already us
Print the pull request URL instead of opening in a browser.
* `--no-fork`:
Don't try to fork the repository.
* `--automerge`:
Open pull requests with automerge and branch cleanup enabled.
* `--mirror`:
Use the specified *`URL`* as a mirror URL. If *`URL`* is a comma-separated list of URLs, multiple mirrors will be added.
* `--fork-org`:
Expand Down Expand Up @@ -2116,6 +2122,9 @@ example, run `export HOMEBREW_NO_INSECURE_REDIRECT=1` rather than just

*Default:* `$BROWSER` or the OS's default browser.

- `HOMEBREW_BUMP_AUTOMERGE`
<br>If set, `brew bump*` commands will attempt to enable automerge and branch cleanup. This will only work if you have `gh` installed and write access on the relevant repository.

- `HOMEBREW_CACHE`
<br>Use this directory as the download cache.

Expand Down
18 changes: 18 additions & 0 deletions manpages/brew.1
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,10 @@ Check only casks\.
Open a pull request for the new version if none have been opened yet\.
.
.TP
\fB\-\-automerge\fR
Open pull requests with automerge and branch cleanup enabled\.
.
.TP
\fB\-\-limit\fR
Limit number of package results returned\.
.
Expand Down Expand Up @@ -1496,6 +1500,10 @@ Print the pull request URL instead of opening in a browser\.
Don\'t try to fork the repository\.
.
.TP
\fB\-\-automerge\fR
Open pull requests with automerge and branch cleanup enabled\.
.
.TP
\fB\-\-version\fR
Specify the new \fIversion\fR for the cask\.
.
Expand Down Expand Up @@ -1575,6 +1583,10 @@ Print the pull request URL instead of opening in a browser\.
Don\'t try to fork the repository\.
.
.TP
\fB\-\-automerge\fR
Open pull requests with automerge and branch cleanup enabled\.
.
.TP
\fB\-\-mirror\fR
Use the specified \fIURL\fR as a mirror URL\. If \fIURL\fR is a comma\-separated list of URLs, multiple mirrors will be added\.
.
Expand Down Expand Up @@ -3041,6 +3053,12 @@ Use this as the browser when opening project homepages\.
\fIDefault:\fR \fB$BROWSER\fR or the OS\'s default browser\.
.
.TP
\fBHOMEBREW_BUMP_AUTOMERGE\fR
.
.br
If set, \fBbrew bump*\fR commands will attempt to enable automerge and branch cleanup\. This will only work if you have \fBgh\fR installed and write access on the relevant repository\.
.
.TP
\fBHOMEBREW_CACHE\fR
.
.br
Expand Down

0 comments on commit 6bd8456

Please sign in to comment.