From d094f9c2e7e5078a4ac4c72fdcb7f460f8b99e68 Mon Sep 17 00:00:00 2001 From: Chris Garrity Date: Mon, 18 May 2020 12:24:06 -0400 Subject: [PATCH] Exclude tags over 32 characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- scripts/freshdesk-api.js | 5 +++- scripts/help-utils.js | 34 +++++++++++++++++++++--- scripts/tx-pull-locale-articles.js | 42 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100755 scripts/tx-pull-locale-articles.js diff --git a/scripts/freshdesk-api.js b/scripts/freshdesk-api.js index e8f067a5..9e44a12e 100644 --- a/scripts/freshdesk-api.js +++ b/scripts/freshdesk-api.js @@ -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; }); } diff --git a/scripts/help-utils.js b/scripts/help-utils.js index cdb72004..8a3b2326 100644 --- a/scripts/help-utils.js +++ b/scripts/help-utils.js @@ -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 diff --git a/scripts/tx-pull-locale-articles.js b/scripts/tx-pull-locale-articles.js new file mode 100755 index 00000000..6b55fb6b --- /dev/null +++ b/scripts/tx-pull-locale-articles.js @@ -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 + });