Skip to content

Commit cb0e8eb

Browse files
authored
feat: branch edit (#844)
1 parent fd5dea8 commit cb0e8eb

File tree

16 files changed

+273
-6
lines changed

16 files changed

+273
-6
lines changed

src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ public void deleteBranch(Long branchId) {
203203
});
204204
}
205205

206+
@Override
207+
public Branch editBranch(Long branchId, List<PatchRequest> requests) {
208+
return executeRequest(() -> this.client.getSourceFilesApi()
209+
.editBranch(this.projectId, branchId, requests)
210+
.getData());
211+
}
212+
206213
@Override
207214
public List<Branch> listBranches() {
208215
return executeRequestFullList((limit, offset) -> this.client.getSourceFilesApi()

src/main/java/com/crowdin/cli/client/ProjectClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ default CrowdinProjectFull downloadFullProject() {
4747

4848
void deleteBranch(Long branchId);
4949

50+
Branch editBranch(Long branchId, List<PatchRequest> requests);
51+
5052
List<Branch> listBranches();
5153

5254
Long uploadStorage(String fileName, InputStream content) throws ResponseException;

src/main/java/com/crowdin/cli/commands/Actions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ NewAction<PropertiesWithFiles, ProjectClient> preTranslate(
120120

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

123+
NewAction<ProjectProperties, ProjectClient> branchEdit(String branch, String name, String title, Priority priority, boolean noProgress, boolean plainView);
124+
123125
NewAction<ProjectProperties, ClientScreenshot> screenshotList(Long stringId, boolean plainView);
124126

125127
NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file, String branchName, List<String> labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.crowdin.cli.commands.actions;
2+
3+
import com.crowdin.cli.client.CrowdinProjectFull;
4+
import com.crowdin.cli.client.ProjectClient;
5+
import com.crowdin.cli.commands.NewAction;
6+
import com.crowdin.cli.commands.Outputter;
7+
import com.crowdin.cli.commands.functionality.RequestBuilder;
8+
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
9+
import com.crowdin.cli.properties.ProjectProperties;
10+
import com.crowdin.cli.utils.console.ConsoleSpinner;
11+
import com.crowdin.cli.utils.console.ExecutionStatus;
12+
import com.crowdin.client.core.model.PatchOperation;
13+
import com.crowdin.client.core.model.PatchRequest;
14+
import com.crowdin.client.core.model.Priority;
15+
import com.crowdin.client.sourcefiles.model.Branch;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.Optional;
20+
21+
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
22+
23+
class BranchEditAction implements NewAction<ProjectProperties, ProjectClient> {
24+
25+
private final String branch;
26+
private final String name;
27+
private final String title;
28+
private final Priority priority;
29+
private final boolean noProgress;
30+
private final boolean plainView;
31+
32+
BranchEditAction(String branch, String name, String title, Priority priority, boolean noProgress, boolean plainView) {
33+
this.branch = branch;
34+
this.name = name;
35+
this.title = title;
36+
this.priority = priority;
37+
this.noProgress = noProgress;
38+
this.plainView = plainView;
39+
}
40+
41+
@Override
42+
public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
43+
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
44+
this.noProgress, this.plainView, client::downloadFullProject);
45+
46+
Optional<Branch> branchObj = project.findBranchByName(this.branch);
47+
if (branchObj.isEmpty()) {
48+
throw new ExitCodeExceptionMapper.NotFoundException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), this.branch));
49+
}
50+
51+
List<PatchRequest> requests = new ArrayList<>();
52+
if (name != null) {
53+
PatchRequest request = RequestBuilder.patch(name, PatchOperation.REPLACE, "/name");
54+
requests.add(request);
55+
}
56+
if (title != null) {
57+
PatchRequest request = RequestBuilder.patch(title, PatchOperation.REPLACE, "/title");
58+
requests.add(request);
59+
}
60+
if (priority != null) {
61+
PatchRequest request = RequestBuilder.patch(priority, PatchOperation.REPLACE, "/priority");
62+
requests.add(request);
63+
}
64+
65+
Branch updatedBranch = client.editBranch(branchObj.get().getId(), requests);
66+
67+
if (!plainView) {
68+
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.branch.list"), updatedBranch.getId(), updatedBranch.getName())));
69+
} else {
70+
out.println(updatedBranch.getName());
71+
}
72+
}
73+
}

src/main/java/com/crowdin/cli/commands/actions/CliActions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ public NewAction<ProjectProperties, ProjectClient> branchDelete(String name) {
249249
return new BranchDeleteAction(name);
250250
}
251251

