Skip to content

Commit b828867

Browse files
authored
[Tooling] Improve new_hotfix_release lane to work when there's no release tag (#22355)
* Improve hotfix lane to work when there's no release tag * Add log when there's no release tag * Improve code consistency * Check Git tags on remote * 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
1 parent 889668d commit b828867

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
@@ -212,7 +212,20 @@
212212

213213
# Parse the provided version into an AppVersion object
214214
parsed_version = VERSION_FORMATTER.parse(new_version)
215+
# Validate that this is a hotfix version (must have a patch component > 0)
216+
UI.user_error!("Invalid hotfix version '#{new_version}'. Must include a patch number.") unless parsed_version.patch.to_i.positive?
215217
previous_version = VERSION_FORMATTER.release_version(VERSION_CALCULATOR.previous_patch_version(version: parsed_version))
218+
previous_release_branch = "release/#{previous_version}"
219+
220+
# Determine the base for the hotfix branch: either a tag or a release branch
221+
base_ref_for_hotfix = if git_tag_exists(tag: previous_version, remote: true)
222+
previous_version
223+
elsif Fastlane::Helper::GitHelper.branch_exists_on_remote?(branch_name: previous_release_branch)
224+
UI.message("ℹ️ Tag '#{previous_version}' not found on the remote. Using release branch '#{previous_release_branch}' as the base for hotfix instead.")
225+
previous_release_branch
226+
else
227+
UI.user_error!("Neither tag '#{previous_version}' nor branch '#{previous_release_branch}' exists on the remote! A hotfix branch cannot be created.")
228+
end
216229

217230
# Check versions
218231
message = <<-MESSAGE
@@ -223,7 +236,7 @@
223236
Current build code: #{current_build_code}
224237
New build code: #{new_build_code}
225238
226-
Branching from tag: #{previous_version}
239+
Branching from #{base_ref_for_hotfix}
227240
228241
MESSAGE
229242

@@ -232,22 +245,27 @@
232245
UI.user_error!('Aborted by user request') unless skip_confirm || UI.confirm('Do you want to continue?')
233246

234247
# Check tags
235-
UI.user_error!("The version `#{new_version}` tag already exists!") if git_tag_exists(tag: new_version)
236-
UI.user_error!("Version #{previous_version} is not tagged! A hotfix branch cannot be created.") unless git_tag_exists(tag: previous_version)
248+
UI.user_error!("Version '#{new_version}' already exists on the remote! Abort!") if git_tag_exists(tag: new_version, remote: true)
249+
250+
# Fetch the base ref to ensure it's available locally
251+
sh('git', 'fetch', 'origin', base_ref_for_hotfix)
252+
253+
hotfix_branch = "release/#{new_version}"
254+
ensure_branch_does_not_exist!(hotfix_branch)
237255

238256
# Create the hotfix branch
239-
UI.message 'Creating hotfix branch...'
240-
Fastlane::Helper::GitHelper.create_branch("release/#{new_version}", from: previous_version)
241-
UI.success "Done! New hotfix branch is: #{git_branch}"
257+
UI.message("Creating hotfix branch from '#{base_ref_for_hotfix}'...")
258+
Fastlane::Helper::GitHelper.create_branch(hotfix_branch, from: base_ref_for_hotfix)
259+
UI.success("Done! New hotfix branch is: '#{git_branch}'")
242260

243261
# Bump the hotfix version and build code and write it to the `version.properties` file
244-
UI.message 'Bumping hotfix version and build code...'
262+
UI.message('Bumping hotfix version and build code...')
245263
VERSION_FILE.write_version(
246264
version_name: new_version,
247265
version_code: new_build_code
248266
)
249267
commit_version_bump
250-
UI.success "Done! New Release Version: #{current_release_version}. New Build Code: #{current_build_code}"
268+
UI.success("Done! New Release Version: '#{current_release_version}'. New Build Code: '#{current_build_code}'")
251269

252270
push_to_git_remote(tags: false)
253271
end

0 commit comments

Comments
 (0)