From ec8447f16028e16c7af3fdc38b2b835e7495838d Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Mon, 12 Aug 2024 14:37:27 +0200 Subject: [PATCH] feat: Configurable repos.csv path --- README.md | 2 ++ .../OrganizationStructureService.java | 21 ++++++++++++++----- .../organizations/ScmConfiguration.java | 12 +++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c867cdf..de16132 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ There is repetition in organizational hierarchy in this format. The reference im If you have multiple organizations with the same display name, you can map id to display name in an `id-mapping.txt`. A simple reference file is included in this repository. +You can either overwrite the existing [repos.csv](src/main/resources/repos.csv) or set the `moderne.scm.reposCsvPath` in [application.yaml](src/main/resources/application.yaml). + ### Mapping repositories Edit [application.yaml](src/main/resources/application.yaml) to list all the origins (host + context path) for the SCM providers listed in the repos.csv, and provide a type: `[GITHUB,GITLAB,BITBUCKET_CLOUD,BITBUCKET,AZURE_DEVOPS]`. diff --git a/src/main/java/io/moderne/organizations/OrganizationStructureService.java b/src/main/java/io/moderne/organizations/OrganizationStructureService.java index fd3c181..f46af06 100644 --- a/src/main/java/io/moderne/organizations/OrganizationStructureService.java +++ b/src/main/java/io/moderne/organizations/OrganizationStructureService.java @@ -8,11 +8,10 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Path; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -22,7 +21,7 @@ */ @Service public class OrganizationStructureService { - private static final String REPOS_CSV = "repos.csv"; + private static final String DEFAULT_REPOS_CSV = "repos.csv"; private static final String NAME_MAPPING = "id-mapping.txt"; private static final Logger log = LoggerFactory.getLogger(OrganizationStructureService.class.getName()); private final ScmConfiguration scmConfiguration; @@ -39,7 +38,19 @@ public Map readOrganizationStructure() { final Map idToNameMapping = readIdToNameMapping(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(new ClassPathResource(REPOS_CSV).getInputStream()))) { + Path reposCsvOverride = scmConfiguration.getReposCsvPath(); + InputStream inputStream; + try { + if (reposCsvOverride == null) { + inputStream = new ClassPathResource(DEFAULT_REPOS_CSV).getInputStream(); + } else { + inputStream = new FileInputStream(reposCsvOverride.toFile()); + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { reader.readLine(); // skip header reader.lines() .forEach(line -> { diff --git a/src/main/java/io/moderne/organizations/ScmConfiguration.java b/src/main/java/io/moderne/organizations/ScmConfiguration.java index 3a462b6..aecd082 100644 --- a/src/main/java/io/moderne/organizations/ScmConfiguration.java +++ b/src/main/java/io/moderne/organizations/ScmConfiguration.java @@ -1,7 +1,9 @@ package io.moderne.organizations; +∑import jakarta.annotation.Nullable; import org.springframework.boot.context.properties.ConfigurationProperties; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -10,6 +12,9 @@ public class ScmConfiguration { private List repositories; private boolean allowMissingScmOrigins; + @Nullable + private Path reposCsvPath; + public static class ScmRepository { String origin; Type type; @@ -54,5 +59,12 @@ public void setAllowMissingScmOrigins(boolean allowMissingScmOrigins) { this.allowMissingScmOrigins = allowMissingScmOrigins; } + @Nullable + public Path getReposCsvPath() { + return reposCsvPath; + } + public void setReposCsvPath(@Nullable Path reposCsvPath) { + this.reposCsvPath = reposCsvPath; + } }