Skip to content

Commit 99980be

Browse files
use relative paths in remotes and add remote origin to resources
1 parent 2970e68 commit 99980be

48 files changed

Lines changed: 101 additions & 76 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/git/tree/GitClientTest.groovy

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import datadog.trace.civisibility.telemetry.CiVisibilityMetricCollectorImpl
99
import java.nio.file.Files
1010
import java.nio.file.Path
1111
import java.nio.file.Paths
12+
import java.util.stream.Collectors
1213
import spock.lang.Specification
1314
import spock.lang.TempDir
1415

@@ -469,19 +470,19 @@ class GitClientTest extends Specification {
469470
}
470471

471472
def "test get base branch sha: #testcaseName"() {
472-
givenGitRepo(repo)
473-
def gitClient = givenGitClient()
473+
givenGitRepos(["ci/git/impacted/repo_origin", "ci/git/impacted/$repoName"])
474+
def gitClient = givenGitClient(repoName)
474475

475476
expect:
476477
gitClient.getBaseCommitSha(baseBranch, null) == expected
477478

478479
where:
479-
testcaseName | repo | baseBranch | expected
480-
"base branch provided" | "ci/git/impacted/source_repo/git" | "master" | "c0a09c420836a5d2c1ce4c74d9c4e732d4ccd065"
481-
"base branch not provided" | "ci/git/impacted/source_repo/git" | null | "c0a09c420836a5d2c1ce4c74d9c4e732d4ccd065"
482-
"fresh clone with remote cloned into master" | "ci/git/impacted/new_clone/git" | null | "c0a09c420836a5d2c1ce4c74d9c4e732d4ccd065"
483-
"no remote clone" | "ci/git/impacted/no_remote/git" | null | null
484-
"Github Actions style clone" | "ci/git/impacted/ghub_actions_clone/git" | null | "c0a09c420836a5d2c1ce4c74d9c4e732d4ccd065"
480+
testcaseName | repoName | baseBranch | expected
481+
"base branch provided" | "source_repo" | "master" | "15567afb8426f72157c523d49dd49c24d6fe855e"
482+
"base branch not provided" | "source_repo" | null | "15567afb8426f72157c523d49dd49c24d6fe855e"
483+
"fresh clone with remote cloned into master" | "new_clone" | null | "15567afb8426f72157c523d49dd49c24d6fe855e"
484+
"no remote clone" | "no_remote" | null | null
485+
"Github Actions style clone" | "ghub_actions_clone" | null | "15567afb8426f72157c523d49dd49c24d6fe855e"
485486
}
486487

