Skip to content

Commit

Permalink
Load all project info per branch (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored Jan 17, 2023
1 parent 9f8fe5e commit f96eca7
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 118 deletions.
21 changes: 15 additions & 6 deletions src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiPredicate;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;

class CrowdinProjectClient extends CrowdinClientCore implements ProjectClient {

private final com.crowdin.client.Client client;
Expand All @@ -42,11 +45,11 @@ public CrowdinProjectClient(com.crowdin.client.Client client, long projectId) {
}

@Override
public CrowdinProjectFull downloadFullProject() {
public CrowdinProjectFull downloadFullProject(String branchName) {
CrowdinProjectFull project = new CrowdinProjectFull();
this.populateProjectWithInfo(project);
this.populateProjectWithLangs(project);
this.populateProjectWithStructure(project);
this.populateProjectWithStructure(project, branchName);
return project;
}

Expand All @@ -65,12 +68,18 @@ public CrowdinProjectInfo downloadProjectInfo() {
return project;
}

private void populateProjectWithStructure(CrowdinProjectFull project) {
private void populateProjectWithStructure(CrowdinProjectFull project, String branchName) {
project.setBranches(this.listBranches());
Optional.ofNullable(branchName)
.map(name -> project.findBranchByName(name)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch")))
)
.ifPresent(project::setBranch);
Long branchId = Optional.ofNullable(project.getBranch()).map(Branch::getId).orElse(null);
project.setFiles(executeRequestFullList((limit, offset) -> this.client.getSourceFilesApi()
.listFiles(this.projectId, null, null, null, null, limit, offset)));
.listFiles(this.projectId, branchId, null, null, true, limit, offset)));
project.setDirectories(executeRequestFullList((limit, offset) -> this.client.getSourceFilesApi()
.listDirectories(this.projectId, null, null, null, null, limit, offset)));
project.setBranches(this.listBranches());
.listDirectories(this.projectId, branchId, null, null, true, limit, offset)));
}

