From 6491e89306cdd9ca91c9fa13c63d36ad8325d9cd Mon Sep 17 00:00:00 2001 From: Ian Maia Date: Tue, 3 Sep 2024 21:26:24 +0200 Subject: [PATCH] Move handling of `origin` to method `point_to_same_commit?` --- ...create_release_backmerge_pull_request_action.rb | 5 +---- .../plugin/wpmreleasetoolkit/helper/git_helper.rb | 13 ++++++++----- spec/git_helper_spec.rb | 14 +++++++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_backmerge_pull_request_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_backmerge_pull_request_action.rb index 9ba142894..2ead2930e 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_backmerge_pull_request_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/create_release_backmerge_pull_request_action.rb @@ -103,10 +103,7 @@ def self.create_backmerge_pr(token:, repository:, title:, head_branch:, base_bra # if there's a callback, make sure it didn't switch branches other_action.ensure_git_branch(branch: "^#{intermediate_branch}/") unless intermediate_branch_created_callback.nil? - base_branch_ref = base_branch.start_with?('origin/') ? base_branch : "origin/#{base_branch}" - head_branch_ref = head_branch.start_with?('origin/') ? head_branch : "origin/#{head_branch}" - - if Fastlane::Helper::GitHelper.point_to_same_commit?(base_branch_ref, head_branch_ref) + if Fastlane::Helper::GitHelper.point_to_same_commit?(base_branch, head_branch) UI.error("No differences between #{head_branch} and #{base_branch}. Skipping PR creation.") return nil end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb index 981a1fc8c..eaf55e5e6 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb @@ -174,20 +174,23 @@ def self.fetch_all_tags # # @param ref1 [String] the first git reference to check. # @param ref2 [String] the second git reference to check. + # @param remote_name [String] the name of the remote repository to use (default is 'origin'). + # If nil or empty, no remote prefix will be used. # # @return [Boolean] true if the two references point to the same commit, false otherwise. # - def self.point_to_same_commit?(ref1, ref2) + def self.point_to_same_commit?(ref1, ref2, remote_name: 'origin') git_repo = Git.open(Dir.pwd) + ref1_full = remote_name.to_s.empty? ? ref1 : "#{remote_name}/#{ref1}" + ref2_full = remote_name.to_s.empty? ? ref2 : "#{remote_name}/#{ref2}" begin - ref1_commit = git_repo.gcommit(ref1) - ref2_commit = git_repo.gcommit(ref2) + ref1_commit = git_repo.gcommit(ref1_full) + ref2_commit = git_repo.gcommit(ref2_full) rescue StandardError => e - puts "Error: #{e.message}" + UI.error "Error fetching commits for #{ref1_full} and #{ref2_full}: #{e.message}" return false end - ref1_commit.sha == ref2_commit.sha end diff --git a/spec/git_helper_spec.rb b/spec/git_helper_spec.rb index 648efa1f1..79e21b954 100644 --- a/spec/git_helper_spec.rb +++ b/spec/git_helper_spec.rb @@ -145,37 +145,37 @@ end it 'checks if a tag and a branch point to the same commit' do - same_commit = described_class.point_to_same_commit?('1.0', 'another-branch') + same_commit = described_class.point_to_same_commit?('1.0', 'another-branch', remote_name: nil) expect(same_commit).to be false end it 'checks if a tag and a branch that had a merge point to the same commit' do - same_commit = described_class.point_to_same_commit?('1.0', 'main') + same_commit = described_class.point_to_same_commit?('1.0', 'main', remote_name: nil) expect(same_commit).to be false end it 'checks if a tag and a commit hash point to the same commit' do - same_commit = described_class.point_to_same_commit?('1.0', commit_hash(commit_message: 'commit D')) + same_commit = described_class.point_to_same_commit?('1.0', commit_hash(commit_message: 'commit D'), remote_name: nil) expect(same_commit).to be false end it 'checks if a commit hash and a branch point to the same commit' do - same_commit = described_class.point_to_same_commit?(commit_hash(commit_message: 'commit B'), 'another-branch') + same_commit = described_class.point_to_same_commit?(commit_hash(commit_message: 'commit B'), 'another-branch', remote_name: nil) expect(same_commit).to be false end it 'checks if commits between the same branch point to the same commit' do - same_commit = described_class.point_to_same_commit?('feature-branch', 'feature-branch') + same_commit = described_class.point_to_same_commit?('feature-branch', 'feature-branch', remote_name: nil) expect(same_commit).to be true end it 'checks if commits between branches that have no difference point to the same commit' do - same_commit = described_class.point_to_same_commit?('another-branch', 'new-branch') + same_commit = described_class.point_to_same_commit?('another-branch', 'new-branch', remote_name: nil) expect(same_commit).to be true end it 'raises error for a non-existent base_ref' do - expect { described_class.point_to_same_commit?('non-existent', 'main') }.to raise_error(StandardError) + expect { described_class.point_to_same_commit?('non-existent', 'main', remote_name: nil) }.to raise_error(StandardError) end end