Skip to content

Commit 96c2c69

Browse files
authored
[Tooling] Update localization sync to create a PR (#790)
* Change localization sync to create a PR instead of committing to `trunk` * Remove unused lane parameter * Add team reviewers to localization PR * Add buildkite annotations when creating localization PR
1 parent 342fd04 commit 96c2c69

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed

.buildkite/commands/sync-localization.sh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,5 @@ source use-bot-for-git
1111
echo "--- :rubygems: Setting up Gems"
1212
install_gems
1313

14-
echo "--- :globe_with_meridians: :arrow_up: Generate the source language PO file for GlotPress based on `wp_localization/localization/en-US/main.ftl`"
15-
bundle exec fastlane generate_source_po_file commit_changes:true
16-
17-
echo "--- :globe_with_meridians: :arrow_down: Download and update translations from GlotPress and update the local Fluent files"
18-
bundle exec fastlane download_translations commit_changes:true
19-
20-
git push origin
14+
echo "--- :globe_with_meridians: :arrows_counterclockwise: Synchronizing localization files with GlotPress"
15+
bundle exec fastlane sync_localization

fastlane/Fastfile

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LANE_VALUE_XCFRAMEWORK_CHECKSUM_PATH = 'WP_XCFRAMEWORK_CHECKSUM_PATH'
1313

1414
GITHUB_REPO = 'automattic/wordpress-rs'
1515
GIT_REMOTE_NAME = 'origin'
16+
GIT_BASE_BRANCH = 'trunk'
1617

1718
# Localization constants
1819
LOCALIZATION_FLUENT_FILES_DIR = File.join(PROJECT_ROOT, 'wp_localization', 'localization')
@@ -179,8 +180,9 @@ end
179180
# The resulting PO file is saved as the source file (.pot) for translations and is synced to GlotPress.
180181
#
181182
# @param commit_changes [Boolean] Whether to commit the generated PO file (default: false)
183+
# @param skip_date_only_changes [Boolean] Whether to skip commit if only date headers changed (default: true)
182184
#
183-
lane :generate_source_po_file do |commit_changes: false|
185+
lane :generate_source_po_file do |commit_changes: false, skip_date_only_changes: true|
184186
UI.header('🔄 Converting English Fluent file to PO format')
185187

186188
FileUtils.mkdir_p(File.dirname(LOCALIZATION_PO_SOURCE_FILE))
@@ -202,6 +204,12 @@ lane :generate_source_po_file do |commit_changes: false|
202204
end
203205

204206
if commit_changes
207+
# Check if we should skip commit when only date headers changed
208+
if skip_date_only_changes && only_date_headers_changed?(LOCALIZATION_PO_SOURCE_FILE)
209+
UI.message('ℹ️ Skipping commit - only date headers changed in PO file')
210+
next
211+
end
212+
205213
commit_changed_files(
206214
files: LOCALIZATION_PO_SOURCE_FILE,
207215
message: 'Update source PO file (en-US.pot) to be synced to GlotPress'
@@ -248,6 +256,74 @@ lane :download_translations do |commit_changes: false|
248256
end
249257
end
250258

259+
# Synchronizes localization files with GlotPress and creates a PR
260+
#
261+
# This lane performs a complete localization sync:
262+
# 1. Generates source PO file from English Fluent file
263+
# 2. Downloads latest translations from GlotPress
264+
# 3. Creates a PR with the changes instead of pushing directly to trunk
265+
#
266+
lane :sync_localization do
267+
github_token = ENV['GITHUB_TOKEN'] || UI.user_error!('GITHUB_TOKEN environment variable is required')
268+
269+
sync_branch = 'localization/sync-translations'
270+
271+
Fastlane::Helper::GitHelper.delete_local_branch_if_exists!(sync_branch)
272+
Fastlane::Helper::GitHelper.create_branch(sync_branch)
273+
274+
po_result = generate_source_po_file(commit_changes: true, skip_date_only_changes: true)
275+
translations_result = download_translations(commit_changes: true)
276+
277+
# Check if either operation resulted in changes
278+
has_changes = !po_result.nil? || !translations_result.nil?
279+
unless has_changes
280+
UI.important('⚠️ No changes to commit')
281+
282+
if is_ci
283+
buildkite_annotate(
284+
context: 'localization-sync-no-changes',
285+
style: 'info',
286+
message: 'No localization changes to commit. No PR was created.'
287+
)
288+
end
289+
290+
next
291+
end
292+
293+
Fastlane::Helper::GitHelper.delete_remote_branch_if_exists!(sync_branch)
294+
295+
# Push the localization sync branch to remote
296+
push_to_git_remote(set_upstream: true, tags: false)
297+
298+
pr_result = create_pull_request(
299+
repo: GITHUB_REPO,
300+
title: 'Sync localization files with GlotPress',
301+
body: <<~BODY,
302+
This PR synchronizes localization files with GlotPress:
303+
304+
- **Source PO file**: Updated from English Fluent file for GlotPress sync
305+
- **Translations**: Downloaded latest translations from GlotPress and converted to Fluent format
306+
307+
🔄 This is an automated localization sync.
308+
BODY
309+
labels: 'Localization',
310+
team_reviewers: ['wordpress-rs'],
311+
base: GIT_BASE_BRANCH,
312+
head: sync_branch,
313+
api_token: github_token
314+
)
315+
316+
UI.success("✅ Created pull request: #{pr_result}") if pr_result
317+
318+
if is_ci && pr_result
319+
buildkite_annotate(
320+
context: 'localization-sync-pr',
321+
style: 'info',
322+
message: "Localization Sync Pull Request: #{pr_result}"
323+
)
324+
end
325+
end
326+
251327
# Downloads PO files from GlotPress for existing project locales
252328
#
253329
# This lane fetches translated PO files from GlotPress for all supported locales
@@ -357,4 +433,34 @@ def commit_changed_files(files:, message:, push: false)
357433
elsif push
358434
push_to_git_remote(set_upstream: true, tags: false)
359435
end
436+
437+
result
438+
end
439+
440+
# Checks if only date headers changed in a PO file
441+
#
442+
# This method uses git diff to check if the only changes in a PO file
443+
# are the POT-Creation-Date and PO-Revision-Date headers.
444+
#
445+
# @param file_path [String] Path to the PO file to check
446+
# @return [Boolean] true if only date headers changed, false otherwise
447+
#
448+
def only_date_headers_changed?(file_path)
449+
begin
450+
diff_output = sh('git', 'diff', file_path).strip
451+
rescue
452+
return false
453+
end
454+
455+
return false if diff_output.empty?
456+
457+
# Parse diff lines that start with + or - (actual changes)
458+
changed_lines = diff_output.split("\n").select do |line|
459+
line.start_with?('+') || line.start_with?('-')
460+
end.reject do |line|
461+
# Exclude diff metadata lines
462+
line.start_with?('+++') || line.start_with?('---')
463+
end
464+
465+
changed_lines.all? { |l| l.include?('"POT-Creation-Date:') || l.include?('"PO-Revision-Date:') }
360466
end

0 commit comments

Comments
 (0)