Skip to content

Commit

Permalink
[action] Adds FL_GIT_BRANCH_DONT_USE_ENV_VARS to git_branch (#21597)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer committed Oct 28, 2023
1 parent 72d83cd commit cec29b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fastlane/lib/fastlane/actions/git_branch.rb
Expand Up @@ -16,7 +16,7 @@ def self.description
end

def self.details
"If no branch could be found, this action will return an empty string. This is a wrapper for the internal action Actions.git_branch"
"If no branch could be found, this action will return an empty string. If `FL_GIT_BRANCH_DONT_USE_ENV_VARS` is `true`, it'll ignore CI ENV vars. This is a wrapper for the internal action Actions.git_branch"
end

def self.available_options
Expand Down
3 changes: 3 additions & 0 deletions fastlane/lib/fastlane/helper/git_helper.rb
Expand Up @@ -121,7 +121,10 @@ def self.last_git_commit_hash(short)

# Returns the current git branch, or "HEAD" if it's not checked out to any branch
# Can be replaced using the environment variable `GIT_BRANCH`
# unless `FL_GIT_BRANCH_DONT_USE_ENV_VARS` is `true`
def self.git_branch
return self.git_branch_name_using_HEAD if FastlaneCore::Env.truthy?('FL_GIT_BRANCH_DONT_USE_ENV_VARS')

env_name = SharedValues::GIT_BRANCH_ENV_VARS.find { |env_var| FastlaneCore::Env.truthy?(env_var) }
ENV.fetch(env_name.to_s) do
self.git_branch_name_using_HEAD
Expand Down
18 changes: 18 additions & 0 deletions fastlane/spec/actions_specs/git_branch_spec.rb
Expand Up @@ -13,6 +13,24 @@
end
end

describe "CI set ENV values but FL_GIT_BRANCH_DONT_USE_ENV_VARS is true" do
Fastlane::Actions::SharedValues::GIT_BRANCH_ENV_VARS.each do |env_var|
it "gets the value from Git directly with #{env_var}" do
expect(Fastlane::Actions).to receive(:sh)
.with("git rev-parse --abbrev-ref HEAD", log: false)
.and_return("branch-name-from-git")

FastlaneSpec::Env.with_env_values(env_var => "#{env_var}-branch-name", 'FL_GIT_BRANCH_DONT_USE_ENV_VARS' => 'true') do
result = Fastlane::FastFile.new.parse("lane :test do
git_branch
end").runner.execute(:test)

expect(result).to eq("branch-name-from-git")
end
end
end
end

describe "with no CI set ENV values" do
it "gets the value from Git directly" do
expect(Fastlane::Actions).to receive(:sh)
Expand Down

0 comments on commit cec29b3

Please sign in to comment.