Skip to content

Commit

Permalink
Extract create backmerge PR methods in helper files (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Sep 26, 2024
2 parents 0d67638 + 6efc4c5 commit 4b5edee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 73 deletions.
2 changes: 2 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ VERSION_FILE = Fastlane::Wpmreleasetoolkit::Versioning::AndroidVersionFile.new(v
import 'lanes/build.rb'
import 'lanes/localization.rb'
import 'lanes/release.rb'
# This helper is only used in the release lanes but it needs to be imported here in order to access Fastlane-specific API and our methods like release_version_current
import 'lib/release_helpers.rb'

platform :android do
ENV['PROJECT_ROOT_FOLDER'] = "#{File.dirname(File.expand_path(__dir__))}/"
Expand Down
76 changes: 3 additions & 73 deletions fastlane/lanes/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@

trigger_beta_build(branch_to_build: release_branch_name(release_version: version))

pr_url = create_backmerge_pr!

message = <<~MESSAGE
Code freeze completed successfully. Next, review and merge the [integration PR](#{pr_url}).
MESSAGE
buildkite_annotate(context: 'code-freeze-completed', style: 'success', message: message) if is_ci
UI.success(message)
create_backmerge_prs!
end

desc 'Updates store metadata and runs the release checks'
Expand Down Expand Up @@ -153,13 +147,7 @@

build_and_upload_release(create_release: true)

pr_url = create_backmerge_pr!

message = <<~MESSAGE
Release finalized successfully. Next, review and merge the [integration PR](#{pr_url}).
MESSAGE
buildkite_annotate(context: 'finalize-release-completed', style: 'success', message: message) if is_ci
UI.success(message)
create_backmerge_prs!

UI.message('Attempting to remove release branch protection in GitHub...')

Expand Down Expand Up @@ -204,26 +192,7 @@
name: version_number
)

pr_urls = create_backmerge_prs!

# It's possible that no backmerge was created when:
#
# - there are no hotfixes in development and the next release code freeze has not been started
# - nothing changes in the current release branch since release finalization
#
# As a matter of fact, in the context of Simplenote Android, the above is the most likely scenario.
style, message = if pr_urls.empty?
['info', 'No backmerge PR was required']
else
[
'success', <<~MESSAGE
The following backmerge PR#{pr_urls.length > 1 ? '(s) were' : ' was'} created:
#{pr_urls.map { |url| "- #{url}" }}
MESSAGE
]
end
buildkite_annotate(style: style, context: 'backmerge-prs-outcome', message: message) if is_ci
UI.success(message)
create_backmerge_prs!

# At this point, an intermediate branch has been created by creating a backmerge PR to a hotfix or the next version release branch.
# This allows us to safely delete the `release/*` branch.
Expand Down Expand Up @@ -312,45 +281,6 @@ def report_milestone_error(error_title:)
buildkite_annotate(style: 'warning', context: 'error-with-milestone', message: error_message) if is_ci
end

def create_backmerge_pr!
pr_urls = create_backmerge_prs!

return pr_urls unless pr_urls.length > 1

backmerge_error_message = UI.user_error! <<~ERROR
Unexpectedly opened more than one backmerge pull request. URLs:
#{pr_urls.map { |url| "- #{url}" }.join("\n")}
ERROR
buildkite_annotate(style: 'error', context: 'error-creating-backmerge', message: backmerge_error_message) if is_ci
UI.user_error!(backmerge_error_message)
end

# Notice the plural in the name.
# The action this method calls may create multiple backmerge PRs, depending on how many release branches with version greater than the source are in the remote.
def create_backmerge_prs!
version = release_version_current

create_release_backmerge_pull_request(
repository: GITHUB_REPO,
source_branch: release_branch_name(release_version: version),
labels: ['Releases'],
milestone_title: release_version_next
)
rescue StandardError => e
error_message = <<-MESSAGE
Error creating backmerge pull request(s):
#{e.message}
If this is not the first time you are running the release task, the backmerge PR(s) for the version `#{version}` might have already been previously created.
Please close any pre-existing backmerge PR for `#{version}`, delete the previous merge branch, then run the release task again.
MESSAGE

buildkite_annotate(style: 'error', context: 'error-creating-backmerge', message: error_message) if is_ci

UI.user_error!(error_message)
end

def trigger_buildkite_release_build(branch:, beta:)
build_url = buildkite_trigger_build(
buildkite_organization: BUILDKITE_ORGANIZATION,
Expand Down
63 changes: 63 additions & 0 deletions fastlane/lib/release_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

# Notice the plural in the name.
# The action this method calls may create multiple backmerge PRs, depending on how many release branches with version greater than the source are in the remote.
def create_backmerge_prs!(
version: release_version_current,
source_branch: release_branch_name(release_version: version),
labels: ['Releases'],
milestone_title: release_version_next
)
begin
pr_urls = create_release_backmerge_pull_request(
repository: GITHUB_REPO,
source_branch: source_branch,
labels: labels,
milestone_title: milestone_title
)
rescue StandardError => e
error_message = create_backmerge_error_message(error: e, version: version)

buildkite_annotate(style: 'error', context: 'error-creating-backmerge', message: error_message) if is_ci

UI.user_error!(error_message)
end

# It's possible that no backmerge was created when:
#
# - there are no hotfixes in development and the next release code freeze has not been started
# - nothing changes in the current release branch since release finalization
#
# As a matter of fact, in the context of Simplenote Android, the above is the most likely scenario.
style = pr_urls.empty? ? 'info' : 'success'
message = create_backmerge_success_message(pr_urls: pr_urls)

buildkite_annotate(style: style, context: 'backmerge-prs-outcome', message: message) if is_ci

UI.success(message)

pr_urls
end

def create_backmerge_error_message(error:, version:)
<<~MESSAGE
Error creating backmerge pull request(s):
#{error.message}
If this is not the first time you are running the release task, the backmerge PR(s) for the version `#{version}` might have already been previously created.
Please close any pre-existing backmerge PR for `#{version}`, delete the previous merge branch, then run the release task again.
MESSAGE
end

def create_backmerge_success_message(pr_urls:)
if pr_urls.empty?
'No backmerge PR was required.'
else
<<~MESSAGE
The following backmerge PR#{pr_urls.length > 1 ? '(s) were' : ' was'} created:
#{pr_urls.map { |url| "- #{url}" }.join("\n")}
MESSAGE
end
end

0 comments on commit 4b5edee

Please sign in to comment.