-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
360 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/com/crowdin/cli/commands/actions/BranchMergeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.CrowdinProjectFull; | ||
import com.crowdin.cli.client.ProjectClient; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.cli.utils.console.ConsoleSpinner; | ||
import com.crowdin.client.branches.model.BranchMergeStatus; | ||
import com.crowdin.client.branches.model.BranchMergeSummary; | ||
import com.crowdin.client.branches.model.MergeBranchRequest; | ||
import com.crowdin.client.projectsgroups.model.Type; | ||
import com.crowdin.client.sourcefiles.model.Branch; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; | ||
import static com.crowdin.cli.utils.console.ExecutionStatus.OK; | ||
|
||
@AllArgsConstructor | ||
class BranchMergeAction implements NewAction<ProjectProperties, ProjectClient> { | ||
|
||
private final String source; | ||
private final String target; | ||
private final boolean noProgress; | ||
private final boolean plainView; | ||
private final boolean dryrun; | ||
private final boolean deleteAfterMerge; | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties properties, ProjectClient client) { | ||
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info", | ||
this.noProgress, plainView, client::downloadFullProject); | ||
|
||
boolean isStringsBasedProject = Objects.equals(project.getType(), Type.STRINGS_BASED); | ||
if (!isStringsBasedProject) { | ||
throw new ExitCodeExceptionMapper.ValidationException(RESOURCE_BUNDLE.getString("error.string_based_only")); | ||
} | ||
|
||
Optional<Branch> sourceBranch = project.findBranchByName(source); | ||
if (sourceBranch.isEmpty()) { | ||
throw new ExitCodeExceptionMapper.NotFoundException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), source)); | ||
} | ||
|
||
Optional<Branch> targetBranch = project.findBranchByName(target); | ||
if (targetBranch.isEmpty()) { | ||
throw new ExitCodeExceptionMapper.NotFoundException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), target)); | ||
} | ||
|
||
MergeBranchRequest request = new MergeBranchRequest(); | ||
request.setSourceBranchId(sourceBranch.get().getId()); | ||
request.setDeleteAfterMerge(deleteAfterMerge); | ||
request.setDryRun(dryrun); | ||
BranchMergeSummary summary = mergeBranch(out, client, targetBranch.get().getId(), request); | ||
|
||
String summaryStr = summary.getDetails().entrySet().stream() | ||
.map(entry -> entry.getKey() + ": " + entry.getValue()) | ||
.collect(Collectors.joining(", ")); | ||
|
||
if (!plainView) { | ||
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.branch.merge"), source, target))); | ||
out.println(String.format(RESOURCE_BUNDLE.getString("message.branch.merge_details"), summaryStr)); | ||
} else { | ||
out.println(String.valueOf(summary.getTargetBranchId())); | ||
} | ||
} | ||
|
||
private BranchMergeSummary mergeBranch(Outputter out, ProjectClient client, Long branchId, MergeBranchRequest request) { | ||
return ConsoleSpinner.execute( | ||
out, | ||
"message.spinner.merging_branch", | ||
"error.branch.merge", | ||
this.noProgress, | ||
false, | ||
() -> { | ||
BranchMergeStatus status = client.mergeBranch(branchId, request); | ||
|
||
while (!status.getStatus().equalsIgnoreCase("finished")) { | ||
ConsoleSpinner.update( | ||
String.format(RESOURCE_BUNDLE.getString("message.spinner.merging_branch_percents"), status.getProgress())); | ||
Thread.sleep(1000); | ||
|
||
status = client.checkMergeBranchStatus(branchId, status.getIdentifier()); | ||
|
||
if (status.getStatus().equalsIgnoreCase("failed")) { | ||
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.branch.merge")); | ||
} | ||
} | ||
ConsoleSpinner.update(String.format(RESOURCE_BUNDLE.getString("message.spinner.merging_branch_percents"), 100)); | ||
return client.getBranchMergeSummary(branchId, status.getIdentifier()); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/crowdin/cli/commands/picocli/BranchMergeSubcommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.crowdin.cli.commands.picocli; | ||
|
||
import com.crowdin.cli.client.ProjectClient; | ||
import com.crowdin.cli.commands.Actions; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Parameters; | ||
|
||
@Command( | ||
sortOptions = false, | ||
name = CommandNames.BRANCH_MERGE | ||
) | ||
class BranchMergeSubcommand extends ActCommandProject { | ||
|
||
@Parameters(descriptionKey = "crowdin.branch.merge.source") | ||
protected String source; | ||
|
||
@Parameters(descriptionKey = "crowdin.branch.merge.target") | ||
protected String target; | ||
|
||
@CommandLine.Option(names = {"--dryrun"}, descriptionKey = "crowdin.branch.merge.dryrun", order = -2) | ||
protected boolean dryrun; | ||
|
||
@CommandLine.Option(names = {"--delete-after-merge"}, descriptionKey = "crowdin.branch.merge.delete-after-merge", order = -2) | ||
protected boolean deleteAfterMerge; | ||
|
||
@CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain") | ||
protected boolean plainView; | ||
|
||
@Override | ||
protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions) { | ||
return actions.branchMerge(source, target, dryrun, deleteAfterMerge, noProgress, plainView); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.