Fix: Translation Verification (#2581)

# Description

Please provide a summary of the changes, including relevant motivation
and context.

Closes #(issue_number)

## Checklist

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have performed a self-review of my own code
- [ ] I have attached images of the change if it is UI based
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] If my code has heavily changed functionality I have updated
relevant docs on [Stirling-PDFs doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
- [ ] My changes generate no new warnings
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)
This commit is contained in:
Ludy 2025-01-02 16:58:08 +01:00 committed by GitHub
parent 807639308a
commit 4544fb7211
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,12 +8,13 @@ on:
permissions: permissions:
contents: read # Allow read access to repository content contents: read # Allow read access to repository content
issues: write # Allow posting comments on issues/PRs
jobs: jobs:
check-files: check-files:
if: github.event_name == 'pull_request_target' if: github.event_name == 'pull_request_target'
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
issues: write # Allow posting comments on issues/PRs
steps: steps:
- name: Harden Runner - name: Harden Runner
uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2 uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2
@ -41,48 +42,125 @@ jobs:
echo "Getting list of changed files from PR..." echo "Getting list of changed files from PR..."
gh pr view ${{ github.event.pull_request.number }} --json files -q ".files[].path" | grep -E '^src/main/resources/messages_[a-zA-Z_]+\.properties$' > changed_files.txt # Filter only matching property files gh pr view ${{ github.event.pull_request.number }} --json files -q ".files[].path" | grep -E '^src/main/resources/messages_[a-zA-Z_]+\.properties$' > changed_files.txt # Filter only matching property files
- name: Determine reference file - name: Get PR data
id: determine-file id: get-pr-data
env: uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} with:
run: | script: |
echo "Determining reference file..." const prNumber = context.payload.pull_request.number;
REPO_OWNER=$(gh pr view ${{ github.event.pull_request.number }} --json author -q '.author.login') # Get PR author's username const repoOwner = context.payload.repository.owner.login;
REPO_NAME=$(gh pr view ${{ github.event.pull_request.number }} --json headRepository -q '.headRepository.name') # Get PR repository name const repoName = context.payload.repository.name;
BRANCH=$(gh pr view ${{ github.event.pull_request.number }} --json headRefName -q '.headRefName') # Get PR branch name const branch = context.payload.pull_request.head.ref;
mkdir -p pr-branch # Create a directory for PR files console.log(`PR Number: ${prNumber}`);
console.log(`Repo Owner: ${repoOwner}`);
console.log(`Repo Name: ${repoName}`);
console.log(`Branch: ${branch}`);
# Download the content of each changed file core.setOutput("pr_number", prNumber);
while IFS= read -r file; do core.setOutput("repo_owner", repoOwner);
mkdir -p "pr-branch/$(dirname "$file")" # Create directories for files core.setOutput("repo_name", repoName);
gh api repos/$REPO_OWNER/$REPO_NAME/contents/$file?ref=$BRANCH --jq '.content' | base64 -d > "pr-branch/src/main/resources/$(basename "$file")" # Save decoded file content core.setOutput("branch", branch);
done < changed_files.txt continue-on-error: true
- name: Determine reference file test
id: determine-file-1
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const prNumber = ${{ steps.get-pr-data.outputs.pr_number }};
const repoOwner = "${{ steps.get-pr-data.outputs.repo_owner }}";
const repoName = "${{ steps.get-pr-data.outputs.repo_name }}";
const branch = "${{ steps.get-pr-data.outputs.branch }}";
# Generate a list of files without the "pr-branch/" prefix console.log(`Determining reference file for PR #${prNumber}`);
find pr-branch/ -type f | awk -F'pr-branch/' '{print $2}' > file_list.txt
mapfile -t FILES_LIST < file_list.txt # Read the file list into an array // Validate inputs
FILES_LIST_STR="${FILES_LIST[*]}" # Join array into a space-separated string const validateInput = (input, regex, name) => {
echo "FILES_LIST=${FILES_LIST_STR}" >> $GITHUB_ENV # Export the file list to the environment if (!regex.test(input)) {
echo "Changed files: ${FILES_LIST_STR}" throw new Error(`Invalid ${name}: ${input}`);
}
};
cat file_list.txt # Display the file list validateInput(repoOwner, /^[a-zA-Z0-9_-]+$/, "repository owner");
validateInput(repoName, /^[a-zA-Z0-9._-]+$/, "repository name");
validateInput(branch, /^[a-zA-Z0-9._/-]+$/, "branch name");
# Determine which reference file to use // Get the list of changed files in the PR
if grep -Fxq "src/main/resources/messages_en_GB.properties" changed_files.txt; then const { data: files } = await github.rest.pulls.listFiles({
echo "Using PR branch reference file" owner: repoOwner,
REFERENCE_FILE="pr-branch-messages_en_GB.properties" repo: repoName,
gh api repos/$REPO_OWNER/$REPO_NAME/contents/src/main/resources/messages_en_GB.properties?ref=${{ github.event.pull_request.head.ref }} \ pull_number: prNumber,
--jq '.content' | base64 -d > $REFERENCE_FILE # Save PR branch reference file });
else
echo "Using main branch reference file"
REFERENCE_FILE="main-branch-messages_en_GB.properties"
gh api repos/Ludy87/test_java/contents/src/main/resources/messages_en_GB.properties?ref=main \
--jq '.content' | base64 -d > $REFERENCE_FILE # Save main branch reference file
fi
echo "REFERENCE_FILE=$REFERENCE_FILE" >> $GITHUB_ENV # Export reference file path to the environment // Filter for relevant files based on the PR changes
const changedFiles = files
.map(file => file.filename)
.filter(file => /^src\/main\/resources\/messages_[a-zA-Z_]+\.properties$/.test(file));
console.log("Changed files:", changedFiles);
// Create a temporary directory for PR files
const tempDir = "pr-branch";
if (!require("fs").existsSync(tempDir)) {
require("fs").mkdirSync(tempDir, { recursive: true });
}
// Download and save each changed file
for (const file of changedFiles) {
const { data: fileContent } = await github.rest.repos.getContent({
owner: repoOwner,
repo: repoName,
path: file,
ref: branch,
});
const content = Buffer.from(fileContent.content, "base64").toString("utf-8");
const filePath = `${tempDir}/${file}`;
const dirPath = require("path").dirname(filePath);
if (!require("fs").existsSync(dirPath)) {
require("fs").mkdirSync(dirPath, { recursive: true });
}
require("fs").writeFileSync(filePath, content);
console.log(`Saved file: ${filePath}`);
}
// Output the list of changed files for further processing
const fileList = changedFiles.join(" ");
core.exportVariable("FILES_LIST", fileList);
console.log("Files saved and listed in FILES_LIST.");
// Determine reference file
let referenceFilePath;
if (changedFiles.includes("src/main/resources/messages_en_GB.properties")) {
console.log("Using PR branch reference file.");
const { data: fileContent } = await github.rest.repos.getContent({
owner: repoOwner,
repo: repoName,
path: "src/main/resources/messages_en_GB.properties",
ref: branch,
});
referenceFilePath = "pr-branch-messages_en_GB.properties";
const content = Buffer.from(fileContent.content, "base64").toString("utf-8");
require("fs").writeFileSync(referenceFilePath, content);
} else {
console.log("Using main branch reference file.");
const { data: fileContent } = await github.rest.repos.getContent({
owner: "Stirling-Tools",
repo: "Stirling-PDF",
path: "src/main/resources/messages_en_GB.properties",
ref: "main",
});
referenceFilePath = "main-branch-messages_en_GB.properties";
const content = Buffer.from(fileContent.content, "base64").toString("utf-8");
require("fs").writeFileSync(referenceFilePath, content);
}
console.log(`Reference file path: ${referenceFilePath}`);
core.exportVariable("REFERENCE_FILE", referenceFilePath);
- name: Run Python script to check files - name: Run Python script to check files
id: run-check id: run-check
@ -92,7 +170,8 @@ jobs:
--actor ${{ github.event.pull_request.user.login }} \ --actor ${{ github.event.pull_request.user.login }} \
--reference-file "${REFERENCE_FILE}" \ --reference-file "${REFERENCE_FILE}" \
--branch "pr-branch" \ --branch "pr-branch" \
--files "${FILES_LIST[@]}" > result.txt || true --files "${FILES_LIST[@]}" > result.txt
continue-on-error: true # Continue the job even if this step fails
- name: Capture output - name: Capture output
id: capture-output id: capture-output