Skip to content

Commit 418b3ff

Browse files
authored
Refactor workflow for i18n consistency check and make it work properly again
* Refactor code * FIx bugs * Update token * Update scripts * Small fix
1 parent 558d4ca commit 418b3ff

File tree

1 file changed

+47
-72
lines changed

1 file changed

+47
-72
lines changed
Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
name: i18n Consistency Check
22

33
on:
4-
pull_request:
5-
branches:
6-
- master
7-
paths:
8-
- .github/workflows/i18n-consistency-check.yml
94
schedule:
10-
- cron: '0 0 1 * *' # run at midnight, on day 1 of the month
5+
- cron: '0 0 1 * *' # run at midnight, on day 1 of the month
116
workflow_dispatch:
127

138
jobs:
@@ -20,87 +15,67 @@ jobs:
2015
- uses: actions/checkout@v3
2116
with:
2217
fetch-depth: '0'
23-
- name: Check consistency
18+
- name: Check consistency and create issue
2419
id: check-consistency
20+
env:
21+
GH_TOKEN: ${{ github.token }}
2522
run: |
26-
# Set the issue header
27-
issue="\
28-
# i18n Contents Consistency Issue\\n\
29-
\\n\
30-
The following files may have consistency issues with the English version. Please check and update the files.\\n\
31-
\\n\
32-
This issue is created when there is an update to content/en. It compares the git update history to let you know what updates are overdue. The issue should be closed when the update is complete.\\n"
33-
34-
# Loop through all the files in the English directory
35-
for file in $(find patterns/2-structured -name '*.md'); do
36-
37-
# Get the translated file name and check if it exists
38-
i18n_filename=$(echo "$file" | sed 's/patterns\/2-structured/translation\/${{matrix.language}}\/patterns/g')
23+
# Declare the flags
24+
declare -A flags=( ["ja"]=":jp: Japanese" ["zh"]=":cn: Chinese")
25+
26+
issue_title="${flags['${{matrix.language}}']}: Content Consistency Issue"
27+
28+
# Heredoc for issue header
29+
cat <<- EOM > issue.md
30+
# i18n Contents Consistency Issue
31+
32+
The following files may have consistency issues with the English version. Please check and update the files.
33+
34+
This issue is created when there is an update to content/en. It compares the git update history to let you know what updates are overdue. The issue should be closed when the update is complete.
35+
EOM
36+
37+
# Loop through all files in the English directory
38+
for file in $(find patterns/{2-structured,3-validated} -name '*.md'); do
39+
[[ $file =~ "3-validated" ]] && continue # if the file is under 3-validated, skip (one liner) - 2023/08/26
40+
i18n_filename=$(echo "$file" | sed "s/patterns\/\(2-structured\|3-validated\)/translation\/${{matrix.language}}\/patterns/g")
41+
3942
if [[ ! -e "$i18n_filename" ]]; then
40-
continue
43+
continue
4144
fi
42-
echo $file
43-
echo $i18n_filename
44-
45-
# Get the last updated date of the original file and the translated file
46-
original_updated_at=$(date -d "$(git log -1 --format=%cd --date=iso $file)" +%s)
45+
46+
original_updated_at=$(date -d "$(git log -1 --format=%cd --date=iso $file)" +%s)
4747
i18n_content_updated_at=$(date -d "$(git log -1 --format=%cd --date=iso $i18n_filename)" +%s)
48-
49-
# print the last updated date of the original file and the translated file
50-
51-
# Check if the translated file is updated after the original file
52-
if [[ $(($original_updated_at - $i18n_content_updated_at)) -ge 1 ]]; then
53-
# Get the title of the content
54-
content_header=$(echo "$(cat "$file")" | grep -E '^title+' | sort -r | head -n1)
48+
49+
if [[ $((original_updated_at - i18n_content_updated_at)) -ge 1 ]]; then
50+
content_header=$(grep -E '^title+' "$file" | sort -r | head -n1)
5551
content_title=$(echo "$content_header" | sed 's/title: //g')
56-
57-
# Get the days since the translated file is overdue
58-
days_since_overdue_updates=$(($(( $(date +%s) - $original_updated_at))/ 60 / 60 / 24))
59-
# Get the diff between the original file and the translated file
52+
days_since_overdue_updates=$(( ($(date +%s) - original_updated_at) / 60 / 60 / 24 ))
6053
original_last_update_hash=$(git log -1 --format=%H $file)
61-
# Get the parent hash of the original last update hash
6254
parent_hash=$(git log -1 --format=%P $original_last_update_hash)
63-
64-
# Get the diff between the original file and the translated file
65-
result=$(echo "$(git diff ${parent_hash} HEAD $file)" | sed '1,4 s/^/# /')
66-
echo -e "$result"
67-
68-
# Add the contents to the issue.md file
69-
issue+="<details><summary><b>$content_title</b> ($file)</summary>\\n\\n"
70-
issue+="- Original File(en): [$file](https://github.com/$GITHUB_REPOSITORY/blob/master/$file)\\n"
71-
issue+="- Translated File(${{matrix.language}}): [$i18n_filename](https://github.com/$GITHUB_REPOSITORY/blob/master/$i18n_filename)\\n"
72-
issue+="- [Diff on GitHub](https://github.com/yuhattor/innersourcecommons.org/compare/$i18n_last_update_hash...$original_last_update_hash)\\n"
73-
issue+="- Days since overdue updates: $days_since_overdue_updates days\\n"
74-
issue+="\`\`\`diff\\n"
75-
issue+="$result"
76-
issue+="\\n\`\`\`\\n"
77-
issue+="</details>\\n"
78-
echo -e "$issue" >> issue.md
79-
issue=""
55+
result=$(git diff "${parent_hash}" HEAD "$file" | sed '1,4 s/^/# /')
56+
57+
# Append to the issue.md file
58+
cat <<- EOM >> issue.md
59+
<details><summary><b>$content_title</b> ($file)</summary>
60+
For more information, please compare [the original file(en)](https://github.com/$GITHUB_REPOSITORY/blob/master/$file) with[the translated file](https://github.com/$GITHUB_REPOSITORY/blob/master/$i18n_filename) . You can view [the differences](https://github.com/yuhattor/innersourcecommons.org/compare/$i18n_last_update_hash...$original_last_update_hash) on GitHub. The number of days since overdue updates is **$days_since_overdue_updates** days.
61+
62+
\`\`\`diff
63+
$result
64+
\`\`\`
65+
</details>
66+
EOM
8067
fi
8168
done
82-
- name: Create Issue
83-
if: ${{ <expression> }}
84-
run: |
85-
# Declare the flags
86-
declare -A flags=(
87-
["ja"]="🇯🇵 Japanese"
88-
["zh"]="🇨🇳 Chinese"
89-
)
90-
91-
# Set the issue title
92-
issue_title="${flags[${{matrix.language}}]}: Content Consistency Issue"
93-
69+
9470
# Get the existing issue ID
95-
existing_issue_id=$(gh issue list -S "state:open type:issue title:$issue_title" | cut -f1)
96-
97-
# If the issue.md file exists, create a new issue or comment on the existing issue
71+
existing_issue_id=$(gh issue list -S "is:issue is:open $issue_title" | cut -f1)
72+
echo "existing_issue_id: $existing_issue_id"
73+
# Create a new issue or comment on the existing one
9874
if [ -f issue.md ]; then
9975
if expr "$existing_issue_id" : '^[0-9]*$' >/dev/null; then
10076
gh issue comment "$existing_issue_id" -F issue.md -R $GITHUB_REPOSITORY
10177
else
10278
gh issue create -t "$issue_title" -F issue.md -R $GITHUB_REPOSITORY -l "Type - Translation"
10379
fi
10480
fi
105-
env:
106-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81+

0 commit comments

Comments
 (0)