Skip to content

Commit

Permalink
feat: use the new long-term storage store method
Browse files Browse the repository at this point in the history
For #383.
  • Loading branch information
liammulh committed Apr 10, 2023
1 parent ef4bf9b commit 156697d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 134 deletions.
21 changes: 14 additions & 7 deletions src/client/js/submitTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/

import axios from 'axios';
import alertErrorMessage from './alertErrorMessage.js';
import KeyTypesEnum from '../../common/KeyTypesEnum.js';
import publicConfig from '../../common/publicConfig.js';
import alertErrorMessage from './alertErrorMessage.js';
import computeTranslatedStringsData from './computeTranslatedStringsData.js';
import KeyTypesEnum from '../../common/KeyTypesEnum.js';
import makeTranslationObject from './makeTranslationObject.js';

/**
Expand All @@ -30,7 +30,7 @@ const submitTranslation = async (
localeName
) => {

let submitted = false;
let submitStatus = null;
const translatedStringsData = computeTranslatedStringsData( values );
const translation = await makeTranslationObject( values, simName, locale );
const messages = {};
Expand All @@ -49,8 +49,8 @@ const submitTranslation = async (
if ( window.confirm( confirmMessage ) ) {
try {
const result = await axios.post( `${publicConfig.translationApiRoute}/submitTranslation`, translation );
submitted = result.data;
if ( submitted ) {
submitStatus = result.data;
if ( submitStatus.allRepoContentsStored && submitStatus.buildRequested ) {
const units = 'hours';
const timeUntilChanges = publicConfig.VALID_METADATA_DURATION / 1000 / 60 / 60;
const submissionMessage = 'Translation submitted.'
Expand All @@ -60,15 +60,22 @@ const submitTranslation = async (
window.alert( submissionMessage );
}
else {
window.alert( 'An error occurred. Translation not submitted.' );
let errorMessage = 'An error occurred while submitting the translation.';
if ( !submitStatus.allRepoContentsStored ) {
errorMessage += ' Not all translation contents (possibly none) were stored in long-term storage.';
}
if ( !submitStatus.buildRequested ) {
errorMessage += ' The build request was not sent to the build server.';
}
window.alert( errorMessage );
}
}
catch( e ) {
alertErrorMessage( e );
}
}

return submitted;
return submitStatus;
};

export default submitTranslation;
52 changes: 32 additions & 20 deletions src/server/translationApi/LongTermStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,40 @@ class LongTermStorage {
*/
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;
const emptyTranslationFileContents = Object.keys( translationFileContents ).length === 0;
if ( privateConfig.PERFORM_STRING_COMMITS && !emptyTranslationFileContents ) {
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 );
}
}
catch( e ) {
logger.error( e );
else {
if ( !privateConfig.PERFORM_STRING_COMMITS ) {
logger.warn( 'config option for committing to long-term storage is false' );
}
if ( emptyTranslationFileContents ) {
logger.warn( `translation file contents for ${simOrLibRepo}/${locale} are empty` );
}
logger.warn( 'translation will not be stored in long-term storage' );
}
return stored;
}
Expand Down
22 changes: 12 additions & 10 deletions src/server/translationApi/api/submitTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import getSimMetadata from '../getSimMetadata.js';
import logger from '../logger.js';
import prepareTranslationForLongTermStorage from '../prepareTranslationForLongTermStorage.js';
import requestBuild from '../requestBuild.js';
import storeTranslationLongTerm from '../storeTranslationLongTerm.js';
import storeEachRepoContents from '../storeEachRepoContents.js';
import { reportObjectCache } from '../translationApi.js';
import getSimNamesAndTitles from '../getSimNamesAndTitles.js';

Expand All @@ -26,19 +26,22 @@ import getSimNamesAndTitles from '../getSimNamesAndTitles.js';
*/
const submitTranslation = async ( req, res ) => {
logger.info( 'attempting to submit translation' );
let submitted = false;
const submitStatus = {
allRepoContentsStored: false,
buildRequested: false
};
try {
logger.info( `sending ${req.body.locale}/${req.body.simName} translation to be prepared for long-term storage` );
const preparedTranslation = await prepareTranslationForLongTermStorage( req.body );
logger.info( `sending ${req.body.locale}/${req.body.simName} translation to be stored long-term` );
const longTermStorageRes = await storeTranslationLongTerm( preparedTranslation );
if ( longTermStorageRes ) {
const repos = await getReposToStoreLongTerm( preparedTranslation );
submitStatus.allRepoContentsStored = await storeEachRepoContents( preparedTranslation, repos );
if ( submitStatus.allRepoContentsStored ) {
logger.info( 'successfully stored translation long term' );

// For each sim repo in the translation, we need to set the sim repo's
// report object to dirty. See https://github.com/phetsims/rosetta/issues/379
// for more info and background on this.
const repos = await getReposToStoreLongTerm( preparedTranslation );
const simNamesAndTitles = getSimNamesAndTitles( await getSimMetadata(), 'true' );
const simNames = Object.keys( simNamesAndTitles );
for ( const repo of repos ) {
Expand Down Expand Up @@ -66,11 +69,10 @@ const submitTranslation = async ( req, res ) => {
else {
logger.warn( 'either deletion of previously saved translation failed or there was no previously saved translation' );
}
const buildRequestRes = await requestBuild( req.body.simName, req.body.locale, req.body.userId );
if ( buildRequestRes ) {
submitStatus.buildRequested = await requestBuild( req.body.simName, req.body.locale, req.body.userId );
if ( submitStatus.buildRequested ) {
logger.info( 'build request succeeded' );
}
submitted = buildRequestRes;
}
else {
logger.error( `long-term storage of ${req.body.locale}/${req.body.simName} failed` );
Expand All @@ -79,9 +81,9 @@ const submitTranslation = async ( req, res ) => {
catch( e ) {
logger.error( e );
}
logger.info( `translation was submitted: ${submitted}` );
logger.info( `build requested: ${submitStatus.buildRequested}` );
logger.info( 'done attempting to submit translation' );
res.send( submitted );
res.send( submitStatus );
};

export default submitTranslation;
27 changes: 27 additions & 0 deletions src/server/translationApi/storeEachRepoContents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023, University of Colorado Boulder

/**
* For a given prepared translation, store each repo's contents in long-term storage.
*
* @author Liam Mulhall <liammulh@gmail.com>
*/

import logger from './logger.js';
import { longTermStorage } from './translationApi.js';

const storeEachRepoContents = async ( preparedTranslation, repos ) => {
logger.info( `attempting to store repo contents for ${preparedTranslation.locale}/${preparedTranslation.simName} long-term` );
let allRepoContentsStored = true;
for ( const repo of repos ) {
const repoContents = preparedTranslation.translationFileContents[ repo ];
const wasStored = await longTermStorage.store( repo, preparedTranslation.locale, repoContents );
if ( !wasStored ) {
allRepoContentsStored = false;
}
}
logger.info( `all repo contents were stored: ${allRepoContentsStored}` );
logger.info( `done attempting to store repo contents for ${preparedTranslation.locale}/${preparedTranslation.simName} long-term` );
return allRepoContentsStored;
};

export default storeEachRepoContents;
97 changes: 0 additions & 97 deletions src/server/translationApi/storeTranslationLongTerm.js

This file was deleted.

0 comments on commit 156697d

Please sign in to comment.