Skip to content

Commit b3663d9

Browse files
authored
Merge pull request #12483 from github/repo-sync
repo sync
2 parents ba382b2 + 9eadcaf commit b3663d9

7 files changed

Lines changed: 136 additions & 20 deletions

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989

9090
- name: Commit crowdin sync
9191
run: |
92-
git add .
92+
git add translations/${{ matrix.language }}
9393
git commit -m "Add crowdin translations" || echo "Nothing to commit"
9494
9595
- name: 'Setup node'
@@ -102,51 +102,59 @@ jobs:
102102
- name: Reset files with broken liquid tags
103103
run: |
104104
node script/i18n/reset-files-with-broken-liquid-tags.js --language=${{ matrix.language_code }}
105-
git add . && git commit -m "run script/i18n/reset-files-with-broken-liquid-tags.js --language=${{ matrix.language_code }}" || echo "Nothing to commit"
105+
git add translations/${{ matrix.language }} && git commit -m "run script/i18n/reset-files-with-broken-liquid-tags.js --language=${{ matrix.language_code }}" || echo "Nothing to commit"
106106
107107
# step 5 in docs-engineering/crowdin.md using script from docs-internal#22709
108108
- name: Reset known broken files
109109
run: |
110110
node script/i18n/reset-known-broken-translation-files.js
111-
git add . && git commit -m "run script/i18n/reset-known-broken-translation-files.js" || echo "Nothing to commit"
111+
git add translations/${{ matrix.language }} && git commit -m "run script/i18n/reset-known-broken-translation-files.js" || echo "Nothing to commit"
112112
env:
113113
GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }}
114114

115115
# step 6 in docs-engineering/crowdin.md
116116
- name: Homogenize frontmatter
117117
run: |
118118
node script/i18n/homogenize-frontmatter.js
119-
git add . && git commit -m "Run script/i18n/homogenize-frontmatter.js" || echo "Nothing to commit"
119+
git add translations/${{ matrix.language }} && git commit -m "Run script/i18n/homogenize-frontmatter.js" || echo "Nothing to commit"
120120
121121
# step 7 in docs-engineering/crowdin.md
122122
- name: Fix translation errors
123123
run: |
124124
node script/i18n/fix-translation-errors.js
125-
git add . && git commit -m "Run script/i18n/fix-translation-errors.js" || echo "Nothing to commit"
125+
git add translations/${{ matrix.language }} && git commit -m "Run script/i18n/fix-translation-errors.js" || echo "Nothing to commit"
126126
127127
# step 8a in docs-engineering/crowdin.md
128128
- name: Check parsing
129129
run: |
130130
node script/i18n/lint-translation-files.js --check parsing
131-
git add . && git commit -m "Run script/i18n/lint-translation-files.js --check parsing" || echo "Nothing to commit"
131+
git add translations/${{ matrix.language }} && git commit -m "Run script/i18n/lint-translation-files.js --check parsing" || echo "Nothing to commit"
132132
133133
# step 8b in docs-engineering/crowdin.md
134134
- name: Check rendering
135135
run: |
136136
node script/i18n/lint-translation-files.js --check rendering
137-
git add . && git commit -m "Run script/i18n/lint-translation-files.js --check rendering" || echo "Nothing to commit"
137+
git add translations/${{ matrix.language }} && git commit -m "Run script/i18n/lint-translation-files.js --check rendering" || echo "Nothing to commit"
138+
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"
138145
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/fix-translation-errors.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ async function main() {
3333
try {
3434
fileContents = await readFileAsync(path, 'utf8')
3535
} catch (e) {
36-
console.error(e.message)
36+
if (fs.existsSync(path)) {
37+
console.error(e.message)
38+
}
3739
return null
3840
}
3941

script/i18n/lint-translation-files.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ function lintAndResetFiles(checkType) {
7979
// We are not passing --prefer-main because we want to remove the file so we
8080
// reset it directly to the English source
8181
filesToReset.forEach((file) => {
82-
execSync(`script/i18n/reset-translated-file.js ${file}`)
82+
execSync(`script/i18n/reset-translated-file.js ${file} --reason="${checkType} error"`, {
83+
stdio: 'inherit',
84+
})
8385
})
8486

8587
// Print a message with next steps

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]())

