Skip to content

Commit 7cc2d8a

Browse files
authored
[Tooling] Improve new_hotfix_release lane to work when there's no release tag (#16364)
2 parents 623370a + 1a9b7f0 commit 7cc2d8a

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

fastlane/Fastfile

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,27 +418,45 @@ platform :ios do
418418

419419
# Parse the provided version into an AppVersion object
420420
parsed_version = VERSION_FORMATTER.parse(version)
421+
# Validate that this is a hotfix version (must have a patch component > 0)
422+
UI.user_error!("Invalid hotfix version '#{version}'. Must include a patch number.") unless parsed_version.patch.to_i.positive?
421423
build_code_hotfix = BUILD_CODE_FORMATTER.build_code(version: parsed_version)
422424
previous_version = VERSION_FORMATTER.release_version(VERSION_CALCULATOR.previous_patch_version(version: parsed_version))
425+
previous_release_branch = "release/#{previous_version}"
426+
427+
# Determine the base for the hotfix branch: either a tag or a release branch
428+
base_ref_for_hotfix = if git_tag_exists(tag: previous_version, remote: true)
429+
previous_version
430+
elsif Fastlane::Helper::GitHelper.branch_exists_on_remote?(branch_name: previous_release_branch)
431+
UI.message("ℹ️ Tag '#{previous_version}' not found on the remote. Using release branch '#{previous_release_branch}' as the base for hotfix instead.")
432+
previous_release_branch
433+
else
434+
UI.user_error!("Neither tag '#{previous_version}' nor branch '#{previous_release_branch}' exists on the remote! A hotfix branch cannot be created.")
435+
end
423436

424437
# Check versions
425438
UI.important <<-MESSAGE
426439
427440
New hotfix version: #{version}
428441
New build code: #{build_code_hotfix}
429-
Branching from tag: #{previous_version}
442+
Branching from #{base_ref_for_hotfix}
430443
431444
MESSAGE
432445
UI.user_error!("Terminating as requested. Don't forget to run the remainder of this automation manually.") unless skip_confirm || UI.confirm('Do you want to continue?')
433446

434447
# Check tags
435-
UI.user_error!("Version #{version} already exists! Abort!") if git_tag_exists(tag: version)
436-
UI.user_error!("Version #{previous_version} is not tagged! A hotfix branch cannot be created.") unless git_tag_exists(tag: previous_version)
448+
UI.user_error!("Version '#{version}' already exists on the remote! Abort!") if git_tag_exists(tag: version, remote: true)
449+
450+
# Fetch the base ref to ensure it's available locally
451+
sh('git', 'fetch', 'origin', base_ref_for_hotfix)
452+
453+
hotfix_branch = "release/#{version}"
454+
ensure_branch_does_not_exist!(hotfix_branch)
437455

438456
# Create the hotfix branch
439-
UI.message('Creating hotfix branch...')
440-
Fastlane::Helper::GitHelper.create_branch("release/#{version}", from: previous_version)
441-
UI.success("Done! New hotfix branch is: #{git_branch}")
457+
UI.message("Creating hotfix branch from '#{base_ref_for_hotfix}'...")
458+
Fastlane::Helper::GitHelper.create_branch(hotfix_branch, from: base_ref_for_hotfix)
459+
UI.success("Done! New hotfix branch is: '#{git_branch}'")
442460

443461
# Bump the hotfix version and build code and write it to the `xcconfig` file
444462
UI.message('Bumping hotfix version and build code...')
@@ -451,7 +469,7 @@ platform :ios do
451469
# Push the newly created hotfix branch
452470
push_to_git_remote(tags: false)
453471

454-
UI.success("Done! New Release Version: #{release_version_current}. New Build Code: #{build_code_current}")
472+
UI.success("Done! New Release Version: '#{release_version_current}'. New Build Code: '#{build_code_current}'")
455473
end
456474

457475
# This lane finalizes the hotfix branch, triggering a release build.

0 commit comments

Comments
 (0)