Skip to content

Commit

Permalink
fixup! Rework the gitconfig configmap configurator
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Feb 20, 2025
1 parent 5599a48 commit b4cf813
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironmentFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.RemoveNamespaceOnWorkspaceRemove;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.CredentialsSecretConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.GitconfigConfigmapConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.GitconfigConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.OAuthTokenSecretsConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.PreferencesConfigMapConfigurator;
Expand Down Expand Up @@ -109,7 +109,7 @@ protected void configure() {
namespaceConfigurators.addBinding().to(WorkspaceServiceAccountConfigurator.class);
namespaceConfigurators.addBinding().to(UserProfileConfigurator.class);
namespaceConfigurators.addBinding().to(UserPreferencesConfigurator.class);
namespaceConfigurators.addBinding().to(GitconfigConfigmapConfigurator.class);
namespaceConfigurators.addBinding().to(GitconfigConfigurator.class);

bind(AuthorizationChecker.class).to(KubernetesAuthorizationCheckerImpl.class);
bind(PermissionsCleaner.class).asEagerSingleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GitconfigConfigmapConfigurator implements NamespaceConfigurator {
public class GitconfigConfigurator implements NamespaceConfigurator {
private final CheServerKubernetesClientFactory cheServerKubernetesClientFactory;
private final Set<GitUserDataFetcher> gitUserDataFetchers;
private static final Logger LOG = LoggerFactory.getLogger(GitconfigConfigmapConfigurator.class);
private static final Logger LOG = LoggerFactory.getLogger(GitconfigConfigurator.class);
private static final String CONFIGMAP_DATA_KEY = "gitconfig";
// TODO: rename to a more generic name, since it is not only for user data
private static final String GITCONFIG_CONFIGMAP_NAME = "workspace-userdata-gitconfig-configmap";
Expand All @@ -63,7 +63,7 @@ public class GitconfigConfigmapConfigurator implements NamespaceConfigurator {
Pattern.compile("\\[(?<sectionName>[a-zA-Z0-9]+)](\\n\\s*\\S*\\s*=.*)*");

@Inject
public GitconfigConfigmapConfigurator(
public GitconfigConfigurator(
CheServerKubernetesClientFactory cheServerKubernetesClientFactory,
Set<GitUserDataFetcher> gitUserDataFetchers) {
this.cheServerKubernetesClientFactory = cheServerKubernetesClientFactory;
Expand Down Expand Up @@ -139,10 +139,10 @@ Optional<String> getOtherStoredSections(String gitconfig) {
private Optional<Pair<String, String>> getUsernameAndEmailFromGitconfig(String gitconfig) {
if (gitconfig.contains("[user]")) {
Matcher usernameMatcher = usernmaePattern.matcher(gitconfig);
Matcher emailaMatcher = emailPattern.matcher(gitconfig);
if (usernameMatcher.find() && emailaMatcher.find()) {
Matcher emailMatcher = emailPattern.matcher(gitconfig);
if (usernameMatcher.find() && emailMatcher.find()) {
String username = usernameMatcher.group("username");
String email = emailaMatcher.group("email");
String email = emailMatcher.group("email");
if (!emptyStringPattern.matcher(username).matches()
&& !emptyStringPattern.matcher(email).matches()) {
return Optional.of(new Pair<>(username, email));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.testng.annotations.Test;

@Listeners(MockitoTestNGListener.class)
public class GitconfigConfigmapConfiguratorTest {
public class GitconfigConfiguratorTest {

private NamespaceConfigurator configurator;

Expand Down Expand Up @@ -65,8 +65,7 @@ public class GitconfigConfigmapConfiguratorTest {
public void setUp()
throws InfrastructureException, ScmCommunicationException, ScmUnauthorizedException {
configurator =
new GitconfigConfigmapConfigurator(
cheServerKubernetesClientFactory, Collections.emptySet());
new GitconfigConfigurator(cheServerKubernetesClientFactory, Collections.emptySet());

serverMock = new KubernetesServer(true, true);
serverMock.before();
Expand All @@ -83,8 +82,7 @@ public void shouldCreateGitconfigConfigmapWithUserSection() throws Exception {
when(gitUserDataFetcher.fetchGitUserData())
.thenReturn(new GitUserData("username", "userEmail"));
configurator =
new GitconfigConfigmapConfigurator(
cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
new GitconfigConfigurator(cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
// when
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME);
// then
Expand Down Expand Up @@ -125,8 +123,7 @@ public void shouldUpdateGitconfigConfigmapWithUserSection()
Collections.singletonMap("gitconfig", "[user]\n\tname = \"\"\n\temail= \"\""));
serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).create(gitconfigConfigmap);
configurator =
new GitconfigConfigmapConfigurator(
cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
new GitconfigConfigurator(cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
when(gitUserDataFetcher.fetchGitUserData())
.thenReturn(new GitUserData("username", "userEmail"));
// when
Expand Down Expand Up @@ -160,8 +157,7 @@ public void shouldUpdateGitconfigConfigmapWithStoredSectionsWithUserSection()
"[other]\n\tkey = value\n[other1]\n\tkey = value\n[user]\n\tname = \"\"\n\temail= \"\""));
serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).create(gitconfigConfigmap);
configurator =
new GitconfigConfigmapConfigurator(
cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
new GitconfigConfigurator(cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
when(gitUserDataFetcher.fetchGitUserData())
.thenReturn(new GitUserData("username", "userEmail"));
// when
Expand Down Expand Up @@ -195,8 +191,7 @@ public void shouldNotUpdateGitconfigConfigmapWithUserSection()
"gitconfig", "[user]\n\tname = gitconfig-username\n\temail = gitconfig-email"));
serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).create(gitconfigConfigmap);
configurator =
new GitconfigConfigmapConfigurator(
cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
new GitconfigConfigurator(cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
when(gitUserDataFetcher.fetchGitUserData())
.thenReturn(new GitUserData("fetcher-username", "fetcher-userEmail"));
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironmentFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.CredentialsSecretConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.GitconfigConfigmapConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.GitconfigConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.OAuthTokenSecretsConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.PreferencesConfigMapConfigurator;
Expand Down Expand Up @@ -115,7 +115,7 @@ protected void configure() {
namespaceConfigurators.addBinding().to(OAuthTokenSecretsConfigurator.class);
namespaceConfigurators.addBinding().to(PreferencesConfigMapConfigurator.class);
namespaceConfigurators.addBinding().to(OpenShiftWorkspaceServiceAccountConfigurator.class);
namespaceConfigurators.addBinding().to(GitconfigConfigmapConfigurator.class);
namespaceConfigurators.addBinding().to(GitconfigConfigurator.class);

bind(AuthorizationChecker.class).to(OpenShiftAuthorizationCheckerImpl.class);
bind(PermissionsCleaner.class).asEagerSingleton();
Expand Down

0 comments on commit b4cf813

Please # to comment.