252+
@Override
253+
public NewAction<ProjectProperties, ProjectClient> branchEdit(String branch, String name, String title, Priority priority, boolean noProgress, boolean plainView) {
254+
return new BranchEditAction(branch, name, title, priority, noProgress, plainView);
255+
}
256+
252257
@Override
253258
public NewAction<ProjectProperties, ClientScreenshot> screenshotList(Long stringId, boolean plainView) {
254259
return new ScreenshotListAction(stringId, plainView);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.crowdin.cli.commands.picocli;
2+
3+
import com.crowdin.cli.client.ProjectClient;
4+
import com.crowdin.cli.commands.Actions;
5+
import com.crowdin.cli.commands.NewAction;
6+
import com.crowdin.cli.properties.ProjectProperties;
7+
import com.crowdin.client.core.model.Priority;
8+
import picocli.CommandLine;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
@CommandLine.Command(
14+
sortOptions = false,
15+
name = CommandNames.EDIT
16+
)
17+
class BranchEditSubcommand extends ActCommandProject {
18+
19+
@CommandLine.Parameters(descriptionKey = "crowdin.branch.edit.name")
20+
protected String name;
21+
22+
@CommandLine.Option(names = "--name", descriptionKey = "crowdin.branch.edit.new-name", paramLabel = "...", order = -2)
23+
protected String newName;
24+
25+
@CommandLine.Option(names = "--title", descriptionKey = "crowdin.branch.edit.title", paramLabel = "...", order = -2)
26+
protected String title;
27+
28+
@CommandLine.Option(names = "--priority", descriptionKey = "crowdin.branch.edit.priority", paramLabel = "...", order = -2)
29+
protected Priority priority;
30+
31+
@CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain")
32+
protected boolean plainView;
33+
34+
@Override
35+
protected List<String> checkOptions() {
36+
List<String> errors = new ArrayList<>();
37+
if (newName == null && title == null && priority == null) {
38+
errors.add(RESOURCE_BUNDLE.getString("error.branch_no_edit"));
39+
}
40+
return errors;
41+
}
42+
43+
@Override
44+
protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions) {
45+
return actions.branchEdit(name, newName, title, priority, noProgress, plainView);
46+
}
47+
}

src/main/java/com/crowdin/cli/commands/picocli/BranchSubcommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
BranchCloneSubcommand.class,
1010
BranchDeleteSubcommand.class,
1111
BranchListSubcommand.class,
12-
BranchMergeSubcommand.class
12+
BranchMergeSubcommand.class,
13+
BranchEditSubcommand.class
1314
}
1415
)
1516
class BranchSubcommand extends HelpCommand {

src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public final class CommandNames {
1111

1212
public static final String LIST = "list";
1313
public static final String ADD = "add";
14+
public static final String EDIT = "edit";
1415
public static final String DELETE = "delete";
1516

1617
public static final String STATUS = "status";
1718
public static final String STATUS_TRANSLATION = "translation";
1819
public static final String STATUS_PROOFREADING = "proofreading";
1920

2021
public static final String STRING = "string";
21-
public static final String STRING_EDIT = "edit";
2222

2323
public static final String BRANCH = "branch";
2424
public static final String BRANCH_CLONE = "clone";

src/main/java/com/crowdin/cli/commands/picocli/StringEditSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.List;
1111

1212
@CommandLine.Command(
13-
name = CommandNames.STRING_EDIT,
13+
name = CommandNames.EDIT,
1414
sortOptions = false
1515
)
1616
class StringEditSubcommand extends ActCommandProject {

src/main/resources/messages/messages.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ crowdin.branch.merge.target=Target branch name
248248
crowdin.branch.merge.dryrun=Simulate merging without making any real changes
249249
crowdin.branch.merge.delete-after-merge=Whether to delete branch after merge
250250

251+
# CROWDIN BRANCH EDIT COMMAND
252+
crowdin.branch.edit.usage.description=Edit existing branch
253+
crowdin.branch.edit.usage.customSynopsis=@|fg(green) crowdin branch edit|@ <name> [CONFIG OPTIONS] [OPTIONS]
254+
crowdin.branch.edit.name=Branch name
255+
crowdin.branch.edit.new-name=Rename branch
256+
crowdin.branch.edit.title=Specify new title for the branch
257+
crowdin.branch.edit.priority=Specify new priority for the branch
258+
251259
crowdin.glossary.upload.usage.description=Upload glossary to localization resources
252260
crowdin.glossary.upload.usage.customSynopsis=@|fg(green) crowdin glossary upload|@ <file> [CONFIG OPTIONS] [OPTIONS]
253261
crowdin.glossary.upload.file=File to upload
@@ -500,6 +508,7 @@ error.file_required=The '--file' parameter is required for this type of project
500508
error.dir_not_exists=Project doesn't contain the '%s' directory
501509
error.branch_not_exists=Project doesn't contain the '%s' branch
502510
error.source_string_no_edit=Specify some parameters to edit the string
511+
error.branch_no_edit=Specify some parameters to edit the branch
503512
error.unexpected_response=Unexpected response from %s: %s
504513
error.error_response=Error from %s: %s
505514
error.in_local_server=Error in raised local server

0 commit comments

Comments
 (0)