Skip to content

Commit f68cbd1

Browse files
committed
fix: retrieve root_repository_path from git dir
1 parent 35f56e4 commit f68cbd1

4 files changed

Lines changed: 94 additions & 9 deletions

File tree

src/ci_provider/buildkite/provider.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
provider::{CIProvider, CIProviderDetector},
1111
},
1212
config::Config,
13-
helpers::get_env_variable,
13+
helpers::{find_repository_root, get_env_variable},
1414
prelude::*,
1515
};
1616

@@ -93,6 +93,20 @@ impl TryFrom<&Config> for BuildkiteProvider {
9393
let is_pr = get_pr_number()?.is_some();
9494
let (owner, repository) = get_owner_and_repository()?;
9595

96+
let repository_root_path = match find_repository_root(&std::env::current_dir()?) {
97+
Some(mut path) => {
98+
// Add a trailing slash to the path
99+
path.push("");
100+
path.to_string_lossy().to_string()
101+
}
102+
None => format!(
103+
"/buildkite/builds/{}/{}/{}/",
104+
get_env_variable("BUILDKITE_AGENT_NAME")?,
105+
get_env_variable("BUILDKITE_ORGANIZATION_SLUG")?,
106+
get_env_variable("BUILDKITE_PIPELINE_SLUG")?,
107+
),
108+
};
109+
96110
Ok(Self {
97111
owner: owner.clone(),
98112
repository: repository.clone(),
@@ -109,12 +123,7 @@ impl TryFrom<&Config> for BuildkiteProvider {
109123
},
110124
commit_hash: get_env_variable("BUILDKITE_COMMIT")?,
111125
event: get_run_event()?,
112-
repository_root_path: format!(
113-
"/buildkite/builds/{}/{}/{}/",
114-
get_env_variable("BUILDKITE_AGENT_NAME")?,
115-
get_env_variable("BUILDKITE_ORGANIZATION_SLUG")?,
116-
get_env_variable("BUILDKITE_PIPELINE_SLUG")?,
117-
),
126+
repository_root_path,
118127
})
119128
}
120129
}

src/ci_provider/github_actions/provider.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
provider::{CIProvider, CIProviderDetector},
1111
},
1212
config::Config,
13-
helpers::get_env_variable,
13+
helpers::{find_repository_root, get_env_variable},
1414
prelude::*,
1515
};
1616

@@ -82,6 +82,14 @@ impl TryFrom<&Config> for GitHubActionsProvider {
8282
let event = serde_json::from_str(&format!("\"{}\"", github_event_name)).context(
8383
format!("Event {} is not supported by CodSpeed", github_event_name),
8484
)?;
85+
let repository_root_path = match find_repository_root(&std::env::current_dir()?) {
86+
Some(mut path) => {
87+
// Add a trailing slash to the path
88+
path.push("");
89+
path.to_string_lossy().to_string()
90+
}
91+
None => format!("/home/runner/work/{}/{}/", repository, repository),
92+
};
8593

8694
Ok(Self {
8795
owner,
@@ -103,7 +111,7 @@ impl TryFrom<&Config> for GitHubActionsProvider {
103111
}),
104112
},
105113
base_ref: get_env_variable("GITHUB_BASE_REF").ok(),
106-
repository_root_path: format!("/home/runner/work/{}/{}/", repository, repository),
114+
repository_root_path,
107115
})
108116
}
109117
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::path::{Path, PathBuf};
2+
3+
// during normal execution, we want to find the repository root by looking for a .git directory
4+
// during tests, we want `find_repository_root` to always return `None`, so that we don't have to
5+
// create a git repository for each test
6+
7+
#[cfg(not(test))]
8+
pub fn find_repository_root(base_dir: &Path) -> Option<PathBuf> {
9+
_find_repository_root(base_dir)
10+
}
11+
12+
#[cfg(test)]
13+
pub fn find_repository_root(_base_dir: &Path) -> Option<PathBuf> {
14+
None
15+
}
16+
17+
// the core logic is extracted into a separate function so that it can be tested
18+
fn _find_repository_root(base_dir: &Path) -> Option<PathBuf> {
19+
let current_dir = base_dir.canonicalize().ok()?;
20+
21+
for ancestor in current_dir.ancestors() {
22+
let git_dir = ancestor.join(".git");
23+
if git_dir.exists() {
24+
return Some(ancestor.to_path_buf());
25+
}
26+
}
27+
28+
log::warn!("Could not find repository root");
29+
30+
None
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::*;
36+
37+
#[test]
38+
fn test_find_repository_root() {
39+
// create an empty directory in a tmp directory, add a nested .git directory
40+
// and check if the repository root is found when calling _find_repository_root from a nested directory
41+
let tmp_dir = tempfile::tempdir().unwrap();
42+
let base_dir = tmp_dir.path().join("base-dir");
43+
let git_dir = base_dir.join(".git");
44+
std::fs::create_dir_all(git_dir).unwrap();
45+
let nested_current_dir = base_dir.join("nested").join("deeply");
46+
std::fs::create_dir_all(&nested_current_dir).unwrap();
47+
48+
let repository_root = _find_repository_root(&nested_current_dir).unwrap();
49+
assert_eq!(repository_root, base_dir.canonicalize().unwrap());
50+
51+
tmp_dir.close().unwrap();
52+
}
53+
54+
#[test]
55+
fn test_find_repository_root_no_git_dir() {
56+
// create an empty directory in a tmp directory and check if the repository root is not found
57+
let tmp_dir = tempfile::tempdir().unwrap();
58+
let base_dir = tmp_dir.path().join("base-dir");
59+
std::fs::create_dir_all(&base_dir).unwrap();
60+
61+
let repository_root = _find_repository_root(&base_dir);
62+
assert_eq!(repository_root, None);
63+
64+
tmp_dir.close().unwrap();
65+
}
66+
}

src/helpers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod find_repository_root;
12
mod get_env_var;
23

4+
pub use find_repository_root::find_repository_root;
35
pub use get_env_var::get_env_variable;

0 commit comments

Comments
 (0)