Skip to content

Commit 127660a

Browse files
Always build ci workspace without trailing separator (#8788)
1 parent ad6d5fe commit 127660a

File tree

8 files changed

+45
-32
lines changed

8 files changed

+45
-32
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilityRepoServices.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import datadog.trace.civisibility.source.index.RepoIndexProvider;
3636
import datadog.trace.civisibility.source.index.RepoIndexSourcePathResolver;
3737
import datadog.trace.util.Strings;
38-
import java.io.File;
3938
import java.nio.file.Path;
4039
import java.nio.file.Paths;
4140
import java.util.Map;
@@ -72,7 +71,7 @@ public class CiVisibilityRepoServices {
7271
LOGGER.info("PR detected: {}", pullRequestInfo);
7372
}
7473

75-
repoRoot = appendSlashIfNeeded(getRepoRoot(ciInfo, services.gitClientFactory));
74+
repoRoot = getRepoRoot(ciInfo, services.gitClientFactory);
7675
moduleName = getModuleName(services.config, repoRoot, path);
7776
ciTags = new CITagsProvider().getCiTags(ciInfo, pullRequestInfo);
7877

@@ -126,7 +125,7 @@ private static PullRequestInfo buildPullRequestInfo(
126125
}
127126

128127
private static String getRepoRoot(CIInfo ciInfo, GitClient.Factory gitClientFactory) {
129-
String ciWorkspace = ciInfo.getNormalizedCiWorkspace();
128+
String ciWorkspace = ciInfo.getCiWorkspace();
130129
if (Strings.isNotBlank(ciWorkspace)) {
131130
return ciWorkspace;
132131

@@ -146,14 +145,6 @@ private static String getRepoRoot(CIInfo ciInfo, GitClient.Factory gitClientFact
146145
}
147146
}
148147

149-
private static String appendSlashIfNeeded(String repoRoot) {
150-
if (repoRoot != null && !repoRoot.endsWith(File.separator)) {
151-
return repoRoot + File.separator;
152-
} else {
153-
return repoRoot;
154-
}
155-
}
156-
157148
static String getModuleName(Config config, @Nullable String repoRoot, Path path) {
158149
// if parent process is instrumented, it will provide build system's module name
159150
String parentModuleName = config.getCiVisibilityModuleName();

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ci/CIInfo.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public CIInfo(
170170
this.ciPipelineNumber = ciPipelineNumber;
171171
this.ciPipelineUrl = ciPipelineUrl;
172172
this.ciJobUrl = ciJobUrl;
173-
this.ciWorkspace = ciWorkspace;
173+
this.ciWorkspace = sanitizeWorkspace(ciWorkspace);
174174
this.ciNodeName = ciNodeName;
175175
this.ciNodeLabels = ciNodeLabels;
176176
this.ciEnvVars = ciEnvVars;
@@ -209,20 +209,18 @@ public String getCiJobUrl() {
209209
return ciJobUrl;
210210
}
211211

212-
/**
213-
* @deprecated This method is here only to satisfy CI spec tests. Use {@link
214-
* #getNormalizedCiWorkspace()}
215-
*/
216-
@Deprecated
217-
public String getCiWorkspace() {
218-
return ciWorkspace;
212+
private String sanitizeWorkspace(String workspace) {
213+
String realCiWorkspace = FileUtils.toRealPath(workspace);
214+
return (realCiWorkspace == null
215+
|| !realCiWorkspace.endsWith(File.separator)
216+
|| realCiWorkspace.length() == 1) // root path "/"
217+
? realCiWorkspace
218+
: (realCiWorkspace.substring(0, realCiWorkspace.length() - 1));
219219
}
220220

221-
public String getNormalizedCiWorkspace() {
222-
String realCiWorkspace = FileUtils.toRealPath(ciWorkspace);
223-
return (realCiWorkspace == null || realCiWorkspace.endsWith(File.separator))
224-
? realCiWorkspace
225-
: (realCiWorkspace + File.separator);
221+
/** @return Workspace path without the trailing separator */
222+
public String getCiWorkspace() {
223+
return ciWorkspace;
226224
}
227225

228226
public String getCiNodeName() {

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ci/CITagsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public CITagsProvider() {
2222
}
2323

2424
public Map<String, String> getCiTags(CIInfo ciInfo, PullRequestInfo pullRequestInfo) {
25-
String repoRoot = ciInfo.getNormalizedCiWorkspace();
25+
String repoRoot = ciInfo.getCiWorkspace();
2626
GitInfo gitInfo = gitInfoProvider.getGitInfo(repoRoot);
2727

2828
return new CITagsBuilder()

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/CompilerAidedSourcePathResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog.trace.civisibility.source;
22

33
import datadog.compiler.utils.CompilerUtils;
4+
import java.io.File;
45
import javax.annotation.Nonnull;
56
import javax.annotation.Nullable;
67

@@ -9,7 +10,7 @@ public class CompilerAidedSourcePathResolver implements SourcePathResolver {
910
private final String repoRoot;
1011

1112
public CompilerAidedSourcePathResolver(String repoRoot) {
12-
this.repoRoot = repoRoot;
13+
this.repoRoot = repoRoot.endsWith(File.separator) ? repoRoot : repoRoot + File.separator;
1314
}
1415

1516
@Nullable
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package datadog.trace.civisibility.ci
2+
3+
4+
import spock.lang.Specification
5+
6+
class CIInfoTest extends Specification {
7+
8+
def "test ci workspace is correctly sanitized #iterationIndex"() {
9+
def builder = CIInfo.builder(null)
10+
builder.ciWorkspace(workspacePath)
11+
def info = builder.build()
12+
13+
info.ciWorkspace == sanitizedPath
14+
15+
where:
16+
workspacePath | sanitizedPath
17+
null | null
18+
"/" | "/"
19+
"/repo/path" | "/repo/path"
20+
"/repo/path/" | "/repo/path"
21+
}
22+
}

dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/codeowners/CodeownersProviderTest.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.nio.file.Files
88

99
class CodeownersProviderTest extends Specification {
1010

11-
private static final String REPO_ROOT = "/repo/root/"
11+
private static final String REPO_ROOT = "/repo/root"
1212

1313
def "test codeowners loading: #path"() {
1414
setup:
@@ -30,10 +30,10 @@ class CodeownersProviderTest extends Specification {
3030

3131
where:
3232
path << [
33-
REPO_ROOT + "CODEOWNERS",
34-
REPO_ROOT + ".github/CODEOWNERS",
35-
REPO_ROOT + ".gitlab/CODEOWNERS",
36-
REPO_ROOT + "docs/CODEOWNERS"
33+
REPO_ROOT + "/CODEOWNERS",
34+
REPO_ROOT + "/.github/CODEOWNERS",
35+
REPO_ROOT + "/.gitlab/CODEOWNERS",
36+
REPO_ROOT + "/docs/CODEOWNERS"
3737
]
3838
}
3939
}

dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/source/CompilerAidedSourcePathResolverTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import spock.lang.Specification
55

66
class CompilerAidedSourcePathResolverTest extends Specification {
77

8-
public static final String REPO_ROOT = "/repo/root/"
8+
public static final String REPO_ROOT = "/repo/root"
99
public static final String SOURCE_PATH_VALUE = "/repo/root/path/to/AClassWithSourceInfoInjected.java"
1010
public static final String SOURCE_PATH_OUTSIDE_REPO_VALUE = "/outside/path/to/AClassWithSourceInfoInjected.java"
1111

internal-api/src/main/java/datadog/trace/api/git/GitInfoProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public GitInfo getGitInfo(@Nullable String repositoryPath) {
5555
if (repositoryPath == null) {
5656
repositoryPath = NULL_PATH_STRING;
5757
}
58+
5859
return gitInfoCache.computeIfAbsent(repositoryPath, this::buildGitInfo);
5960
}
6061

0 commit comments

Comments
 (0)