Skip to content

Commit bfebd1f

Browse files
committed
fixup! Add Azure DevOps Server support
1 parent 3531467 commit bfebd1f

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public void configure(NamespaceResolutionContext namespaceResolutionContext, Str
101101
? usernameAndEmailFromGitconfigOptional
102102
: usernameAndEmailFromFetcherOptional;
103103
Optional<String> gitconfigSectionsOptional =
104-
generateGitconfigSections(usernameAndEmailOptional, tokenFromSecretOptional);
104+
generateGitconfigSections(
105+
gitconfigOptional, usernameAndEmailOptional, tokenFromSecretOptional);
105106
gitconfigSecret.setData(
106107
ImmutableMap.of(
107108
CONFIGMAP_DATA_KEY,
@@ -136,7 +137,9 @@ private boolean needUpdateGitconfigSecret(
136137
}
137138

138139
private Optional<String> generateGitconfigSections(
139-
Optional<Pair<String, String>> usernameAndEmailOptional, Optional<String> tokenOptional) {
140+
Optional<String> gitconfigOptional,
141+
Optional<Pair<String, String>> usernameAndEmailOptional,
142+
Optional<String> tokenOptional) {
140143
Optional<String> userSectionOptional = Optional.empty();
141144
Optional<String> httpSectionOPtional = Optional.empty();
142145
if (usernameAndEmailOptional.isPresent()) {
@@ -149,11 +152,27 @@ private Optional<String> generateGitconfigSections(
149152
httpSectionOPtional = Optional.of(generateHttpSection(tokenOptional.get()));
150153
}
151154
StringJoiner joiner = new StringJoiner("\n");
155+
if (gitconfigOptional.isPresent()) {
156+
Optional<String> otherSectionsOptional = getOtherStoredSections(gitconfigOptional.get());
157+
otherSectionsOptional.ifPresent(joiner::add);
158+
}
152159
userSectionOptional.ifPresent(joiner::add);
153160
httpSectionOPtional.ifPresent(joiner::add);
154161
return joiner.length() > 0 ? Optional.of(joiner.toString()) : Optional.empty();
155162
}
156163

164+
Optional<String> getOtherStoredSections(String gitconfig) {
165+
String[] sections = gitconfig.split("\\s*\\[");
166+
StringJoiner joiner = new StringJoiner("\n");
167+
for (int i = 1; i < sections.length; i++) {
168+
String section = sections[i];
169+
if (!section.startsWith("user") && !section.startsWith("http")) {
170+
joiner.add("[" + section);
171+
}
172+
}
173+
return joiner.length() > 0 ? Optional.of(joiner.toString()) : Optional.empty();
174+
}
175+
157176
private Optional<Pair<String, String>> getUsernameAndEmailFromGitconfig(String gitconfig) {
158177
if (gitconfig.contains("[user]")) {
159178
Matcher usernameMatcher = usernmaePattern.matcher(gitconfig);

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,48 @@ public void shouldUpdateGitconfigSecretWithHttpSection()
201201
Assert.assertEquals(secrets.get(1).getData().get("gitconfig"), encode(expected));
202202
}
203203

204+
@Test
205+
public void shouldUpdateGitconfigSecretWithStoredSectionsWithHttpSection()
206+
throws InfrastructureException, InterruptedException {
207+
// given
208+
Secret tokenSecret =
209+
new SecretBuilder()
210+
.withNewMetadata()
211+
.withName("personal-access-token-name")
212+
.withLabels(TOKEN_SECRET_LABELS)
213+
.withAnnotations(ImmutableMap.of("che.eclipse.org/scm-provider-name", "azure-devops"))
214+
.endMetadata()
215+
.build();
216+
tokenSecret.setData(Collections.singletonMap("token", encode("new-token")));
217+
serverMock.getClient().secrets().inNamespace(TEST_NAMESPACE_NAME).create(tokenSecret);
218+
Secret gitconfigSecret =
219+
new SecretBuilder()
220+
.withNewMetadata()
221+
.withName(GITCONFIG_SECRET_NAME)
222+
.withLabels(GITCONFIG_SECRET_LABELS)
223+
.withAnnotations(GITCONFIG_SECRET_ANNOTATIONS)
224+
.endMetadata()
225+
.build();
226+
gitconfigSecret.setData(
227+
Collections.singletonMap(
228+
"gitconfig",
229+
encode(
230+
"[other]\n\tkey = value\n[http]\n\textraHeader = Authorization: Basic "
231+
+ encode((":old-token")))));
232+
serverMock.getClient().secrets().inNamespace(TEST_NAMESPACE_NAME).create(gitconfigSecret);
233+
// when
234+
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME);
235+
// then
236+
Assert.assertEquals(serverMock.getLastRequest().getMethod(), "PUT");
237+
var secrets =
238+
serverMock.getClient().secrets().inNamespace(TEST_NAMESPACE_NAME).list().getItems();
239+
Assert.assertEquals(secrets.size(), 2);
240+
String expected =
241+
"[other]\n\tkey = value\n[http]\n\textraHeader = Authorization: Basic "
242+
+ encode(":new-token");
243+
Assert.assertEquals(secrets.get(1).getData().get("gitconfig"), encode(expected));
244+
}
245+
204246
@Test
205247
public void shouldClearGitconfigSecretIfNoTokenSecretIsFound()
206248
throws InfrastructureException, InterruptedException {
@@ -329,6 +371,40 @@ public void shouldUpdateGitconfigSecretWithUserSection()
329371
Assert.assertEquals(secrets.get(0).getData().get("gitconfig"), encode(expected));
330372
}
331373

374+
@Test
375+
public void shouldUpdateGitconfigSecretWithStoredSectionsWithUserSection()
376+
throws InfrastructureException, InterruptedException, ScmItemNotFoundException,
377+
ScmCommunicationException, ScmUnauthorizedException, ScmConfigurationPersistenceException,
378+
ScmBadRequestException {
379+
// given
380+
Secret gitconfigSecret =
381+
new SecretBuilder()
382+
.withNewMetadata()
383+
.withName(GITCONFIG_SECRET_NAME)
384+
.withLabels(GITCONFIG_SECRET_LABELS)
385+
.withAnnotations(GITCONFIG_SECRET_ANNOTATIONS)
386+
.endMetadata()
387+
.build();
388+
gitconfigSecret.setData(
389+
Collections.singletonMap(
390+
"gitconfig", encode("[other]\n\tkey = value\n[user]\n\tname = \"\"\n\temail= \"\"")));
391+
serverMock.getClient().secrets().inNamespace(TEST_NAMESPACE_NAME).create(gitconfigSecret);
392+
configurator =
393+
new GitconfigAutomauntSecretConfigurator(
394+
cheServerKubernetesClientFactory, Set.of(gitUserDataFetcher));
395+
when(gitUserDataFetcher.fetchGitUserData())
396+
.thenReturn(new GitUserData("username", "userEmail"));
397+
// when
398+
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME);
399+
// then
400+
Assert.assertEquals(serverMock.getLastRequest().getMethod(), "PUT");
401+
var secrets =
402+
serverMock.getClient().secrets().inNamespace(TEST_NAMESPACE_NAME).list().getItems();
403+
Assert.assertEquals(secrets.size(), 1);
404+
String expected = "[other]\n\tkey = value\n[user]\n\tname = username\n\temail = userEmail";
405+
Assert.assertEquals(secrets.get(0).getData().get("gitconfig"), encode(expected));
406+
}
407+
332408
@Test
333409
public void shouldNotUpdateGitconfigSecretWithUserSection()
334410
throws InfrastructureException, InterruptedException, ScmItemNotFoundException,

0 commit comments

Comments
 (0)