1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-12 16:52:10 -04:00

i18n: don't rely on po webhooks

This commit is contained in:
bbedward
2025-10-17 08:47:18 -04:00
parent 7474d5a7bf
commit 0086e42a86

View File

@@ -2,7 +2,7 @@ name: POEditor Diff & Sync
on: on:
push: push:
branches: [ poeditor-sucks ] branches: [ master ]
workflow_dispatch: {} workflow_dispatch: {}
concurrency: concurrency:
@@ -10,106 +10,98 @@ concurrency:
cancel-in-progress: false cancel-in-progress: false
jobs: jobs:
diff-and-trigger: sync-translations:
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 20 timeout-minutes: 20
strategy:
fail-fast: false
matrix:
include:
- name: ja
po_lang: "ja"
repo_file: "translations/poexports/ja.json"
webhook_secret: "POEDITOR_WEBHOOK_JA"
- name: zh_hans
po_lang: "zh-Hans"
repo_file: "translations/poexports/zh_CN.json"
webhook_secret: "POEDITOR_WEBHOOK_ZH_HANS"
- name: pt
po_lang: "pt-br"
repo_file: "translations/poexports/pt.json"
webhook_secret: "POEDITOR_WEBHOOK_PT"
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Install jq - name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq run: sudo apt-get update && sudo apt-get install -y jq
- name: Export from POEditor (key/value JSON) and compare - name: Export and update translations from POEditor
id: diffcheck
env: env:
API_TOKEN: ${{ secrets.POEDITOR_API_TOKEN }} API_TOKEN: ${{ secrets.POEDITOR_API_TOKEN }}
PROJECT_ID: ${{ secrets.POEDITOR_PROJECT_ID }} PROJECT_ID: ${{ secrets.POEDITOR_PROJECT_ID }}
PO_LANG: ${{ matrix.po_lang }}
REPO_FILE: ${{ matrix.repo_file }}
run: | run: |
set -euo pipefail set -euo pipefail
# 1) Request an export URL for key/value json LANGUAGES=(
echo "::group::POEditor export request" "ja:translations/poexports/ja.json"
RESP=$(curl -sS -X POST https://api.poeditor.com/v2/projects/export \ "zh-Hans:translations/poexports/zh_CN.json"
-d api_token="$API_TOKEN" \ "pt-br:translations/poexports/pt.json"
-d id="$PROJECT_ID" \ )
-d language="$PO_LANG" \
-d type="key_value_json")
echo "$RESP" | jq -r '.'
STATUS=$(echo "$RESP" | jq -r '.response.status')
if [[ "$STATUS" != "success" ]]; then
echo "POEditor export request failed: $RESP" >&2
exit 1
fi
URL=$(echo "$RESP" | jq -r '.result.url')
if [[ -z "$URL" || "$URL" == "null" ]]; then
echo "No export URL returned from POEditor." >&2
exit 1
fi
echo "::endgroup::"
# 2) Download exported content ANY_CHANGED=false
curl -sS -L "$URL" -o /tmp/po_export.json
# 3) Normalize JSON (sorted keys) for stable diff for lang_pair in "${LANGUAGES[@]}"; do
jq -S . /tmp/po_export.json > /tmp/po_export.norm.json IFS=':' read -r PO_LANG REPO_FILE <<< "$lang_pair"
# 4) Normalize repo file (or empty {}) and diff echo "::group::Processing $PO_LANG"
if [[ -f "$REPO_FILE" ]]; then
jq -S . "$REPO_FILE" > /tmp/repo.norm.json || echo "{}" > /tmp/repo.norm.json
else
echo "{}" > /tmp/repo.norm.json
fi
# 5) Set output changed=true|false RESP=$(curl -sS -X POST https://api.poeditor.com/v2/projects/export \
if diff -q /tmp/po_export.norm.json /tmp/repo.norm.json >/dev/null; then -d api_token="$API_TOKEN" \
echo "changed=false" >> "$GITHUB_OUTPUT" -d id="$PROJECT_ID" \
echo "No changes for $PO_LANG" -d language="$PO_LANG" \
else -d type="key_value_json")
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "Detected changes for $PO_LANG"
fi
- name: Trigger POEditor webhook for this language, if changed STATUS=$(echo "$RESP" | jq -r '.response.status')
if: steps.diffcheck.outputs.changed == 'true' if [[ "$STATUS" != "success" ]]; then
env: echo "POEditor export request failed for $PO_LANG: $RESP" >&2
WEBHOOK_URL: ${{ secrets[matrix.webhook_secret] }} continue
fi
URL=$(echo "$RESP" | jq -r '.result.url')
if [[ -z "$URL" || "$URL" == "null" ]]; then
echo "No export URL returned for $PO_LANG" >&2
continue
fi
curl -sS -L "$URL" -o "/tmp/po_export_${PO_LANG}.json"
jq -S . "/tmp/po_export_${PO_LANG}.json" > "/tmp/po_export_${PO_LANG}.norm.json"
if [[ -f "$REPO_FILE" ]]; then
jq -S . "$REPO_FILE" > "/tmp/repo_${PO_LANG}.norm.json" || echo "{}" > "/tmp/repo_${PO_LANG}.norm.json"
else
echo "{}" > "/tmp/repo_${PO_LANG}.norm.json"
fi
if diff -q "/tmp/po_export_${PO_LANG}.norm.json" "/tmp/repo_${PO_LANG}.norm.json" >/dev/null; then
echo "No changes for $PO_LANG"
else
echo "Detected changes for $PO_LANG"
mkdir -p "$(dirname "$REPO_FILE")"
cp "/tmp/po_export_${PO_LANG}.norm.json" "$REPO_FILE"
ANY_CHANGED=true
fi
echo "::endgroup::"
done
echo "any_changed=$ANY_CHANGED" >> "$GITHUB_OUTPUT"
id: export
- name: Commit and push translation updates
if: steps.export.outputs.any_changed == 'true'
run: | run: |
set -euo pipefail set -euo pipefail
if [[ -z "${WEBHOOK_URL:-}" ]]; then
echo "Missing webhook secret for this language: ${{ matrix.webhook_secret }}" >&2 git config user.name "github-actions[bot]"
exit 1 git config user.email "github-actions[bot]@users.noreply.github.com"
fi
echo "Calling POEditor export webhook for ${{ matrix.name }}" git add translations/poexports/*.json
git commit -m "i18n: update translations"
for attempt in 1 2 3; do for attempt in 1 2 3; do
code=$(curl -sS -o /tmp/resp.txt -w '%{http_code}' -X POST "$WEBHOOK_URL" || true) if git push; then
if [[ "$code" == "200" || "$code" == "204" ]]; then echo "Successfully pushed translation updates"
echo "Webhook OK ($code)"
exit 0 exit 0
fi fi
echo "Attempt $attempt failed ($code) → $(cat /tmp/resp.txt)" echo "Push attempt $attempt failed, pulling and retrying..."
sleep $((attempt*3)) git pull --rebase
sleep $((attempt*2))
done done
echo "Webhook failed after retries." >&2
echo "Failed to push after retries" >&2
exit 1 exit 1