-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2726 from newrelic/campfire/translate-deletions
Create check for out of date translated files
- Loading branch information
Showing
9 changed files
with
254 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
scripts/actions/__tests__/check-for-outdated-translations.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const fs = require('fs'); | ||
const fetch = require('node-fetch'); | ||
|
||
const checkForOutdatedTranslations = require('../check-for-outdated-translations'); | ||
|
||
// Mock node-fetch so we can avoid calling out to GitHub | ||
jest.mock('node-fetch'); | ||
|
||
global.process.exit = jest.fn(); | ||
|
||
// Helper function to mock a response from Github | ||
const mockGithubResponse = (result) => { | ||
fetch.mockResolvedValueOnce({ | ||
ok: true, | ||
json: jest.fn(() => Promise.resolve(result)), | ||
headers: { get: jest.fn() }, | ||
}); | ||
}; | ||
|
||
// Mock fs so we don't manipulate local files | ||
jest.mock('fs'); | ||
|
||
// helper function to construct and mock the response from readFileSync | ||
const mockReadFileSync = (translate = []) => { | ||
const mdx = `--- | ||
title: A test file | ||
${translate.length ? `translate:\n - ${translate.join('\n - ')}` : ''} | ||
--- | ||
This is a test file | ||
`; | ||
|
||
fs.readFileSync.mockReturnValueOnce(mdx); | ||
}; | ||
|
||
const mockExistsSync = (bool) => fs.existsSync.mockReturnValueOnce(bool); | ||
|
||
const STATUS = { | ||
ADDED: 'added', | ||
MODIFIED: 'modified', | ||
REMOVED: 'removed', | ||
}; | ||
|
||
describe('Action: Check for outdated translations', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('should succeed when no files are found for deletion', async () => { | ||
const filename = '/content/bar.mdx'; | ||
mockGithubResponse([ | ||
{ | ||
filename, | ||
status: STATUS.ADDED, | ||
}, | ||
]); | ||
|
||
mockReadFileSync(['jp']); | ||
mockExistsSync(false); | ||
|
||
await checkForOutdatedTranslations(); | ||
|
||
expect(global.process.exit).toHaveBeenLastCalledWith(0); | ||
}); | ||
|
||
test('should fail when files are found for deletion', async () => { | ||
const filename = '/content/bar.mdx'; | ||
mockGithubResponse([ | ||
{ | ||
filename, | ||
status: STATUS.REMOVED, | ||
}, | ||
]); | ||
|
||
mockExistsSync(true); | ||
await checkForOutdatedTranslations(); | ||
|
||
expect(global.process.exit).toHaveBeenLastCalledWith(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fetch = require('node-fetch'); | ||
const frontmatter = require('@github-docs/frontmatter'); | ||
const parseLinkHeader = require('parse-link-header'); | ||
|
||
const checkArgs = require('./utils/check-args'); | ||
const { prop } = require('../utils/functional'); | ||
const { ADDITIONAL_LOCALES } = require('../utils/constants'); | ||
|
||
const doI18nFilesExist = (fileName, locales) => { | ||
const i18nPrefix = path.join(process.cwd(), 'src/i18n/content'); | ||
const baseFileName = fileName.replace('src/content/', ''); | ||
|
||
return locales | ||
.map((locale) => { | ||
const filePath = path.join(i18nPrefix, locale, baseFileName); | ||
const fileExists = fs.existsSync(filePath); | ||
return fileExists ? filePath : null; | ||
}) | ||
.filter(Boolean); | ||
}; | ||
|
||
const fetchFilesFromGH = async (url) => { | ||
let files = []; | ||
let nextPageLink = url; | ||
|
||
while (nextPageLink) { | ||
const resp = await fetch(nextPageLink, { | ||
headers: { authorization: `token ${process.env.GITHUB_TOKEN}` }, | ||
}); | ||
if (!resp.ok) { | ||
throw new Error( | ||
`Github API returned status ${resp.code} - ${resp.message}` | ||
); | ||
} | ||
const page = await resp.json(); | ||
nextPageLink = getNextLink(resp.headers.get('Link')); | ||
files = [...files, ...page]; | ||
} | ||
|
||
return files; | ||
}; | ||
|
||
const getNextLink = (linkHeader) => { | ||
const parsedLinkHeader = parseLinkHeader(linkHeader); | ||
if (parsedLinkHeader && parsedLinkHeader.next) { | ||
return parsedLinkHeader.next.url || null; | ||
} | ||
return null; | ||
}; | ||
|
||
/** | ||
* @param {string} url The API url that is used to fetch files. | ||
*/ | ||
const checkOutdatedTranslations = async (url) => { | ||
const files = await fetchFilesFromGH(url); | ||
const mdxFiles = files | ||
? files.filter((file) => path.extname(file.filename) === '.mdx') | ||
: []; | ||
|
||
const mdxFilesContent = mdxFiles | ||
.filter((file) => file.status !== 'removed') | ||
.reduce((files, file) => { | ||
const contents = fs.readFileSync(path.join(process.cwd(), file.filename)); | ||
const { data } = frontmatter(contents); | ||
return [...files, { path: file.filename, locales: data.translate || [] }]; | ||
}, []); | ||
|
||
const removedMdxFileNames = mdxFiles | ||
.filter((f) => f.status === 'removed') | ||
.map(prop('filename')); | ||
|
||
// if a locale was removed from the translate frontmatter, we want to remove the translated version of that file. | ||
|
||
const modifiedFiles = mdxFilesContent.flatMap((file) => { | ||
const unsetLocales = ADDITIONAL_LOCALES.filter( | ||
(l) => !file.locales.includes(l) | ||
); | ||
return doI18nFilesExist(file.path, unsetLocales); | ||
}); | ||
|
||
const removedFiles = removedMdxFileNames.flatMap((name) => | ||
doI18nFilesExist(name, ADDITIONAL_LOCALES) | ||
); | ||
|
||
const orphanedI18nFiles = [...modifiedFiles, ...removedFiles]; | ||
|
||
if (orphanedI18nFiles.length > 0) { | ||
orphanedI18nFiles.forEach((f) => | ||
console.log( | ||
`ACTION NEEDED: Unpaired translation found -> ${f.replace( | ||
`${process.cwd()}/`, | ||
'' | ||
)}` | ||
) | ||
); | ||
throw new Error( | ||
'Files without matching english counterparts were found, see logs for filenames' | ||
); | ||
} | ||
}; | ||
|
||
/** Entrypoint. */ | ||
const main = async () => { | ||
try { | ||
checkArgs(3); | ||
const url = process.argv[2]; | ||
|
||
await checkOutdatedTranslations(url); | ||
process.exit(0); | ||
} catch (error) { | ||
console.log(error); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
module.exports = main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters