|
| 1 | +name: Pull from translation service |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - staging |
| 7 | + - setup-i18n-actions |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + pull-translations: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + defaults: |
| 15 | + run: |
| 16 | + shell: bash -o pipefail {0} |
| 17 | + working-directory: ./packages/dialtone-vue2 |
| 18 | + |
| 19 | + env: |
| 20 | + SMARTLING_BASE_URL: "https://api.smartling.com" |
| 21 | + SMARTLING_AUTH_PATH: "/auth-api/v2/authenticate" |
| 22 | + SMARTLING_FILES_PATH: "/files-api/v2/projects" |
| 23 | + SMARTLING_PROJECTS_PATH: "/projects-api/v2/projects" |
| 24 | + SMARTLING_JOBS_PATH: "/jobs-api/v3/projects" |
| 25 | + SMARTLING_PROJECT_ID: ${{ vars.SMARTLING_PROJECT_ID }} |
| 26 | + INJECT_PLACEHOLDER_WORKAROUND: ${{ vars.INJECT_PLACEHOLDER_WORKAROUND }} |
| 27 | + SMARTLING_API_USER: ${{ secrets.SMARTLING_API_USER }} |
| 28 | + SMARTLING_API_SECRET: ${{ secrets.SMARTLING_API_SECRET }} |
| 29 | + GH_TOKEN: ${{ github.token }} |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout code |
| 33 | + uses: actions/checkout@v4 |
| 34 | + |
| 35 | + - name: Setup Environment |
| 36 | + uses: ./.github/actions/setup-environment |
| 37 | + |
| 38 | + - name: Check if translations need to be synced |
| 39 | + id: check_translations |
| 40 | + run: | |
| 41 | + result=$(pnpm exec should-pull --use-ftl-flow ${{ vars.USE_FTL_FLOW }} | tail -n 1) |
| 42 | + echo "should_create_pr=$result" >> $GITHUB_OUTPUT |
| 43 | +
|
| 44 | + - name: Pull translations if needed |
| 45 | + if: steps.check_translations.outputs.should_create_pr == 'true' |
| 46 | + run: | |
| 47 | + pnpm exec pull-translations --use-ftl-flow ${{ vars.USE_FTL_FLOW }} |
| 48 | +
|
| 49 | + - name: Commit changes if there are any |
| 50 | + if: steps.check_translations.outputs.should_create_pr == 'true' |
| 51 | + run: | |
| 52 | + TIMESTAMP=$(date +%Y%m%d%H%M%S) |
| 53 | + BRANCH_NAME="translation-sync-${TIMESTAMP}" |
| 54 | +
|
| 55 | + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV |
| 56 | +
|
| 57 | + git config --global user.name 'GitHub Actions Bot' |
| 58 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 59 | + git checkout -b "$BRANCH_NAME" |
| 60 | + git add . |
| 61 | +
|
| 62 | + if git diff --cached --quiet; then |
| 63 | + echo "No changes to commit." |
| 64 | + exit 0 |
| 65 | + else |
| 66 | + git commit -m "Sync translations" |
| 67 | + git push origin "$BRANCH_NAME" |
| 68 | + fi |
| 69 | +
|
| 70 | + - name: Get commit hash of latest translation sync commit |
| 71 | + if: steps.check_translations.outputs.should_create_pr == 'true' |
| 72 | + id: get_commit_hash |
| 73 | + run: | |
| 74 | + COMMIT_HASH=$(git log --pretty=format:"%H" --author="github-actions.*" --grep="Sync translations" -n 1) |
| 75 | + echo "commit_hash=$COMMIT_HASH" >> $GITHUB_OUTPUT |
| 76 | +
|
| 77 | + - name: Get open Translation PRs |
| 78 | + if: steps.check_translations.outputs.should_create_pr == 'true' |
| 79 | + id: get_open_prs |
| 80 | + run: | |
| 81 | + OPEN_PRS=$(gh pr list --state open --search "ci: [Automated] Sync Translations" --json number --jq '.[] | .number' | paste -sd "," -) |
| 82 | + echo "open_prs=$OPEN_PRS" >> $GITHUB_OUTPUT |
| 83 | +
|
| 84 | + - name: Check if any open PRs have the same commit |
| 85 | + if: steps.check_translations.outputs.should_create_pr == 'true' |
| 86 | + id: check_existing_pr |
| 87 | + run: | |
| 88 | + echo "Checking open PRs for duplicate Sync Translations commits..." |
| 89 | +
|
| 90 | + IFS=',' read -r -a PRS_ARRAY <<< "${{steps.get_open_prs.outputs.open_prs}}" |
| 91 | +
|
| 92 | + git fetch origin --quiet |
| 93 | +
|
| 94 | + for pr in "${PRS_ARRAY[@]}"; do |
| 95 | + PR_COMMITS=$(gh pr view $pr --json commits --jq '.commits[].oid') |
| 96 | + for pr_commit in $PR_COMMITS; do |
| 97 | + COMMIT_DIFF=$(diff <(git show --pretty=format:"" ${{steps.get_commit_hash.outputs.commit_hash}}) <(git show --pretty=format:"" $pr_commit)) |
| 98 | + if [ -z "$COMMIT_DIFF" ]; then |
| 99 | + echo "Found duplicate PR with same changes in commit $pr_commit for PR #$pr" |
| 100 | + echo "pr_exists=true" >> $GITHUB_OUTPUT |
| 101 | + exit 0 |
| 102 | + fi |
| 103 | + done |
| 104 | + done |
| 105 | + echo "No duplicate PR found" |
| 106 | + echo "pr_exists=false" >> $GITHUB_OUTPUT |
| 107 | +
|
| 108 | + - name: Create PR if no duplicate found |
| 109 | + if: steps.check_translations.outputs.should_create_pr == 'true' && steps.check_existing_pr.outputs.pr_exists == 'false' |
| 110 | + run: | |
| 111 | + PR_URL=$(gh pr create --title "ci: [Automated] Sync Translations" \ |
| 112 | + --body "This PR updates translations." \ |
| 113 | + --base main \ |
| 114 | + --head "$BRANCH_NAME") |
| 115 | + echo "Pull Request created: $PR_URL" |
0 commit comments