Skip to content

Commit 666caf9

Browse files
authored
[Tooling] Improve new_hotfix_release lane to work when there's no release tag (#1791)
2 parents 2c6489e + 49a9a4e commit 666caf9

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

fastlane/Fastfile

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,25 @@ platform :android do
7979
new_version = version_name
8080
version_code_new = version_code
8181

82+
# Validate that this is a hotfix version (must have a patch component > 0)
83+
parsed_version = VERSION_FORMATTER.parse(new_version)
84+
UI.user_error!("Invalid hotfix version '#{new_version}'. Must include a patch number.") unless parsed_version.patch.to_i.positive?
8285
previous_version = VERSION_FORMATTER.release_version(
83-
VERSION_CALCULATOR.previous_patch_version(version: VERSION_FORMATTER.parse(new_version))
86+
VERSION_CALCULATOR.previous_patch_version(version: parsed_version)
8487
)
88+
previous_release_branch = release_branch_name(release_version: previous_version)
8589

86-
UI.user_error!("The version `#{new_version}` tag already exists!") if git_tag_exists(tag: new_version)
87-
UI.user_error!("Version #{previous_version} is not tagged! A hotfix branch cannot be created.") unless git_tag_exists(tag: previous_version)
90+
UI.user_error!("Version '#{new_version}' already exists on the remote! Abort!") if git_tag_exists(tag: new_version, remote: true)
91+
92+
# Determine the base for the hotfix branch: either a tag or a release branch
93+
base_ref_for_hotfix = if git_tag_exists(tag: previous_version, remote: true)
94+
previous_version
95+
elsif Fastlane::Helper::GitHelper.branch_exists_on_remote?(branch_name: previous_release_branch)
96+
UI.message("ℹ️ Tag '#{previous_version}' not found on the remote. Using release branch '#{previous_release_branch}' as the base for hotfix instead.")
97+
previous_release_branch
98+
else
99+
UI.user_error!("Neither tag '#{previous_version}' nor branch '#{previous_release_branch}' exists on the remote! A hotfix branch cannot be created.")
100+
end
88101

89102
message = <<~MESSAGE
90103
Hotfix release:
@@ -94,15 +107,21 @@ platform :android do
94107
- Current build code: #{build_code_current}
95108
- New build code: #{version_code_new}
96109
97-
Branching from tag: #{previous_version}
110+
Branching from #{base_ref_for_hotfix}
98111
MESSAGE
99112
UI.important(message)
100113

101114
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?')
102115

103-
UI.message('Creating hotfix branch...')
104-
Fastlane::Helper::GitHelper.create_branch(release_branch_name(release_version: new_version), from: previous_version)
105-
UI.success("Done! New hotfix branch is: #{git_branch}")
116+
# Fetch the base ref to ensure it's available locally
117+
sh('git', 'fetch', 'origin', base_ref_for_hotfix)
118+
119+
hotfix_branch = release_branch_name(release_version: new_version)
120+
ensure_branch_does_not_exist!(hotfix_branch)
121+
122+
UI.message("Creating hotfix branch from '#{base_ref_for_hotfix}'...")
123+
Fastlane::Helper::GitHelper.create_branch(hotfix_branch, from: base_ref_for_hotfix)
124+
UI.success("Done! New hotfix branch is: '#{git_branch}'")
106125

107126
UI.message('Bumping hotfix version and build code...')
108127
VERSION_FILE.write_version(
@@ -111,7 +130,7 @@ platform :android do
111130
)
112131
commit_version_bump
113132
# Print computed version and build to let user double-check outcome in logs
114-
UI.success("Done! New release version: #{release_version_current}. New build code: #{build_code_current}.")
133+
UI.success("Done! New release version: '#{release_version_current}'. New build code: '#{build_code_current}'.")
115134

116135
UI.important('Pushing new hotfix branch to remote...')
117136
push_to_git_remote(tags: false)

0 commit comments

Comments
 (0)