Skip to content

Commit 855034f

Browse files
authored
Report files reset during the translation batch (#23159)
* Add script to report reset files * Has the workflow report the reset files * Fix script name * Filter report by language * Update .github/workflows/create-translation-batch-pr.yml * Use build in array methods
1 parent 6e01e74 commit 855034f

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

.github/workflows/create-translation-batch-pr.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,25 @@ jobs:
136136
node script/i18n/lint-translation-files.js --check rendering
137137
git add translations/${{ matrix.language }} && git commit -m "Run script/i18n/lint-translation-files.js --check rendering" || echo "Nothing to commit"
138138
139+
- name: Check in CSV report
140+
run: |
141+
mkdir -p log
142+
csvFile=log/${{ matrix.language_code }}-resets.csv
143+
script/i18n/report-reset-files.js --report-type=csv --language=${{ matrix.language_code }} --log-file=/tmp/batch.log > $csvFile
144+
git add -f $csvFile && git commit -m "Check in ${{ matrix.language }} CSV report" || echo "Nothing to commit"
145+
139146
- name: Create Pull Request
140147
env:
141148
GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }}
142149
# We'll try to create the pull request based on the changes we pushed up in the branch.
143150
# If there are actually no differences between the branch and `main`, we'll delete it.
144151
run: |
152+
script/i18n/report-reset-files.js --report-type=pull-request-body --language=${{ matrix.language_code }} --log-file=/tmp/batch.log > /tmp/pr-body.txt
145153
git push origin ${{ steps.set-branch.outputs.BRANCH_NAME }}
146-
gh pr create -t "New translation batch for ${{ matrix.language }}" \
154+
gh pr create --title "New translation batch for ${{ matrix.language }}" \
147155
--base=main \
148156
--head=${{ steps.set-branch.outputs.BRANCH_NAME }} \
149-
-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 }}
157+
--body-file /tmp/pr-body.txt || git push origin :${{ steps.set-branch.outputs.BRANCH_NAME }}
150158
151159
# When the maximum execution time is reached for this job, Actions cancels the workflow run.
152160
# This emits a notification for the first responder to triage.

script/i18n/report-reset-files.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env node
2+
3+
import program from 'commander'
4+
import fs from 'fs'
5+
import languages from '../../lib/languages.js'
6+
7+
const defaultWorkflowUrl = [
8+
process.env.GITHUB_SERVER_URL,
9+
process.env.GITHUB_REPOSITORY,
10+
'actions/runs',
11+
process.env.GITHUB_RUN_ID,
12+
].join('/')
13+
14+
const reportTypes = {
15+
'pull-request-body': pullRequestBodyReport,
16+
csv: csvReport,
17+
}
18+
19+
program
20+
.description('Reads a translation batch log and generates a report')
21+
.requiredOption('--language <language>', 'The language to compare')
22+
.requiredOption('--log-file <log-file>', 'The batch log file')
23+
.requiredOption(
24+
'--report-type <report-type>',
25+
'The batch log file, I.E: ' + Object.keys(reportTypes).join(', ')
26+
)
27+
.option('--workflow-url <workflow-url>', 'The workflow url', defaultWorkflowUrl)
28+
.parse(process.argv)
29+
30+
const options = program.opts()
31+
const language = languages[options.language]
32+
const { logFile, workflowUrl, reportType } = options
33+
34+
if (!Object.keys(reportTypes).includes(reportType)) {
35+
throw new Error(`Invalid report type: ${reportType}`)
36+
}
37+
38+
const logFileContents = fs.readFileSync(logFile, 'utf8')
39+
40+
const revertLines = logFileContents
41+
.split('\n')
42+
.filter((line) => line.match(/^-> reverted to English/))
43+
.filter((line) => line.match(language.dir))
44+
45+
const reportEntries = revertLines.sort().map((line) => {
46+
const [, file, reason] = line.match(/^-> reverted to English: (.*) Reason: (.*)$/)
47+
return { file, reason }
48+
})
49+
50+
function pullRequestBodyReport() {
51+
const body = [
52+
`New translation batch for ${language.name}. Product of [this workflow](${workflowUrl}).`,
53+
'\n',
54+
`## ${reportEntries.length} files reverted.`,
55+
]
56+
57+
const filesByReason = {}
58+
59+
reportEntries.forEach(({ file, reason }) => {
60+
filesByReason[reason] = filesByReason[reason] || []
61+
filesByReason[reason].push(file)
62+
})
63+
64+
Object.keys(filesByReason)
65+
.sort()
66+
.forEach((reason) => {
67+
const files = filesByReason[reason]
68+
body.push(`\n### ${reason}`)
69+
body.push(`\n${files.length} files:\n`)
70+
const checkBoxes = files.map((file) => `- [ ] ${file}`)
71+
body.push(checkBoxes)
72+
})
73+
74+
return body.join('\n')
75+
}
76+
77+
function csvReport() {
78+
const lines = reportEntries.map(({ file, reason }) => {
79+
return [file, reason].join(',')
80+
})
81+
82+
return ['file,reason', lines].flat().join('\n')
83+
}
84+
85+
console.log(reportTypes[reportType]())

0 commit comments

Comments
 (0)