Exclude tags over 32 characters

Freshdesk doesn’t allow tags over 32 characters long, so filter them out and ignore them.

Information from Freshdesk “Bad Request” was not very helpful for debugging what was wrong. Added new script to pull just one locale from transifex, and optionally save local files instead of sending to Freshdesk.
This commit is contained in:
Chris Garrity 2020-05-18 12:24:06 -04:00
parent b06d84de8b
commit d094f9c2e7
3 changed files with 77 additions and 4 deletions

View file

@ -111,7 +111,10 @@ class FreshdeskApi {
.then(this.checkStatus)
.then(res => res.json());
}
// re-raise the error otherwise
if (err.code === 429) {
this.rateLimited = true;
}
process.stdout.write(`Error processing id ${id} for locale ${locale}: ${err.message}\n`);
throw err;
});
}

View file

@ -8,8 +8,9 @@
const transifex = require('transifex');
const FreshdeskApi = require('./freshdesk-api.js');
const util = require('util');
// const fs = require('fs');
// const mkdirp = require('mkdirp');
const fs = require('fs');
const fsPromises = fs.promises;
const mkdirp = require('mkdirp');
const FD = new FreshdeskApi('https://mitscratch.freshdesk.com', process.env.FRESHDESK_TOKEN);
const TX_PROJECT = 'scratch-help';
@ -116,7 +117,12 @@ const serializeFolderSave = async (json, locale) => {
status: 2 // set status to published
};
if (value.hasOwnProperty('tags')) {
body.tags = value.tags.string.split(',');
let tags = value.tags.string.split(',');
let validTags = tags.filter(tag => tag.length < 33);
if (validTags.length !== tags.length) {
process.stdout.write(`Warning: tags too long in ${id} for ${locale}\n`);
}
body.tags = validTags;
}
let status = await FD.updateArticleTranslation(id, freshdeskLocale(locale), body);
if (status === -1) {
@ -145,6 +151,28 @@ exports.localizeFolder = async (folder, locale) => {
});
};
/**
* Save Transifex resource corresponding to a Knowledge base folder locally for debugging
* @param {object} folder Transifex resource json corresponding to a KB folder
* @param {string} locale locale to pull and save
* @return {Promise} [description]
*/
exports.debugFolder = async (folder, locale) => {
mkdirp.sync('tmpDebug');
getTranslation(TX_PROJECT, folder.slug, locale, {mode: 'default'})
.then(data => {
const json = JSON.parse(data);
fsPromises.writeFile(
`tmpDebug/${folder.slug}_${locale}.json`,
JSON.stringify(json, null, 2)
);
})
.catch((e) => {
process.stdout.write(`Error processing ${folder.slug}, ${locale}: ${e.message}\n`);
process.exitCode = 1; // not ok
});
};
/**
* Process KEYVALUEJSON resources from scratch-help on transifex
* Category and Folder names are stored as plain json

View file

@ -0,0 +1,42 @@
#!/usr/bin/env node
/**
* @fileoverview
* Script to pull scratch-help translations from transifex and push to FreshDesk.
*/
const args = process.argv.slice(2);
const usage = `
Pull knowledge base articles from transifexfor debugging translation errors. Usage:
node tx-pull-locale-articles.js -d locale-code
NOTE:
FRESHDESK_TOKEN environment variable needs to be set to a FreshDesk API key with
access to the Knowledge Base.
TX_TOKEN environment variable needs to be set with a Transifex API token. See
the Localization page on the GUI wiki for information about setting up Transifex.
`;
// Fail immediately if the API tokens are not defined, or missing argument
if (!process.env.TX_TOKEN || !process.env.FRESHDESK_TOKEN || args.length === 0) {
process.stdout.write(usage);
process.exit(1);
}
const {getInputs, saveItem, localizeFolder, debugFolder} = require('./help-utils.js');
let locale = args[0];
let debug = false;
if (locale === '-d') {
debug = true;
locale = args[1];
}
const saveFn = debug ? debugFolder : localizeFolder;
getInputs()
.then(([languages, folders, names]) => { // eslint-disable-line no-unused-vars
process.stdout.write('Processing articles pulled from Transifex\n');
return folders.map(item => saveItem(item, [locale], saveFn));
})
.catch((e) => {
process.stdout.write(`Error: ${e.message}\n`);
process.exitCode = 1; // not ok
});