487488
private void givenGitRepo() {
@@ -491,12 +492,36 @@ class GitClientTest extends Specification {
491492
private void givenGitRepo(String resourceName) {
492493
def gitFolder = Paths.get(getClass().getClassLoader().getResource(resourceName).toURI())
493494
def tempGitFolder = tempDir.resolve(GIT_FOLDER)
494-
Files.createDirectories(tempGitFolder)
495-
IOUtils.copyFolder(gitFolder, tempGitFolder)
495+
copyFolder(gitFolder, tempGitFolder)
496496
}
497497

498-
private givenGitClient() {
498+
private void givenGitRepos(List<String> resourceDirs) {
499+
def resources = resourceDirs.stream().map(dir -> Paths.get(getClass().getClassLoader().getResource(dir).toURI())).collect(Collectors.toList())
500+
for (def resource : resources) {
501+
def gitFolder = resource.resolve("git")
502+
def destFolder = tempDir.resolve(resource.getFileName())
503+
if (Files.isDirectory(gitFolder)) {
504+
// repos with git/ folder
505+
def tempGitFolder = destFolder.resolve(GIT_FOLDER)
506+
copyFolder(gitFolder, tempGitFolder)
507+
} else {
508+
// dirs with no git/ folder, i.e. a remote
509+
copyFolder(resource, destFolder)
510+
}
511+
}
512+
}
513+
514+
private static void copyFolder(Path src, Path dest) {
515+
Files.createDirectories(dest)
516+
IOUtils.copyFolder(src, dest)
517+
}
518+
519+
private givenGitClient(String tempRelPath) {
499520
def metricCollector = Stub(CiVisibilityMetricCollectorImpl)
500-
new ShellGitClient(metricCollector, tempDir.toString(), "25 years ago", 10, GIT_COMMAND_TIMEOUT_MILLIS)
521+
new ShellGitClient(metricCollector, tempDir.resolve(tempRelPath).toString(), "25 years ago", 10, GIT_COMMAND_TIMEOUT_MILLIS)
522+
}
523+
524+
private givenGitClient() {
525+
givenGitClient("")
501526
}
502527
}
Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,65 @@
11
#!/bin/bash
22

3-
origin_path="/tmp/impacted/repo_origin"
4-
source_path="/tmp/impacted/source_repo"
5-
new_clone_path="/tmp/impacted/new_clone"
6-
no_remote_path="/tmp/impacted/no_remote"
7-
ghub_actions_path="/tmp/impacted/ghub_actions_clone"
3+
# IMPORTANT: always use relative paths, as tests move the folders to temp dirs
4+
base_path="/tmp/impacted/"
5+
origin_path="repo_origin"
6+
source_path="source_repo"
7+
new_clone_path="new_clone"
8+
no_remote_path="no_remote"
9+
ghub_actions_path="ghub_actions_clone"
10+
11+
base_branch="master"
12+
feature_branch="feature"
13+
14+
mkdir -p $base_path
15+
cd $base_path
816

917
# create origin
1018
mkdir -p $origin_path
11-
(cd $origin_path && git init --bare)
19+
cd $origin_path && git init --bare
20+
cd ..
1221

1322
# create git repo
14-
mkdir -p $source_path
15-
(cd $source_path && git init && git remote add origin $origin_path)
16-
(cd $source_path && echo "Hello, world!" >>README.md && git add README.md && git commit -m "Initial commit")
17-
(cd $source_path && echo "Hello, world!" >>README.md && git add README.md && git commit -m "Update README")
18-
(cd $source_path && git push origin master)
19-
20-
base_branch="master"
21-
feature_branch="feature"
22-
base_commit=$(cd $source_path && git rev-parse HEAD)
23-
23+
mkdir -p $source_path && cd $source_path
24+
git init && git remote add origin "../$origin_path"
25+
echo "Hello, world!" >>README.md && git add README.md && git commit -m "Initial commit"
26+
echo "Hello, world!" >>README.md && git add README.md && git commit -m "Update README"
27+
git push origin master
28+
base_commit=$(git rev-parse HEAD)
2429
# create feature branch
25-
(cd $source_path && git checkout -b $feature_branch)
26-
(cd $source_path && echo "Feature branch change" >>README.md && git add README.md && git commit -m "Updated README")
27-
(cd $source_path && git push origin $feature_branch)
30+
git checkout -b $feature_branch
31+
echo "Feature branch change" >>README.md && git add README.md && git commit -m "Feature branch commit"
32+
git push origin $feature_branch
33+
cd ..
2834

29-
# create new clone
30-
mkdir -p $new_clone_path
31-
(cd $new_clone_path && git init)
32-
(cd $new_clone_path && git remote add origin $origin_path)
33-
(cd $new_clone_path && git fetch origin $feature_branch)
34-
(cd $new_clone_path && git reset --hard "origin/$feature_branch")
35+
# clone with remote branch cloned into master branch of local repo
36+
mkdir -p $new_clone_path && cd $new_clone_path
37+
git init
38+
git remote add origin "../$origin_path"
39+
git fetch origin $feature_branch
40+
git reset --hard "origin/$feature_branch"
41+
cd ..
3542

3643
# remote pointing to non existing repo
37-
mkdir -p $no_remote_path
38-
(cd $no_remote_path && git init)
39-
(cd $no_remote_path && echo "base branch file" >>README.md && git add README.md && git commit -m "first commit")
40-
(cd $no_remote_path && git remote add origin "git@git.com:datadog/non_existing_repo.git")
44+
mkdir -p $no_remote_path && cd $no_remote_path
45+
git init
46+
echo "base branch file" >>README.md && git add README.md && git commit -m "first commit"
47+
git remote add origin "git@git.com:datadog/non_existing_repo.git"
48+
cd ..
4149

4250
# github actions style clone
43-
mkdir -p $ghub_actions_path
44-
(cd $ghub_actions_path && git init)
45-
(cd $ghub_actions_path && git remote add origin $origin_path)
46-
(cd $ghub_actions_path && git fetch --no-tags --prune --no-recurse-submodules origin $feature_branch)
47-
(cd $ghub_actions_path && git checkout --progress --force -B $feature_branch "refs/remotes/origin/$feature_branch")
51+
mkdir -p $ghub_actions_path && cd $ghub_actions_path
52+
git init
53+
git remote add origin "../$origin_path"
54+
git fetch --no-tags --prune --no-recurse-submodules origin $feature_branch
55+
git checkout --progress --force -B $feature_branch "refs/remotes/origin/$feature_branch"
56+
cd ..
4857

4958
echo "BASE COMMIT: $base_commit"
5059

5160
# cleanup
52-
(cd $source_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index && mv .git git)
53-
(cd $new_clone_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index && mv .git git)
54-
(cd $no_remote_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index && mv .git git)
55-
(cd $ghub_actions_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index && mv .git git)
61+
(cd $origin_path && rm -rf hooks info logs COMMIT_EDITMSG description index README.md)
62+
(cd $source_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index README.md && mv .git git)
63+
(cd $new_clone_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index README.md && mv .git git)
64+
(cd $no_remote_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index README.md && mv .git git)
65+
(cd $ghub_actions_path && rm -rf .git/hooks .git/info .git/logs .git/COMMIT_EDITMSG .git/description .git/index README.md && mv .git git)

dd-java-agent/agent-ci-visibility/src/test/resources/ci/git/impacted/ghub_actions_clone/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2fdf0c87f0a59400612ea7bff833507e8b4572eb branch 'feature' of /tmp/impacted/repo_origin
1+
260c03f5848ec8054374407916e806a80bec3729 branch 'feature' of ../repo_origin

dd-java-agent/agent-ci-visibility/src/test/resources/ci/git/impacted/ghub_actions_clone/git/config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
ignorecase = true
77
precomposeunicode = true
88
[remote "origin"]
9-
url = /tmp/impacted/repo_origin
9+
url = ../repo_origin
1010
fetch = +refs/heads/*:refs/remotes/origin/*
1111
[branch "feature"]
1212
remote = origin

0 commit comments

Comments
 (0)