Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,7 +48,7 @@ public class CiVisibilityRepoServices {

private static final Logger LOGGER = LoggerFactory.getLogger(CiVisibilityRepoServices.class);

final String repoRoot;
@Nullable final String repoRoot;
final String moduleName;
final Provider ciProvider;
final Map<String, String> ciTags;
Expand Down Expand Up @@ -135,7 +136,7 @@ private static String appendSlashIfNeeded(String repoRoot) {
}
}

static String getModuleName(Config config, String repoRoot, Path path) {
static String getModuleName(Config config, @Nullable String repoRoot, Path path) {
// if parent process is instrumented, it will provide build system's module name
String parentModuleName = config.getCiVisibilityModuleName();
if (parentModuleName != null) {
Expand Down Expand Up @@ -175,7 +176,7 @@ private static ExecutionSettingsFactory buildExecutionSettingsFactory(
GitRepoUnshallow gitRepoUnshallow,
GitDataUploader gitDataUploader,
PullRequestInfo pullRequestInfo,
String repoRoot) {
@Nullable String repoRoot) {
ConfigurationApi configurationApi;
if (backendApi == null) {
LOGGER.warn(
Expand Down Expand Up @@ -208,7 +209,7 @@ private static GitDataUploader buildGitDataUploader(
GitClient gitClient,
GitRepoUnshallow gitRepoUnshallow,
BackendApi backendApi,
String repoRoot) {
@Nullable String repoRoot) {
if (!config.isCiVisibilityGitUploadEnabled()) {
return () -> CompletableFuture.completedFuture(null);
}
Expand Down Expand Up @@ -239,7 +240,7 @@ private static GitDataUploader buildGitDataUploader(
}

private static SourcePathResolver buildSourcePathResolver(
String repoRoot, RepoIndexProvider indexProvider) {
@Nullable String repoRoot, RepoIndexProvider indexProvider) {
SourcePathResolver compilerAidedResolver =
repoRoot != null
? new CompilerAidedSourcePathResolver(repoRoot)
Expand All @@ -248,7 +249,7 @@ private static SourcePathResolver buildSourcePathResolver(
return new BestEffortSourcePathResolver(compilerAidedResolver, indexResolver);
}

private static Codeowners buildCodeowners(String repoRoot) {
private static Codeowners buildCodeowners(@Nullable String repoRoot) {
if (repoRoot != null) {
return new CodeownersProvider().build(repoRoot);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ExecutionSettingsFactoryImpl implements ExecutionSettingsFactory {
private final GitRepoUnshallow gitRepoUnshallow;
private final GitDataUploader gitDataUploader;
private final PullRequestInfo pullRequestInfo;
private final String repositoryRoot;
@Nullable private final String repositoryRoot;

public ExecutionSettingsFactoryImpl(
Config config,
Expand All @@ -63,7 +63,7 @@ public ExecutionSettingsFactoryImpl(
GitRepoUnshallow gitRepoUnshallow,
GitDataUploader gitDataUploader,
PullRequestInfo pullRequestInfo,
String repositoryRoot) {
@Nullable String repositoryRoot) {
this.config = config;
this.configurationApi = configurationApi;
this.gitClient = gitClient;
Expand All @@ -75,21 +75,19 @@ public ExecutionSettingsFactoryImpl(

/** @return Executions settings by module name */
public Map<String, ExecutionSettings> create(@Nonnull JvmInfo jvmInfo) {
TracerEnvironment tracerEnvironment = buildTracerEnvironment(repositoryRoot, jvmInfo, null);
TracerEnvironment tracerEnvironment = buildTracerEnvironment(jvmInfo, null);
return create(tracerEnvironment);
}

@Override
public ExecutionSettings create(@Nonnull JvmInfo jvmInfo, @Nullable String moduleName) {
TracerEnvironment tracerEnvironment =
buildTracerEnvironment(repositoryRoot, jvmInfo, moduleName);
TracerEnvironment tracerEnvironment = buildTracerEnvironment(jvmInfo, moduleName);
Map<String, ExecutionSettings> settingsByModule = create(tracerEnvironment);
ExecutionSettings settings = settingsByModule.get(moduleName);
return settings != null ? settings : settingsByModule.get(DEFAULT_SETTINGS);
}

private TracerEnvironment buildTracerEnvironment(
String repositoryRoot, JvmInfo jvmInfo, @Nullable String moduleName) {
private TracerEnvironment buildTracerEnvironment(JvmInfo jvmInfo, @Nullable String moduleName) {
GitInfo gitInfo = GitInfoProvider.INSTANCE.getGitInfo(repositoryRoot);

TracerEnvironment.Builder builder = TracerEnvironment.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.BitSet;
Expand All @@ -42,6 +43,7 @@
import org.jacoco.core.data.SessionInfoStore;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.ISourceFileLocator;
import org.jacoco.report.InputStreamSourceFileLocator;
import org.jacoco.report.html.HTMLFormatter;
import org.jacoco.report.xml.XMLFormatter;
Expand All @@ -55,13 +57,13 @@ public static final class Factory
implements CoverageCalculator.Factory<JacocoCoverageCalculator> {
private final Config config;
private final RepoIndexProvider repoIndexProvider;
private final String repoRoot;
@Nullable private final String repoRoot;
private final ModuleSignalRouter moduleSignalRouter;

public Factory(
Config config,
RepoIndexProvider repoIndexProvider,
String repoRoot,
@Nullable String repoRoot,
ModuleSignalRouter moduleSignalRouter) {
this.config = config;
this.repoIndexProvider = repoIndexProvider;
Expand Down Expand Up @@ -100,7 +102,7 @@ public JacocoCoverageCalculator moduleCoverage(

private final RepoIndexProvider repoIndexProvider;

private final String repoRoot;
@Nullable private final String repoRoot;

private final long eventId;

Expand All @@ -116,7 +118,10 @@ public JacocoCoverageCalculator moduleCoverage(
private final Collection<File> outputClassesDirs = new HashSet<>();

private JacocoCoverageCalculator(
Config config, RepoIndexProvider repoIndexProvider, String repoRoot, long sessionId) {
Config config,
RepoIndexProvider repoIndexProvider,
@Nullable String repoRoot,
long sessionId) {
this.parent = null;
this.config = config;
this.repoIndexProvider = repoIndexProvider;
Expand All @@ -128,7 +133,7 @@ private JacocoCoverageCalculator(
Config config,
RepoIndexProvider repoIndexProvider,
ExecutionSettings executionSettings,
String repoRoot,
@Nullable String repoRoot,
long moduleId,
@Nullable BuildModuleLayout moduleLayout,
ModuleSignalRouter moduleSignalRouter,
Expand Down Expand Up @@ -297,29 +302,33 @@ private void dumpCoverageReport(IBundleCoverage coverageBundle, File reportFolde
final IReportVisitor htmlVisitor =
htmlFormatter.createVisitor(new FileMultiReportOutput(reportFolder));
htmlVisitor.visitInfo(Collections.emptyList(), Collections.emptyList());
htmlVisitor.visitBundle(
coverageBundle, new RepoIndexFileLocator(repoIndexProvider.getIndex(), repoRoot));
htmlVisitor.visitBundle(coverageBundle, createSourceFileLocator());
htmlVisitor.visitEnd();

File xmlReport = new File(reportFolder, "jacoco.xml");
try (FileOutputStream xmlReportStream = new FileOutputStream(xmlReport)) {
XMLFormatter xmlFormatter = new XMLFormatter();
IReportVisitor xmlVisitor = xmlFormatter.createVisitor(xmlReportStream);
xmlVisitor.visitInfo(Collections.emptyList(), Collections.emptyList());
xmlVisitor.visitBundle(
coverageBundle, new RepoIndexFileLocator(repoIndexProvider.getIndex(), repoRoot));
xmlVisitor.visitBundle(coverageBundle, createSourceFileLocator());
xmlVisitor.visitEnd();
}
} catch (Exception e) {
LOGGER.error("Error while creating report in {}", reportFolder, e);
}
}

private ISourceFileLocator createSourceFileLocator() {
return repoRoot != null
? new RepoIndexFileLocator(repoIndexProvider.getIndex(), repoRoot)
: NoOpFileLocator.INSTANCE;
}

private static final class RepoIndexFileLocator extends InputStreamSourceFileLocator {
private final RepoIndex repoIndex;
private final String repoRoot;
@Nonnull private final String repoRoot;

private RepoIndexFileLocator(RepoIndex repoIndex, String repoRoot) {
private RepoIndexFileLocator(RepoIndex repoIndex, @Nonnull String repoRoot) {
super("utf-8", 4);
this.repoIndex = repoIndex;
this.repoRoot = repoRoot;
Expand All @@ -343,6 +352,22 @@ protected InputStream getSourceStream(String path) throws IOException {
}
}

private static final class NoOpFileLocator implements ISourceFileLocator {
private static final NoOpFileLocator INSTANCE = new NoOpFileLocator();

private NoOpFileLocator() {}

@Override
public Reader getSourceFile(String s, String s1) {
return null;
}

@Override
public int getTabWidth() {
return 0;
}
}

private long getCoveragePercentage(IBundleCoverage coverageBundle) {
if (backendCoverageData.isEmpty()) {
return getLocalCoveragePercentage(coverageBundle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

public class NoOpTestEventsHandler<SuiteKey, TestKey>
implements TestEventsHandler<SuiteKey, TestKey> {
Expand Down Expand Up @@ -99,7 +98,7 @@ public SkipReason skipReason(TestIdentifier test) {
return null;
}

@NotNull
@Nonnull
@Override
public TestExecutionPolicy executionPolicy(
TestIdentifier test, TestSourceData source, Collection<String> testTags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ LineDiff getGitDiff(String baseCommit, String targetCommit)
throws IOException, TimeoutException, InterruptedException;

interface Factory {
GitClient create(String repoRoot);
GitClient create(@Nullable String repoRoot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class NoOpGitClient implements GitClient {

Expand Down Expand Up @@ -54,7 +54,7 @@ public String getCurrentBranch() {
return null;
}

@NotNull
@Nonnull
@Override
public List<String> getTags(String commit) {
return Collections.emptyList();
Expand Down Expand Up @@ -108,13 +108,13 @@ public String getCommitterDate(String commit) {
return null;
}

@NotNull
@Nonnull
@Override
public List<String> getLatestCommits() {
return Collections.emptyList();
}

@NotNull
@Nonnull
@Override
public List<String> getObjects(
Collection<String> commitsToSkip, Collection<String> commitsToInclude) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ShellGitClient implements GitClient {
*/
ShellGitClient(
CiVisibilityMetricCollector metricCollector,
String repoRoot,
@Nonnull String repoRoot,
String latestCommitsSince,
int latestCommitsLimit,
long timeoutMillis) {
Expand Down Expand Up @@ -651,10 +651,15 @@ public Factory(Config config, CiVisibilityMetricCollector metricCollector) {
}

@Override
public GitClient create(String repoRoot) {
public GitClient create(@Nullable String repoRoot) {
long commandTimeoutMillis = config.getCiVisibilityGitCommandTimeoutMillis();
return new ShellGitClient(
metricCollector, repoRoot, "1 month ago", 1000, commandTimeoutMillis);
if (repoRoot != null) {
return new ShellGitClient(
metricCollector, repoRoot, "1 month ago", 1000, commandTimeoutMillis);
} else {
LOGGER.debug("Could not determine repository root, using no-op git client");
return NoOpGitClient.INSTANCE;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datadog.trace.api.cache.DDCache;
import datadog.trace.api.cache.DDCaches;
import java.nio.file.FileSystem;
import javax.annotation.Nullable;

public class CachingRepoIndexBuilderFactory implements RepoIndexProvider.Factory {

Expand All @@ -25,7 +26,7 @@ public CachingRepoIndexBuilderFactory(
}

@Override
public RepoIndexProvider create(String repoRoot) {
public RepoIndexProvider create(@Nullable String repoRoot) {
if (repoRoot == null) {
return () -> RepoIndex.EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,7 +38,7 @@ public class RepoIndexBuilder implements RepoIndexProvider {

public RepoIndexBuilder(
Config config,
String repoRoot,
@Nonnull String repoRoot,
PackageResolver packageResolver,
ResourceResolver resourceResolver,
FileSystem fileSystem) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package datadog.trace.civisibility.source.index;

import javax.annotation.Nullable;

public interface RepoIndexProvider {
RepoIndex getIndex();

interface Factory {
RepoIndexProvider create(String repoRoot);
RepoIndexProvider create(@Nullable String repoRoot);
}
}