Skip to content

Commit

Permalink
Fix issues related to tag names having some character converted
Browse files Browse the repository at this point in the history
  • Loading branch information
aurambaj committed Jun 22, 2024
1 parent 1cabf59 commit c539331
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,14 @@ void mapMojitoAndThirdPartyTextUnits(
thirdPartyTextUnits.stream()
.collect(
groupingBy(
o -> assetCache.getUnchecked(o.getAssetPath()).orElse(null),
o ->
assetCache
.getUnchecked(o.getAssetPath())
.orElseThrow(
() ->
new RuntimeException(
"Trying to map a third party text unit for an asset (%s) that does not exist in the repository"
.formatted(o.getAssetPath()))),
LinkedHashMap::new,
toList()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public List<ThirdPartyTextUnit> getThirdPartyTextUnits(

List<ThirdPartyTextUnit> thirdPartyTextUnits = new ArrayList<>();

List<TranslationKey> phraseTranslationKeys = phraseClient.getKeys(projectId);
String currentTagsForRepository = getCurrentTagsForRepository(repository, projectId);

List<TranslationKey> phraseTranslationKeys =
phraseClient.getKeys(projectId, currentTagsForRepository);

for (TranslationKey translationKey : phraseTranslationKeys) {

Expand Down Expand Up @@ -181,9 +184,7 @@ public void removeUnusedKeysAndTags(
.map(Tag::getName)
.filter(Objects::nonNull)
.filter(tagName -> tagName.startsWith(TAG_PREFIX))
.filter(
tagName ->
!tagName.startsWith(TAG_PREFIX_WITH_REPOSITORY.formatted(repositoryName)))
.filter(tagName -> !tagName.startsWith(getTagNamePrefixForRepository(repositoryName)))
.toList();

List<String> allActiveTags = new ArrayList<>(tagsForOtherRepositories);
Expand Down Expand Up @@ -241,12 +242,25 @@ private List<TextUnitDTO> getSourceTextUnitDTOsPluralOnly(
public static String getTagForUpload(String repositoryName) {
ZonedDateTime zonedDateTime = JSR310Migration.dateTimeNowInUTC();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss_SSS");
return ("%s%s_%s_%s")
.formatted(
TAG_PREFIX,
repositoryName,
formatter.format(zonedDateTime),
Math.abs(UUID.randomUUID().getLeastSignificantBits() % 1000));
return normalizeTagName(
"%s%s_%s_%s"
.formatted(
TAG_PREFIX,
repositoryName,
formatter.format(zonedDateTime),
Math.abs(UUID.randomUUID().getLeastSignificantBits() % 1000)));
}

private static String getTagNamePrefixForRepository(String repositoryName) {
return normalizeTagName(TAG_PREFIX_WITH_REPOSITORY.formatted(repositoryName));
}

/**
* At least "/" are getting converted by phrase into "_", apply that logic on our side to have
* consistent filtering
*/
private static String normalizeTagName(String repositoryName) {
return repositoryName.replace("/", "_");
}

@Override
Expand All @@ -268,7 +282,7 @@ public PollableFuture<Void> pull(

for (RepositoryLocale repositoryLocale : repositoryLocalesWithoutRootLocale) {
String localeTag = repositoryLocale.getLocale().getBcp47Tag();
logger.info("Downloading locale: {} from Phrase", localeTag);
logger.info("Downloading locale: {} from Phrase with tags: {}", localeTag, currentTags);

String fileContent =
phraseClient.localeDownload(
Expand All @@ -293,12 +307,25 @@ public PollableFuture<Void> pull(
return null;
}

/**
* It should typically return a single tag, but if concurrent syncs, it could return more. That
* should not be a problem when downloading or keys for mapping or pulling strings
*/
private String getCurrentTagsForRepository(Repository repository, String projectId) {
return phraseClient.listTags(projectId).stream()
.map(Tag::getName)
.filter(Objects::nonNull)
.filter(tagName -> tagName.startsWith(repository.getName()))
.collect(Collectors.joining(","));
String tagNamePrefixForRepository = getTagNamePrefixForRepository(repository.getName());
String tags =
phraseClient.listTags(projectId).stream()
.map(Tag::getName)
.filter(Objects::nonNull)
.filter(tagName -> tagName.startsWith(tagNamePrefixForRepository))
.collect(Collectors.joining(","));

if (tags.isBlank()) {
throw new RuntimeException(
"There are no current tags for the repository, make sure push was run first or that the repository name does not contain special character (which will need to get normalized)");
}

return tags;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ public class ThridPartyTMSPhraseException extends RuntimeException {
public ThridPartyTMSPhraseException(String msg, Throwable e) {
super(msg, e);
}

public ThridPartyTMSPhraseException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,23 @@ String uploadCreateFile(

Path tmpWorkingDirectory = null;

logger.info(
"uploadCreateFile: projectId: {}, localeId: {}, fileName: {}, tags: {}",
projectId,
localeId,
fileName,
tags);

try {
tmpWorkingDirectory = createTempDirectory("phrase-integration");

if (tmpWorkingDirectory.toFile().exists()) {
logger.info("Created temporary working directory: {}", tmpWorkingDirectory);
logger.debug("Created temporary working directory: {}", tmpWorkingDirectory);
}

Path fileToUpload = tmpWorkingDirectory.resolve(fileName);

logger.info("Create file: {}", fileToUpload);
logger.debug("Create file: {}", fileToUpload);
createDirectories(fileToUpload.getParent());
write(fileToUpload, fileContent);

Expand Down Expand Up @@ -294,7 +301,7 @@ public String localeDownload(
.block();
}

public List<TranslationKey> getKeys(String projectId) {
public List<TranslationKey> getKeys(String projectId, String tags) {
KeysApi keysApi = new KeysApi(apiClient);
AtomicInteger page = new AtomicInteger(0);
int batchSize = BATCH_SIZE;
Expand All @@ -305,7 +312,15 @@ public List<TranslationKey> getKeys(String projectId) {
() -> {
logger.info("Fetching keys for project: {}, page: {}", projectId, page);
return keysApi.keysList(
projectId, null, page.get(), batchSize, null, null, null, null, null);
projectId,
null,
page.get(),
batchSize,
null,
null,
null,
"tags:%s".formatted(tags),
null);
})
.retryWhen(
retryBackoffSpec.doBeforeRetry(
Expand Down

0 comments on commit c539331

Please sign in to comment.