-
Notifications
You must be signed in to change notification settings - Fork 97
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
287 additions
and
6 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
79 changes: 79 additions & 0 deletions
79
src/main/java/com/crowdin/cli/commands/actions/BranchEditAction.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,79 @@ | ||
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.functionality.RequestBuilder; | ||
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.cli.utils.console.ConsoleSpinner; | ||
import com.crowdin.cli.utils.console.ExecutionStatus; | ||
import com.crowdin.client.core.model.PatchOperation; | ||
import com.crowdin.client.core.model.PatchRequest; | ||
import com.crowdin.client.core.model.Priority; | ||
import com.crowdin.client.sourcefiles.model.Branch; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; | ||
|
||
class BranchEditAction implements NewAction<ProjectProperties, ProjectClient> { | ||
|
||
private final String branch; | ||
private final String name; | ||
private final String title; | ||
private final Priority priority; | ||
private final String exportPattern; | ||
private final boolean noProgress; | ||
private final boolean plainView; | ||
|
||
BranchEditAction(String branch, String name, String title, Priority priority, String exportPattern, boolean noProgress, boolean plainView) { | ||
this.branch = branch; | ||
this.name = name; | ||
this.title = title; | ||
this.priority = priority; | ||
this.exportPattern = exportPattern; | ||
this.noProgress = noProgress; | ||
this.plainView = plainView; | ||
} | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties pb, ProjectClient client) { | ||
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info", | ||
this.noProgress, this.plainView, client::downloadFullProject); | ||
|
||
Optional<Branch> branchObj = project.findBranchByName(this.branch); | ||
if (branchObj.isEmpty()) { | ||
throw new ExitCodeExceptionMapper.NotFoundException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), this.branch)); | ||
} | ||
|
||
List<PatchRequest> requests = new ArrayList<>(); | ||
if (name != null) { | ||
PatchRequest request = RequestBuilder.patch(name, PatchOperation.REPLACE, "/name"); | ||
requests.add(request); | ||
} | ||
if (title != null) { | ||
PatchRequest request = RequestBuilder.patch(title, PatchOperation.REPLACE, "/title"); | ||
requests.add(request); | ||
} | ||
if (priority != null) { | ||
PatchRequest request = RequestBuilder.patch(priority, PatchOperation.REPLACE, "/priority"); | ||
requests.add(request); | ||
} | ||
if (exportPattern != null) { | ||
PatchRequest request = RequestBuilder.patch(exportPattern, PatchOperation.REPLACE, "/exportPattern"); | ||
requests.add(request); | ||
} | ||
|
||
Branch updatedBranch = client.editBranch(branchObj.get().getId(), requests); | ||
|
||
if (!plainView) { | ||
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.branch.list"), updatedBranch.getId(), updatedBranch.getName()))); | ||
} else { | ||
out.println(updatedBranch.getName()); | ||
} | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/main/java/com/crowdin/cli/commands/picocli/BranchEditSubcommand.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,50 @@ | ||
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 com.crowdin.client.core.model.Priority; | ||
import picocli.CommandLine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@CommandLine.Command( | ||
sortOptions = false, | ||
name = CommandNames.EDIT | ||
) | ||
class BranchEditSubcommand extends ActCommandProject { | ||
|
||
@CommandLine.Parameters(descriptionKey = "crowdin.branch.edit.name") | ||
protected String name; | ||
|
||
@CommandLine.Option(names = "--name", descriptionKey = "crowdin.branch.edit.new-name", paramLabel = "...", order = -2) | ||
protected String newName; | ||
|
||
@CommandLine.Option(names = "--title", descriptionKey = "crowdin.branch.edit.title", paramLabel = "...", order = -2) | ||
protected String title; | ||
|
||
@CommandLine.Option(names = "--priority", descriptionKey = "crowdin.branch.edit.priority", paramLabel = "...", order = -2) | ||
protected Priority priority; | ||
|
||
@CommandLine.Option(names = "--export-pattern", descriptionKey = "crowdin.branch.edit.export-pattern", paramLabel = "...", order = -2) | ||
protected String exportPattern; | ||
|
||
@CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain") | ||
protected boolean plainView; | ||
|
||
@Override | ||
protected List<String> checkOptions() { | ||
List<String> errors = new ArrayList<>(); | ||
if (newName == null && title == null && priority == null && exportPattern == null) { | ||
errors.add(RESOURCE_BUNDLE.getString("error.branch_no_edit")); | ||
} | ||
return errors; | ||
} | ||
|
||
@Override | ||
protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions) { | ||
return actions.branchEdit(name, newName, title, priority, exportPattern, 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
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
77 changes: 77 additions & 0 deletions
77
src/test/java/com/crowdin/cli/commands/actions/BranchEditActionTest.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,77 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.ProjectBuilder; | ||
import com.crowdin.cli.client.ProjectClient; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.commands.functionality.RequestBuilder; | ||
import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.cli.properties.PropertiesWithFiles; | ||
import com.crowdin.cli.utils.Utils; | ||
import com.crowdin.client.core.model.PatchOperation; | ||
import com.crowdin.client.core.model.PatchRequest; | ||
import com.crowdin.client.core.model.Priority; | ||
import com.crowdin.client.sourcefiles.model.Branch; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class BranchEditActionTest { | ||
|
||
private PropertiesWithFiles pb; | ||
private ProjectClient client = mock(ProjectClient.class); | ||
private NewAction<ProjectProperties, ProjectClient> action; | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
public void testBranchEdit(String branch, String name, String title, Priority priority, String exportPattern) { | ||
Long branchId = 1L; | ||
|
||
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder | ||
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") | ||
.setBasePath(Utils.PATH_SEPARATOR); | ||
pb = pbBuilder.build(); | ||
when(client.downloadFullProject()) | ||
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())) | ||
.addBranches(branchId, branch).build()); | ||
|
||
when(client.editBranch(any(), any())).thenReturn(new Branch()); | ||
|
||
action = new BranchEditAction(branch, name, title, priority, exportPattern, false, false); | ||
action.act(Outputter.getDefault(), pb, client); | ||
|
||
List<PatchRequest> patches = new ArrayList<>() {{ | ||
if (name != null) { | ||
add(RequestBuilder.patch(name, PatchOperation.REPLACE, "/name")); | ||
} | ||
if (title != null) { | ||
add(RequestBuilder.patch(title, PatchOperation.REPLACE, "/title")); | ||
} | ||
if (priority != null) { | ||
add(RequestBuilder.patch(priority, PatchOperation.REPLACE, "/priority")); | ||
} | ||
if (exportPattern != null) { | ||
add(RequestBuilder.patch(exportPattern, PatchOperation.REPLACE, "/exportPattern")); | ||
} | ||
}}; | ||
verify(client).editBranch(branchId, patches); | ||
} | ||
|
||
public static Stream<Arguments> testBranchEdit() { | ||
return Stream.of( | ||
arguments("main", "dev", null, null, null), | ||
arguments("main", null, "test", null, null), | ||
arguments("main", null, null, Priority.HIGH, null), | ||
arguments("main", null, null, null, "%three_letters_code%"), | ||
arguments("main", "dev", "test", Priority.HIGH, "%three_letters_code%") | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/com/crowdin/cli/commands/picocli/BranchEditSubcommandTest.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,29 @@ | ||
package com.crowdin.cli.commands.picocli; | ||
|
||
import com.crowdin.client.core.model.Priority; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
public class BranchEditSubcommandTest extends PicocliTestUtils { | ||
|
||
@Test | ||
public void testBranchEdit() { | ||
String branchName = "main"; | ||
Priority priority = Priority.HIGH; | ||
this.execute(CommandNames.BRANCH, CommandNames.EDIT, branchName, "--priority", priority.name()); | ||
verify(actionsMock) | ||
.branchEdit(branchName, null, null, priority, null, false, false); | ||
this.check(true); | ||
} | ||
|
||
@Test | ||
public void testBranchEditInvalidOptions() { | ||
this.executeInvalidParams(CommandNames.BRANCH, CommandNames.EDIT); | ||
} | ||
|
||
@Test | ||
public void testBranchEditInvalidOptions2() { | ||
this.executeInvalidParams(CommandNames.BRANCH, CommandNames.EDIT, "--name", "test"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
:includedir: ../generated-picocli-docs | ||
:command: crowdin-branch-edit | ||
|
||
== crowdin branch edit | ||
|
||
include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-description] | ||
|
||
include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-synopsis] | ||
|
||
include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-arguments] | ||
|
||
include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-commands] | ||
|
||
include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-options] | ||
|
||
include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-footer] |
Oops, something went wrong.