-
-
Notifications
You must be signed in to change notification settings - Fork 88
235 lines (197 loc) · 8.06 KB
/
i18n-update-pull.yml
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
name: i18n-update-pull
on:
issue_comment:
types: [created]
env:
LOKALISE_PROJECT_ID: ${{ vars.LOKALISE_PROJECT_ID }}
LOKALISE_API_TOKEN: ${{ secrets.LOKALISE_API_TOKEN }}
NODE_VERSION: 18.x
CI: true
jobs:
precheck:
name: Pre-check
runs-on: ubuntu-latest
if: github.event.issue.pull_request && github.event.issue.pull_request.merged_at && github.event.issue.state == 'closed' && github.event.comment.body == '.i18n-update-pull' && (github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER')
outputs:
skip: ${{ steps.fetch-pr.outputs.skip }}
skipReason: ${{ steps.fetch-pr.outputs.skipReason }}
newBranch: ${{ steps.fetch-pr.outputs.newBranch }}
branch: ${{ steps.fetch-pr.outputs.branch }}
steps:
- name: Generate Github Token for CI Bot
uses: actions/create-github-app-token@v1
id: generate-token
with:
app-id: ${{ secrets.CI_APP_ID }}
private-key: ${{ secrets.CI_APP_PRIVATE_KEY }}
- name: Check out repository
uses: actions/checkout@v4
with:
token: ${{ steps.generate-token.outputs.token }}
- name: Fetch PR details
id: fetch-pr
run: |
PR_DETAILS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "${{ github.event.issue.pull_request.url }}")
PR_BRANCH=$(echo "$PR_DETAILS" | jq -r '.head.ref')
skip () {
echo "$1 Exiting..."
echo "skip=true" >> $GITHUB_OUTPUT
echo "skipReason=$1" >> $GITHUB_OUTPUT
}
if [[ $PR_BRANCH == "i18n/"* ]]; then
skip "Branch \`$PR_BRANCH\` is a i18n branch."
fi
PR_BRANCH=$(echo "$PR_DETAILS" | jq -r '.head.label' | sed 's/:/\//g')
NEW_BRANCH="i18n/$PR_BRANCH"
echo "newBranch=$NEW_BRANCH" >> $GITHUB_OUTPUT
echo "branch=$PR_BRANCH" >> $GITHUB_OUTPUT
git config --global user.name "livecodes-ci[bot]"
git config --global user.email "186997172+livecodes-ci[bot]@users.noreply.github.com"
if [[ ! $(git ls-remote --heads origin $NEW_BRANCH) ]]; then
skip "Branch \`$NEW_BRANCH\` does not exist."
fi
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
runner:
name: Runner
runs-on: ubuntu-latest
needs: precheck
if: needs.precheck.outputs.skip != 'true'
env:
NEW_BRANCH: ${{ needs.precheck.outputs.newBranch }}
PR_BRANCH: ${{ needs.precheck.outputs.branch }}
steps:
- name: Generate Github Token for CI Bot
uses: actions/create-github-app-token@v1
id: generate-token
with:
app-id: ${{ secrets.CI_APP_ID }}
private-key: ${{ secrets.CI_APP_PRIVATE_KEY }}
- name: Check out repository
uses: actions/checkout@v4
with:
ref: ${{ needs.precheck.outputs.newBranch }}
token: ${{ steps.generate-token.outputs.token }}
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
- name: Install dependencies
run: npm ci
- name: Import from Lokalise
run: |
mkdir -p $LOKALISE_TEMP && touch $LOKALISE_TEMP/locales.zip && npm run i18n-update-pull -- $PR_BRANCH && rm -rf $LOKALISE_TEMP
env:
LOKALISE_TEMP: lokalise_tmp
- name: Generate Lokalise JSON files
run: npm run i18n-lokalise-json all
- name: Linting and fixing
run: npm run fix
- name: Commit changes
run: |
git config --global user.name "livecodes-ci[bot]"
git config --global user.email "186997172+livecodes-ci[bot]@users.noreply.github.com"
git add .
# Only commit if there are changes
git diff-index --quiet HEAD || git commit -m "i18n: pull translation from Lokalise"
# Save SHA of the latest commit to locale
echo "LAST_COMMIT_SHA=$(git log -n 1 --format="%H" -- src/livecodes/i18n/locales)" >> $GITHUB_ENV
- name: Push changes
run: git push origin $NEW_BRANCH
- name: Create a new i18n PR, comment on source PR and reaction
uses: actions/github-script@v7
with:
github-token: ${{ steps.generate-token.outputs.token }}
script: |
const repoURL = context.payload.repository.html_url;
const branchURL = `${repoURL}/tree/${process.env.NEW_BRANCH}`;
const prTitle = `i18n: ${{ github.event.issue.title }}`;
const prBody = `## What type of PR is this? (check all applicable)
- [ ] ✨ Feature
- [ ] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
- [ ] ♻️ Code Refactor
- [ ] 🔥 Performance Improvements
- [ ] ✅ Test
- [ ] 🤖 Build
- [ ] 🔁 CI
- [ ] 📦 Chore (Release)
- [ ] ⏩ Revert
- [x] 🌐 Internationalization / Translation
## Description
### i18n Actions: \`.i18n-update-pull\`
Localization pulled from Lokalise.
| Name | Description |
| --- | --- |
| **i18n Branch** | [\`${process.env.NEW_BRANCH}\`](${branchURL}) |
| **Last Commit SHA** | ${process.env.LAST_COMMIT_SHA} |
## Related Tickets & Documents
- **Source PR**: #${{ github.event.issue.number }}
`;
const prInfo = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: prTitle,
body: prBody,
head: process.env.NEW_BRANCH,
base: '${{ github.event.repository.default_branch }}'
});
const commentBody = `## i18n Actions: \`.i18n-update-pull\`
Localization pulled from Lokalise.
| Name | Description |
| --- | --- |
| **i18n Branch** | [\`${process.env.NEW_BRANCH}\`](${branchURL}) |
| **Last Commit SHA** | ${process.env.LAST_COMMIT_SHA} |
| **i18n PR** | #${prInfo.data.number} |
`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
})
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ github.event.comment.id }},
content: 'rocket'
});
exception:
name: Exception
runs-on: ubuntu-latest
needs: precheck
if: needs.precheck.outputs.skip == 'true'
env:
SKIP_REASON: ${{ needs.precheck.outputs.skipReason }}
steps:
- name: Generate Github Token for CI Bot
uses: actions/create-github-app-token@v1
id: generate-token
with:
app-id: ${{ secrets.CI_APP_ID }}
private-key: ${{ secrets.CI_APP_PRIVATE_KEY }}
- name: Create reaction on PR
uses: actions/github-script@v7
with:
github-token: ${{ steps.generate-token.outputs.token }}
script: |
const runURL = `${context.payload.repository.html_url}/actions/runs/${process.env.GITHUB_RUN_ID}`;
const commentBody = `## i18n Actions: \`.i18n-update-pull\`
Failed to perform action due to following reason: **${process.env.SKIP_REASON}**
Please check [action logs](${runURL}) for more details.
`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
})
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ github.event.comment.id }},
content: 'confused'
});