Skip to content

Commit

Permalink
Merge pull request #81 from CodeKaio/44-module-description
Browse files Browse the repository at this point in the history
✨ : add module description screen
  • Loading branch information
juwit authored Aug 2, 2019
2 parents dddc112 + e3fac2d commit 5078358
Show file tree
Hide file tree
Showing 22 changed files with 1,656 additions and 306 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@
<artifactId>bootstrap-vue</artifactId>
<version>2.0.0-rc.26</version>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>marked</artifactId>
<version>0.6.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>momentjs</artifactId>
<version>2.24.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package io.codeka.gaia.controller;

import io.codeka.gaia.bo.TerraformModule;
import io.codeka.gaia.repository.TerraformModuleGitRepository;
import io.codeka.gaia.repository.TerraformModuleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Optional;

@Controller
public class TerraformModuleController {

private TerraformModuleRepository terraformModuleRepository;
private TerraformModuleGitRepository terraformModuleGitRepository;

@Autowired
public TerraformModuleController(TerraformModuleRepository terraformModuleRepository) {
public TerraformModuleController(
TerraformModuleRepository terraformModuleRepository,
TerraformModuleGitRepository terraformModuleGitRepository) {
this.terraformModuleRepository = terraformModuleRepository;
this.terraformModuleGitRepository = terraformModuleGitRepository;
}

@GetMapping("/modules")
Expand All @@ -42,4 +48,18 @@ public String saveModule(@ModelAttribute TerraformModule module, Model model){
return modulesList(model);
}

@GetMapping("/modules/{id}/description")
public String description(@PathVariable String id, Model model) {
model.addAttribute("module", terraformModuleRepository.findById(id).orElseThrow());
return "module_description";
}

@GetMapping("/modules/{id}/readme")
@Produces(MediaType.TEXT_PLAIN)
@ResponseBody
public Optional<String> readme(@PathVariable String id) {
var module = terraformModuleRepository.findById(id).orElseThrow();
return terraformModuleGitRepository.getReadme(module);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class TerraformCLIRepository {
private String terraformReleasesVersionMin;
private RestTemplate restTemplate;

private class CLIVersion {
private static class CLIVersion {
private short major;
private short minor;
private short patch;
Expand All @@ -50,8 +50,6 @@ public TerraformCLIRepository(
/**
* Returns acceptable Terraform CLI versions by scanning HashiCorp releases url for terraform.
* Versions lower than the one in parameter and non final versions are ignored.
*
* @return
*/
public List<String> listCLIVersion() {
var response = restTemplate.exchange(
Expand Down Expand Up @@ -103,9 +101,9 @@ private Optional<CLIVersion> getCLIVersion(String version) {
if (!matcher.matches()) {
return Optional.empty();
}
var result = new CLIVersion(Short.valueOf(matcher.group(1)),
Short.valueOf(matcher.group(2)),
Short.valueOf(matcher.group(3)));
var result = new CLIVersion(Short.parseShort(matcher.group(1)),
Short.parseShort(matcher.group(2)),
Short.parseShort(matcher.group(3)));
return Optional.of(result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.codeka.gaia.repository;

import io.codeka.gaia.bo.TerraformModule;
import io.codeka.gaia.repository.strategy.GitHubStrategy;
import io.codeka.gaia.repository.strategy.GitLabStrategy;
import io.codeka.gaia.repository.strategy.GitPlatformStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public class TerraformModuleGitRepository {

List<GitPlatformStrategy> gitPlatformStrategies;

@Autowired
public TerraformModuleGitRepository() {
gitPlatformStrategies = List.of(new GitHubStrategy(), new GitLabStrategy());
}

/**
* Returns the url of the README file
*
* @param module module contains the README file
* @return the url of the README file
*/
public Optional<String> getReadme(TerraformModule module) {
Optional<GitPlatformStrategy> strategy = gitPlatformStrategies.stream()
.filter(s -> s.matches(module.getGitRepositoryUrl()))
.findFirst();

if (strategy.isEmpty()) {
return Optional.empty();
}
var url = strategy.get().getRawUrl(module.getGitRepositoryUrl(), module.getGitBranch(), module.getDirectory());
return Optional.of(url + "/README.md");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.codeka.gaia.repository.strategy;

import java.util.regex.Pattern;

public class GitHubStrategy extends GitPlatformStrategy {

@Override
protected final Pattern getPattern() {
return Pattern.compile("^http[s]?://[www.]?github.com(.*).git$");
}

@Override
protected String getTemplateRawUrl() {
return "https://raw.githubusercontent.com{0}/{1}";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.codeka.gaia.repository.strategy;

import java.util.regex.Pattern;

public class GitLabStrategy extends GitPlatformStrategy {

@Override
protected final Pattern getPattern() {
return Pattern.compile("^(http[s]?://[www.]?gitlab.*).git$");
}

@Override
protected String getTemplateRawUrl() {
return "{0}/raw/{1}";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.codeka.gaia.repository.strategy;

import org.apache.commons.lang.StringUtils;

import java.text.MessageFormat;
import java.util.regex.Pattern;

public abstract class GitPlatformStrategy {

protected static final String DEFAULT_BRANCH = "master";

/**
* Returns the pattern to match the repository url.
* The pattern should contains at least one group to extract the part of the repository to keep
*/
protected abstract Pattern getPattern();

/**
* Returns the template corresponding to the raw url>
* Can include parameters inside treated by {@link MessageFormat}.
* {0} matches the part of the repository url extracted by the pattern
* {1} matches the branch of the repository
*/
protected abstract String getTemplateRawUrl();

public boolean matches(String url) {
return getPattern().matcher(url).matches();
}

public String getRawUrl(String url, String branch, String directory) {
var gitBranch = branch == null ? DEFAULT_BRANCH : branch;
var matcher = getPattern().matcher(url);
if (matcher.matches()) {
var result = MessageFormat.format(getTemplateRawUrl(), matcher.group(1), gitBranch);
if (StringUtils.isNotBlank(directory)) {
result += "/" + directory;
}
return result;
}
return StringUtils.EMPTY;
}

}
Loading

0 comments on commit 5078358

Please sign in to comment.