diff --git a/.github/workflows/validate-json.yml b/.github/workflows/validate-json.yml index c6226772..e7027201 100644 --- a/.github/workflows/validate-json.yml +++ b/.github/workflows/validate-json.yml @@ -1,25 +1,42 @@ +# Workflow: JSON Validation for ABRP Translations +# Purpose: +# Automatically validates JSON formatting in translation files and provides +# helpful feedback when issues are found. Comments on the PR with validation results. +# +# Frequency: +# - Runs on every PR that modifies JSON files +# +# Prerequisites: +# - None, uses default GitHub token for authentication + name: Validate JSON +run-name: "JSON Validation for PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}" on: pull_request: paths: - '**.json' -permissions: write-all +permissions: + pull-requests: write + contents: read jobs: validate-json: + name: Validate Translation Files runs-on: ubuntu-latest + steps: - - uses: actions/checkout@v4 + - name: Checkout Repository + uses: actions/checkout@v4 - - name: Get changed files + - name: Get Changed Files id: changed-files uses: tj-actions/changed-files@v42 with: files: '**.json' - - name: Validate JSON files + - name: Validate JSON Files id: validate run: | ERROR_LOG="" @@ -40,42 +57,52 @@ jobs: exit 1 fi - - name: Comment on PR if validation fails - if: failure() - uses: actions/github-script@v7 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - try { - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - headers: { - accept: 'application/vnd.github+json' - }, - body: 'āŒ **JSON Validation Failed**\n\nšŸ‘‹ Hello and thank you for helping with translations! We found some formatting issues:\n\n```\n${{ env.error_log }}\n```\n\n### Common fixes:\n1ļøāƒ£ Check for missing commas between items:\n```json\n{\n "key1": "value1", // āœ… Correct - has comma\n "key2": "value2" // āœ… Correct - no comma on last item\n}\n```\n\n2ļøāƒ£ Make sure quotes are properly closed:\n```json\n"key": "correct value", // āœ… Correct\n"key": "missing quote, // āŒ Wrong\n```\n\n3ļøāƒ£ Don\'t add comma after the last item:\n```json\n{\n "first": "value", // āœ… Comma here\n "last": "value" // āœ… No comma on last item\n}\n```\n\nNeed help? Feel free to ask and we\'ll assist you! Your contribution helps make ABRP accessible to more users worldwide. šŸŒ' - }); - } catch (error) { - core.setFailed(`Action failed with error: ${error}`); - } - - - name: Comment on PR if validation succeeds - if: success() + - name: Comment Validation Results + if: always() uses: actions/github-script@v7 + env: + ERROR_LOG: ${{ env.error_log }} with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - try { - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - headers: { - accept: 'application/vnd.github+json' - }, - body: 'āœØ **Thank you for your translation contribution!**\n\nThe file format looks good and we\'ll review your changes soon.\n\nYour help in making ABRP accessible to more users is greatly appreciated! šŸŒŸ' - }); - } catch (error) { - core.setFailed(`Action failed with error: ${error}`); - } + const output = failure() ? + `### āŒ JSON Validation Failed + + Hello @${{ github.actor }}, thank you for your translation contribution! We found some formatting issues: + + \`\`\` + ${process.env.ERROR_LOG || 'No specific error details available'} + \`\`\` + + #### Common JSON Format Rules: + 1ļøāƒ£ Check for missing commas between items: + \`\`\`json + { + "key1": "value1", // āœ… Correct - has comma + "key2": "value2" // āœ… Correct - no comma on last item + } + \`\`\` + + 2ļøāƒ£ Make sure quotes are properly closed: + \`\`\`json + "key": "correct value", // āœ… Correct + "key": "missing quote, // āŒ Wrong + \`\`\` + + Need help? Feel free to ask and we'll assist you! Your contribution helps make ABRP accessible to more users worldwide. šŸŒ` + : + `### āœ… JSON Format Validation Passed + + Thank you @${{ github.actor }} for your translation contribution! The file format looks good and we'll review your changes soon. + + Your help in making ABRP accessible to more users is greatly appreciated! šŸŒŸ + + __Action__: \`${{ github.event_name }}\` + __Files Changed__: \`${{ steps.changed-files.outputs.all_changed_files }}\``; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: output + })