Skip to content
Snippets Groups Projects
Commit 415c3604 authored by Jonathan Chang's avatar Jonathan Chang
Browse files

dev-cmd/pr-pull: use GitHub API to get PR commits

parent 1d778807
No related branches found
No related tags found
No related merge requests found
......@@ -275,22 +275,20 @@ module Homebrew
raise
end
def cherry_pick_pr!(pr, args:, path: ".")
def cherry_pick_pr!(user, repo, pr, args:, path: ".")
if args.dry_run?
puts <<~EOS
git fetch --force origin +refs/pull/#{pr}/head
git merge-base HEAD FETCH_HEAD
git cherry-pick --ff --allow-empty $merge_base..FETCH_HEAD
EOS
else
safe_system "git", "-C", path, "fetch", "--quiet", "--force", "origin", "+refs/pull/#{pr}/head"
merge_base = Utils.popen_read("git", "-C", path, "merge-base", "HEAD", "FETCH_HEAD").strip
commit_count = Utils.popen_read("git", "-C", path, "rev-list", "#{merge_base}..FETCH_HEAD").lines.count
ohai "Using #{commit_count} commit#{"s" unless commit_count == 1} from ##{pr}"
Utils::Git.cherry_pick!(path, "--ff", "--allow-empty", "#{merge_base}..FETCH_HEAD",
verbose: args.verbose?, resolve: args.resolve?)
return
end
commits = GitHub.pull_request_commits(user, repo, pr)
safe_system "git", "-C", path, "fetch", "--quiet", "--force", "origin", commits.last
ohai "Using #{commits.count} commit#{"s" unless commits.count == 1} from ##{pr}"
Utils::Git.cherry_pick!(path, "--ff", "--allow-empty", *commits, verbose: args.verbose?, resolve: args.resolve?)
end
def check_branch(path, ref, args:)
......@@ -391,7 +389,7 @@ module Homebrew
Dir.mktmpdir pr do |dir|
cd dir do
original_commit = Utils.popen_read("git", "-C", tap.path, "rev-parse", "HEAD").chomp
cherry_pick_pr!(pr, path: tap.path, args: args)
cherry_pick_pr!(user, repo, pr, path: tap.path, args: args)
autosquash!(original_commit, path: tap.path, args: args) if args.autosquash?
signoff!(pr, tap: tap, args: args) unless args.clean?
......
......@@ -75,4 +75,11 @@ describe GitHub do
expect(url).to eq("https://api.github.com/repos/Homebrew/homebrew-core/actions/artifacts/3557392/zip")
end
end
describe "::pull_request_commits", :needs_network do
it "gets the correct commits hashes for a pull request" do
hashes = %w[188606a4a9587365d930b02c98ad6857b1d00150 25a71fe1ea1558415d6496d23834dc70778ddee5]
expect(subject.pull_request_commits("Homebrew", "legacy-homebrew", 50678)).to eq(hashes)
end
end
end
......@@ -747,4 +747,8 @@ module GitHub
end
end
end
def pull_request_commits(user, repo, pr)
open_api(url_to("repos", user, repo, "pulls", pr, "commits?per_page=100")).map { |c| c["sha"] }
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment