Skip to content

Commit

Permalink
#261 - implement task management (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzerk authored Jan 23, 2023
1 parent f96eca7 commit 3c7e253
Show file tree
Hide file tree
Showing 24 changed files with 845 additions and 13 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,3 @@ task staticAnalysis(type: GradleBuild) {
tasks = ['checkstyleMain', 'checkstyleTest', 'spotbugsMain', 'spotbugsTest']
}


15 changes: 15 additions & 0 deletions src/main/java/com/crowdin/cli/client/ClientTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.crowdin.cli.client;

import com.crowdin.client.tasks.model.AddTaskRequest;
import com.crowdin.client.tasks.model.Status;
import com.crowdin.client.tasks.model.Task;

import java.util.List;

public interface ClientTask extends Client {

List<Task> listTask(Status status);

Task addTask(AddTaskRequest request);

}
5 changes: 5 additions & 0 deletions src/main/java/com/crowdin/cli/client/Clients.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static ClientTm getClientTm(String apiToken, String baseUrl) {
return new CrowdinClientTm(client);
}

public static ClientTask getClientTask(String apiToken, String baseUrl, String projectId) {
com.crowdin.client.Client client = prepareClient(apiToken, baseUrl);
return new CrowdinClientTask(client, projectId);
}

// mb divide args to move token and url to constructor?
public static ProjectClient getProjectClient(String apiToken, String baseUrl, long projectId) {
com.crowdin.client.Client client = prepareClient(apiToken, baseUrl);
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/crowdin/cli/client/CrowdinClientTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.crowdin.cli.client;

import com.crowdin.client.tasks.model.AddTaskRequest;
import com.crowdin.client.tasks.model.Status;
import com.crowdin.client.tasks.model.Task;

import java.util.List;

public class CrowdinClientTask extends CrowdinClientCore implements ClientTask {

private final com.crowdin.client.Client client;
private final String projectId;

public CrowdinClientTask(com.crowdin.client.Client client, String projectId) {
this.client = client;
this.projectId = projectId;
}

@Override
public List<Task> listTask(Status status) {
return executeRequestFullList((limit, offset) -> this.client.getTasksApi()
.listTasks(Long.valueOf(projectId), limit, offset, status));
}

@Override
public Task addTask(AddTaskRequest taskRequest) {
return executeRequest(() -> this.client.getTasksApi()
.addTask(Long.valueOf(projectId), taskRequest)
.getData());
}

}
9 changes: 5 additions & 4 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.crowdin.cli.commands;

import com.crowdin.cli.BaseCli;
import com.crowdin.cli.client.ClientGlossary;
import com.crowdin.cli.client.ClientTm;
import com.crowdin.cli.client.NoClient;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.client.*;
import com.crowdin.cli.commands.functionality.FilesInterface;
import com.crowdin.cli.properties.BaseProperties;
import com.crowdin.cli.properties.ProjectProperties;
Expand Down Expand Up @@ -83,6 +80,10 @@ NewAction<BaseProperties, ClientTm> tmDownload(
Long id, String name, TranslationMemoryFormat format, String sourceLanguageId,
String targetLanguageId, boolean noProgress, File to, FilesInterface files);

NewAction<ProjectProperties, ClientTask> taskList(boolean plainView, boolean isVerbose, String status, Long assigneeId);

NewAction<ProjectProperties, ClientTask> taskAdd(String title, Integer type, String language, List<Long> fileId, Long workflowStep, String description, boolean skipAssignedStrings, boolean skipUntranslatedStrings, List<Long> labels);

NewAction<PropertiesWithTargets, ProjectClient> downloadTargets(
List<String> targetNames, FilesInterface files, boolean noProgress,
List<String> langIds, boolean isVerbose, boolean plainView, boolean debug, String branchName);
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/crowdin/cli/commands/actions/CliActions.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.BaseCli;
import com.crowdin.cli.client.ClientGlossary;
import com.crowdin.cli.client.ClientTm;
import com.crowdin.cli.client.NoClient;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.client.*;
import com.crowdin.cli.commands.Actions;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.functionality.FilesInterface;
Expand Down Expand Up @@ -159,6 +156,16 @@ public NewAction<BaseProperties, ClientTm> tmDownload(
return new TmDownloadAction(id, name, format, sourceLanguageId, targetLanguageId, noProgress, to, files);
}

@Override
public NewAction<ProjectProperties, ClientTask> taskList(boolean plainView, boolean isVerbose, String status, Long assigneeId) {
return new TaskListAction(plainView, isVerbose, status, assigneeId);
}

@Override
public NewAction<ProjectProperties, ClientTask> taskAdd(String title, Integer type, String language, List<Long> fileId, Long workflowStep, String description, boolean skipAssignedStrings, boolean skipUntranslatedStrings, List<Long> labels) {
return new TaskAddAction(title, type, language, fileId, workflowStep, description, skipAssignedStrings, skipUntranslatedStrings, labels);
}

@Override
public NewAction<NoProperties, NoClient> checkNewVersion() {
return new CheckNewVersionAction();
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/com/crowdin/cli/commands/actions/TaskAddAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientTask;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;

import com.crowdin.cli.commands.functionality.PropertiesBeanUtils;
import com.crowdin.cli.properties.ProjectProperties;

import com.crowdin.client.tasks.model.AddTaskRequest;
import com.crowdin.client.tasks.model.CrowdinTaskCreateFormRequest;
import com.crowdin.client.tasks.model.EnterpriseTaskCreateFormRequest;
import com.crowdin.client.tasks.model.Task;

import lombok.AllArgsConstructor;

import java.util.List;
import java.util.Optional;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

@AllArgsConstructor
class TaskAddAction implements NewAction<ProjectProperties, ClientTask> {

private String title;

private Integer type;

private String language;

private List<Long> fileId;

private Long workflowStep;

private String description;

private boolean skipAssignedStrings;

private boolean skipUntranslatedStrings;

private List<Long> labels;

@Override
public void act(Outputter out, ProjectProperties pb, ClientTask client) {
boolean isOrganization = PropertiesBeanUtils.isOrganization(pb.getBaseUrl());
Task task;
AddTaskRequest addTaskRequest;
if (isOrganization) {
addTaskRequest = new EnterpriseTaskCreateFormRequest();
Optional.ofNullable(title).ifPresent(value -> ((EnterpriseTaskCreateFormRequest) addTaskRequest).setTitle(value));
Optional.ofNullable(language).ifPresent(value -> ((EnterpriseTaskCreateFormRequest) addTaskRequest).setLanguageId(value));
Optional.ofNullable(fileId).ifPresent(value -> ((EnterpriseTaskCreateFormRequest) addTaskRequest).setFileIds(value));
Optional.ofNullable(description).ifPresent(value -> ((EnterpriseTaskCreateFormRequest) addTaskRequest).setDescription(value));
Optional.ofNullable(skipAssignedStrings).ifPresent(value -> ((EnterpriseTaskCreateFormRequest) addTaskRequest).setSkipAssignedStrings(value));
Optional.ofNullable(labels).ifPresent(value -> ((EnterpriseTaskCreateFormRequest) addTaskRequest).setLabelIds(value));
Optional.ofNullable(workflowStep).ifPresent(value -> ((EnterpriseTaskCreateFormRequest) addTaskRequest).setWorkflowStepId(value));
} else {
addTaskRequest = new CrowdinTaskCreateFormRequest();
Optional.ofNullable(title).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setTitle(value));
Optional.ofNullable(type).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setType(value));
Optional.ofNullable(language).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setLanguageId(value));
Optional.ofNullable(fileId).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setFileIds(value));
Optional.ofNullable(description).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setDescription(value));
Optional.ofNullable(skipAssignedStrings).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setSkipAssignedStrings(value));
Optional.ofNullable(skipUntranslatedStrings).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setSkipUntranslatedStrings(value));
Optional.ofNullable(labels).ifPresent(value -> ((CrowdinTaskCreateFormRequest) addTaskRequest).setLabelIds(value));
}

