Skip to content

Commit

Permalink
Add NO_DELETE push type to the push command
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aurambaj committed Jun 5, 2020
1 parent 7212f96 commit 7f15d72
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
60 changes: 41 additions & 19 deletions cli/src/main/java/com/box/l10n/mojito/cli/command/PushService.java
Expand Up @@ -64,9 +64,36 @@ public void push(Repository repository, Stream<SourceAsset> 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<Long> 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<Long> 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<PollableTask> pollableTasks) throws CommandException {
try {
logger.debug("Wait for all \"push\" tasks to be finished");
for (PollableTask pollableTask : pollableTasks) {
Expand All @@ -75,23 +102,6 @@ public void push(Repository repository, Stream<SourceAsset> 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<Long> 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 {
Expand All @@ -111,6 +121,18 @@ enum PushType {
* <p>
* 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
}
}
Expand Up @@ -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";
}

0 comments on commit 7f15d72

Please sign in to comment.