Skip to content

Commit

Permalink
feat: branch edit (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored Sep 23, 2024
1 parent fd5dea8 commit cb0e8eb
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ public void deleteBranch(Long branchId) {
});
}

@Override
public Branch editBranch(Long branchId, List<PatchRequest> requests) {
return executeRequest(() -> this.client.getSourceFilesApi()
.editBranch(this.projectId, branchId, requests)
.getData());
}

@Override
public List<Branch> listBranches() {
return executeRequestFullList((limit, offset) -> this.client.getSourceFilesApi()
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ default CrowdinProjectFull downloadFullProject() {

void deleteBranch(Long branchId);

Branch editBranch(Long branchId, List<PatchRequest> requests);

List<Branch> listBranches();

Long uploadStorage(String fileName, InputStream content) throws ResponseException;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ NewAction<PropertiesWithFiles, ProjectClient> preTranslate(

NewAction<ProjectProperties, ProjectClient> branchDelete(String name);

NewAction<ProjectProperties, ProjectClient> branchEdit(String branch, String name, String title, Priority priority, boolean noProgress, boolean plainView);

NewAction<ProjectProperties, ClientScreenshot> screenshotList(Long stringId, boolean plainView);

NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file, String branchName, List<String> labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 boolean noProgress;
private final boolean plainView;

BranchEditAction(String branch, String name, String title, Priority priority, boolean noProgress, boolean plainView) {
this.branch = branch;
this.name = name;
this.title = title;
this.priority = priority;
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);
}

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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ public NewAction<ProjectProperties, ProjectClient> branchDelete(String name) {
return new BranchDeleteAction(name);
}

@Override
public NewAction<ProjectProperties, ProjectClient> branchEdit(String branch, String name, String title, Priority priority, boolean noProgress, boolean plainView) {
return new BranchEditAction(branch, name, title, priority, noProgress, plainView);
}

@Override
public NewAction<ProjectProperties, ClientScreenshot> screenshotList(Long stringId, boolean plainView) {
return new ScreenshotListAction(stringId, plainView);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 = {"--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) {
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, noProgress, plainView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
BranchCloneSubcommand.class,
BranchDeleteSubcommand.class,
BranchListSubcommand.class,
BranchMergeSubcommand.class
BranchMergeSubcommand.class,
BranchEditSubcommand.class
}
)
class BranchSubcommand extends HelpCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public final class CommandNames {

public static final String LIST = "list";
public static final String ADD = "add";
public static final String EDIT = "edit";
public static final String DELETE = "delete";

public static final String STATUS = "status";
public static final String STATUS_TRANSLATION = "translation";
public static final String STATUS_PROOFREADING = "proofreading";

public static final String STRING = "string";
public static final String STRING_EDIT = "edit";

public static final String BRANCH = "branch";
public static final String BRANCH_CLONE = "clone";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

@CommandLine.Command(
name = CommandNames.STRING_EDIT,
name = CommandNames.EDIT,
sortOptions = false
)
class StringEditSubcommand extends ActCommandProject {
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ crowdin.branch.merge.target=Target branch name
crowdin.branch.merge.dryrun=Simulate merging without making any real changes
crowdin.branch.merge.delete-after-merge=Whether to delete branch after merge

# CROWDIN BRANCH EDIT COMMAND
crowdin.branch.edit.usage.description=Edit existing branch
crowdin.branch.edit.usage.customSynopsis=@|fg(green) crowdin branch edit|@ <name> [CONFIG OPTIONS] [OPTIONS]
crowdin.branch.edit.name=Branch name
crowdin.branch.edit.new-name=Rename branch
crowdin.branch.edit.title=Specify new title for the branch
crowdin.branch.edit.priority=Specify new priority for the branch

crowdin.glossary.upload.usage.description=Upload glossary to localization resources
crowdin.glossary.upload.usage.customSynopsis=@|fg(green) crowdin glossary upload|@ <file> [CONFIG OPTIONS] [OPTIONS]
crowdin.glossary.upload.file=File to upload
Expand Down Expand Up @@ -500,6 +508,7 @@ error.file_required=The '--file' parameter is required for this type of project
error.dir_not_exists=Project doesn't contain the '%s' directory
error.branch_not_exists=Project doesn't contain the '%s' branch
error.source_string_no_edit=Specify some parameters to edit the string
error.branch_no_edit=Specify some parameters to edit the branch
error.unexpected_response=Unexpected response from %s: %s
error.error_response=Error from %s: %s
error.in_local_server=Error in raised local server
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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) {
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, 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"));
}
}};
verify(client).editBranch(branchId, patches);
}

public static Stream<Arguments> testBranchEdit() {
return Stream.of(
arguments("main", "dev", null, null),
arguments("main", null, "test", null),
arguments("main", null, null, Priority.HIGH),
arguments("main", "dev", "test", Priority.HIGH)
);
}
}
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, 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ void mockActions() {
.thenReturn(actionMock);
when(actionsMock.branchMerge(any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
when(actionsMock.branchEdit(any(), any(), any(), any(), anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
}

private void mockBuilders() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ public class StringEditSubcommandTest extends PicocliTestUtils {

@Test
public void testStringEdit() {
this.execute(CommandNames.STRING, CommandNames.STRING_EDIT, "42", "--text", "NeW tExT");
this.execute(CommandNames.STRING, CommandNames.EDIT, "42", "--text", "NeW tExT");
verify(actionsMock)
.stringEdit(anyBoolean(), anyBoolean(), any(), any(), any(), any(), any(), any(), any(), anyBoolean());
this.check(true);
}

@Test
public void testStringEditInvalidOptions() {
this.executeInvalidParams(CommandNames.STRING, CommandNames.STRING_EDIT);
this.executeInvalidParams(CommandNames.STRING, CommandNames.EDIT);
}

@Test
public void testStringEditInvalidOptions2() {
this.executeInvalidParams(CommandNames.STRING, CommandNames.STRING_EDIT, "--id", "42", "--identifier", "4242");
this.executeInvalidParams(CommandNames.STRING, CommandNames.EDIT, "--id", "42", "--identifier", "4242");
}
}
16 changes: 16 additions & 0 deletions website/mantemplates/crowdin-branch-edit.adoc
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]
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const sidebars = {
'commands/crowdin-branch-clone',
'commands/crowdin-branch-merge',
'commands/crowdin-branch-delete',
'commands/crowdin-branch-edit',
]
},
{
Expand Down

0 comments on commit cb0e8eb

Please sign in to comment.