script/i18n/reset-known-broken-translation-files.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ async function main() {
5050
// This is done sequentially to ensure only one Git operation is running at any given time.
5151
brokenFilesArray.forEach((file) => {
5252
console.log(`Resetting ${file}`)
53-
execSync(`node script/i18n/reset-translated-file.js ${file}`)
53+
execSync(
54+
`script/i18n/reset-translated-file.js ${file} --reason="Listed in localization-support#489"`,
55+
{ stdio: 'inherit' }
56+
)
5457
})
5558

5659
// Print a message with next steps.

script/i18n/reset-translated-file.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ program
3030
'-m, --prefer-main',
3131
'Reset file to the translated file, try using the file from `main` branch first, if not found (usually due to renaming), fall back to English source.'
3232
)
33+
.option('-d, --dry-run', 'Just pretend to reset files')
34+
.option('-r, --reason <reason>', 'A reason why the file is getting reset')
3335
.parse(process.argv)
3436

37+
const dryRun = program.opts().dryRun
38+
const reason = program.opts().reason
39+
const reasonMessage = reason ? `Reason: ${reason}` : ''
40+
3541
const resetToEnglishSource = (translationFilePath) => {
3642
assert(
3743
translationFilePath.startsWith('translations/'),
@@ -45,15 +51,21 @@ const resetToEnglishSource = (translationFilePath) => {
4551
const relativePath = translationFilePath.split(path.sep).slice(2).join(path.sep)
4652
const englishFile = path.join(process.cwd(), relativePath)
4753

48-
if (!fs.existsSync(englishFile)) {
54+
if (!dryRun && !fs.existsSync(englishFile)) {
4955
fs.unlinkSync(translationFilePath)
5056
return
5157
}
5258

53-
// replace file with English source
54-
const englishContent = fs.readFileSync(englishFile, 'utf8')
55-
fs.writeFileSync(translationFilePath, englishContent)
56-
console.log('-> reverted to English: %s', path.relative(process.cwd(), translationFilePath))
59+
if (!dryRun) {
60+
// replace file with English source
61+
const englishContent = fs.readFileSync(englishFile, 'utf8')
62+
fs.writeFileSync(translationFilePath, englishContent)
63+
}
64+
console.log(
65+
'-> reverted to English: %s %s',
66+
path.relative(process.cwd(), translationFilePath),
67+
reasonMessage
68+
)
5769
}
5870

5971
const [pathArg] = program.args
@@ -64,8 +76,10 @@ const relativePath = fs.existsSync(pathArg) ? path.relative(process.cwd(), pathA
6476

6577
if (program.opts().preferMain) {
6678
try {
67-
execSync(`git checkout main -- ${relativePath}`, { stdio: 'pipe' })
68-
console.log('-> reverted to file from main branch: %s', relativePath)
79+
if (!dryRun) {
80+
execSync(`git checkout main -- ${relativePath}`, { stdio: 'pipe' })
81+
}
82+
console.log('-> reverted to file from main branch: %s %s', relativePath, reasonMessage)
6983
} catch (e) {
7084
if (e.message.includes('pathspec')) {
7185
console.warn(

script/i18n/test-render-translation.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ async function loadAndPatchSiteData(filesWithKnownIssues = {}) {
9494

9595
// Reset the file
9696
console.warn(`resetting file "${relPath}" due to loadSiteData error: ${error.toString()}`)
97-
await exec(`script/i18n/reset-translated-file.js --prefer-main ${relPath}`)
97+
await exec(
98+
`script/i18n/reset-translated-file.js --prefer-main ${relPath} --reason="loadSiteData error"`
99+
)
98100

99101
// Try to load the site data again
100102
return loadAndPatchSiteData(filesWithKnownIssues)

0 commit comments

Comments
 (0)