-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from CodeKaio/44-module-description
✨ : add module description screen
- Loading branch information
Showing
22 changed files
with
1,656 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/io/codeka/gaia/repository/TerraformModuleGitRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/codeka/gaia/repository/strategy/GitHubStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/codeka/gaia/repository/strategy/GitLabStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/io/codeka/gaia/repository/strategy/GitPlatformStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.