Skip to content

Commit b823572

Browse files
committed
Do not fetch git username and email if the gitconfig configmap contains the data (#777)
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 d13bfb8 commit b823572

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();
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();
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
@@ -11,7 +11,9 @@
1111
*/
1212
package org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator;
1313

14+
import static org.mockito.Mockito.never;
1415
import static org.mockito.Mockito.spy;
16+
import static org.mockito.Mockito.verify;
1517
import static org.mockito.Mockito.when;
1618

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

0 commit comments

Comments
 (0)