Skip to content

Commit bea1db2

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 bea1db2

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

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 fetching the repository\n\
226+
If necessary, install git before running `actions/checkout`.\n\
227+
If running in docker and running into permission issues, 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: 13 additions & 24 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,16 +80,19 @@ pub trait RunEnvironmentProvider {
9480
run_part: self.get_run_provider_run_part(),
9581
})
9682
}
97-
}
9883

99-
#[cfg(test)]
100-
mod tests {
101-
use super::*;
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+
let repo = Repository::open(repository_root_path).context(format!(
87+
"Failed to open repository at path: {repository_root_path}"
88+
))?;
10289

103-
#[test]
104-
fn test_get_commit_hash() {
105-
let commit_hash = get_commit_hash(env!("CARGO_MANIFEST_DIR")).unwrap();
106-
// ensure that the commit hash is correct, thus it has 40 characters
107-
assert_eq!(commit_hash.len(), 40);
90+
let commit_hash = repo
91+
.head()
92+
.and_then(|head| head.peel_to_commit())
93+
.context("Failed to get HEAD commit")?
94+
.id()
95+
.to_string();
96+
Ok(commit_hash)
10897
}
10998
}

0 commit comments

Comments
 (0)