Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils/github: support multiple commits in create_bump_pr #16804

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions Library/Homebrew/dev-cmd/bump-cask-pr.rb
Expand Up @@ -176,14 +176,12 @@
run_cask_style(cask, old_contents, args: args)

pr_info = {
branch_name: branch_name,
commit_message: commit_message,
old_contents: old_contents,
pr_message: "Created with `brew bump-cask-pr`.",
sourcefile_path: cask.sourcefile_path,
tap: cask.tap,
branch: branch_name,

Check warning on line 179 in Library/Homebrew/dev-cmd/bump-cask-pr.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump-cask-pr.rb#L179

Added line #L179 was not covered by tests
commits: [[commit_message.to_s, cask.sourcefile_path, nil, old_contents]],
pr_message: "Created with `brew bump-cask-pr`.",
tap: cask.tap,
}
GitHub.create_bump_pr(pr_info, args: args)
GitHub.create_bump_pr(**pr_info, args: args)

Check warning on line 184 in Library/Homebrew/dev-cmd/bump-cask-pr.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump-cask-pr.rb#L184

Added line #L184 was not covered by tests
end

sig { params(version: Cask::DSL::Version, cask: Cask::Cask).returns(Cask::DSL::Version) }
Expand Down
21 changes: 9 additions & 12 deletions Library/Homebrew/dev-cmd/bump-formula-pr.rb
Expand Up @@ -381,19 +381,16 @@
end

pr_info = {
sourcefile_path: formula.path,
old_contents: old_contents,
additional_files: alias_rename,
remote: remote,
remote_branch: remote_branch,
branch_name: "bump-#{formula.name}-#{new_formula_version}",
commit_message: "#{formula.name} #{new_formula_version}",
previous_branch: previous_branch,
tap: formula.tap,
tap_remote_repo: tap_remote_repo,
pr_message: pr_message,
commits: [["#{formula.name} #{new_formula_version}", formula.path, alias_rename, old_contents]],

Check warning on line 384 in Library/Homebrew/dev-cmd/bump-formula-pr.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump-formula-pr.rb#L384

Added line #L384 was not covered by tests
remote: remote,
remote_branch: remote_branch,
branch: "bump-#{formula.name}-#{new_formula_version}",
previous_branch: previous_branch,
tap: formula.tap,
tap_remote_repo: tap_remote_repo,
pr_message: pr_message,
}
GitHub.create_bump_pr(pr_info, args: args)
GitHub.create_bump_pr(**pr_info, args: args)

Check warning on line 393 in Library/Homebrew/dev-cmd/bump-formula-pr.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump-formula-pr.rb#L393

Added line #L393 was not covered by tests
end

def determine_mirror(url)
Expand Down
181 changes: 115 additions & 66 deletions Library/Homebrew/utils/github.rb
Expand Up @@ -613,25 +613,49 @@
[remote_url, username]
end

def self.create_bump_pr(info, args:)
tap = info[:tap]
sourcefile_path = info[:sourcefile_path]
old_contents = info[:old_contents]
additional_files = info[:additional_files] || []
remote = info[:remote] || "origin"
remote_branch = info[:remote_branch] || tap.git_repo.origin_branch_name
branch = info[:branch_name]
commit_message = info[:commit_message]
previous_branch = info[:previous_branch] || "-"
tap_remote_repo = info[:tap_remote_repo] || tap.full_name
pr_message = info[:pr_message]

sourcefile_path.parent.cd do
git_dir = Utils.popen_read("git", "rev-parse", "--git-dir").chomp
shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow")
changed_files = [sourcefile_path]
changed_files += additional_files if additional_files.present?
# @param commits data on commits to apply consisting of the commit message,
# the source file path, any additional files, and the old contents of file.
# Must be non-empty and all source file paths must be part of same git repo.
sig {
params(

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L620

Added line #L620 was not covered by tests
tap: Tap,
branch: String,
commits: T::Array[[String, Pathname, T.nilable(T::Array[String]), String]],
Copy link
Member Author

Choose a reason for hiding this comment

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

May switch to a Struct to be more explicit rather than having to read docs to know what index means.

args: T.untyped,
pr_message: T.nilable(String),
tap_remote_repo: T.nilable(String),
remote: T.nilable(String),
remote_branch: T.nilable(String),
previous_branch: T.nilable(String),
).void
}
def self.create_bump_pr(tap:, branch:, commits:, args:, pr_message: nil, tap_remote_repo: nil,
remote: nil, remote_branch: nil, previous_branch: nil)
tap_remote_repo ||= tap.full_name
remote ||= "origin"
remote_branch ||= tap.git_repo.origin_branch_name
previous_branch ||= "-"

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L634-L637

Added lines #L634 - L637 were not covered by tests

odie "Unable to create pull request with empty `commits`!" if commits.blank?
git_dir = ""
commits.each do |_, sourcefile_path, _, _|
sourcefile_path.parent.cd do
sourcefile_git_dir = T.cast(Utils.popen_read("git", "rev-parse", "--git-dir").chomp, String)
if git_dir.empty?

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L640-L644

Added lines #L640 - L644 were not covered by tests
git_dir = sourcefile_git_dir
elsif git_dir != sourcefile_git_dir
commits.each { |_, sourcefile_path, _, old_contents| sourcefile_path.atomic_write(old_contents) }
odie "Unable to create pull request with source files in different git repositories!"

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L648

Added line #L648 was not covered by tests
end
end
end

shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow")
remote_url = T.let(nil, T.nilable(String))
username = T.let(nil, T.nilable(String))
first_sourcefile_path = T.must(commits.first)[1]

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L653-L656

