Skip to content

Commit aa1d94d

Browse files
iangmaiaAliSoftwaremokagio
authored
[Tooling] Improve new_hotfix_release lane to work when there's no release tag (#24996)
* Improve hotfix lane to work when there's no release tag * Add log when there's no release tag * Check if tag exists on remote Co-authored-by: Olivier Halligon <[email protected]> * Clarify logs to make it clearer we're checking tags on remote Co-authored-by: Gio Lodi <[email protected]> * Improve logs for consistency * Add hotfix version validation * Update to fetch base ref to ensure it's available locally * Update code to ensure the new hotfix branch doesn't exist --------- Co-authored-by: Olivier Halligon <[email protected]> Co-authored-by: Gio Lodi <[email protected]>
1 parent 91ee879 commit aa1d94d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

fastlane/lanes/release.rb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,20 @@
277277

278278
# Parse the provided version into an AppVersion object
279279
parsed_version = VERSION_FORMATTER.parse(new_version)
280+
# Validate that this is a hotfix version (must have a patch component > 0)
281+
UI.user_error!("Invalid hotfix version '#{new_version}'. Must include a patch number.") unless parsed_version.patch.to_i.positive?
280282
previous_version = VERSION_FORMATTER.release_version(VERSION_CALCULATOR.previous_patch_version(version: parsed_version))
283+
previous_release_branch = compute_release_branch_name(options: options, version: previous_version)
284+
285+
# Determine the base for the hotfix branch: either a tag or a release branch
286+
base_ref_for_hotfix = if git_tag_exists(tag: previous_version, remote: true)
287+
previous_version
288+
elsif Fastlane::Helper::GitHelper.branch_exists_on_remote?(branch_name: previous_release_branch)
289+
UI.message("ℹ️ Tag '#{previous_version}' not found on the remote. Using release branch '#{previous_release_branch}' as the base for hotfix instead.")
290+
previous_release_branch
291+
else
292+
UI.user_error!("Neither tag '#{previous_version}' nor branch '#{previous_release_branch}' exists on the remote! A hotfix branch cannot be created.")
293+
end
281294

282295
# Check versions
283296
message = <<~MESSAGE
@@ -286,28 +299,33 @@
286299
• Current release version and build code: #{release_version_current} (#{build_code_current}).
287300
• New release version and build code: #{new_version} (#{build_code_hotfix}).
288301
289-
Branching from tag: #{previous_version}
302+
Branching from #{base_ref_for_hotfix}
290303
MESSAGE
291304

292305
UI.important(message)
293306
UI.user_error!('Aborted by user request') unless options[:skip_confirm] || UI.confirm('Do you want to continue?')
294307

295308
# Check tags
296-
UI.user_error!("Version #{new_version} already exists! Abort!") if git_tag_exists(tag: new_version)
297-
UI.user_error!("Version #{previous_version} is not tagged! A hotfix branch cannot be created.") unless git_tag_exists(tag: previous_version)
309+
UI.user_error!("Version '#{new_version}' already exists on the remote! Abort!") if git_tag_exists(tag: new_version, remote: true)
310+
311+
# Fetch the base ref to ensure it's available locally
312+
sh('git', 'fetch', 'origin', base_ref_for_hotfix)
313+
314+
hotfix_branch = compute_release_branch_name(options: options, version: new_version)
315+
ensure_branch_does_not_exist!(hotfix_branch)
298316

299317
# Create the hotfix branch
300-
UI.message 'Creating hotfix branch...'
301-
Fastlane::Helper::GitHelper.create_branch(compute_release_branch_name(options: options, version: new_version), from: previous_version)
302-
UI.success "Done! New hotfix branch is: #{git_branch}"
318+
UI.message("Creating hotfix branch from '#{base_ref_for_hotfix}'...")
319+
Fastlane::Helper::GitHelper.create_branch(hotfix_branch, from: base_ref_for_hotfix)
320+
UI.success("Done! New hotfix branch is: '#{git_branch}'")
303321

304322
# Bump the hotfix version and build code and write it to the `xcconfig` file
305-
UI.message 'Bumping hotfix version and build code...'
323+
UI.message('Bumping hotfix version and build code...')
306324
PUBLIC_VERSION_FILE.write(
307325
version_short: new_version,
308326
version_long: build_code_hotfix
309327
)
310-
UI.success "Done! New Release Version: #{release_version_current}. New Build Code: #{build_code_current}"
328+
UI.success("Done! New Release Version: '#{release_version_current}'. New Build Code: '#{build_code_current}'")
311329

312330
commit_version_and_build_files
313331

0 commit comments

Comments
 (0)