diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java index 496e9fbaf6..20ebb54934 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java @@ -7,6 +7,7 @@ import com.box.l10n.mojito.cli.command.extraction.ExtractionDiffsPaths; import com.box.l10n.mojito.cli.command.extraction.ExtractionsPaths; import com.box.l10n.mojito.cli.command.extraction.MissingExtractionDirectoryExcpetion; +import com.box.l10n.mojito.cli.command.param.Param; import com.box.l10n.mojito.cli.console.ConsoleWriter; import com.box.l10n.mojito.json.ObjectMapper; import com.box.l10n.mojito.rest.entity.Repository; @@ -98,7 +99,7 @@ public class ExtractionDiffCommand extends Command { @Parameter(names = {"--push-to-branch-createdby", "-pbc"}, arity = 1, required = false, description = "Optional username who owns the branch when pusing to a repository") String pushToBranchCreatedBy; - @Parameter(names = "--push-type", arity = 1, required = false, description = "To choose the push type. Don't change unless you know exactly what it does") + @Parameter(names = Param.PUSH_TYPE_LONG, arity = 1, required = false, description = Param.PUSH_TYPE_DESCRIPTION) PushService.PushType pushType = PushService.PushType.NORMAL; @Parameter(names = "--fail-safe", arity = 1, required = false, description = "To fail safe, the command will exit succesfuly even if the processing failed") diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/PushCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/PushCommand.java index a85170a1fe..096062c567 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/PushCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/PushCommand.java @@ -61,6 +61,9 @@ public class PushCommand extends Command { @Parameter(names = {"--branch-createdby", "-bc"}, arity = 1, required = false, description = "username of text unit author") String branchCreatedBy; + @Parameter(names = Param.PUSH_TYPE_LONG, arity = 1, required = false, description = Param.PUSH_TYPE_DESCRIPTION) + PushService.PushType pushType = PushService.PushType.NORMAL; + @Autowired RepositoryClient repositoryClient; @@ -101,7 +104,7 @@ public void execute() throws CommandException { return sourceAsset; }); - pushService.push(repository, sourceAssetStream, branchName, PushService.PushType.NORMAL); + pushService.push(repository, sourceAssetStream, branchName, pushType); consoleWriter.fg(Ansi.Color.GREEN).newLine().a("Finished").println(2); } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/PushService.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/PushService.java index ab1605b549..72c5256fc4 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/PushService.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/PushService.java @@ -64,9 +64,36 @@ public void push(Repository repository, Stream sourceAssetStream, S consoleWriter.fg(Ansi.Color.YELLOW).a("Warning you are using push type: SEND_ASSET_NO_WAIT_NO_DELETE. The" + "command won't wait for the asset processing to finish (ie. if any error " + "happens it will silently fail) and it will skip the asset delete."); - return; + } else { + waitForPollableTasks(pollableTasks); + optinalDeleteUnusedAssets(repository, branchName, pushType, usedAssetIds); } + } + + void optinalDeleteUnusedAssets(Repository repository, String branchName, PushType pushType, Set usedAssetIds) throws CommandException { + Branch branch = repositoryClient.getBranch(repository.getId(), branchName); + + if (PushType.NO_DELETE.equals(pushType)) { + logger.debug("Don't delete unused asset"); + } else { + if (branch == null) { + logger.debug("No branch in the repository, no asset must have been pushed yet, no need to delete"); + } else { + logger.debug("process deleted assets here"); + Set assetIds = Sets.newHashSet(assetClient.getAssetIds(repository.getId(), false, false, branch.getId())); + + assetIds.removeAll(usedAssetIds); + if (!assetIds.isEmpty()) { + consoleWriter.newLine().a("Delete assets from repository, ids: ").fg(Ansi.Color.CYAN).a(assetIds.toString()).println(); + PollableTask pollableTask = assetClient.deleteAssetsInBranch(assetIds, branch.getId()); + consoleWriter.a(" --> task id: ").fg(Ansi.Color.MAGENTA).a(pollableTask.getId()).println(); + commandHelper.waitForPollableTask(pollableTask.getId()); + } + } + } + } + void waitForPollableTasks(List pollableTasks) throws CommandException { try { logger.debug("Wait for all \"push\" tasks to be finished"); for (PollableTask pollableTask : pollableTasks) { @@ -75,23 +102,6 @@ public void push(Repository repository, Stream sourceAssetStream, S } catch (PollableTaskException e) { throw new CommandException(e.getMessage(), e.getCause()); } - - Branch branch = repositoryClient.getBranch(repository.getId(), branchName); - - if (branch == null) { - logger.debug("No branch in the repository, no asset must have been pushed yet, no need to delete"); - } else { - logger.debug("process deleted assets here"); - Set assetIds = Sets.newHashSet(assetClient.getAssetIds(repository.getId(), false, false, branch.getId())); - - assetIds.removeAll(usedAssetIds); - if (!assetIds.isEmpty()) { - consoleWriter.newLine().a("Delete assets from repository, ids: ").fg(Ansi.Color.CYAN).a(assetIds.toString()).println(); - PollableTask pollableTask = assetClient.deleteAssetsInBranch(assetIds, branch.getId()); - consoleWriter.a(" --> task id: ").fg(Ansi.Color.MAGENTA).a(pollableTask.getId()).println(); - commandHelper.waitForPollableTask(pollableTask.getId()); - } - } } enum PushType { @@ -111,6 +121,18 @@ enum PushType { *

* Don't use unless you know what you're doing. */ - SEND_ASSET_NO_WAIT_NO_DELETE + SEND_ASSET_NO_WAIT_NO_DELETE, + /** + * Send asset but don't remove unused assets. + * + * While it could be used to keep adding assets to a repository, that use case has never really showed up as it. + * + * The actual use case we have now is to be used as a workaround for the fact that we can't provide multiple + * source directories (this should eventually be supported in some ways but is not simple). With NO_DELETE + * option you can chain push command with different source directory without marking the asset as deleted. + * + * The first call of the chain can do a normal push which will end removing used asset in the end + */ + NO_DELETE } } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/param/Param.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/param/Param.java index e048b105cc..8049178e8d 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/param/Param.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/param/Param.java @@ -135,4 +135,6 @@ public class Param { public static final String EXTRACTION_INPUT_SHORT = "-i"; public static final String EXTRACTION_INPUT_DESCRIPTION = "The input directory of the extractions commands (if not specified, uses the output directory)"; + public static final String PUSH_TYPE_LONG = "--push-type"; + public static final String PUSH_TYPE_DESCRIPTION = "To choose the push type. Don't change unless you know exactly what it does"; }