
com.hubspot.blazar.discovery.BlazarConfigModuleDiscovery Maven / Gradle / Ivy
The newest version!
package com.hubspot.blazar.discovery;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.hubspot.blazar.base.BuildConfig;
import com.hubspot.blazar.base.CommitInfo;
import com.hubspot.blazar.base.DependencyInfo;
import com.hubspot.blazar.base.DiscoveredModule;
import com.hubspot.blazar.base.DiscoveryResult;
import com.hubspot.blazar.base.GitInfo;
import com.hubspot.blazar.base.MalformedFile;
import com.hubspot.blazar.util.GitHubHelper;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTree;
import org.kohsuke.github.GHTreeEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
@Singleton
public class BlazarConfigModuleDiscovery implements ModuleDiscovery {
private static final Logger LOG = LoggerFactory.getLogger(BlazarConfigModuleDiscovery.class);
private final GitHubHelper gitHubHelper;
@Inject
public BlazarConfigModuleDiscovery(GitHubHelper gitHubHelper) {
this.gitHubHelper = gitHubHelper;
}
@Override
public boolean shouldRediscover(GitInfo gitInfo, CommitInfo commitInfo) throws IOException {
for (String path : gitHubHelper.affectedPaths(commitInfo)) {
if (isBlazarConfig(path)) {
return true;
}
}
return false;
}
@Override
public DiscoveryResult discover(GitInfo gitInfo) throws IOException {
GHRepository repository = gitHubHelper.repositoryFor(gitInfo);
GHTree tree = gitHubHelper.treeFor(repository, gitInfo);
Set blazarConfigs = new HashSet<>();
for (GHTreeEntry entry : tree.getTree()) {
if (isBlazarConfig(entry.getPath())) {
blazarConfigs.add(entry.getPath());
}
}
Set modules = new HashSet<>();
Set malformedFiles = new HashSet<>();
for (String blazarConfig : blazarConfigs) {
if (disabled(blazarConfig, repository, gitInfo)) {
modules.add(new DiscoveredModule(
Optional.absent(),
"disabled",
"config",
blazarConfig,
"",
false,
System.currentTimeMillis(),
System.currentTimeMillis(),
Optional.absent(),
DependencyInfo.unknown()
));
continue;
}
final BuildConfig buildConfig;
try {
buildConfig = gitHubHelper.configFor(blazarConfig, repository, gitInfo).get();
} catch (JsonProcessingException e) {
LOG.warn("Error parsing config at path {} for repository {}@{}", blazarConfig, gitInfo.getFullRepositoryName(), gitInfo.getBranch());
malformedFiles.add(new MalformedFile(gitInfo.getId().get(), "config", blazarConfig, Throwables.getStackTraceAsString(e)));
continue;
}
if (canBuild(buildConfig)) {
String moduleName = moduleName(gitInfo, blazarConfig);
String glob = (blazarConfig.contains("/") ? blazarConfig.substring(0, blazarConfig.lastIndexOf('/') + 1) : "") + "**";
modules.add(new DiscoveredModule(moduleName, "config", blazarConfig, glob, buildConfig.getBuildpack(), DependencyInfo.unknown()));
}
}
return new DiscoveryResult(modules, malformedFiles);
}
private boolean disabled(String blazarConfig, GHRepository repository, GitInfo gitInfo) throws IOException {
return gitHubHelper.contentsFor(blazarConfig, repository, gitInfo).contains("enabled: false");
}
private boolean canBuild(BuildConfig buildConfig) {
return (buildConfig.getSteps().size() > 0 || buildConfig.getBuildpack().isPresent());
}
private static String moduleName(GitInfo gitInfo, String path) {
return path.contains("/") ? folderName(path) : gitInfo.getRepository();
}
private static String folderName(String path) {
String folderPath = path.substring(0, path.lastIndexOf('/'));
return folderPath.contains("/") ? folderPath.substring(folderPath.lastIndexOf('/') + 1) : folderPath;
}
private static boolean isBlazarConfig(String path) {
return ".blazar.yaml".equals(path) || path.endsWith("/.blazar.yaml");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy