-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·330 lines (244 loc) · 9.91 KB
/
entrypoint.sh
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/sh
if [ "$INPUT_DEBUG_MODE" = true ] || [ -n "$RUNNER_DEBUG" ]; then
echo '---------------------------'
printenv
echo '---------------------------'
fi
upload_sources() {
if [ -n "$INPUT_UPLOAD_SOURCES_ARGS" ]; then
UPLOAD_SOURCES_OPTIONS="${UPLOAD_SOURCES_OPTIONS} ${INPUT_UPLOAD_SOURCES_ARGS}"
fi
echo "UPLOAD SOURCES"
crowdin upload sources "$@" $UPLOAD_SOURCES_OPTIONS
}
upload_translations() {
if [ -n "$INPUT_UPLOAD_LANGUAGE" ]; then
UPLOAD_TRANSLATIONS_OPTIONS="${UPLOAD_TRANSLATIONS_OPTIONS} --language=${INPUT_UPLOAD_LANGUAGE}"
fi
if [ "$INPUT_AUTO_APPROVE_IMPORTED" = true ]; then
UPLOAD_TRANSLATIONS_OPTIONS="${UPLOAD_TRANSLATIONS_OPTIONS} --auto-approve-imported"
fi
if [ "$INPUT_IMPORT_EQ_SUGGESTIONS" = true ]; then
UPLOAD_TRANSLATIONS_OPTIONS="${UPLOAD_TRANSLATIONS_OPTIONS} --import-eq-suggestions"
fi
if [ -n "$INPUT_UPLOAD_TRANSLATIONS_ARGS" ]; then
UPLOAD_TRANSLATIONS_OPTIONS="${UPLOAD_TRANSLATIONS_OPTIONS} ${INPUT_UPLOAD_TRANSLATIONS_ARGS}"
fi
echo "UPLOAD TRANSLATIONS"
crowdin upload translations "$@" $UPLOAD_TRANSLATIONS_OPTIONS
}
download_translations() {
if [ -n "$INPUT_DOWNLOAD_LANGUAGE" ]; then
DOWNLOAD_TRANSLATIONS_OPTIONS="${DOWNLOAD_TRANSLATIONS_OPTIONS} --language=${INPUT_DOWNLOAD_LANGUAGE}"
elif [ -n "$INPUT_LANGUAGE" ]; then #back compatibility for older versions
DOWNLOAD_TRANSLATIONS_OPTIONS="${DOWNLOAD_TRANSLATIONS_OPTIONS} --language=${INPUT_LANGUAGE}"
fi
if [ "$INPUT_SKIP_UNTRANSLATED_STRINGS" = true ]; then
DOWNLOAD_TRANSLATIONS_OPTIONS="${DOWNLOAD_TRANSLATIONS_OPTIONS} --skip-untranslated-strings"
fi
if [ "$INPUT_SKIP_UNTRANSLATED_FILES" = true ]; then
DOWNLOAD_TRANSLATIONS_OPTIONS="${DOWNLOAD_TRANSLATIONS_OPTIONS} --skip-untranslated-files"
fi
if [ "$INPUT_EXPORT_ONLY_APPROVED" = true ]; then
DOWNLOAD_TRANSLATIONS_OPTIONS="${DOWNLOAD_TRANSLATIONS_OPTIONS} --export-only-approved"
fi
if [ -n "$INPUT_DOWNLOAD_TRANSLATIONS_ARGS" ]; then
DOWNLOAD_TRANSLATIONS_OPTIONS="${DOWNLOAD_TRANSLATIONS_OPTIONS} ${INPUT_DOWNLOAD_TRANSLATIONS_ARGS}"
fi
echo "DOWNLOAD TRANSLATIONS"
crowdin download "$@" $DOWNLOAD_TRANSLATIONS_OPTIONS
}
create_pull_request() {
LOCALIZATION_BRANCH="${1}"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
HEADER="Accept: application/vnd.github.v3+json; application/vnd.github.antiope-preview+json; application/vnd.github.shadow-cat-preview+json"
if [ -n "$INPUT_GITHUB_API_BASE_URL" ]; then
REPO_URL="https://${INPUT_GITHUB_API_BASE_URL}/repos/${GITHUB_REPOSITORY}"
else
REPO_URL="https://api.${INPUT_GITHUB_BASE_URL}/repos/${GITHUB_REPOSITORY}"
fi
PULLS_URL="${REPO_URL}/pulls"
echo "CHECK IF ISSET SAME PULL REQUEST"
if [ -n "$INPUT_PULL_REQUEST_BASE_BRANCH_NAME" ]; then
BASE_BRANCH="$INPUT_PULL_REQUEST_BASE_BRANCH_NAME"
else
if [ -n "$GITHUB_HEAD_REF" ]; then
BASE_BRANCH=${GITHUB_HEAD_REF}
else
BASE_BRANCH=${GITHUB_REF#refs/heads/}
fi
fi
PULL_REQUESTS_QUERY_PARAMS="?base=${BASE_BRANCH}&head=${LOCALIZATION_BRANCH}"
PULL_REQUESTS=$(echo "$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X GET "${PULLS_URL}${PULL_REQUESTS_QUERY_PARAMS}")" | jq --raw-output '.[] | .head.ref ')
# check if pull request exist
if echo "$PULL_REQUESTS " | grep -q "$LOCALIZATION_BRANCH "; then
echo "PULL REQUEST ALREADY EXIST"
else
echo "CREATE PULL REQUEST"
if [ -n "$INPUT_PULL_REQUEST_BODY" ]; then
BODY=",\"body\":\"${INPUT_PULL_REQUEST_BODY//$'\n'/\\n}\""
fi
PULL_RESPONSE_DATA="{\"title\":\"${INPUT_PULL_REQUEST_TITLE}\", \"base\":\"${BASE_BRANCH}\", \"head\":\"${LOCALIZATION_BRANCH}\" ${BODY}}"
# create pull request
PULL_RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X POST --data "${PULL_RESPONSE_DATA}" "${PULLS_URL}")
set +x
PULL_REQUESTS_URL=$(echo "${PULL_RESPONSE}" | jq '.html_url')
PULL_REQUESTS_NUMBER=$(echo "${PULL_RESPONSE}" | jq '.number')
view_debug_output
if [ -n "$INPUT_PULL_REQUEST_LABELS" ]; then
PULL_REQUEST_LABELS=$(echo "[\"${INPUT_PULL_REQUEST_LABELS}\"]" | sed 's/, \|,/","/g')
if [ "$(echo "$PULL_REQUEST_LABELS" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
echo "ADD LABELS TO PULL REQUEST"
ISSUE_URL="${REPO_URL}/issues/${PULL_REQUESTS_NUMBER}"
LABELS_DATA="{\"labels\":${PULL_REQUEST_LABELS}}"
# add labels to created pull request
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X PATCH --data "${LABELS_DATA}" "${ISSUE_URL}"
else
echo "JSON OF pull_request_labels IS INVALID: ${PULL_REQUEST_LABELS}"
fi
fi
if [ -n "$INPUT_PULL_REQUEST_ASSIGNEES" ]; then
PULL_REQUEST_ASSIGNEES=$(echo "[\"${INPUT_PULL_REQUEST_ASSIGNEES}\"]" | sed 's/, \|,/","/g')
if [ "$(echo "$PULL_REQUEST_ASSIGNEES" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
echo "ADD ASSIGNEES TO PULL REQUEST"
ASSIGNEES_URL="${REPO_URL}/issues/${PULL_REQUESTS_NUMBER}/assignees"
ASSIGNEES_DATA="{\"assignees\":${PULL_REQUEST_ASSIGNEES}}"
# add assignees to created pull request
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X POST --data "${ASSIGNEES_DATA}" "${ASSIGNEES_URL}"
else
echo "JSON OF pull_request_assignees IS INVALID: ${PULL_REQUEST_ASSIGNEES}"
fi
fi
echo "PULL REQUEST CREATED: ${PULL_REQUESTS_URL}"
fi
}
push_to_branch() {
LOCALIZATION_BRANCH=${INPUT_LOCALIZATION_BRANCH_NAME}
REPO_URL="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_GITHUB_BASE_URL}/${GITHUB_REPOSITORY}.git"
echo "CONFIGURATION GIT USER"
git config --global user.email "${INPUT_GITHUB_USER_EMAIL}"
git config --global user.name "${INPUT_GITHUB_USER_NAME}"
if [ "$INPUT_SKIP_REF_CHECKOUT" != true ] && [ ${GITHUB_REF#refs/heads/} != $GITHUB_REF ]; then
git checkout "${GITHUB_REF#refs/heads/}"
fi
if [ -n "$(git show-ref refs/heads/${LOCALIZATION_BRANCH})" ]; then
git checkout "${LOCALIZATION_BRANCH}"
else
git checkout -b "${LOCALIZATION_BRANCH}"
fi
git add .
if [ ! -n "$(git status -s)" ]; then
echo "NOTHING TO COMMIT"
return
fi
echo "PUSH TO BRANCH ${LOCALIZATION_BRANCH}"
git commit --no-verify -m "${INPUT_COMMIT_MESSAGE}"
git push --no-verify --force "${REPO_URL}"
if [ "$INPUT_CREATE_PULL_REQUEST" = true ]; then
create_pull_request "${LOCALIZATION_BRANCH}"
fi
}
view_debug_output() {
if [ "$INPUT_DEBUG_MODE" = true ] || [ -n "$RUNNER_DEBUG" ]; then
set -x
fi
}
setup_commit_signing() {
echo "FOUND PRIVATE KEY, WILL SETUP GPG KEYSTORE"
echo "${INPUT_GPG_PRIVATE_KEY}" > private.key
gpg --import --batch private.key
GPG_KEY_ID=$(gpg --list-secret-keys --keyid-format=long | grep -o "rsa\d\+\/\(\w\+\)" | head -n1 | sed "s/rsa\d\+\/\(\w\+\)/\1/")
GPG_KEY_OWNER_NAME=$(gpg --list-secret-keys --keyid-format=long | grep "uid" | sed "s/.\+] \(.\+\) <\(.\+\)>/\1/")
GPG_KEY_OWNER_EMAIL=$(gpg --list-secret-keys --keyid-format=long | grep "uid" | sed "s/.\+] \(.\+\) <\(.\+\)>/\2/")
echo "Imported key information:"
echo " Key id: ${GPG_KEY_ID}"
echo " Owner name: ${GPG_KEY_OWNER_NAME}"
echo " Owner email: ${GPG_KEY_OWNER_EMAIL}"
git config --global user.signingkey "$GPG_KEY_ID"
git config --global commit.gpgsign true
export GPG_TTY=$(tty)
# generate sign to store passphrase in cache for "git commit"
echo "test" | gpg --clearsign --pinentry-mode=loopback --passphrase "${INPUT_GPG_PASSPHRASE}" > /dev/null 2>&1
rm private.key
}
get_branch_available_options() {
for OPTION in "$@" ; do
if echo "$OPTION" | egrep -vq "^(--dryrun|--branch|--source|--translation)"; then
AVAILABLE_OPTIONS="${AVAILABLE_OPTIONS} ${OPTION}"
fi
done
echo "$AVAILABLE_OPTIONS"
}
echo "STARTING CROWDIN ACTION"
cd "${GITHUB_WORKSPACE}" || exit 1
git config --global --add safe.directory $GITHUB_WORKSPACE
view_debug_output
set -e
#SET OPTIONS
set -- --no-progress --no-colors
if [ "$INPUT_DEBUG_MODE" = true ] || [ -n "$RUNNER_DEBUG" ]; then
set -- "$@" --verbose --debug
fi
if [ -n "$INPUT_CROWDIN_BRANCH_NAME" ]; then
set -- "$@" --branch="${INPUT_CROWDIN_BRANCH_NAME}"
fi
if [ -n "$INPUT_IDENTITY" ]; then
set -- "$@" --identity="${INPUT_IDENTITY}"
fi
if [ -n "$INPUT_CONFIG" ]; then
set -- "$@" --config="${INPUT_CONFIG}"
fi
if [ "$INPUT_DRYRUN_ACTION" = true ]; then
set -- "$@" --dryrun
fi
#SET CONFIG OPTIONS
if [ -n "$INPUT_PROJECT_ID" ]; then
set -- "$@" --project-id=${INPUT_PROJECT_ID}
fi
if [ -n "$INPUT_TOKEN" ]; then
set -- "$@" --token="${INPUT_TOKEN}"
fi
if [ -n "$INPUT_BASE_URL" ]; then
set -- "$@" --base-url="${INPUT_BASE_URL}"
fi
if [ -n "$INPUT_BASE_PATH" ]; then
set -- "$@" --base-path="${INPUT_BASE_PATH}"
fi
if [ -n "$INPUT_SOURCE" ]; then
set -- "$@" --source="${INPUT_SOURCE}"
fi
if [ -n "$INPUT_TRANSLATION" ]; then
set -- "$@" --translation="${INPUT_TRANSLATION}"
fi
#EXECUTE COMMANDS
if [ -n "$INPUT_ADD_CROWDIN_BRANCH" ]; then
NEW_BRANCH_OPTIONS=$( get_branch_available_options "$@" )
if [ -n "$INPUT_NEW_BRANCH_PRIORITY" ]; then
NEW_BRANCH_OPTIONS="${NEW_BRANCH_OPTIONS} --priority=${INPUT_NEW_BRANCH_PRIORITY}"
fi
echo "CREATING BRANCH $INPUT_ADD_CROWDIN_BRANCH"
crowdin branch add "$INPUT_ADD_CROWDIN_BRANCH" $NEW_BRANCH_OPTIONS --title="${INPUT_NEW_BRANCH_TITLE}" --export-pattern="${INPUT_NEW_BRANCH_EXPORT_PATTERN}"
fi
if [ "$INPUT_UPLOAD_SOURCES" = true ]; then
upload_sources "$@"
fi
if [ "$INPUT_UPLOAD_TRANSLATIONS" = true ]; then
upload_translations "$@"
fi
if [ "$INPUT_DOWNLOAD_TRANSLATIONS" = true ]; then
download_translations "$@"
if [ "$INPUT_PUSH_TRANSLATIONS" = true ]; then
[ -z "${GITHUB_TOKEN}" ] && {
echo "CAN NOT FIND 'GITHUB_TOKEN' IN ENVIRONMENT VARIABLES"
exit 1
}
[ -n "${INPUT_GPG_PRIVATE_KEY}" ] && {
setup_commit_signing
}
push_to_branch
fi
fi
if [ -n "$INPUT_DELETE_CROWDIN_BRANCH" ]; then
echo "REMOVING BRANCH $INPUT_DELETE_CROWDIN_BRANCH"
crowdin branch delete "$INPUT_DELETE_CROWDIN_BRANCH" $( get_branch_available_options "$@" )
fi