Skip to content

Commit

Permalink
Fix module name detection for headless sessions (#7807)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog authored Oct 18, 2024
1 parent 217b146 commit 501bcb9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ public class CiVisibilityRepoServices {
}
}

private static String getModuleName(Config config, Path path, CIInfo ciInfo) {
static String getModuleName(Config config, Path path, CIInfo ciInfo) {
// if parent process is instrumented, it will provide build system's module name
String parentModuleName = config.getCiVisibilityModuleName();
if (parentModuleName != null) {
return parentModuleName;
}
String repoRoot = ciInfo.getNormalizedCiWorkspace();
if (repoRoot != null
&& path.startsWith(repoRoot)
// module name cannot be empty
&& !path.toString().equals(repoRoot)) {
return Paths.get(repoRoot).relativize(path).toString();
if (repoRoot != null && path.startsWith(repoRoot)) {
String relativePath = Paths.get(repoRoot).relativize(path).toString();
if (!relativePath.isEmpty()) {
return relativePath;
}
}
return config.getServiceName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package datadog.trace.civisibility


import datadog.trace.api.Config
import datadog.trace.civisibility.ci.CIInfo
import spock.lang.Specification

import java.nio.file.Paths

class CiVisibilityRepoServicesTest extends Specification {

def "test get parent module name: #parentModuleName, #repoSubFolder, #serviceName"() {
given:
def config = Stub(Config)
config.getCiVisibilityModuleName() >> parentModuleName
config.getServiceName() >> serviceName

def repoRoot = "/path/to/repo/root/"
def path = Paths.get(repoRoot + repoSubFolder)

def ciInfo = Stub(CIInfo)
ciInfo.getNormalizedCiWorkspace() >> repoRoot

expect:
CiVisibilityRepoServices.getModuleName(config, path, ciInfo) == moduleName

where:
parentModuleName | repoSubFolder | serviceName | moduleName
"parent-module" | "child-module" | "service-name" | "parent-module"
null | "child-module" | "service-name" | "child-module"
null | "" | "service-name" | "service-name"
}
}

0 comments on commit 501bcb9

Please sign in to comment.