Skip to content

Commit

Permalink
try using a github check
Browse files Browse the repository at this point in the history
  • Loading branch information
porter-stripe committed Feb 8, 2024
1 parent 1fd1865 commit 0397e9b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/check-translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Check Missing Translations
on:
pull_request:
types: [opened, labeled, unlabeled, synchronize]
paths:
- '**.strings'

jobs:
check-translations:
runs-on: ubuntu-latest
permissions:
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
- name: Install dependencies
run: gem install net-http net-uri json
- name: Run translation check script
env:
LOKALISE_API_KEY: ${{ secrets.LOKALISE_API_KEY }}
run: ruby .github/workflows/check_translations.rb
- name: Read missing translations
id: missing
run: echo "::set-output name=translations::$(<missing_translations.txt)"
- uses: peter-evans/find-comment@a54c31d7fa095754bfef525c0c8e5e5674c4b4b1
id: find_comment
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: Missing Translations
- uses: peter-evans/create-or-update-comment@67dcc547d311b736a8e6c5c236542148a47adc3d
with:
body: |
Missing Translations
The following translations are missing:
${{ steps.missing.outputs.translations }}
By adding the label `ship-without-translations` to this PR, I acknowledge that there are missing translations.
edit-mode: replace
comment-id: ${{ steps.find_comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: Fail if not accepted
if: "!contains(github.event.pull_request.labels.*.name, 'ship-without-translations')"
run: exit 1
34 changes: 14 additions & 20 deletions ci_scripts/check_for_untranslated_strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
$SCRIPT_DIR = __dir__
$ROOT_DIR = File.expand_path('..', $SCRIPT_DIR)

def should_skip_translation_check()
last_commit_message = ENV['BITRISE_GIT_MESSAGE']
if last_commit_message&.include?('Translations are not required for this pull request')
puts 'Translation check skipped due to pull request summary containing "Translations are not required for this pull request.".'
exit 0
end
end

def api_get_request(endpoint, token)
uri = URI(endpoint)
req = Net::HTTP::Get.new(uri)
Expand All @@ -27,14 +19,14 @@ def api_get_request(endpoint, token)

def get_added_strings(current_dir)
new_strings = {}

strings_files = `git diff --name-only origin/master...`.split("\n").select { |f| f.end_with?(".strings") }

strings_files.each do |file|
added_lines = `git diff origin/master... -- #{file}`.split("\n").select do |line|
line.start_with?('+') && !line.start_with?('+++') && line.include?('=') && !line.match(/^\/\//)
end

new_strings[file] = added_lines.map do |line|
line.delete_prefix('+').strip.split('=')[0].gsub(/\"/, '').strip
end
Expand All @@ -45,8 +37,8 @@ def get_added_strings(current_dir)

def check_lokalise_translations(api_token, project_id, new_added_strings)
keys = api_get_request("https://api.lokalise.com/api2/projects/#{project_id}/keys?limit=5000&include_translations=1", api_token)
all_keys_exist = true
missing_translations = []

new_added_strings.each do |file_path, new_strings|
puts "Checking translation for file #{file_path}"

Expand All @@ -55,23 +47,25 @@ def check_lokalise_translations(api_token, project_id, new_added_strings)

if key
translated_count = key['translations'].count { |t| !t['translation'].empty? }
if translated_count > 30 #Arbitrary number, if we have 30 translations at least we consider it translated
if translated_count > 30
puts "Translations for '#{str}' exists."
else
puts "Translations for '#{str}' do not exist."
all_keys_exist = false
missing_translations << str
end
else
puts "String '#{str}' does not exist. Make sure you have uploaded your strings to Lokalise."
all_keys_exist = false
missing_translations << str
end
end
end
puts 'If you would like to skip this check, update your pull request summary to include "Translations are not required for this pull request." and re-run this check.'
exit 1 unless all_keys_exist

missing_translations
end

# early exit if last commit has '[skip translations]' prefix.
should_skip_translation_check()
new_strings_added = get_added_strings($ROOT_DIR)
check_lokalise_translations(ENV['LOKALISE_API_KEY'], '747824695e51bc2f4aa912.89576472', new_strings_added)
missing_translations = check_lokalise_translations(ENV['LOKALISE_API_KEY'], '747824695e51bc2f4aa912.89576472', new_strings_added)

if missing_translations.any?
File.open("missing_translations.txt", 'w') { |f| f.write missing_translations.join("\n") }
end

0 comments on commit 0397e9b

Please sign in to comment.