From 397080c50c6577bd0d5f28bea2610bd4ee3f231a Mon Sep 17 00:00:00 2001 From: Joseph Flinn Date: Fri, 13 Aug 2021 07:06:28 -0700 Subject: [PATCH 1/4] Adding the crowdin sync workflow --- .github/workflows/crowdin-sync.yml | 133 +++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 .github/workflows/crowdin-sync.yml diff --git a/.github/workflows/crowdin-sync.yml b/.github/workflows/crowdin-sync.yml new file mode 100644 index 000000000000..5b2bf1f7125c --- /dev/null +++ b/.github/workflows/crowdin-sync.yml @@ -0,0 +1,133 @@ +name: Crowdin Sync + +on: + workflow_dispatch: + inputs: {} + #schedule: + # - cron: '0 0 * * *' + +jobs: + crowdin-sync: + name: Autosync + runs-on: ubuntu-20.04 + steps: + - name: Checkout repo + uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # v2.3.4 + + - name: Setup git config + run: | + git config user.name = "GitHub Action Bot" + git config user.email = "<>" + + - name: Get Crowndin Sync Branch + id: branch + run: | + BRANCH_NAME=crowdin-auto-sync + BRANCH_EXISTED=true + + git fetch -a + git switch master + if [ $(git branch -a | egrep "remotes/origin/${BRANCH_NAME}$" | wc -l) -eq 0 ]; then + BRANCH_EXISTED=false + git switch -c $BRANCH_NAME + else + git switch $BRANCH_NAME + fi + git branch + + echo "::set-output name=branch-existed::${BRANCH_EXISTED}" + echo "::set-output name=branch-name::${BRANCH_NAME}" + + - name: Login to Azure + uses: Azure/login@77f1b2e3fb80c0e8645114159d17008b8a2e475a + with: + creds: ${{ secrets.AZURE_PROD_KV_CREDENTIALS }} + + - name: Retrieve secrets + id: retrieve-secrets + uses: Azure/get-keyvault-secrets@80ccd3fafe5662407cc2e55f202ee34bfff8c403 + with: + keyvault: "bitwarden-prod-kv" + secrets: "crowdin-api-token" + + - name: Get Crowdin updates + env: + CROWDIN_BASE_URL="https://api.crowdin.com/api/v2/projects" + CROWDIN_PROJECT_ID="268134" + run: | + # Step 1: GET master branchId + BRANCH_ID=$( + curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ + $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/branches | jq -r '.data[0].data.id' + ) + + # Step 2: POST Build the translations and get store build id + BUILD_ID=$( + curl -X POST -s \ + -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ + -H "Content-Type: application/json" \ + $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds \ + -d "{\"branchId\": $BRANCH_ID}" | jq -r '.data.id' + ) + + MAX_TRIES=12 + for try in {1..$MAX_TRIES}; do + BRANCH_STATUS=$( + curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ + $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$BUILD_ID | jq -r '.data.status' + ) + echo "[*] Build status: $BRANCH_STATUS" + if [[ "$BRANCH_STATUS" == "finished" ]]; then + break + fi + + if [[ $try -eq $MAX_TRIES ]]; then + echo "[!] Exceeded tries: $try" + exit 1 + else + sleep 5 + fi + done + + # Step 4: when build is finished, get download url + DOWNLOAD_URL=$( + curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ + $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$BUILD_ID/download | jq -r '.data.url' + ) + + # Step 5: download the translations via the download url + SAVE_FILE=translations.zip + curl -s $DOWNLOAD_URL --output $SAVE_FILE + echo "[*] Saved to: $SAVE_FILE" + + # Step 6: Unzip and cleanup + unzip -o $SAVE_FILE + rm $SAVE_FILE + + - name: Commit changes + env: + BRANCH_NAME: ${{ steps.branch.outputs.branch-name }} + run: | + echo "[*] Adding new translations" + git add . + echo "=====Translations Changed=====" + git status + echo "==============================" + echo "[*] Committing" + git commit -m "Autosync Crowdin translations" + echo "[*] Pushing" + git push -u origin $BRANCH_NAME + + - name: Create/Update PR + env: + BRANCH_NAME: ${{ steps.cherry-pick.outputs.branch-name }} + BRANCH_EXISTED: ${{ steps.cherry-pick.outputs.branch-existed }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + if [ "$BRANCH_EXISTED" == "false" ]; then + echo "[*] Creating PR" + gh pr create --title "Autosync Crowdin Translations" \ + --body "Autosync the updated translations" + else + echo "[*] Existing PR updated" + fi From 4533e043119e4279236e00d11f44a457db0d9910 Mon Sep 17 00:00:00 2001 From: Joseph Flinn Date: Fri, 13 Aug 2021 07:10:44 -0700 Subject: [PATCH 2/4] using the updated github action from web --- .github/workflows/crowdin-sync.yml | 81 ++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/.github/workflows/crowdin-sync.yml b/.github/workflows/crowdin-sync.yml index 5b2bf1f7125c..ebf125dbd811 100644 --- a/.github/workflows/crowdin-sync.yml +++ b/.github/workflows/crowdin-sync.yml @@ -10,33 +10,36 @@ jobs: crowdin-sync: name: Autosync runs-on: ubuntu-20.04 + env: + CROWDIN_BASE_URL: "https://api.crowdin.com/api/v2/projects" + CROWDIN_PROJECT_ID: "268134" steps: - name: Checkout repo uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # v2.3.4 - name: Setup git config run: | - git config user.name = "GitHub Action Bot" - git config user.email = "<>" + git config user.name "github-actions" + git config user.email "<>" - name: Get Crowndin Sync Branch id: branch run: | BRANCH_NAME=crowdin-auto-sync - BRANCH_EXISTED=true + BRANCH_EXISTS=true git fetch -a git switch master if [ $(git branch -a | egrep "remotes/origin/${BRANCH_NAME}$" | wc -l) -eq 0 ]; then - BRANCH_EXISTED=false + BRANCH_EXISTS=false git switch -c $BRANCH_NAME else git switch $BRANCH_NAME fi git branch - echo "::set-output name=branch-existed::${BRANCH_EXISTED}" - echo "::set-output name=branch-name::${BRANCH_NAME}" + echo "::set-output name=branch-exists::$BRANCH_EXISTS" + echo "::set-output name=branch-name::$BRANCH_NAME" - name: Login to Azure uses: Azure/login@77f1b2e3fb80c0e8645114159d17008b8a2e475a @@ -50,38 +53,48 @@ jobs: keyvault: "bitwarden-prod-kv" secrets: "crowdin-api-token" - - name: Get Crowdin updates + - name: Get Crowdin master branch id + id: crowdin-master-branch env: - CROWDIN_BASE_URL="https://api.crowdin.com/api/v2/projects" - CROWDIN_PROJECT_ID="268134" + CROWDIN_API_TOKEN: ${{ steps.retrieve-secrets.outputs.crowdin-api-token }} run: | # Step 1: GET master branchId BRANCH_ID=$( curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/branches | jq -r '.data[0].data.id' ) + echo "::set-output name=id::$BRANCH_ID" + - name: Get Crowdin build id + id: crowdin-build + env: + CROWDIN_API_TOKEN: ${{ steps.retrieve-secrets.outputs.crowdin-api-token }} + CROWDIN_MASTER_BRANCH_ID: ${{ steps.crowdin-master-branch.outputs.id }} + run: | # Step 2: POST Build the translations and get store build id BUILD_ID=$( curl -X POST -s \ -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ -H "Content-Type: application/json" \ $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds \ - -d "{\"branchId\": $BRANCH_ID}" | jq -r '.data.id' + -d "{\"branchId\": $CROWDIN_MASTER_BRANCH_ID}" | jq -r '.data.id' ) + echo "::set-output name=id::$BUILD_ID" - MAX_TRIES=12 + - name: Wait for Crowdin build to finish + env: + MAX_TRIES: 12 + CROWDIN_API_TOKEN: ${{ steps.retrieve-secrets.outputs.crowdin-api-token }} + CROWDIN_BUILD_ID: ${{ steps.crowdin-build.outputs.id }} + run: | for try in {1..$MAX_TRIES}; do BRANCH_STATUS=$( curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ - $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$BUILD_ID | jq -r '.data.status' + $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$CROWDIN_BUILD_ID | jq -r '.data.status' ) echo "[*] Build status: $BRANCH_STATUS" - if [[ "$BRANCH_STATUS" == "finished" ]]; then - break - fi - - if [[ $try -eq $MAX_TRIES ]]; then + if [[ "$BRANCH_STATUS" == "finished" ]]; then break; fi + if [[ "$try" == "$MAX_TRIES" ]]; then echo "[!] Exceeded tries: $try" exit 1 else @@ -89,24 +102,35 @@ jobs: fi done + - name: Get Crowdin download URL + id: crowdin-download-url + env: + CROWDIN_API_TOKEN: ${{ steps.retrieve-secrets.outputs.crowdin-api-token }} + CROWDIN_BUILD_ID: ${{ steps.crowdin-build.outputs.id }} + run: | # Step 4: when build is finished, get download url DOWNLOAD_URL=$( curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ - $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$BUILD_ID/download | jq -r '.data.url' + $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$CROWDIN_BUILD_ID/download | jq -r '.data.url' ) + echo "::set-output name=value::$DOWNLOAD_URL" + - name: Download Crowdin translations + env: + CROWDIN_API_TOKEN: ${{ steps.retrieve-secrets.outputs.crowdin-api-token }} + CROWDIN_DOWNLOAD_URL: ${{ steps.crowdin-download-url.outputs.value }} + SAVE_FILE: "translations.zip" + run: | # Step 5: download the translations via the download url - SAVE_FILE=translations.zip - curl -s $DOWNLOAD_URL --output $SAVE_FILE + curl -s $CROWDIN_DOWNLOAD_URL --output $SAVE_FILE echo "[*] Saved to: $SAVE_FILE" - - # Step 6: Unzip and cleanup unzip -o $SAVE_FILE rm $SAVE_FILE - name: Commit changes env: BRANCH_NAME: ${{ steps.branch.outputs.branch-name }} + BRANCH_EXISTS: ${{ steps.branch.outputs.branch-exists }} run: | echo "[*] Adding new translations" git add . @@ -115,16 +139,21 @@ jobs: echo "==============================" echo "[*] Committing" git commit -m "Autosync Crowdin translations" + echo "[*] Pushing" - git push -u origin $BRANCH_NAME + if [ "$BRANCH_EXISTS" == "false" ]; then + git push -u origin $BRANCH_NAME + else + git push + fi - name: Create/Update PR env: - BRANCH_NAME: ${{ steps.cherry-pick.outputs.branch-name }} - BRANCH_EXISTED: ${{ steps.cherry-pick.outputs.branch-existed }} + BRANCH_NAME: ${{ steps.branch.outputs.branch-name }} + BRANCH_EXISTS: ${{ steps.branch.outputs.branch-exists }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - if [ "$BRANCH_EXISTED" == "false" ]; then + if [ "$BRANCH_EXISTS" == "false" ]; then echo "[*] Creating PR" gh pr create --title "Autosync Crowdin Translations" \ --body "Autosync the updated translations" From cfba106abd220aaac0101faf6d00e8d41c7ed9ea Mon Sep 17 00:00:00 2001 From: Joseph Flinn Date: Fri, 13 Aug 2021 10:04:12 -0700 Subject: [PATCH 3/4] adding some debugging --- .github/workflows/crowdin-sync.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/crowdin-sync.yml b/.github/workflows/crowdin-sync.yml index ebf125dbd811..436aab4b8422 100644 --- a/.github/workflows/crowdin-sync.yml +++ b/.github/workflows/crowdin-sync.yml @@ -63,6 +63,7 @@ jobs: curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/branches | jq -r '.data[0].data.id' ) + echo "[*] Crowin master branch id: $BRANCH_ID" echo "::set-output name=id::$BRANCH_ID" - name: Get Crowdin build id @@ -79,6 +80,7 @@ jobs: $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds \ -d "{\"branchId\": $CROWDIN_MASTER_BRANCH_ID}" | jq -r '.data.id' ) + echo "[*] Crowin translations build id: $BRANCH_ID" echo "::set-output name=id::$BUILD_ID" - name: Wait for Crowdin build to finish @@ -113,6 +115,7 @@ jobs: curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \ $CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$CROWDIN_BUILD_ID/download | jq -r '.data.url' ) + echo "[*] Crowin translations download url: $DOWNLOAD_URL" echo "::set-output name=value::$DOWNLOAD_URL" - name: Download Crowdin translations From 617074eb09ab28e2a101839d25fa1f40c97112cd Mon Sep 17 00:00:00 2001 From: Joseph Flinn Date: Fri, 13 Aug 2021 10:51:13 -0700 Subject: [PATCH 4/4] adding logic to handle if there are no changes --- .github/workflows/crowdin-sync.yml | 40 +++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/.github/workflows/crowdin-sync.yml b/.github/workflows/crowdin-sync.yml index 436aab4b8422..c645bdc66661 100644 --- a/.github/workflows/crowdin-sync.yml +++ b/.github/workflows/crowdin-sync.yml @@ -130,27 +130,49 @@ jobs: unzip -o $SAVE_FILE rm $SAVE_FILE + - name: Check changes + id: files-changed + env: + BRANCH_NAME: ${{ steps.branch.outputs.branch-name }} + BRANCH_EXISTS: ${{ steps.branch.outputs.branch-exists }} + run: | + DIFF_BRANCH=master + if [[ "$BRANCH_EXISTS" == "true" ]]; then + DIFF_BRANCH=$BRANCH_NAME + fi + + DIFF_LEN=$(git diff origin/${DIFF_BRANCH} | wc -l | xargs) + echo "[*] git diff lines: ${DIFF_LEN}" + echo "::set-output name=num::$DIFF_LEN" + - name: Commit changes env: BRANCH_NAME: ${{ steps.branch.outputs.branch-name }} BRANCH_EXISTS: ${{ steps.branch.outputs.branch-exists }} + DIFF_BRANCH: master + DIFF_LEN: ${{ steps.files-changed.outputs.num }} run: | - echo "[*] Adding new translations" - git add . echo "=====Translations Changed=====" - git status + git diff --name-only origin/${DIFF_BRANCH} echo "==============================" - echo "[*] Committing" - git commit -m "Autosync Crowdin translations" - echo "[*] Pushing" - if [ "$BRANCH_EXISTS" == "false" ]; then - git push -u origin $BRANCH_NAME + if [ "$DIFF_LEN" != "0" ]; then + echo "[*] Adding new translations" + git add . + echo "[*] Committing" + git commit -m "Autosync Crowdin translations" + echo "[*] Pushing" + if [ "$BRANCH_EXISTS" == "false" ]; then + git push -u origin $BRANCH_NAME + else + git push + fi else - git push + echo "[*] No new docs" fi - name: Create/Update PR + if: ${{ steps.files-changed.outputs.num }} != 0 env: BRANCH_NAME: ${{ steps.branch.outputs.branch-name }} BRANCH_EXISTS: ${{ steps.branch.outputs.branch-exists }}