Skip to content

Commit

Permalink
feat: Improve localization workflow such that it allows for branch/PR…
Browse files Browse the repository at this point in the history
… focused translation (#8565)

This allows for branch/PR focused translations

## **Description**

Currently, translation only happens once changes to the en.json file are
merged to main. These changes could include new keys being added or
changes to english text. What this means is that there is no way
currently to require that translations be in place before the feature
goes live. It could make it through translation before being released
but there is currently no way to require that we wait for translations.

## **Related issues**

Fixes: [8564](#8564)

## **Manual testing steps - To be done after merge and with Desi**
**Use case 1 (feature branch requires translation):**

- Developer creates a branch for feature which requires translations
- They add keys or change english source and when they are ready for
translations to be done they create a PR and add the label
“ready-for-translation”
- Manually trigger crowdin-branch-pr-ready-for-translation.yml ON THE
BRANCH
- Confirm branch created in crowdin with just the untranslated keys
(plus any untranslated ones from main or any translated but not merged
keys from main) - This is where we could get confusion on the BLEND
side.
- Wait for translations (Desi will likely have to talk to BLEND to let
them know what we are doing and get these translations done so that we
can finish out the test)
- Manually trigger the translation check before translations complete
(confirm no translation PR created)
- Manually trigger the translation check after translations complete
(confirm PR created off this branch)
- Merge translation PR into branch, manually remove label (later PR will
do this automatically), manually delete localization branch for this
branch (later PR will do this automatically)
- Merge PR for feature to main
- Manually trigger crowdin-upload-both-sources-translations.yml and
ensure translations show on main in crowdin.
- Manually trigger crowdin-branch-cleanup.yml and make sure the branch
is removed from crowdin

**Use Case 2 (main branch translations):**

- Developer is working just normally and adds new keys or changes
english version in some way.
- They go through normal PR flow once they merge to main
- Manually trigger crowdin-upload-both-sources-translations.yml
- Confirm new keys are on main
- Work with Blend to get one thing translated so we can see
crowdin-pull-branch-pr-completed-translations.yml pull down the partial
translation
- Manually trigger crowdin-pull-branch-pr-completed-translations.yml and
check the localization PR has the translations.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've clearly explained what problem this PR is solving and how it
is solved.
- [x] I've linked related issues
- [x] I've included manual testing steps
- [ ] I've included screenshots/recordings if applicable
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [ ] I’ve properly set the pull request status:
  - [ ] In case it's not yet "ready for review", I've set it to "draft".
- [ ] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
  • Loading branch information
desi and mcmire authored Apr 17, 2024
1 parent 191456d commit 3ef99f1
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/crowdin-branch-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Crowdin - Branch and Label Cleanup for merged localization PR
# This action should delete the branch from Crowdin after the localization PR is
# merged to the original branch. It should also remove the "ready-for-translation" label

# TODO: Add trigger for merge of localization PR.
on: workflow_dispatch

jobs:
prestep:
runs-on: ubuntu-latest
outputs:
branch: ${{ steps.extract_current_branch.outputs.branch }}
pr: ${{ steps.get-prs.outputs.pr }}
steps:
- name: Extract current branch name
shell: bash
run: |
echo "running on branch ${GITHUB_REF##*/}"
echo "other version: ${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
id: extract_current_branch

- name: Get PR with Label for this branch
id: get-prs
run: |
LABEL="ready-for-translation"
API_URL="https://api.github.com/repos/Metamask/crowdin-sandbox/pulls?head:${{steps.extract_current_branch.outputs.branch}}&state=open&per_page=100"
# Fetch the list of open pull requests with the specified label using curl
PRS=$(curl -sS --header "Authorization: Bearer $GITHUB_TOKEN" "$API_URL")
PR=$(echo "$PRS" | jq -r '.[] | select(.labels[].name == "'"$LABEL"'") | .number | @json')
echo "Found PR: $PR"
echo "pr=$PR" >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CURRENT_BRANCH: ${{ steps.extract_current_branch.outputs.branch }}

github_cleanup:
runs-on: ubuntu-latest
needs: prestep
steps:
- name: Remove label from PR
uses: actions/github-script@v3
if: needs.prestep.outputs.pr != null || needs.prestep.outputs.pr != ''
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const label = "ready-for-translation";
await github.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ needs.prestep.outputs.pr }},
name: label
});
crowdin_cleanup:
runs-on: ubuntu-latest
needs: prestep
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ needs.prestep.outputs.branch }}

- name: Delete branch within Crowdin
if: needs.prestep.outputs.branch != 'main'
uses: crowdin/github-action@c953b17499daa6be3e5afbf7a63616fb02d8b18d
with:
command: branch delete ${{ needs.prestep.outputs.branch }}
command_args: -v
50 changes: 50 additions & 0 deletions .github/workflows/crowdin-branch-pr-ready-for-translation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Crowdin - Ready for translations label added, push to crowdin

# When an individual is working on a feature which requires translations, they can
# add a label "ready-for-translation" which will trigger this action to push the
# source and translation files to Crowdin. We will always push main as the base of
# the crowdin branch creation and then push in the changes over the top. This ensures
# that the translations which have already been done and approved previously do not
# show as needing to be translated again in the crowdin branch.

# TODO: switch to trigger on label add once testing complete
on: workflow_dispatch

jobs:
crowdin-upload:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

steps:
- name: Extract current branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
id: extract_current_branch

- name: Checkout
uses: actions/checkout@v3
with:
ref: main

- name: Crowdin push main as baseline
uses: crowdin/github-action@c953b17499daa6be3e5afbf7a63616fb02d8b18d
with:
crowdin_branch_name: ${{ steps.extract_current_branch.outputs.branch }}
upload_sources: true
upload_translations_args: --import-eq-suggestions --auto-approve-imported --verbose
upload_translations: true

