Skip to content

Commit 6a9a71a

Browse files
authored
Do not fetch git username and email if the gitconfig configmap contains the data (#777) (#778)
Check the workspace-userdata-gitconfig-configmap configmap if it contains gitconfig with username and email and the data is not empty. Only then, try to fetch username and email from a git provider.
1 parent 9bf5a49 commit 6a9a71a

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/namespace/configurator/GitconfigConfigurator.java

+20-26
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,26 @@ public void configure(NamespaceResolutionContext namespaceResolutionContext, Str
7575
throws InfrastructureException {
7676
KubernetesClient client = cheServerKubernetesClientFactory.create();
7777
Optional<String> gitconfigOptional = getGitconfig(client, namespaceName);
78-
Optional<Pair<String, String>> usernameAndEmailFromGitconfigOptional = Optional.empty();
79-
Optional<Pair<String, String>> usernameAndEmailFromFetcherOptional =
80-
getUsernameAndEmailFromFetcher(namespaceName);
81-
if (gitconfigOptional.isPresent()) {
82-
String gitconfig = gitconfigOptional.get();
83-
usernameAndEmailFromGitconfigOptional = getUsernameAndEmailFromGitconfig(gitconfig);
84-
}
85-
if (needUpdateGitconfigConfigmap(
86-
usernameAndEmailFromGitconfigOptional, usernameAndEmailFromFetcherOptional)) {
87-
ConfigMap gitconfigConfigmap = buildGitconfigConfigmap();
88-
Optional<Pair<String, String>> usernameAndEmailOptional =
89-
usernameAndEmailFromGitconfigOptional.isPresent()
90-
? usernameAndEmailFromGitconfigOptional
91-
: usernameAndEmailFromFetcherOptional;
92-
Optional<String> gitconfigSectionsOptional =
93-
generateGitconfigSections(gitconfigOptional, usernameAndEmailOptional);
94-
gitconfigConfigmap.setData(
95-
ImmutableMap.of(CONFIGMAP_DATA_KEY, gitconfigSectionsOptional.orElse("")));
96-
client.configMaps().inNamespace(namespaceName).createOrReplace(gitconfigConfigmap);
78+
// Try to get username and email from the gitconfig configmap if present.
79+
Optional<Pair<String, String>> usernameAndEmailFromGitconfigOptional =
80+
gitconfigOptional.isPresent()
81+
? getUsernameAndEmailFromGitconfig(gitconfigOptional.get())
82+
: Optional.empty();
83+
// If username and email are not present in the gitconfig configmap, or the values are empty, or
84+
// the gitconfig configmap is empty, try to get them from the fetcher.
85+
if (usernameAndEmailFromGitconfigOptional.isEmpty()) {
86+
Optional<Pair<String, String>> usernameAndEmailFromFetcher =
87+
// Fetch username and email from a token secret if it is present.
88+
getUsernameAndEmailFromFetcher(namespaceName);
89+
// If username and email are present, create or update the gitconfig configmap.
90+
if (usernameAndEmailFromFetcher.isPresent()) {
91+
ConfigMap gitconfigConfigmap = buildGitconfigConfigmap();
92+
Optional<String> gitconfigSectionsOptional =
93+
generateGitconfigSections(gitconfigOptional, usernameAndEmailFromFetcher);
94+
gitconfigConfigmap.setData(
95+
ImmutableMap.of(CONFIGMAP_DATA_KEY, gitconfigSectionsOptional.orElse("")));
96+
client.configMaps().inNamespace(namespaceName).createOrReplace(gitconfigConfigmap);
97+
}
9798
}
9899
}
99100

@@ -107,13 +108,6 @@ private ConfigMap buildGitconfigConfigmap() {
107108
.build();
108109
}
109110

110-
private boolean needUpdateGitconfigConfigmap(
111-
Optional<Pair<String, String>> usernameAndEmailFromGitconfigOptional,
112-
Optional<Pair<String, String>> usernameAndEmailFromFetcher) {
113-
return usernameAndEmailFromGitconfigOptional.isEmpty()
114-
&& usernameAndEmailFromFetcher.isPresent();
115-
}
116-
117111
private Optional<String> generateGitconfigSections(
118112
Optional<String> gitconfigOptional, Optional<Pair<String, String>> usernameAndEmailOptional) {
119113
Optional<String> userSectionOptional =

infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/namespace/configurator/GitconfigConfiguratorTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
package org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator;
1313

1414
import static org.mockito.ArgumentMatchers.anyString;
15+
import static org.mockito.Mockito.never;
1516
import static org.mockito.Mockito.spy;
17+
import static org.mockito.Mockito.verify;
1618
import static org.mockito.Mockito.when;
1719

1820
import com.google.common.collect.ImmutableMap;
@@ -193,8 +195,6 @@ public void shouldNotUpdateGitconfigConfigmapWithUserSection()
193195
serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).create(gitconfigConfigmap);
194196
configurator =
195197
new GitconfigConfigurator(cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
196-
when(gitUserDataFetcher.fetchGitUserData(anyString()))
197-
.thenReturn(new GitUserData("fetcher-username", "fetcher-userEmail"));
198198
// when
199199
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME);
200200
// then
@@ -204,5 +204,7 @@ public void shouldNotUpdateGitconfigConfigmapWithUserSection()
204204
Assert.assertEquals(configMaps.size(), 1);
205205
String expected = "[user]\n\tname = gitconfig-username\n\temail = gitconfig-email";
206206
Assert.assertEquals(configMaps.get(0).getData().get("gitconfig"), expected);
207+
// Check that fetchGitUserData was not called.
208+
verify(gitUserDataFetcher, never()).fetchGitUserData(anyString());
207209
}
208210
}

0 commit comments

Comments
 (0)