try {
task = client.addTask(addTaskRequest);
} catch (Exception e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.task_is_not_added"), addTaskRequest), e);
}
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.task.added"), task.getTitle())));
}

}
58 changes: 58 additions & 0 deletions src/main/java/com/crowdin/cli/commands/actions/TaskListAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientTask;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.client.tasks.model.Status;
import com.crowdin.client.tasks.model.Task;

import java.util.List;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.*;

class TaskListAction implements NewAction<ProjectProperties, ClientTask> {

private final boolean plainView;
private final boolean isVerbose;

private final String status;

private final Long assigneeId;

public TaskListAction(boolean plainView, boolean isVerbose, String status, Long assigneeId) {
this.plainView = plainView;
this.isVerbose = isVerbose;
this.status = status;
this.assigneeId = assigneeId;
}

@Override
public void act(Outputter out, ProjectProperties pb, ClientTask client) {
Status sts = status == null ? null : Status.valueOf(status.toUpperCase());
List<Task> tasks = client.listTask(sts);
if (assigneeId != null) {
tasks = tasks.stream()
.filter(task -> task.getAssignees().stream()
.anyMatch(assignee -> assignee.getId().equals(assigneeId)))
.collect(Collectors.toList());
}
for (Task task : tasks) {
String okMessage = isVerbose ? "message.task.list.verbose" : "message.task.list";
String deadline = task.getDeadline() == null ? "NoDueDate" : task.getDeadline().toString();
if (!plainView) {
out.println(LIST_ITEM.withIcon(
String.format(RESOURCE_BUNDLE.getString(okMessage), task.getId(), task.getTargetLanguageId(), task.getTitle(), task.getStatus(), task.getWordsCount(), deadline)));
} else {
out.println(String.format(RESOURCE_BUNDLE.getString(okMessage), task.getId(), task.getTargetLanguageId(), task.getTitle(), task.getStatus(), task.getWordsCount(), deadline));
}
}
if (tasks.isEmpty()) {
if (!plainView) {
out.println(OK.withIcon(RESOURCE_BUNDLE.getString("message.task.list_empty")));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.crowdin.client.labels.model.AddLabelRequest;
import com.crowdin.client.sourcefiles.model.AddBranchRequest;
import com.crowdin.client.sourcestrings.model.AddSourceStringRequest;
import com.crowdin.client.tasks.model.CrowdinTaskCreateFormRequest;
import com.crowdin.client.tasks.model.EnterpriseTaskCreateFormRequest;
import com.crowdin.client.translationmemory.model.AddTranslationMemoryRequest;
import com.crowdin.client.translationmemory.model.TranslationMemoryExportRequest;
import com.crowdin.client.translationmemory.model.TranslationMemoryFormat;
Expand Down Expand Up @@ -41,6 +43,31 @@ public static AddSourceStringRequest addString(String text, String identifier, I
return request;
}

public static CrowdinTaskCreateFormRequest addCrowdinTask(String title, Integer type, String languageId, List<Long> fileId, String description, boolean skipAssignedStrings, boolean skipUntranslatedStrings, List<Long> labelIds) {
CrowdinTaskCreateFormRequest request = new CrowdinTaskCreateFormRequest();
request.setTitle(title);
request.setType(type);
request.setLanguageId(languageId);
request.setFileIds(fileId);
request.setDescription(description);
request.setSkipAssignedStrings(skipAssignedStrings);
request.setSkipUntranslatedStrings(skipUntranslatedStrings);
request.setLabelIds(labelIds);
return request;
}

public static EnterpriseTaskCreateFormRequest addEnterpriseTask(String title, String languageId, List<Long> fileId, String description, boolean skipAssignedStrings, List<Long> labelIds, Long workflowStepId) {
EnterpriseTaskCreateFormRequest request = new EnterpriseTaskCreateFormRequest();
request.setTitle(title);
request.setLanguageId(languageId);
request.setFileIds(fileId);
request.setDescription(description);
request.setSkipAssignedStrings(skipAssignedStrings);
request.setLabelIds(labelIds);
request.setWorkflowStepId(workflowStepId);
return request;
}

public static UploadTranslationsRequest uploadTranslations(Long fileId, boolean importEqSuggestions, boolean autoApproveImported, boolean translateHidden) {
UploadTranslationsRequest request = new UploadTranslationsRequest();
request.setFileId(fileId);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/crowdin/cli/commands/picocli/ActCommandTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientTask;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectParams;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.properties.PropertiesBuilders;
import picocli.CommandLine;

public abstract class ActCommandTask extends GenericActCommand<ProjectProperties, ClientTask> {

@CommandLine.Mixin
private ConfigurationFilesProperties properties;

@CommandLine.ArgGroup(exclusive = false, headingKey = "params.heading")
private ProjectParams params;

@Override
protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, Outputter out) {
return propertiesBuilders.buildProjectProperties(out, properties.getConfigFile(), properties.getIdentityFile(), params);
}

@Override
protected ClientTask getClient(ProjectProperties properties) {
return Clients.getClientTask(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public final class CommandNames {
public static final String UPLOAD_TRANSLATIONS = "translations";

public static final String PRE_TRANSLATE = "pre-translate";

public static final String TASK = "task";
public static final String TASK_LIST = "list";
public static final String TASK_ADD = "add";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public abstract class GenericActCommand<P extends Properties, C extends Client> extends GenericCommand {

private static Actions actions;
private static PropertiesBuilders propertiesBuilders;
protected static PropertiesBuilders propertiesBuilders;

public static void init(Actions actions, PropertiesBuilders propertiesBuilders) {
GenericActCommand.actions = actions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
StringSubcommand.class,
GlossarySubcommand.class,
TmSubcommand.class,
TaskSubcommand.class,
CheckVersionSubcommand.class,
PreTranslateSubcommand.class,
BranchSubcommand.class
Expand Down
Loading

0 comments on commit 3c7e253

Please sign in to comment.