Skip to content

Commit 51ec793

Browse files
authored
[CI] check-i18n: add support for drifted_from_default field (open-telemetry#6444)
1 parent afb7e18 commit 51ec793

File tree

3 files changed

+78
-29
lines changed

3 files changed

+78
-29
lines changed

.github/workflows/check-i18n.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ jobs:
1313
with:
1414
fetch-depth: 0 # all
1515
- name: Any files missing hash key?
16-
run: scripts/check-i18n.sh -n -x -v
16+
run: |
17+
scripts/check-i18n.sh -n -x -v
18+
.github/workflows/scripts/check-i18n-helper.sh
1719
- name: Any files with invalid hash keys?
1820
run: scripts/check-i18n.sh -v
19-
- run: .github/workflows/scripts/check-i18n-helper.sh
21+
- name: Drifted status needs updating?
22+
run: |
23+
scripts/check-i18n.sh -D
24+
# npm run _diff:fail

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@
7373
"fix:filenames": "npm run _rename-to-kebab-case",
7474
"fix:format": "npm run format; npm run _fix:trailing-spaces",
7575
"fix:htmltest-config": "scripts/htmltest-config.sh",
76-
"fix:i18n:all": "scripts/check-i18n.sh -a -c HEAD",
77-
"fix:i18n:drifted": "scripts/check-i18n.sh -c HEAD",
76+
"fix:i18n:status": "scripts/check-i18n.sh -D",
7877
"fix:i18n:new": "scripts/check-i18n.sh -n -c HEAD",
79-
"fix:i18n": "npm run fix:i18n:new",
78+
"fix:i18n": "npm run seq -- fix:i18n:new fix:i18n:status",
8079
"fix:markdown": "npm run check:markdown -- --fix; echo '\nTrimming trailing whitespace'; npm run _fix:trailing-spaces",
8180
"fix:refcache:refresh": "npm run _refcache:prune -- -n ${PRUNE_N:-128}",
8281
"fix:refcache": "npm run check:links",

scripts/check-i18n.sh

+69-24
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ DEFAULT_LANG="en"
88
DEFAULT_TARGET="$DEFAULT_CONTENT"
99
EXIT_STATUS=0
1010
EXTRA_DIFF_ARGS="--numstat"
11-
FLAG_DIFF_DETAILS=""
11+
FLAG_CHANGE_OR_ADD=0
12+
FLAG_DIFF_DETAILS=0
13+
FLAG_DRIFTED_STATUS=0
1214
FLAG_FAIL_ON_LIST_OR_MISSING=0
1315
FLAG_INFO=""
1416
FLAG_QUIET=""
1517
FLAG_VERBOSE=""
1618
I18N_DLC_KEY="default_lang_commit"
19+
I18N_DLD_KEY="drifted_from_default" # true, false, file not found
1720
LIST_KIND="DRIFTED" # or "ALL" or "NEW"
1821
TARGET_PATHS=""
1922

@@ -38,22 +41,24 @@ Options:
3841
3942
-a List/process all localization page files accessible through target paths.
4043
41-
-c HASH Update or add the '$I18N_DLC_KEY' key value to HASH for all selected
44+
-c HASH Change or add the '$I18N_DLC_KEY' key value to HASH for all selected
4245
localization pages. Use 'HEAD' as a shorthand for the hash of 'main'
43-
at HEAD (read locally).
46+
at HEAD (read locally). Also sets '$I18N_DLD_KEY' to true for files
47+
that have drifted.
4448
4549
TIP: first fetch and pull the upstream 'main' if you want to use the
4650
remote HEAD hash.
4751
4852
-d Output diff details.
53+
-D Update or add the '$I18N_DLD_KEY' key to all target localization pages.
4954
-h Help! Output this usage info.
5055
5156
-i Print commit hashes of the local 'main' branch that might be useful to
52-
se as an argument to -c.
57+
use as an argument to -c.
5358
5459
-n List/process only new localization pages, those without a '$I18N_DLC_KEY' key.
55-
-q Quiet mode. Do not list processed files.
56-
-v Enables verbose command progress and status output.
60+
-q Quiet mode. Do not list processed files. Prints summary unless -x is set.
61+
-v Verbose mode. List all processed files and their status.
5762
-x Return non-zero exit code if files were listed or hashes are missing.
5863
EOS
5964
}
@@ -65,15 +70,18 @@ function usage() {
6570
}
6671