Added lines #L653 - L656 were not covered by tests

first_sourcefile_path.parent.cd do

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L658

Added line #L658 was not covered by tests
if args.dry_run? || (args.write_only? && !args.commit?)
remote_url = if args.no_fork?
Utils.popen_read("git", "remote", "get-url", "--push", "origin").chomp
Expand All @@ -642,67 +666,92 @@
"FORK_URL"
end
ohai "git fetch --unshallow origin" if shallow
ohai "git add #{changed_files.join(" ")}"
ohai "git checkout --no-track -b #{branch} #{remote}/#{remote_branch}"
ohai "git commit --no-edit --verbose --message='#{commit_message}' " \
"-- #{changed_files.join(" ")}"
ohai "git push --set-upstream #{remote_url} #{branch}:#{branch}"
ohai "git checkout --quiet #{previous_branch}"
ohai "create pull request with GitHub API (base branch: #{remote_branch})"
else

unless args.commit?
if args.no_fork?
remote_url = Utils.popen_read("git", "remote", "get-url", "--push", "origin").chomp
add_auth_token_to_url!(remote_url)
username = tap.user
else
begin
remote_url, username = forked_repo_info!(tap_remote_repo, org: args.fork_org)
rescue *API::ERRORS => e
sourcefile_path.atomic_write(old_contents)
odie "Unable to fork: #{e.message}!"
end
elsif !args.commit?
if args.no_fork?
remote_url = Utils.popen_read("git", "remote", "get-url", "--push", "origin").chomp
add_auth_token_to_url!(remote_url)
username = tap.user

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L673-L674

Added lines #L673 - L674 were not covered by tests
else
begin
remote_url, username = forked_repo_info!(tap_remote_repo, org: args.fork_org)
rescue *API::ERRORS => e
commits.each { |_, sourcefile_path, _, old_contents| sourcefile_path.atomic_write(old_contents) }
odie "Unable to fork: #{e.message}!"

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L679-L680

Added lines #L679 - L680 were not covered by tests
end
end

safe_system "git", "fetch", "--unshallow", "origin" if shallow
safe_system "git", "fetch", "--unshallow", "origin" if shallow
safe_system "git", "checkout", "--no-track", "-b", branch, "#{remote}/#{remote_branch}"

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L685

Added line #L685 was not covered by tests
end
end

commits.each do |commit_message, sourcefile_path, additional_files, _old_contents|
sourcefile_path.parent.cd do
changed_files = [sourcefile_path]

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L689-L691

Added lines #L689 - L691 were not covered by tests
changed_files += additional_files if additional_files.present?

if args.dry_run? || (args.write_only? && !args.commit?)

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L694

Added line #L694 was not covered by tests
ohai "git add #{changed_files.join(" ")}"
ohai "git commit --no-edit --verbose --message='#{commit_message}' " \

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L696

Added line #L696 was not covered by tests
"-- #{changed_files.join(" ")}"
else
safe_system "git", "add", *changed_files
safe_system "git", "commit", "--no-edit", "--verbose",

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L700

Added line #L700 was not covered by tests
"--message=#{commit_message}",
"--", *changed_files
end
end
end

safe_system "git", "add", *changed_files
safe_system "git", "checkout", "--no-track", "-b", branch, "#{remote}/#{remote_branch}" unless args.commit?
safe_system "git", "commit", "--no-edit", "--verbose",
"--message=#{commit_message}",
"--", *changed_files
return if args.commit?
if args.dry_run? || (args.write_only? && !args.commit?)
ohai "git push --set-upstream #{remote_url} #{branch}:#{branch}"
ohai "git checkout --quiet #{previous_branch}"
ohai "create pull request with GitHub API (base branch: #{remote_branch})"
return

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L709-L711

Added lines #L709 - L711 were not covered by tests
end
return if args.commit?

first_sourcefile_path.parent.cd do
system_command!("git", args: ["push", "--set-upstream", remote_url, "#{branch}:#{branch}"],

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L715-L716

Added lines #L715 - L716 were not covered by tests
print_stdout: true)
safe_system "git", "checkout", "--quiet", previous_branch

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L718

Added line #L718 was not covered by tests
pr_message = <<~EOS
#{pr_message}

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L720

Added line #L720 was not covered by tests
EOS

system_command!("git", args: ["push", "--set-upstream", remote_url, "#{branch}:#{branch}"],
print_stdout: true)
safe_system "git", "checkout", "--quiet", previous_branch
commit_messages = commits.map(&:first)

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L723

Added line #L723 was not covered by tests
if commit_messages.length > 1
pr_message = <<~EOS
#{commit_messages.drop(1).map { |m| "* #{m}" }.join("\n")}

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L726

Added line #L726 was not covered by tests

---

#{pr_message}
EOS
user_message = args.message
if user_message
pr_message = <<~EOS
#{user_message}
end

---
user_message = args.message

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L734

Added line #L734 was not covered by tests
if user_message
pr_message = <<~EOS
#{user_message}

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L737

Added line #L737 was not covered by tests

#{pr_message}
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
rescue *API::ERRORS => e
odie "Unable to open pull request: #{e.message}!"
#{pr_message}
EOS
end

begin
url = create_pull_request(tap_remote_repo, commit_messages.first,

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L746

Added line #L746 was not covered by tests
"#{username}:#{branch}", remote_branch, pr_message)["html_url"]
if args.no_browse?

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L748

Added line #L748 was not covered by tests
puts url
else
exec_browser url
end
rescue *API::ERRORS => e
odie "Unable to open pull request: #{e.message}!"

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/github.rb#L754

Added line #L754 was not covered by tests
end
end
end
Expand Down