Skip to content

Commit 528bf01

Browse files
feat: make get_commit_hash different based on the provider
This allows us to add more detailed errors for github actions.
1 parent f42ed78 commit 528bf01

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

src/run/run_environment/github_actions/provider.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use git2::Repository;
12
use lazy_static::lazy_static;
23
use regex::Regex;
34
use serde_json::Value;
@@ -217,6 +218,24 @@ impl RunEnvironmentProvider for GitHubActionsProvider {
217218
metadata,
218219
})
219220
}
221+
222+
fn get_commit_hash(&self, repository_root_path: &str) -> Result<String> {
223+
let repo = Repository::open(repository_root_path).context(format!(
224+
"Failed to open repository at path: {repository_root_path}\n\
225+
Make sure git is installed, and that `actions/checkout` used git to fetch the repository\n\
226+
If necessary, install git before running `actions/checkout`.\n\
227+
If you run into permission issues when running in Docker, you may need to also run \
228+
`git config --global --add safe.directory $GITHUB_WORKSPACE` "
229+
))?;
230+
231+
let commit_hash = repo
232+
.head()
233+
.and_then(|head| head.peel_to_commit())
234+
.context("Failed to get HEAD commit")?
235+
.id()
236+
.to_string();
237+
Ok(commit_hash)
238+
}
220239
}
221240

222241
#[cfg(test)]

src/run/run_environment/provider.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ pub trait RunEnvironmentDetector {
1616
fn detect() -> bool;
1717
}
1818

19-
fn get_commit_hash(repository_root_path: &str) -> Result<String> {
20-
let repo = Repository::open(repository_root_path).context(format!(
21-
"Failed to open repository at path: {repository_root_path}"
22-
))?;
23-
24-
let commit_hash = repo
25-
.head()
26-
.and_then(|head| head.peel_to_commit())
27-
.context("Failed to get HEAD commit")?
28-
.id()
29-
.to_string();
30-
Ok(commit_hash)
31-
}
32-
3319
/// `RunEnvironmentProvider` is a trait that defines the necessary methods
3420
/// for a continuous integration provider.
3521
pub trait RunEnvironmentProvider {
@@ -73,7 +59,7 @@ pub trait RunEnvironmentProvider {
7359
) -> Result<UploadMetadata> {
7460
let run_environment_metadata = self.get_run_environment_metadata()?;
7561

76-
let commit_hash = get_commit_hash(&run_environment_metadata.repository_root_path)?;
62+
let commit_hash = self.get_commit_hash(&run_environment_metadata.repository_root_path)?;
7763

7864
Ok(UploadMetadata {
7965
version: Some(LATEST_UPLOAD_METADATA_VERSION),
@@ -94,6 +80,25 @@ pub trait RunEnvironmentProvider {
9480
run_part: self.get_run_provider_run_part(),
9581
})
9682
}
83+
84+
/// Returns the HEAD commit hash of the repository at the given path.
85+
fn get_commit_hash(&self, repository_root_path: &str) -> Result<String> {
86+
get_commit_hash_default_impl(repository_root_path)
87+
}
88+
}
89+
90+
fn get_commit_hash_default_impl(repository_root_path: &str) -> Result<String> {
91+
let repo = Repository::open(repository_root_path).context(format!(
92+
"Failed to open repository at path: {repository_root_path}"
93+
))?;
94+
95+
let commit_hash = repo
96+
.head()
97+
.and_then(|head| head.peel_to_commit())
98+
.context("Failed to get HEAD commit")?
99+
.id()
100+
.to_string();
101+
Ok(commit_hash)
97102
}
98103

99104
#[cfg(test)]
@@ -102,7 +107,7 @@ mod tests {
102107

103108
#[test]
104109
fn test_get_commit_hash() {
105-
let commit_hash = get_commit_hash(env!("CARGO_MANIFEST_DIR")).unwrap();
110+
let commit_hash = get_commit_hash_default_impl(env!("CARGO_MANIFEST_DIR")).unwrap();
106111
// ensure that the commit hash is correct, thus it has 40 characters
107112
assert_eq!(commit_hash.len(), 40);
108113
}

0 commit comments

Comments
 (0)