Skip to content

Commit

Permalink
feat: add store method to LongTermStorage class
Browse files Browse the repository at this point in the history
For #383.
  • Loading branch information
liammulh committed Apr 10, 2023
1 parent a1c6b6a commit ef4bf9b
Showing 1 changed file with 88 additions and 3 deletions.
91 changes: 88 additions & 3 deletions src/server/translationApi/LongTermStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import logger from './logger.js';
* @author Liam Mulhall <liammulh@gmail.com>
*/

const OWNER = 'phetsims';
const REPO = 'babel';

class LongTermStorage {

constructor() {
Expand All @@ -19,6 +22,18 @@ class LongTermStorage {
this.octokit = new Octokit( { auth: privateConfig.GITHUB_PAT } );
}

/**
* Get the path to a translation file in the phetsims/babel repo.
*
* @param {String} simOrLibRepo - repository where the strings come from
* @param {String} locale - ISO 639-1 locale code, e.g. es for Spanish
* @returns {String} - path to the translation file
* @private
*/
_getFilePath( simOrLibRepo, locale ) {
return `${simOrLibRepo}/${simOrLibRepo}-strings_${locale}.json`;
}

/**
* Get a translation for a given repo and locale if it exists.
*
Expand All @@ -29,12 +44,13 @@ class LongTermStorage {
* @public
*/
async get( simOrLibRepo, locale, branch = null ) {
logger.info( `attempting to get translation for ${simOrLibRepo}/${locale} from long-term storage` );
let translatedStringFileContents = {};
try {
const response = await this.octokit.repos.getContent( {
owner: 'phetsims',
repo: 'babel',
path: `${simOrLibRepo}/${simOrLibRepo}-strings_${locale}.json`,
owner: OWNER,
repo: REPO,
path: this._getFilePath( simOrLibRepo, locale ),
ref: branch ? branch : privateConfig.BABEL_BRANCH
} );
const content = Buffer.from( response.data.content, 'base64' ).toString( 'utf-8' );
Expand All @@ -50,6 +66,75 @@ class LongTermStorage {
}
return translatedStringFileContents;
}

/**
* Get the SHA of a translation file in the phetsims/babel repo.
*
* @param {String} simOrLibRepo - repository where the strings come from
* @param {String} locale - ISO 639-1 locale code, e.g. es for Spanish
* @param {String} branch - phetsims/babel branch to get the translation from
* @returns {Promise<String>} - the SHA of the translation file
* @private
*/
async _getGitShaOfFile( simOrLibRepo, locale, branch = null ) {
let sha = '';
try {
const response = await this.octokit.repos.getContent( {
owner: OWNER,
repo: REPO,
path: this._getFilePath( simOrLibRepo, locale ),
ref: branch ? branch : privateConfig.BABEL_BRANCH
} );
sha = response.data.sha;
}
catch( e ) {
if ( Number( e.response.status ) === 404 ) {
logger.warn( `no translation file for ${simOrLibRepo} in ${locale}` );
}
else {
logger.error( e );
}
}
return sha;
}

/**
* Store a translation in long-term storage.
*
* @param {String} simOrLibRepo - repository where the strings come from
* @param {String} locale - ISO 639-1 locale code, e.g. es for Spanish
* @param {Object} translationFileContents - translation file contents
* @param {String} branch - phetsims/babel branch to store the translation in
* @returns {Promise<Boolean>} - whether the translation was stored
* @public
*/
async store( simOrLibRepo, locale, translationFileContents, branch = null ) {
let stored = false;
logger.info( `attempting to store translation for ${simOrLibRepo}/${locale} in long-term storage` );
const sha = await this._getGitShaOfFile( simOrLibRepo, locale, branch );
try {
const response = await this.octokit.repos.createOrUpdateFileContents( {
owner: OWNER,
repo: REPO,
path: this._getFilePath( simOrLibRepo, locale ),
branch: branch ? branch : privateConfig.BABEL_BRANCH,
message: `automated commit from rosetta for sim/lib ${simOrLibRepo}, locale ${locale}`,
content: Buffer.from(
JSON.stringify( translationFileContents, null, 2 ), 'utf-8'
).toString( 'base64' ),
sha: sha === '' ? undefined : sha // SHA is required to update a file, not required to create a file.
} );
if ( response.status >= 200 && response.status < 300 ) {
logger.info( `stored translation for ${simOrLibRepo}/${locale} in long-term storage` );
logger.verbose( `commit sha: ${response.data.commit.sha}` );
stored = true;
}
}
catch( e ) {
logger.error( e );
}
return stored;
}
}

export default LongTermStorage;

0 comments on commit ef4bf9b

Please sign in to comment.