Skip to content

Commit d7be878

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

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void configure(NamespaceResolutionContext namespaceResolutionContext, Str
101101
? usernameAndEmailFromGitconfigOptional
102102
: usernameAndEmailFromFetcherOptional;
103103
Optional<String> gitconfigSectionsOptional =
104-
generateGitconfigSections(usernameAndEmailOptional, tokenFromSecretOptional);
104+
generateGitconfigSections(gitconfigOptional, usernameAndEmailOptional, tokenFromSecretOptional);
105105
gitconfigSecret.setData(
106106
ImmutableMap.of(
107107
CONFIGMAP_DATA_KEY,
@@ -136,7 +136,7 @@ private boolean needUpdateGitconfigSecret(
136136
}
137137

138138
private Optional<String> generateGitconfigSections(
139-
Optional<Pair<String, String>> usernameAndEmailOptional, Optional<String> tokenOptional) {
139+
Optional<String> gitconfigOptional, Optional<Pair<String, String>> usernameAndEmailOptional, Optional<String> tokenOptional) {
140140
Optional<String> userSectionOptional = Optional.empty();
141141
Optional<String> httpSectionOPtional = Optional.empty();
142142
if (usernameAndEmailOptional.isPresent()) {
@@ -149,11 +149,27 @@ private Optional<String> generateGitconfigSections(
149149
httpSectionOPtional = Optional.of(generateHttpSection(tokenOptional.get()));
150150
}
151151
StringJoiner joiner = new StringJoiner("\n");
152+
if (gitconfigOptional.isPresent()) {
153+
Optional<String> otherSectionsOptional = getOtherStoredSections(gitconfigOptional.get());
154+
otherSectionsOptional.ifPresent(joiner::add);
155+
}
152156
userSectionOptional.ifPresent(joiner::add);
153157
httpSectionOPtional.ifPresent(joiner::add);
154158
return joiner.length() > 0 ? Optional.of(joiner.toString()) : Optional.empty();
155159
}
156160

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

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,46 @@ 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("[other]\n\tkey = value\n[http]\n\textraHeader = Authorization: Basic " + encode((":old-token")))));
230+
serverMock.getClient().secrets().inNamespace(TEST_NAMESPACE_NAME).create(gitconfigSecret);
231+
// when
232+
configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME);
233+
// then
234+
Assert.assertEquals(serverMock.getLastRequest().getMethod(), "PUT");
235+
var secrets =
236+
serverMock.getClient().secrets().inNamespace(TEST_NAMESPACE_NAME).list().getItems();
237+
Assert.assertEquals(secrets.size(), 2);
238+
String expected =
239+
"[other]\n\tkey = value\n[http]\n\textraHeader = Authorization: Basic "
240+
+ encode(":new-token");
241+
Assert.assertEquals(secrets.get(1).getData().get("gitconfig"), encode(expected));
242+
}
243+
204244
@Test
205245
public void shouldClearGitconfigSecretIfNoTokenSecretIsFound()
206246
throws InfrastructureException, InterruptedException {
@@ -329,6 +369,40 @@ public void shouldUpdateGitconfigSecretWithUserSection()
329369
Assert.assertEquals(secrets.get(0).getData().get("gitconfig"), encode(expected));
330370
}
331371

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

0 commit comments

Comments
 (0)