- name: Checkout Branch and push to crowdin
uses: actions/checkout@v3
with:
ref: ${{ steps.extract_current_branch.outputs.branch }}
- name: Crowdin sources push
uses: crowdin/github-action@c953b17499daa6be3e5afbf7a63616fb02d8b18d
with:
crowdin_branch_name: ${{ steps.extract_current_branch.outputs.branch }}
upload_sources: true
upload_sources_args: --auto-update --verbose
upload_translations: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Crowdin - Find all branches with translations and trigger completion checks
# This workflow will run on a schedule. It will pull all pull requests with a label of
# ready-for-translation and create a matrix of the associated branches to run the
# crowdin-reusable-translation-download.yml workflow on.
# That workflow will check the status of the translations and if complete create a pull
# request with the translations off of the branch.

permissions:
contents: write
pull-requests: write

# TODO: Add a schedule to run this workflow twice a day(?) once the testing is complete
on: workflow_dispatch

jobs:
run-check-and-download-for-branch:
needs: get-branches
if: ${{ needs.get-branches.outputs.matrix != '[]' && needs.get-branches.outputs.matrix != '' }}
strategy:
fail-fast: false
matrix:
branch: ${{fromJson(needs.get-branches.outputs.matrix)}}
uses: ./.github/workflows/crowdin-reusable-translation-download.yml
with:
branch: ${{ matrix.branch }}
secrets:
gh_token: ${{ secrets.GITHUB_TOKEN }}
crowdin_personal_token: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
crowdin_project_id: ${{ secrets.CROWDIN_PROJECT_ID }}

get-branches:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix-outputs.outputs.matrix }}
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Get Branches with Label
id: get-branches
run: |
GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}"
LABEL="ready-for-translation"
API_URL="https://api.github.com/repos/Metamask/crowdin-sandbox/pulls?state=open&per_page=100"
# Fetch the list of open pull requests with the specified label using curl
PRS=$(curl -sS --header "Authorization: Bearer $GITHUB_TOKEN" "$API_URL")
BRANCHES=$(echo "$PRS" | jq -r '[.[] | select(.labels[].name == "'"$LABEL"'") | .head.ref] | @json')
echo "Found branches: $BRANCHES"
echo "branches=$BRANCHES" >> "$GITHUB_OUTPUT"
- name: Set up matrix
id: matrix-outputs
run: |
# Parse the branches output and create a matrix
BRANCHES="${{ toJson(steps.get-branches.outputs.branches) }}"
echo "Creating matrix from branches..."
MATRIX="${BRANCHES}"
echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"
64 changes: 64 additions & 0 deletions .github/workflows/crowdin-reusable-translation-download.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Crowdin - Check translation progress and download if complete (unless main)
# This is a reusable workflow that is called by crowdin-pull-branch-pr-completed-translations
# across all branches which have a label of "ready-for-translation" aka being translated.
# This workflow will check the translation progress and download the translations if
# they are 100% translated. If the branch that is running this is main it will skip completion
# check and just pull whatever translations are available.


permissions:
contents: write
pull-requests: write

on:
workflow_call:
inputs:
branch:
required: true
type: string
secrets:
gh_token:
required: true
crowdin_project_id:
required: true
crowdin_personal_token:
required: true

jobs:
crowdin:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.gh_token }}
CROWDIN_PERSONAL_TOKEN: ${{ secrets.crowdin_personal_token }}
CROWDIN_PROJECT_ID: ${{ secrets.crowdin_project_id }}

steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ inputs.branch }}

- name: Check translation progress
# when main just pull whatever you have (aka skip this) - need to test
if: ${{ inputs.branch != 'main' }}
uses: crowdin/github-action@c953b17499daa6be3e5afbf7a63616fb02d8b18d
with:
command: 'status translation'
command_args: '-b ${{ inputs.branch }} --fail-if-incomplete'

- name: Synchronize with Crowdin
uses: crowdin/github-action@c953b17499daa6be3e5afbf7a63616fb02d8b18d
with:
crowdin_branch_name: ${{ inputs.branch }}
upload_sources: false
upload_translations: false
download_translations: true
skip_untranslated_strings: true
export_only_approved: true
localization_branch_name: l10n_crowdin_translations_${{ inputs.branch }}

create_pull_request: true
skip_ref_checkout: true
pull_request_title: New Crowdin translations for ${{ inputs.branch }}
pull_request_body: New Crowdin pull request with translations for ${{ inputs.branch }}
pull_request_base_branch_name: ${{ inputs.branch }}
31 changes: 31 additions & 0 deletions .github/workflows/crowdin-upload-both-sources-translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Crowdin - Upload Both Sources and Translations Crowdin Action
# This action is intended to ensure our main branch on crowdin is in sync with our
# main branch on github and will run on every push to main that has changes to any
# locales files.

# TODO: Change to trigger on merge to main when locales files are changed (after testing)
# This should replace the current crowdin_action.yml file (after testing)
on: workflow_dispatch

jobs:
crowdin-upload:
runs-on: ubuntu-latest
steps:
- name: Extract current branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
id: extract_current_branch
- name: Checkout
uses: actions/checkout@v3

- name: Crowdin push
uses: crowdin/github-action@c953b17499daa6be3e5afbf7a63616fb02d8b18d
with:
crowdin_branch_name: ${{ steps.extract_current_branch.outputs.branch }}
upload_sources: true
upload_translations: true
upload_translations_args: --import-eq-suggestions --verbose
download_translations: false
env:
CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

0 comments on commit 3ef99f1

Please sign in to comment.