From 415c36041a87ef59eab0c04f557f8f498d762b90 Mon Sep 17 00:00:00 2001 From: Jonathan Chang <me@jonathanchang.org> Date: Thu, 17 Sep 2020 16:06:41 +1000 Subject: [PATCH] dev-cmd/pr-pull: use GitHub API to get PR commits --- Library/Homebrew/dev-cmd/pr-pull.rb | 18 ++++++++---------- Library/Homebrew/test/utils/github_spec.rb | 7 +++++++ Library/Homebrew/utils/github.rb | 4 ++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index 61eeae1ac6..28218da84c 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -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? diff --git a/Library/Homebrew/test/utils/github_spec.rb b/Library/Homebrew/test/utils/github_spec.rb index 37f2fd0483..6c7739ff71 100644 --- a/Library/Homebrew/test/utils/github_spec.rb +++ b/Library/Homebrew/test/utils/github_spec.rb @@ -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 diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 2ca8dc3198..ab34ee32d7 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -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 -- GitLab