From 105b123bdc21d26d391eae7da4fce69641e8dddb Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Tue, 17 Oct 2023 15:05:05 -0400 Subject: [PATCH 1/4] Adds ensure_current_branch_using_HEAD git helper --- .../wpmreleasetoolkit/helper/git_helper.rb | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb index d3126025a..71c4a6a27 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb @@ -193,6 +193,31 @@ def self.current_git_branch Fastlane::Actions.git_branch_name_using_HEAD end + # Raises an exception and stop the lane execution if the repo is not on a specific branch + # It uses the the `current_git_branch` method which doesn't allow for environment variable modifications. + # + # Modified from fastlane's original `ensure_git_branch` implementation: + # + # https://github.com/fastlane/fastlane/blob/2.213.0/fastlane/lib/fastlane/actions/ensure_git_branch.rb + # + # Note that picking a proper name for this method is a bit hard as the following considerations need + # to be taken into account: + # + # 1. fastlane's original action is called `ensure_git_branch`. + # 2. We have a deprecated helper called `ensure_on_branch`. We could have used this name, but + # the implementation is different, so it'd be a breaking change for all clients. + # 3. This method relies on the `current_git_branch` helper which internally + # uses the `git_branch_name_using_HEAD` fastlane helper. + def self.ensure_current_branch_using_HEAD(branch) + current_branch = current_git_branch + branch_expr = /#{branch}/ + if current_branch =~ branch_expr + UI.success("Git branch matches `#{branch}`, all good! 💪") + else + UI.user_error!("Git is not on a branch matching `#{branch}`. Current branch is `#{current_branch}`! Please ensure the repo is checked out to the correct branch.") + end + end + # Checks if a branch exists locally. # # @param [String] branch_name The name of the branch to check for From 37e61829ffd7b11ca80482748ab210482e494767 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Tue, 17 Oct 2023 15:05:20 -0400 Subject: [PATCH 2/4] Adds unit tests for ensure_current_branch_using_HEAD git helper --- spec/git_helper_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/git_helper_spec.rb b/spec/git_helper_spec.rb index b2ca50d4f..9c85f0b9a 100644 --- a/spec/git_helper_spec.rb +++ b/spec/git_helper_spec.rb @@ -66,6 +66,19 @@ expect(described_class.has_git_lfs?).to be false end + it 'can ensure currently on release branch' do + allow(described_class).to receive(:current_git_branch).and_return('release/23.5') + expect(described_class.ensure_current_branch_using_HEAD('^release/')).to be true + end + + it 'fails for unexpected git branch' do + current_branch = 'trunk' + branch = '^release/' + expected_error = "Git is not on a branch matching `#{branch}`. Current branch is `#{current_branch}`! Please ensure the repo is checked out to the correct branch." + allow(described_class).to receive(:current_git_branch).and_return('trunk') + expect(described_class.ensure_current_branch_using_HEAD(branch)).to raise_error(FastlaneCore::Interface::FastlaneError, expected_error) + end + context('commit(message:, files:)') do before(:each) do allow_fastlane_action_sh From dd816abd41b25238dc648dc7834da55bf6637cd6 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Tue, 17 Oct 2023 15:09:12 -0400 Subject: [PATCH 3/4] Deprecate ensure_on_branch! git helper --- lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb index 71c4a6a27..9ae5f061f 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb @@ -228,6 +228,8 @@ def self.branch_exists?(branch_name) !Action.sh('git', 'branch', '--list', branch_name).empty? end + # DEPRECATED! + # # Ensure that we are on the expected branch, and abort if not. # # @param [String] branch_name The name of the branch we expect to be on @@ -235,6 +237,7 @@ def self.branch_exists?(branch_name) # @raise [UserError] Raises a user_error! and interrupts the lane if we are not on the expected branch. # def self.ensure_on_branch!(branch_name) + UI.important 'Warning: This helper is deprecated, please use Fastlane::Helper::GitHelper.ensure_current_branch_using_HEAD instead!' current_branch_name = Action.sh('git', 'symbolic-ref', '-q', 'HEAD') UI.user_error!("This command works only on #{branch_name} branch") unless current_branch_name.include?(branch_name) end From 0b0669ca2722069305be609bf19d37eeb86b9fad Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Tue, 17 Oct 2023 15:11:15 -0400 Subject: [PATCH 4/4] Adds changelog for ensure_current_branch_using_HEAD --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77c08e3d6..3e4777853 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ _None_ ### New Features -_None_ +- Adds `ensure_current_branch_using_HEAD` git helper. [#520] ### Bug Fixes @@ -18,7 +18,7 @@ _None_ ### Internal Changes -_None_ +- Deprecates `ensure_on_branch!` in favor of `ensure_current_branch_using_HEAD` [#520] ## 9.1.0