Skip to content

Commit

Permalink
Merge pull request #61 from crowdin/improve-branches
Browse files Browse the repository at this point in the history
Add exception handling to getting git branch name
  • Loading branch information
andrii-bodnar committed Sep 3, 2021
2 parents f6d66c8 + a24ece4 commit 6b04e08
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 30 deletions.
4 changes: 1 addition & 3 deletions src/main/java/com/crowdin/action/DownloadAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ public void performInBackground(AnActionEvent anActionEvent, ProgressIndicator i
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);

CrowdinFileUtil.checkBranchName(branchName);
String branchName = ActionUtils.getBranchName(project, properties, true);
indicator.checkCanceled();

CrowdinProjectCacheProvider.CrowdinProjectCache crowdinProjectCache =
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/crowdin/action/UploadAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public void performInBackground(@NotNull final AnActionEvent anActionEvent, Prog
.map(key -> String.format(MESSAGES_BUNDLE.getString("messages.debug.upload_sources.list_of_patterns_item"), key, properties.getSourcesWithPatterns().get(key)))
.collect(Collectors.joining()));

String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);

CrowdinFileUtil.checkBranchName(branchName);
String branchName = ActionUtils.getBranchName(project, properties, true);

CrowdinProjectCacheProvider.CrowdinProjectCache crowdinProjectCache =
CrowdinProjectCacheProvider.getInstance(crowdin, branchName, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public void performInBackground(AnActionEvent anActionEvent, ProgressIndicator i
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);

CrowdinFileUtil.checkBranchName(branchName);
String branchName = ActionUtils.getBranchName(project, properties, true);
indicator.checkCanceled();

CrowdinProjectCacheProvider.CrowdinProjectCache crowdinProjectCache =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public void performInBackground(@NotNull AnActionEvent e, ProgressIndicator indi
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
indicator.checkCanceled();

String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);

CrowdinFileUtil.checkBranchName(branchName);
String branchName = ActionUtils.getBranchName(project, properties, true);

CrowdinProjectCacheProvider.CrowdinProjectCache crowdinProjectCache =
CrowdinProjectCacheProvider.getInstance(crowdin, branchName, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ public void performInBackground(AnActionEvent anActionEvent, ProgressIndicator i
VirtualFile root = FileUtil.getProjectBaseDir(project);
CrowdinProperties properties = CrowdinPropertiesLoader.load(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);

if (!CrowdinFileUtil.isValidBranchName(branchName)) {
throw new RuntimeException(MESSAGES_BUNDLE.getString("errors.branch_contains_forbidden_symbols"));
}
String branchName = ActionUtils.getBranchName(project, properties, true);
indicator.checkCanceled();

CrowdinProjectCacheProvider.CrowdinProjectCache crowdinProjectCache =
Expand Down Expand Up @@ -136,7 +132,7 @@ public void update(AnActionEvent e) {
VirtualFile root = FileUtil.getProjectBaseDir(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);
String branchName = ActionUtils.getBranchName(project, properties, false);

CrowdinProjectCacheProvider.CrowdinProjectCache crowdinProjectCache =
CrowdinProjectCacheProvider.getInstance(crowdin, branchName, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.crowdin.client.CrowdinProperties;
import com.crowdin.client.CrowdinPropertiesLoader;
import com.crowdin.event.FileChangeListener;
import com.crowdin.util.ActionUtils;
import com.crowdin.util.GitUtil;
import com.crowdin.util.NotificationUtil;
import com.crowdin.util.PropertyUtil;
Expand All @@ -29,7 +30,7 @@ public void runActivity(@NotNull Project project) {
properties = CrowdinPropertiesLoader.load(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);
String branchName = ActionUtils.getBranchName(project, properties, false);

ProgressManager.getInstance().run(new Task.Backgroundable(project, "Crowdin") {
@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/event/FileChangeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void run(@NotNull ProgressIndicator indicator) {
return;
}
indicator.checkCanceled();
String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);
String branchName = ActionUtils.getBranchName(project, properties, false);
if (!CrowdinFileUtil.isValidBranchName(branchName)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.crowdin.client.translationstatus.model.LanguageProgress;
import com.crowdin.ui.TranslationProgressWindow;
import com.crowdin.ui.TranslationProgressWindowFactory;
import com.crowdin.util.ActionUtils;
import com.crowdin.util.CrowdinFileUtil;
import com.crowdin.util.FileUtil;
import com.crowdin.util.GitUtil;
Expand Down Expand Up @@ -75,9 +76,7 @@ protected void performInBackground(@NonNull AnActionEvent e, @NonNull ProgressIn
NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);

CrowdinFileUtil.checkBranchName(branchName);
String branchName = ActionUtils.getBranchName(project, properties, true);

CrowdinProjectCacheProvider.CrowdinProjectCache crowdinProjectCache =
CrowdinProjectCacheProvider.getInstance(crowdin, branchName, true);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/crowdin/util/ActionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.crowdin.util;

import com.crowdin.client.CrowdinProperties;
import com.intellij.openapi.project.Project;

import static com.crowdin.Constants.MESSAGES_BUNDLE;

public class ActionUtils {

public static String getBranchName(Project project, CrowdinProperties properties, boolean performCheck) {
String branchName = properties.isDisabledBranches() ? "" : GitUtil.getCurrentBranch(project);
if (performCheck) {
if (!CrowdinFileUtil.isValidBranchName(branchName)) {
throw new RuntimeException(MESSAGES_BUNDLE.getString("errors.branch_contains_forbidden_symbols"));
}
}
return branchName;
}
}
6 changes: 0 additions & 6 deletions src/main/java/com/crowdin/util/CrowdinFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,4 @@ private static String getExportPattern(ExportOptions exportOptions) {
public static boolean isValidBranchName(String branchName) {
return StringUtils.containsNone(branchName, "/\\:*?\"<>");
}

public static void checkBranchName(String branchName) {
if (!isValidBranchName(branchName)) {
throw new RuntimeException(MESSAGES_BUNDLE.getString("errors.branch_contains_forbidden_symbols"));
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/com/crowdin/util/GitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import git4idea.repo.GitRepository;
import org.jetbrains.annotations.NotNull;

import static com.crowdin.Constants.MESSAGES_BUNDLE;

public final class GitUtil {

private GitUtil() {
Expand All @@ -18,10 +20,16 @@ public static String getCurrentBranch(@NotNull final Project project) {
String branchName = "";
try {
repository = GitBranchUtil.getCurrentRepository(project);
if (repository == null) {
throw new RuntimeException(MESSAGES_BUNDLE.getString("errors.not_found_git_branch"));
}
localBranch = repository.getCurrentBranch();
if (localBranch == null) {
throw new RuntimeException(MESSAGES_BUNDLE.getString("errors.not_found_git_branch"));
}
branchName = localBranch.getName();
} catch (Exception e) {
e.getMessage();
throw new RuntimeException(String.format(MESSAGES_BUNDLE.getString("errors.get_git_branch_name"), e.getMessage()), e);
}
return branchName;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ errors.missing_translation=Translation file '%s' is missing in the project
errors.uploaded_zero_translations=Couldn't find any translation file to upload
errors.branch_not_exists=Branch '%s' does not exists in Crowdin. Try switching to another branch locally
errors.branch_contains_forbidden_symbols=Branch name can't contain any of the following characters: \\ / : * ? " < > |
errors.not_found_git_branch=Not found git branch. You can disable branch usage by 'disable-branches=true'
errors.get_git_branch_name=Error getting git branch name: %s

# MESSAGES
messages.source_updated=File '%s' updated in Crowdin
Expand Down

0 comments on commit 6b04e08

Please sign in to comment.