forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
141 lines (123 loc) · 4.88 KB
/
sync_translations.yml
File metadata and controls
141 lines (123 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
name: 🌐 Sync Translations
on:
workflow_dispatch:
concurrency:
group: sync-translations
cancel-in-progress: true
# We use a machine account PAT from secrets so workflows are triggered
# the default token is not needed and should be fully restricted
permissions: {}
jobs:
sync_translations:
name: 'Sync Translations with Crowdin'
timeout-minutes: 50
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
token: ${{ secrets.MACHINE_ACCOUNT_PAT }}
ref: 'main'
fetch-depth: 0
- name: Credential Prep
run: |
echo "CROWDIN_APIv2_PAT=${{ secrets.CROWDIN_APIv2_PAT }}" >> $GITHUB_ENV
shell: bash
- name: GIT Setup
run: |
git config --global user.name 'AnkiDroid Translations'
git config --global user.email 'ankidroid@ankidroid.org'
git checkout -b i18n_sync
git reset --hard origin/main
shell: bash
- name: Get second to latest run time
env:
GH_TOKEN: ${{ secrets.MACHINE_ACCOUNT_PAT }}
run: |
OUTPUT=$(gh run list --workflow sync_translations.yml --json updatedAt | jq -r '.[1].updatedAt')
echo "LATEST_RUN=$OUTPUT" >> $GITHUB_ENV
- name: Calculate seconds passed
id: calc_time
uses: actions/github-script@v8
with:
script: |
const lastRun = new Date(process.env.LATEST_RUN);
const secondsPassed = Math.floor((Date.now() - lastRun.getTime()) / 1000);
const timeLeft = 1800 - secondsPassed > 0 ? 1800 - secondsPassed: 0;
console.log('Seconds to sleep for: ' + timeLeft);
core.setOutput('time_left', timeLeft);
- name: Await CrowdIn API Cooldown If Needed
if: steps.calc_time.outputs.time_left > 0
run: sleep ${{ steps.calc_time.outputs.time_left }}
- uses: actions/setup-node@v6
with:
node-version: 20
- name: Install dependencies and build
run: |
cd ./tools/localization
yarn
yarn build
- name: Push translation sources to crowdin
run: |
cd ./tools/localization
yarn start upload
- name: Pull translation updates from crowdin
run: |
cd ./tools/localization
yarn start download
- name: Extract downloaded ankidroid.zip file
run: |
cd ./tools/localization
yarn start extract
- name: Update translation to AnkiDroid res
run: |
cd ./tools/localization
yarn start update
- name: Commit changes
run: |
git add docs/marketing/localized_description AnkiDroid/src/main/res
git commit -am 'Updated strings from Crowdin'
git push --set-upstream origin +i18n_sync
echo "The results of the sync are on the i18n_sync branch, PR them from there for merge."
echo "https://github.com/ankidroid/Anki-Android/compare/i18n_sync?expand=1"
- name: Check if there are strings changes
id: tr_check
run: |
git checkout i18n_sync
COMMITS_COUNT=`git rev-list --count HEAD ^main`
if [ "$COMMITS_COUNT" -gt 0 ]; then
echo "HAS_CHANGES=true" >> $GITHUB_OUTPUT
else
echo "HAS_CHANGES=false" >> $GITHUB_OUTPUT
fi
- name: Create PR for strings changes if needed
if: ${{ steps.tr_check.outputs.HAS_CHANGES }}
uses: actions/github-script@v9
with:
github-token: ${{ secrets.MACHINE_ACCOUNT_PAT }}
script: |
const now = new Date();
// Date format used: YYYY/MM/DD HH:MM , UTC time(ex: 2023/01/13 08:38)
const formattedDate =
now.getUTCFullYear() + "/" +
("0" + (now.getUTCMonth() + 1)).slice(-2) + "/" +
("0" + now.getUTCDate()).slice(-2) + " " +
("0" + now.getUTCHours()).slice(-2) + ":" +
("0" + now.getUTCMinutes()).slice(-2);
try {
await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Updated strings from Crowdin " + formattedDate,
head: "i18n_sync",
base: "main",
body: "Contains the newest strings changes from Crowdin.\n\nhttps://crowdin.com/project/ankidroid/activity-stream\n\n[Re-sync translations](https://github.com/ankidroid/Anki-Android/actions/workflows/sync_translations.yml)",
maintainer_can_modify: true
});
} catch(err) {
if (err.status === 422) {
console.log("A PR containing translations sync already exists!");
} else {
console.log("Unexpected error creating pull request: " + err);
throw err;
}
}