Skip to content

Commit

Permalink
feat: Introduce /config/repository-names to fetch configured repo names
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanmaity112 committed Jan 23, 2025
1 parent b6e4b0b commit 4e48ecc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ public ResponseEntity<Object> login(
}

private String createAccessToken(String username, String password) {
final byte[] encodedAuth =
Base64.encodeBase64("%s:%s".formatted(username, password).getBytes(UTF_8), false);
return "Basic %s".formatted(new String(encodedAuth, UTF_8));
return "Basic %s"
.formatted(
new String(
Base64.encodeBase64("%s:%s".formatted(username, password).getBytes(UTF_8), false),
UTF_8));
}

private Cookie createCookie(String cookieName, String value, int maxAge) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,35 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@SuppressFBWarnings("EI_EXPOSE_REP2")
@RequestMapping("/config")
@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
public class ConfigurationController {
private final List<AuthenticationMechanism> authenticationMechanisms;
private final Boolean secretsScanEnabled;
private final Boolean codeScanEnabled;
private final String projectVersion;
private final List<String> repoNames;

@Autowired
public ConfigurationController(
List<AuthenticationMechanism> authenticationMechanisms,
@Qualifier("enableSecretsScanMonitoring") Boolean secretsScanEnabled,
@Qualifier("enableCodeScanMonitoring") Boolean codeScanEnabled,
@Qualifier("projectVersion") String projectVersion) {
@Qualifier("projectVersion") String projectVersion,
@Qualifier("repoNames") List<String> repoNames) {
this.authenticationMechanisms = authenticationMechanisms;
this.secretsScanEnabled = secretsScanEnabled;
this.codeScanEnabled = codeScanEnabled;
this.projectVersion = projectVersion;
this.repoNames = repoNames;
}

@GetMapping(path = "/config")
@GetMapping
public Config getAvailableConfig() {
return Config.builder()
.availableAuths(List.copyOf(authenticationMechanisms))
Expand All @@ -42,6 +47,11 @@ public Config getAvailableConfig() {
.build();
}

@GetMapping("/repository-names")
public List<String> getRepositoryNames() {
return repoNames;
}

@Value
@Builder
public static class Config {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package de.otto.platform.gitactionboard.adapters.controller;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import de.otto.platform.gitactionboard.IntegrationTest;
import de.otto.platform.gitactionboard.Parallel;
import java.util.Base64;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -47,4 +50,26 @@ void shouldGiveAvailableConfig() {
.andExpect(jsonPath("$.githubSecretsScanMonitoringEnabled").value("false"))
.andExpect(jsonPath("$.version").exists());
}

@Test
@SneakyThrows
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
void shouldFetchListOfRepositories() {
mockMvc
.perform(
MockMvcRequestBuilders.get("/config/repository-names")
.header(
AUTHORIZATION,
"Basic %s"
.formatted(
new String(
Base64.getEncoder()
.encode("%s:%s".formatted("admin", "password").getBytes(UTF_8)),
UTF_8))))
.andExpect(status().isOk())
.andExpect(header().stringValues(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0]").value("hello-world"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import static de.otto.platform.gitactionboard.domain.AuthenticationMechanism.BASIC_AUTH;
import static de.otto.platform.gitactionboard.domain.AuthenticationMechanism.OAUTH2;
import static de.otto.platform.gitactionboard.fixtures.WorkflowsFixture.REPO_NAME;
import static org.assertj.core.api.Assertions.assertThat;

import de.otto.platform.gitactionboard.Parallel;
import de.otto.platform.gitactionboard.domain.AuthenticationMechanism;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -64,7 +66,11 @@ void shouldGiveValidConfig(

final ConfigurationController controller =
new ConfigurationController(
authenticationMechanisms, secretsScanEnabled, codeScanEnabled, projectVersion);
authenticationMechanisms,
secretsScanEnabled,
codeScanEnabled,
projectVersion,
List.of());
assertThat(controller.getAvailableConfig())
.satisfies(
config -> {
Expand All @@ -76,4 +82,13 @@ void shouldGiveValidConfig(
assertThat(config.getVersion()).isEqualTo(expectedProjectVersion);
});
}

@Test
void shouldReturnListOfRepositoriesName() {
final List<String> repoNames = List.of(REPO_NAME, "dummy");
final ConfigurationController configurationController =
new ConfigurationController(List.of(), false, null, null, repoNames);

assertThat(configurationController.getRepositoryNames()).isEqualTo(repoNames);
}
}

0 comments on commit 4e48ecc

Please sign in to comment.