2021-07-26 16:53:35 +02:00
|
|
|
name: Crowdin Sync
|
|
|
|
|
|
|
|
on:
|
|
|
|
workflow_dispatch:
|
|
|
|
inputs: {}
|
|
|
|
#schedule:
|
|
|
|
# - cron: '0 0 * * *'
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
crowdin-sync:
|
|
|
|
name: Autosync
|
|
|
|
runs-on: ubuntu-20.04
|
2021-08-11 18:01:29 +02:00
|
|
|
env:
|
|
|
|
CROWDIN_BASE_URL: "https://api.crowdin.com/api/v2/projects"
|
|
|
|
CROWDIN_PROJECT_ID: "308189"
|
2021-07-26 16:53:35 +02:00
|
|
|
steps:
|
|
|
|
- name: Checkout repo
|
|
|
|
uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # v2.3.4
|
|
|
|
|
|
|
|
- name: Setup git config
|
|
|
|
run: |
|
2021-08-11 18:01:29 +02:00
|
|
|
git config user.name "github-actions"
|
|
|
|
git config user.email "<>"
|
2021-07-26 16:53:35 +02:00
|
|
|
|
|
|
|
- name: Get Crowndin Sync Branch
|
|
|
|
id: branch
|
|
|
|
run: |
|
|
|
|
BRANCH_NAME=crowdin-auto-sync
|
2021-08-11 18:01:29 +02:00
|
|
|
BRANCH_EXISTS=true
|
2021-07-26 16:53:35 +02:00
|
|
|
|
|
|
|
git fetch -a
|
|
|
|
git switch master
|
|
|
|
if [ $(git branch -a | egrep "remotes/origin/${BRANCH_NAME}$" | wc -l) -eq 0 ]; then
|
2021-08-11 18:01:29 +02:00
|
|
|
BRANCH_EXISTS=false
|
2021-07-26 16:53:35 +02:00
|
|
|
git switch -c $BRANCH_NAME
|
|
|
|
else
|
|
|
|
git switch $BRANCH_NAME
|
|
|
|
fi
|
|
|
|
git branch
|
|
|
|
|
2021-08-11 18:01:29 +02:00
|
|
|
echo "::set-output name=branch-exists::$BRANCH_EXISTS"
|
|
|
|
echo "::set-output name=branch-name::$BRANCH_NAME"
|
2021-07-26 16:53:35 +02:00
|
|
|
|
|
|
|
- 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"
|
|
|
|
|
2021-08-11 18:01:29 +02:00
|
|
|
- name: Get Crowdin master branch id
|
|
|
|
id: crowdin-master-branch
|
2021-07-26 16:53:35 +02:00
|
|
|
env:
|
2021-08-11 18:01:29 +02:00
|
|
|
CROWDIN_API_TOKEN: ${{ steps.retrieve-secrets.outputs.crowdin-api-token }}
|
2021-07-26 16:53:35 +02:00
|
|
|
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'
|
|
|
|
)
|
2021-08-11 18:01:29 +02:00
|
|
|
echo "::set-output name=id::$BRANCH_ID"
|
2021-07-26 16:53:35 +02:00
|
|
|
|
2021-08-11 18:01:29 +02:00
|
|
|
- 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: |
|
2021-07-26 16:53:35 +02:00
|
|
|
# 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 \
|
2021-08-11 18:01:29 +02:00
|
|
|
-d "{\"branchId\": $CROWDIN_MASTER_BRANCH_ID}" | jq -r '.data.id'
|
2021-07-26 16:53:35 +02:00
|
|
|
)
|
2021-08-13 22:27:56 +02:00
|
|
|
echo "[*] Crowin translations build id: $BRANCH_ID"
|
2021-08-11 18:01:29 +02:00
|
|
|
echo "::set-output name=id::$BUILD_ID"
|
2021-07-26 16:53:35 +02:00
|
|
|
|
2021-08-11 18:01:29 +02:00
|
|
|
- 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: |
|
2021-07-26 16:53:35 +02:00
|
|
|
for try in {1..$MAX_TRIES}; do
|
|
|
|
BRANCH_STATUS=$(
|
|
|
|
curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \
|
2021-08-11 18:01:29 +02:00
|
|
|
$CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$CROWDIN_BUILD_ID | jq -r '.data.status'
|
2021-07-26 16:53:35 +02:00
|
|
|
)
|
|
|
|
echo "[*] Build status: $BRANCH_STATUS"
|
2021-08-11 18:01:29 +02:00
|
|
|
if [[ "$BRANCH_STATUS" == "finished" ]]; then break; fi
|
|
|
|
if [[ "$try" == "$MAX_TRIES" ]]; then
|
2021-07-26 16:53:35 +02:00
|
|
|
echo "[!] Exceeded tries: $try"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
sleep 5
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2021-08-11 18:01:29 +02:00
|
|
|
- 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: |
|
2021-07-26 16:53:35 +02:00
|
|
|
# Step 4: when build is finished, get download url
|
|
|
|
DOWNLOAD_URL=$(
|
|
|
|
curl -s -H "Authorization: Bearer $CROWDIN_API_TOKEN" \
|
2021-08-11 18:01:29 +02:00
|
|
|
$CROWDIN_BASE_URL/$CROWDIN_PROJECT_ID/translations/builds/$CROWDIN_BUILD_ID/download | jq -r '.data.url'
|
2021-07-26 16:53:35 +02:00
|
|
|
)
|
2021-08-13 22:27:56 +02:00
|
|
|
echo "[*] Crowin translations download url: $DOWNLOAD_URL"
|
2021-08-11 18:01:29 +02:00
|
|
|
echo "::set-output name=value::$DOWNLOAD_URL"
|
2021-07-26 16:53:35 +02:00
|
|
|
|
2021-08-11 18:01:29 +02:00
|
|
|
- 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: |
|
2021-07-26 16:53:35 +02:00
|
|
|
# Step 5: download the translations via the download url
|
2021-08-11 18:01:29 +02:00
|
|
|
curl -s $CROWDIN_DOWNLOAD_URL --output $SAVE_FILE
|
2021-07-26 16:53:35 +02:00
|
|
|
echo "[*] Saved to: $SAVE_FILE"
|
|
|
|
unzip -o $SAVE_FILE
|
|
|
|
rm $SAVE_FILE
|
|
|
|
|
2021-08-13 22:27:56 +02:00
|
|
|
- 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"
|
|
|
|
|
2021-07-26 16:53:35 +02:00
|
|
|
- name: Commit changes
|
|
|
|
env:
|
|
|
|
BRANCH_NAME: ${{ steps.branch.outputs.branch-name }}
|
2021-08-11 18:01:29 +02:00
|
|
|
BRANCH_EXISTS: ${{ steps.branch.outputs.branch-exists }}
|
2021-08-13 22:27:56 +02:00
|
|
|
DIFF_BRANCH: master
|
|
|
|
DIFF_LEN: ${{ steps.files-changed.outputs.num }}
|
2021-07-26 16:53:35 +02:00
|
|
|
run: |
|
|
|
|
echo "=====Translations Changed====="
|
2021-08-13 22:27:56 +02:00
|
|
|
git diff --name-only origin/${DIFF_BRANCH}
|
2021-07-26 16:53:35 +02:00
|
|
|
echo "=============================="
|
2021-08-11 18:01:29 +02:00
|
|
|
|
2021-08-13 22:27:56 +02:00
|
|
|
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
|
2021-08-11 18:01:29 +02:00
|
|
|
else
|
2021-08-13 22:27:56 +02:00
|
|
|
echo "[*] No new docs"
|
2021-08-11 18:01:29 +02:00
|
|
|
fi
|
2021-07-26 16:53:35 +02:00
|
|
|
|
|
|
|
- name: Create/Update PR
|
2021-08-13 22:27:56 +02:00
|
|
|
if: ${{ steps.files-changed.outputs.num }} != 0
|
2021-07-26 16:53:35 +02:00
|
|
|
env:
|
2021-08-11 18:01:29 +02:00
|
|
|
BRANCH_NAME: ${{ steps.branch.outputs.branch-name }}
|
|
|
|
BRANCH_EXISTS: ${{ steps.branch.outputs.branch-exists }}
|
2021-07-26 16:53:35 +02:00
|
|
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
run: |
|
2021-08-11 18:01:29 +02:00
|
|
|
if [ "$BRANCH_EXISTS" == "false" ]; then
|
2021-07-26 16:53:35 +02:00
|
|
|
echo "[*] Creating PR"
|
|
|
|
gh pr create --title "Autosync Crowdin Translations" \
|
|
|
|
--body "Autosync the updated translations"
|
|
|
|
else
|
|
|
|
echo "[*] Existing PR updated"
|
2021-08-18 23:24:34 +02:00
|
|
|
fi
|
2021-08-13 22:27:56 +02:00
|
|
|
|