6772
function process_CLI_args() {
68-
while getopts ":ac:dhinqvx" opt; do
73+
while getopts ":ac:dDhinqvx" opt; do
6974
case $opt in
7075
a)
7176
LIST_KIND="ALL";;
7277
c)
78+
FLAG_CHANGE_OR_ADD=1;
7379
COMMIT_HASH_ARG="$OPTARG";;
7480
d)
7581
FLAG_DIFF_DETAILS=1
7682
EXTRA_DIFF_ARGS="";;
83+
D)
84+
FLAG_DRIFTED_STATUS=1;;
7785
h)
7886
usage;;
7987
i)
@@ -98,16 +106,21 @@ function process_CLI_args() {
98106
if [[ -n $COMMIT_HASH_ARG ]]; then
99107
COMMIT_HASH_ARG=$(echo $COMMIT_HASH_ARG | tr '[:upper:]' '[:lower:]')
100108
validate_hash $COMMIT_HASH_ARG
109+
fi
101110

102-
if [[ -n $FLAG_DIFF_DETAILS ]]; then
103-
echo -e "ERROR: you can't use -c and -d at the same time, specify one or the other.\n"
104-
usage 1
105-
fi
111+
if (( FLAG_CHANGE_OR_ADD + FLAG_DIFF_DETAILS + FLAG_DRIFTED_STATUS > 1 )); then
112+
echo -e "ERROR: you can't use -c, -d, and -D at the same time; choose one. For help use -h.\n"
113+
exit 1
106114
fi
107115

108-
if [[ -n $FLAG_QUIET && -n $FLAG_DIFF_DETAILS ]]; then
109-
echo -e "ERROR: you can't use -d and -q at the same time, specify one or the other.\n"
110-
usage 1
116+
if [[ -n $FLAG_QUIET && $FLAG_DIFF_DETAILS != 0 ]]; then
117+
echo -e "ERROR: use -d or -q not both. For help use -h.\n"
118+
exit 1
119+
fi
120+
121+
if [[ -n $FLAG_QUIET && ($LIST_KIND == "ALL" || -n $FLAG_VERBOSE) ]]; then
122+
echo -e "ERROR: -q flag ignored when -a or -v is used. For help use -h.\n"
123+
exit 1
111124
fi
112125

113126
if [[ $LIST_KIND == "ALL" && -n $COMMIT_HASH_ARG ]]; then
@@ -209,14 +222,41 @@ function update_file_i18n_hash() {
209222
# echo "WARNING: the given hash is not on 'main', using this instead: $HASH" >&2
210223
# fi
211224

212-
if ! (git branch --contains $HASH | grep -q "^\s*main\b"); then
213-
echo "ERROR: hash is empty or isn't on 'main', aborting: $HASH - $f" >&2
225+
if ! (git branch --contains $HASH | grep -qEe "^\S?\s*main$"); then
226+
echo "ERROR: hash isn't on the default branch (main), aborting: $HASH - $f" >&2
214227
exit 1
215228
fi
216229

217230
set_file_i18n_hash "$f" "$HASH" "$msg" $pre_msg $post_msg
218231
}
219232

233+
function set_file_drifted_status() {
234+
if [[ $FLAG_DRIFTED_STATUS == 0 ]]; then return; fi
235+
236+
local f="$1"
237+
local status="$2"
238+
local pre_msg="${3:- \t }"
239+
local post_msg="${4:-$I18N_DLD_KEY key}" # Not used atm
240+
241+
if [[ $status == "false" ]]; then
242+
perl -i -pe "s/(^$I18N_DLD_KEY):.*$//g" "$f"
243+
elif grep -q "^$I18N_DLD_KEY:" "$f"; then
244+
perl -i -pe "s/(^$I18N_DLD_KEY):.*$/\$1: $status/" "$f"
245+
post_msg="$post_msg UPDATED"
246+
elif ! grep -q "^$I18N_DLC_KEY:" "$f"; then
247+
echo "ERROR: $I18N_DLC_KEY key is missing. Cannot set $I18N_DLC_KEY in $f" >&2
248+
exit 1
249+
else
250+
# Add drifted status immediately after the i18n hash
251+
perl -i -0777 -pe "s/($I18N_DLC_KEY:.*?\n)/\$1$I18N_DLD_KEY: $status\n/sm" "$f"
252+
# perl -i -0777 -pe "s/^(---.*?)(\n---\n)/\$1\n$I18N_DLD_KEY: $status\$2/sm" "$f"
253+
post_msg="$post_msg ADDED"
254+
fi
255+
if [[ -n $FLAG_VERBOSE ]]; then
256+
echo -e "$pre_msg\t$f $I18N_DLD_KEY key set to $status"
257+
fi
258+
}
259+
220260
function main() {
221261
process_CLI_args "$@"
222262

@@ -276,29 +316,33 @@ function main() {
276316
EN_VERSION=$(echo "$f" | sed "s/$DEFAULT_CONTENT\/.\{2,5\}\//$DEFAULT_CONTENT\/$DEFAULT_LANG\//g")
277317
if [[ ! -e "$EN_VERSION" ]]; then
278318
((FILE_PROCESSED_COUNT++))
279-
echo -e "File not found\t$EN_VERSION - $f - $DEFAULT_LANG was removed or renamed"
319+
if [[ -z $FLAG_QUIET ]]; then
320+
echo -e "File not found:\t$EN_VERSION - $f - $DEFAULT_LANG was removed or renamed"
321+
fi
322+
set_file_drifted_status "$f" "file not found"
280323
continue
281324
fi
282325

283326
# Check default-language version for changes
284327
DIFF=$(git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT...HEAD "$EN_VERSION" 2>&1)
285328
DIFF_STATUS=$?
329+
DRIFTED_STATUS="false"
286330
if [ $DIFF_STATUS -gt 1 ]; then
287331
((FILE_PROCESSED_COUNT++))
288332
EXIT_STATUS=$DIFF_STATUS
289-
# if [[ -z $FLAG_QUIET ]]; then
290-
echo -e "HASH\tERROR\t$f: git diff error ($DIFF_STATUS) or invalid hash $LASTCOMMIT. For details, use -v."
291-
# fi
333+
echo -e "HASH\tERROR\t$f: git diff error ($DIFF_STATUS) or invalid hash $LASTCOMMIT. For details, use -v."
292334
if [[ -n $FLAG_VERBOSE ]]; then echo "$DIFF"; fi
293335
continue
294336
elif [[ -n "$DIFF" ]]; then
295337
((FILE_PROCESSED_COUNT++))
296-
if [[ -n $FLAG_DIFF_DETAILS ]]; then
297-
echo -n "$DIFF"
338+
DRIFTED_STATUS="true"
339+
if [[ $FLAG_DIFF_DETAILS != 0 ]]; then
340+
echo "$DIFF"
298341
elif [[ -n $COMMIT_HASH_ARG ]]; then
299342
update_file_i18n_hash "$f" "$COMMIT_HASH_ARG" "$DIFF"
300343
elif [[ -z $FLAG_QUIET ]]; then
301-
echo "$DIFF - $f"
344+
echo -n "> Drifted file: $f"
345+
if [[ -n $FLAG_VERBOSE ]]; then echo "; diff summary: $DIFF"; else echo; fi
302346
fi
303347
elif [[ -z $LASTCOMMIT ]]; then
304348
((FILE_PROCESSED_COUNT++))
@@ -312,9 +356,10 @@ function main() {
312356
((FILE_PROCESSED_COUNT++))
313357
echo -e "File is in sync\t$f - $LASTCOMMIT"
314358
fi
359+
set_file_drifted_status "$f" $DRIFTED_STATUS
315360
done
316361

317-
if [[ -z $FLAG_QUIET ]]; then
362+
if [[ -z $FLAG_QUIET || $FLAG_FAIL_ON_LIST_OR_MISSING == 0 ]]; then
318363
echo "$LIST_KIND files: $FILE_PROCESSED_COUNT out of $FILE_COUNT"
319364
fi
320365

0 commit comments

Comments
 (0)