Skip to content

Commit

Permalink
✨ : compute registry details for Github ssh urls
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed May 6, 2022
1 parent 1bfcecc commit c0c3153
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main/java/io/gaia_app/modules/ModuleServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import io.gaia_app.registries.RegistryType;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

@Service
public class ModuleServiceImpl implements ModuleService {
Expand All @@ -15,16 +17,25 @@ public class ModuleServiceImpl implements ModuleService {
* The first group (.+?) expands as few times as possible.
* The second non-capturing group (?:) matches the end of the line or .git
*/
private static final String GITHUB_HTTPS_REPOSITORY_URL_REGEX = "https://github\\.com/(.+?)(?:$|\\.git$)";
private static final Pattern GITHUB_HTTPS_REPOSITORY_URL_REGEX = Pattern.compile("https://github\\.com/(.+?)(?:$|\\.git$)");

/**
* matches git@github.com:something(.git), and captures 'something'.
* The first group (.+?) expands as few times as possible.
* The second non-capturing group (?:) matches the end of the line or .git
*/
private static final Pattern GITHUB_SSH_REPOSITORY_URL_REGEX = Pattern.compile("git@github\\.com:(.+?)(?:$|\\.git$)");

@Override
public void updateRegistryDetails(TerraformModule module) {
var pattern = Pattern.compile(GITHUB_HTTPS_REPOSITORY_URL_REGEX);
var matcher = pattern.matcher(module.getGitRepositoryUrl());

if (matcher.matches()) {
var projectId = matcher.group(1);
module.setRegistryDetails(new RegistryDetails(RegistryType.GITHUB, projectId));
}
// find first pattern that matches, then computes the details
Stream.of(GITHUB_HTTPS_REPOSITORY_URL_REGEX, GITHUB_SSH_REPOSITORY_URL_REGEX)
.map(pattern -> pattern.matcher(module.getGitRepositoryUrl()))
.filter(Matcher::matches)
.findFirst()
.ifPresent(it -> {
var projectId = it.group(1);
module.setRegistryDetails(new RegistryDetails(RegistryType.GITHUB, projectId));
});
}
}
42 changes: 42 additions & 0 deletions src/test/java/io/gaia_app/modules/ModuleServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.gaia_app.modules;

import io.gaia_app.modules.bo.TerraformModule;
import io.gaia_app.registries.RegistryType;
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;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class ModuleServiceImplTest {

public static Stream<String> updateRegistryDetails_forGithubModule() {
return Stream.of(
"https://github.com/juwit/terraform-docker-mongo",
"https://github.com/juwit/terraform-docker-mongo.git",
"git@github.com:juwit/terraform-docker-mongo",
"git@github.com:juwit/terraform-docker-mongo.git"
);
}

@ParameterizedTest
@MethodSource
void updateRegistryDetails_forGithubModule(String registryUrl) {
var module = new TerraformModule();
module.setGitRepositoryUrl(registryUrl);

var moduleService = new ModuleServiceImpl();
moduleService.updateRegistryDetails(module);

assertThat(module.getRegistryDetails())
.isNotNull()
.satisfies(it -> {
assertThat(it.getRegistryType()).isEqualTo(RegistryType.GITHUB);
assertThat(it.getProjectId()).isEqualTo("juwit/terraform-docker-mongo");
});
}
}

0 comments on commit c0c3153

Please sign in to comment.