From 4544fb721158617ef22f509d8421f7a25d930ddd Mon Sep 17 00:00:00 2001 From: Ludy Date: Thu, 2 Jan 2025 16:58:08 +0100 Subject: [PATCH] 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) --- .github/workflows/check_properties.yml | 153 +++++++++++++++++++------ 1 file changed, 116 insertions(+), 37 deletions(-) diff --git a/.github/workflows/check_properties.yml b/.github/workflows/check_properties.yml index 5fc4dce9..62a3537f 100644 --- a/.github/workflows/check_properties.yml +++ b/.github/workflows/check_properties.yml @@ -8,12 +8,13 @@ on: permissions: contents: read # Allow read access to repository content - issues: write # Allow posting comments on issues/PRs jobs: check-files: if: github.event_name == 'pull_request_target' runs-on: ubuntu-latest + permissions: + issues: write # Allow posting comments on issues/PRs steps: - name: Harden Runner uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2 @@ -41,48 +42,125 @@ jobs: 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 - - name: Determine reference file - id: determine-file - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - echo "Determining reference file..." - REPO_OWNER=$(gh pr view ${{ github.event.pull_request.number }} --json author -q '.author.login') # Get PR author's username - REPO_NAME=$(gh pr view ${{ github.event.pull_request.number }} --json headRepository -q '.headRepository.name') # Get PR repository name - BRANCH=$(gh pr view ${{ github.event.pull_request.number }} --json headRefName -q '.headRefName') # Get PR branch name + - name: Get PR data + id: get-pr-data + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + script: | + const prNumber = context.payload.pull_request.number; + const repoOwner = context.payload.repository.owner.login; + const repoName = context.payload.repository.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 - while IFS= read -r file; do - mkdir -p "pr-branch/$(dirname "$file")" # Create directories for files - 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 - done < changed_files.txt + core.setOutput("pr_number", prNumber); + core.setOutput("repo_owner", repoOwner); + core.setOutput("repo_name", repoName); + core.setOutput("branch", branch); + 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 - find pr-branch/ -type f | awk -F'pr-branch/' '{print $2}' > file_list.txt + console.log(`Determining reference file for PR #${prNumber}`); - mapfile -t FILES_LIST < file_list.txt # Read the file list into an array - FILES_LIST_STR="${FILES_LIST[*]}" # Join array into a space-separated string - echo "FILES_LIST=${FILES_LIST_STR}" >> $GITHUB_ENV # Export the file list to the environment - echo "Changed files: ${FILES_LIST_STR}" + // Validate inputs + const validateInput = (input, regex, name) => { + if (!regex.test(input)) { + 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 - if grep -Fxq "src/main/resources/messages_en_GB.properties" changed_files.txt; then - echo "Using PR branch reference file" - REFERENCE_FILE="pr-branch-messages_en_GB.properties" - gh api repos/$REPO_OWNER/$REPO_NAME/contents/src/main/resources/messages_en_GB.properties?ref=${{ github.event.pull_request.head.ref }} \ - --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 + // Get the list of changed files in the PR + const { data: files } = await github.rest.pulls.listFiles({ + owner: repoOwner, + repo: repoName, + pull_number: prNumber, + }); - 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 id: run-check @@ -92,7 +170,8 @@ jobs: --actor ${{ github.event.pull_request.user.login }} \ --reference-file "${REFERENCE_FILE}" \ --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 id: capture-output