Skip to content

Commit

Permalink
Merge pull request #5008 from newrelic/clark/2536-modify-queue-script
Browse files Browse the repository at this point in the history
Clark/2536 modify queue script
  • Loading branch information
rudouglas committed Nov 23, 2021
2 parents 264fbba + a9dd996 commit 9db4a3f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
40 changes: 31 additions & 9 deletions scripts/actions/add-files-to-translation-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,33 @@ const translationDifference = (pendingFiles, prChanges) =>
const slugIntersection = (pendingFiles, filesToRemove) =>
pendingFiles.filter((file) => filesToRemove.includes(file.slug));

const humanTranslatedProjectID = process.env.HUMAN_TRANSLATION_PROJECT_ID;
const machineTranslatedProjectID = process.env.MACHINE_TRANSLATION_PROJECT_ID;

/**
* Determines if a particular locale should be human or machine translated based on the files frontmatter.
*
* @param {String[]} translateFM
* @returns {Function => String} projectId
*/

const getProjectId = (translateFM) => (locale) => {
return Array.isArray(translateFM) && translateFM.includes(locale)
? humanTranslatedProjectID
: machineTranslatedProjectID;
};

const getLocalizedFileData = (prFile) => {
const contents = fs.readFileSync(path.join(process.cwd(), prFile.filename));
const { data } = frontmatter(contents);
return data.translate
? data.translate.map((locale) => ({
...prFile,
locale: LOCALE_IDS[locale],
}))
: [];
const checkLocale = getProjectId(data.translate);

// Loops over all supported locales, determines translation project via getProjectId func.
return Object.keys(LOCALE_IDS).map((locale) => ({
...prFile,
locale: LOCALE_IDS[locale],
project_id: checkLocale(locale),
}));
};

const removedFiles = (prFiles) =>
Expand All @@ -52,7 +70,6 @@ const main = async () => {
const url = process.argv[2];

const queue = await getTranslations({ status: STATUS.PENDING });

const prFileData = await fetchPaginatedGHResults(
url,
process.env.GITHUB_TOKEN
Expand All @@ -69,8 +86,13 @@ const main = async () => {
);

await Promise.all(
fileDataToAddToQueue.map(({ filename, locale }) =>
addTranslation({ slug: filename, status: STATUS.PENDING, locale })
fileDataToAddToQueue.map(({ filename, locale, project_id }) =>
addTranslation({
slug: filename,
status: STATUS.PENDING,
locale,
project_id,
})
)
);

Expand Down
9 changes: 7 additions & 2 deletions scripts/actions/translation_workflow/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ const getStatuses = async (filters = {}) => {
* @param {string} locale - string value of locale enum
* @returns newly created translation
*/
const addTranslation = async ({ slug, status, locale }) => {
const translation = await Models.Translation.create({ slug, status, locale });
const addTranslation = async ({ slug, status, locale, project_id }) => {
const translation = await Models.Translation.create({
slug,
status,
locale,
project_id,
});
return translation;
};

Expand Down

0 comments on commit 9db4a3f

Please sign in to comment.