diff --git a/.github/workflows/create-translation-batch-pr.yml b/.github/workflows/create-translation-batch-pr.yml index b02a32b04b..b60933fc98 100644 --- a/.github/workflows/create-translation-batch-pr.yml +++ b/.github/workflows/create-translation-batch-pr.yml @@ -136,17 +136,25 @@ jobs: node script/i18n/lint-translation-files.js --check rendering git add translations/${{ matrix.language }} && git commit -m "Run script/i18n/lint-translation-files.js --check rendering" || echo "Nothing to commit" + - name: Check in CSV report + run: | + mkdir -p log + csvFile=log/${{ matrix.language_code }}-resets.csv + script/i18n/report-reset-files.js --report-type=csv --language=${{ matrix.language_code }} --log-file=/tmp/batch.log > $csvFile + git add -f $csvFile && git commit -m "Check in ${{ matrix.language }} CSV report" || echo "Nothing to commit" + - name: Create Pull Request env: GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }} # We'll try to create the pull request based on the changes we pushed up in the branch. # If there are actually no differences between the branch and `main`, we'll delete it. run: | + script/i18n/report-reset-files.js --report-type=pull-request-body --language=${{ matrix.language_code }} --log-file=/tmp/batch.log > /tmp/pr-body.txt git push origin ${{ steps.set-branch.outputs.BRANCH_NAME }} - gh pr create -t "New translation batch for ${{ matrix.language }}" \ + gh pr create --title "New translation batch for ${{ matrix.language }}" \ --base=main \ --head=${{ steps.set-branch.outputs.BRANCH_NAME }} \ - -b "New batch for ${{ matrix.language }} created by [this workflow]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)" || git push origin :${{ steps.set-branch.outputs.BRANCH_NAME }} + --body-file /tmp/pr-body.txt || git push origin :${{ steps.set-branch.outputs.BRANCH_NAME }} # When the maximum execution time is reached for this job, Actions cancels the workflow run. # This emits a notification for the first responder to triage. diff --git a/script/i18n/report-reset-files.js b/script/i18n/report-reset-files.js new file mode 100755 index 0000000000..6ed9a165af --- /dev/null +++ b/script/i18n/report-reset-files.js @@ -0,0 +1,85 @@ +#!/usr/bin/env node + +import program from 'commander' +import fs from 'fs' +import languages from '../../lib/languages.js' + +const defaultWorkflowUrl = [ + process.env.GITHUB_SERVER_URL, + process.env.GITHUB_REPOSITORY, + 'actions/runs', + process.env.GITHUB_RUN_ID, +].join('/') + +const reportTypes = { + 'pull-request-body': pullRequestBodyReport, + csv: csvReport, +} + +program + .description('Reads a translation batch log and generates a report') + .requiredOption('--language ', 'The language to compare') + .requiredOption('--log-file ', 'The batch log file') + .requiredOption( + '--report-type ', + 'The batch log file, I.E: ' + Object.keys(reportTypes).join(', ') + ) + .option('--workflow-url ', 'The workflow url', defaultWorkflowUrl) + .parse(process.argv) + +const options = program.opts() +const language = languages[options.language] +const { logFile, workflowUrl, reportType } = options + +if (!Object.keys(reportTypes).includes(reportType)) { + throw new Error(`Invalid report type: ${reportType}`) +} + +const logFileContents = fs.readFileSync(logFile, 'utf8') + +const revertLines = logFileContents + .split('\n') + .filter((line) => line.match(/^-> reverted to English/)) + .filter((line) => line.match(language.dir)) + +const reportEntries = revertLines.sort().map((line) => { + const [, file, reason] = line.match(/^-> reverted to English: (.*) Reason: (.*)$/) + return { file, reason } +}) + +function pullRequestBodyReport() { + const body = [ + `New translation batch for ${language.name}. Product of [this workflow](${workflowUrl}).`, + '\n', + `## ${reportEntries.length} files reverted.`, + ] + + const filesByReason = {} + + reportEntries.forEach(({ file, reason }) => { + filesByReason[reason] = filesByReason[reason] || [] + filesByReason[reason].push(file) + }) + + Object.keys(filesByReason) + .sort() + .forEach((reason) => { + const files = filesByReason[reason] + body.push(`\n### ${reason}`) + body.push(`\n${files.length} files:\n`) + const checkBoxes = files.map((file) => `- [ ] ${file}`) + body.push(checkBoxes) + }) + + return body.join('\n') +} + +function csvReport() { + const lines = reportEntries.map(({ file, reason }) => { + return [file, reason].join(',') + }) + + return ['file,reason', lines].flat().join('\n') +} + +console.log(reportTypes[reportType]())