private void populateProjectWithLangs(CrowdinProject project) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/crowdin/cli/client/CrowdinProjectFull.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class CrowdinProjectFull extends CrowdinProject {
private List<? extends FileInfo> files;
private List<Directory> directories;
private List<Branch> branches;
private Branch branch;

void setFiles(List<? extends FileInfo> files) {
this.files = files;
Expand All @@ -39,6 +40,14 @@ public Map<Long, Branch> getBranches() {
.collect(Collectors.toMap(Branch::getId, Function.identity()));
}

public Branch getBranch() {
return branch;
}

public void setBranch(Branch branch) {
this.branch = branch;
}

public void addBranchToLocalList(Branch branch) {
this.branches.add(branch);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

public interface ProjectClient extends Client {

CrowdinProjectFull downloadFullProject();
default CrowdinProjectFull downloadFullProject() {
return this.downloadFullProject(null);
}

CrowdinProjectFull downloadFullProject(String branchName);

CrowdinProject downloadProjectWithLanguages();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {

CrowdinProjectFull project = ConsoleSpinner
.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, client::downloadFullProject);
this.noProgress, this.plainView, () -> client.downloadFullProject(this.branchName));

if (!project.isManagerAccess()) {
if (!plainView) {
Expand All @@ -115,9 +115,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
.orElseThrow(() -> new RuntimeException(
String.format(RESOURCE_BUNDLE.getString("error.language_not_exist"), lang))))
.collect(Collectors.toList());
Optional<Branch> branch = Optional.ofNullable(this.branchName)
.map(br -> project.findBranchByName(br)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch"))));
Optional<Branch> branch = Optional.ofNullable(project.getBranch());

Map<String, com.crowdin.client.sourcefiles.model.File> serverSources = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getFiles());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli

CrowdinProjectFull project = ConsoleSpinner
.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, client::downloadFullProject);
this.noProgress, this.plainView, () -> client.downloadFullProject(this.branchName));

Long branchId = Optional.ofNullable(this.branchName)
.map(br -> project.findBranchByName(br)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch"))))
Long branchId = Optional.ofNullable(project.getBranch())
.map(Branch::getId)
.orElse(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.crowdin.client.translations.model.ExportProjectTranslationRequest;
import lombok.NonNull;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.io.IOException;
Expand Down Expand Up @@ -70,16 +69,13 @@ public void act(Outputter out, PropertiesWithTargets pb, ProjectClient client) {

CrowdinProjectFull project = ConsoleSpinner
.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, client::downloadFullProject);
this.noProgress, this.plainView, () -> client.downloadFullProject(branchName));

PlaceholderUtil placeholderUtil =
new PlaceholderUtil(
project.getSupportedLanguages(), project.getProjectLanguages(true), pb.getBasePath());

if (StringUtils.isNotEmpty(branchName) && !project.findBranchByName(branchName).isPresent()) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch"));
}
Optional<Long> branchId = project.findBranchByName(branchName).map(Branch::getId);
Optional<Long> branchId = Optional.ofNullable(project.getBranch()).map(Branch::getId);

Map<String, Long> projectFiles = (branchId.isPresent()
? ProjectFilesUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.sourcefiles.model.Branch;
import org.apache.commons.lang3.StringUtils;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import java.util.Optional;

class ListProjectAction implements NewAction<ProjectProperties, ProjectClient> {

Expand All @@ -30,13 +29,9 @@ public ListProjectAction(boolean noProgress, String branchName, boolean treeView
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);
this.noProgress, this.plainView, () -> client.downloadFullProject(this.branchName));

Long branchId = (StringUtils.isNotEmpty(this.branchName))
? project.findBranchByName(this.branchName)
.map(Branch::getId)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch")))
: null;
Long branchId = Optional.ofNullable(project.getBranch()).map(Branch::getId).orElse(null);

(new DryrunProjectFiles(project.getFileInfos(), project.getDirectories(), project.getBranches(), branchId)).run(out, treeView, plainView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ListSourcesAction(boolean deleteObsolete, String branchName, boolean noPr
@Override
public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
CrowdinProject project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, (deleteObsolete) ? client::downloadFullProject : client::downloadProjectWithLanguages);
this.noProgress, this.plainView, (deleteObsolete) ? () -> client.downloadFullProject(this.branchName) : client::downloadProjectWithLanguages);
PlaceholderUtil placeholderUtil = new PlaceholderUtil(project.getSupportedLanguages(), project.getProjectLanguages(false), pb.getBasePath());

if (!project.isManagerAccess() && deleteObsolete) {
Expand All @@ -50,9 +50,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {

if (deleteObsolete) {
CrowdinProjectFull projectFull = (CrowdinProjectFull) project;
Long branchId = Optional.ofNullable(branchName)
.map(br -> projectFull.findBranchByName(br)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch"))))
Long branchId = Optional.ofNullable(((CrowdinProjectFull) project).getBranch())
.map(Branch::getId)
.orElse(null);
(new DryrunObsoleteSources(pb, placeholderUtil, projectFull.getDirectories(branchId), projectFull.getFiles(branchId))).run(out, treeView, plainView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public PreTranslateAction(
@Override
public void act(Outputter out, PropertiesWithFiles properties, ProjectClient client) {
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, client::downloadFullProject);
this.noProgress, this.plainView, () -> client.downloadFullProject(this.branchName));

List<String> languages = this.prepareLanguageIds(project);
List<Long> fileIds = this.prepareFileIds(out, properties, project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ public StringListAction(boolean noProgress, boolean isVerbose, String file, Stri
@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, false, client::downloadFullProject);
this.noProgress, false, () -> client.downloadFullProject(this.branchName));

Long branchId = Optional.ofNullable(this.branchName)
.map(br -> project.findBranchByName(br)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch"))))
Long branchId = Optional.ofNullable(project.getBranch())
.map(Branch::getId)
.orElse(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public UploadTranslationsAction(
@Override
public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, client::downloadFullProject);
this.noProgress, this.plainView, () -> client.downloadFullProject(this.branchName));

if (!project.isManagerAccess()) {
if (!plainView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void testDownloadProjectFull() {
when(httpClientMock.get(eq(listBranchesUrl), any(), eq(BranchResponseList.class)))
.thenReturn(branchesResponse);

CrowdinProject crowdinProject = client.downloadFullProject();
CrowdinProject crowdinProject = client.downloadFullProject(null);
assertEquals(1, crowdinProject.getProjectLanguages(false).size());
assertEquals(2, crowdinProject.getSupportedLanguages().size());
assertTrue(crowdinProject.findLanguageById("ua", false).isPresent());
Expand Down
Loading

0 comments on commit f96eca7

Please sign in to comment.