Introduces the checking of message_*.properties (#1758)

* Introduces the checking of message_*.properties

* Update check_properties.yml
This commit is contained in:
Ludy 2024-08-25 23:04:28 +02:00 committed by GitHub
parent 363f5a4c23
commit 1732531174
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 211 additions and 0 deletions

View file

@ -0,0 +1,115 @@
import os
import argparse
def read_properties(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return file.read().splitlines()
def check_difference(reference_file, file_list, branch):
reference_branch = reference_file.split("/")[0]
basename_reference_file = os.path.basename(reference_file)
report = []
report.append(
f"#### Checking with the file `{basename_reference_file}` from the `{reference_branch}` - Checking the `{branch}`"
)
reference_list = read_properties(reference_file)
is_diff = False
for file_path in file_list:
basename_current_file = os.path.basename(branch + "/" + file_path)
if (
branch + "/" + file_path == reference_file
or not file_path.endswith(".properties")
or not basename_current_file.startswith("messages_")
):
# report.append(f"File '{basename_current_file}' is ignored.")
continue
report.append(f"Checking the language file `{basename_current_file}`...")
current_list = read_properties(branch + "/" + file_path)
reference_list_len = len(reference_list)
current_list_len = len(current_list)
if reference_list_len != current_list_len:
report.append("")
report.append("- ❌ Test 1 failed! Difference in the file!")
is_diff = True
if reference_list_len > current_list_len:
report.append(
f" - Missing lines! Either comments, empty lines, or translation strings are missing! {reference_list_len}:{current_list_len}"
)
elif reference_list_len < current_list_len:
report.append(
f" - Too many lines! Check your translation files! {reference_list_len}:{current_list_len}"
)
else:
report.append("- ✅ Test 1 passed")
if 1 == 1:
current_keys = []
reference_keys = []
for item in current_list:
if not item.startswith("#") and item != "" and "=" in item:
key, _ = item.split("=", 1)
current_keys.append(key)
for item in reference_list:
if not item.startswith("#") and item != "" and "=" in item:
key, _ = item.split("=", 1)
reference_keys.append(key)
current_set = set(current_keys)
reference_set = set(reference_keys)
set_test1 = current_set.difference(reference_set)
set_test2 = reference_set.difference(current_set)
set_test1_list = list(set_test1)
set_test2_list = list(set_test2)
if len(set_test1_list) > 0 or len(set_test2_list) > 0:
is_diff = True
set_test1_list = "`, `".join(set_test1_list)
set_test2_list = "`, `".join(set_test2_list)
report.append("- ❌ Test 2 failed")
if len(set_test1_list) > 0:
report.append(
f" - There are keys in ***{basename_current_file}*** `{set_test1_list}` that are not present in ***{basename_reference_file}***!"
)
if len(set_test2_list) > 0:
report.append(
f" - There are keys in ***{basename_reference_file}*** `{set_test2_list}` that are not present in ***{basename_current_file}***!"
)
else:
report.append("- ✅ Test 2 passed")
report.append("")
report.append("")
if is_diff:
report.append("## ❌ Check fail")
else:
report.append("## ✅ Check success")
print("\n".join(report))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Find missing keys")
parser.add_argument(
"--reference-file",
required=True,
help="Path to the reference file.",
)
parser.add_argument(
"--branch",
type=str,
required=True,
help="Branch name.",
)
parser.add_argument(
"--files",
nargs="+",
required=True,
help="List of changed files, separated by spaces.",
)
args = parser.parse_args()
file_list = args.files
check_difference(args.reference_file, file_list, args.branch)

96
.github/workflows/check_properties.yml vendored Normal file
View file

@ -0,0 +1,96 @@
name: Check Properties Files in PR
on:
pull_request_target:
types: [opened, synchronize, reopened]
paths:
- "src/main/resources/messages_*.properties"
jobs:
check-files:
runs-on: ubuntu-latest
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
path: pr-branch
fetch-depth: 0
- name: Checkout main branch
uses: actions/checkout@v4
with:
ref: main
path: main-branch
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install GitHub CLI
run: sudo apt-get update && sudo apt-get install -y gh
- name: Fetch PR changed files
id: fetch-pr-changes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Fetching PR changed files..."
cd pr-branch
gh pr view ${{ github.event.pull_request.number }} --json files -q ".files[].path" > ../changed_files.txt
cd ..
echo $(cat changed_files.txt)
BRANCH_PATH="pr-branch"
echo "BRANCH_PATH=${BRANCH_PATH}" >> $GITHUB_ENV
CHANGED_FILES=$(cat changed_files.txt | tr '\n' ' ')
echo "CHANGED_FILES=${CHANGED_FILES}" >> $GITHUB_ENV
echo "Changed files: ${CHANGED_FILES}"
echo "Branch: ${BRANCH_PATH}"
- name: Determine reference file
id: determine-file
run: |
echo "Determining reference file..."
if echo "${{ env.CHANGED_FILES }}"| grep -q 'src/main/resources/messages_en_GB.properties'; then
echo "REFERENCE_FILE=pr-branch/src/main/resources/messages_en_GB.properties" >> $GITHUB_ENV
else
echo "REFERENCE_FILE=main-branch/src/main/resources/messages_en_GB.properties" >> $GITHUB_ENV
fi
- name: Show REFERENCE_FILE
run: echo "Reference file is set to ${{ env.REFERENCE_FILE }}"
- name: Run Python script to check files
id: run-check
run: |
python main-branch/.github/scripts/check_language_properties.py --reference-file ${{ env.REFERENCE_FILE }} --branch ${{ env.BRANCH_PATH }} --files ${{ env.CHANGED_FILES }} > failure.txt || true
- name: Capture output
id: capture-output
run: |
if [ -f failure.txt ]; then
echo "Test failed, capturing output..."
# Use the cat command to avoid issues with special characters in environment variables
ERROR_OUTPUT=$(cat failure.txt)
echo "ERROR_OUTPUT<<EOF" >> $GITHUB_ENV
echo "$ERROR_OUTPUT" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo $ERROR_OUTPUT
fi
- name: Post comment on PR
uses: actions/github-script@v7
with:
script: |
const { GITHUB_REPOSITORY, ERROR_OUTPUT } = process.env;
const [repoOwner, repoName] = GITHUB_REPOSITORY.split('/');
const prNumber = context.issue.number; // Pull request number from context
await github.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
body: `## Translation Verification Summary\n\n\n${ERROR_OUTPUT}\n`
});