Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/run/run_environment/github_actions/provider.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use git2::Repository;
use lazy_static::lazy_static;
use regex::Regex;
use serde_json::Value;
Expand Down Expand Up @@ -90,7 +91,15 @@ impl TryFrom<&Config> for GitHubActionsProvider {
path.push("");
path.to_string_lossy().to_string()
}
None => format!("/home/runner/work/{repository}/{repository}/"),
None => {
// Fallback to GITHUB_WORKSPACE, the default repository location when using the checkout action
// https://docs.github.com/en/actions/reference/workflows-and-actions/variables
if let Ok(github_workspace) = env::var("GITHUB_WORKSPACE") {
format!("{github_workspace}/")
} else {
format!("/home/runner/work/{repository}/{repository}/")
}
}
};

Ok(Self {
Expand Down Expand Up @@ -209,6 +218,24 @@ impl RunEnvironmentProvider for GitHubActionsProvider {
metadata,
})
}

fn get_commit_hash(&self, repository_root_path: &str) -> Result<String> {
let repo = Repository::open(repository_root_path).context(format!(
"Failed to open repository at path: {repository_root_path}\n\
Make sure git is installed, and that `actions/checkout` used git to fetch the repository\n\
If necessary, install git before running `actions/checkout`.\n\
If you run into permission issues when running in Docker, you may need to also run \
`git config --global --add safe.directory $GITHUB_WORKSPACE` "
))?;

let commit_hash = repo
.head()
.and_then(|head| head.peel_to_commit())
.context("Failed to get HEAD commit")?
.id()
.to_string();
Ok(commit_hash)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -297,6 +324,10 @@ mod tests {
("GITHUB_HEAD_REF", Some("feat/codspeed-runner")),
("GITHUB_JOB", Some("log-env")),
("GITHUB_REF", Some("refs/pull/22/merge")),
(
"GITHUB_WORKSPACE",
Some("/home/runner/work/adrien-python-test/adrien-python-test"),
),
("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")),
("GITHUB_RUN_ID", Some("6957110437")),
("VERSION", Some("0.1.0")),
Expand Down Expand Up @@ -346,6 +377,10 @@ mod tests {
("GITHUB_JOB", Some("log-env")),
("GITHUB_REF", Some("refs/pull/22/merge")),
("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")),
(
"GITHUB_WORKSPACE",
Some("/home/runner/work/adrien-python-test/adrien-python-test"),
),
("GITHUB_RUN_ID", Some("6957110437")),
("VERSION", Some("0.1.0")),
("GH_MATRIX", Some("null")),
Expand Down Expand Up @@ -402,6 +437,10 @@ mod tests {
("GITHUB_HEAD_REF", Some("feat/codspeed-runner")),
("GITHUB_JOB", Some("log-env")),
("GITHUB_REF", Some("refs/pull/22/merge")),
(
"GITHUB_WORKSPACE",
Some("/home/runner/work/adrien-python-test/adrien-python-test"),
),
("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")),
("GITHUB_RUN_ID", Some("6957110437")),
("VERSION", Some("0.1.0")),
Expand Down
37 changes: 21 additions & 16 deletions src/run/run_environment/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ pub trait RunEnvironmentDetector {
fn detect() -> bool;
}

fn get_commit_hash(repository_root_path: &str) -> Result<String> {
let repo = Repository::open(repository_root_path).context(format!(
"Failed to open repository at path: {repository_root_path}"
))?;

let commit_hash = repo
.head()
.and_then(|head| head.peel_to_commit())
.context("Failed to get HEAD commit")?
.id()
.to_string();
Ok(commit_hash)
}

/// `RunEnvironmentProvider` is a trait that defines the necessary methods
/// for a continuous integration provider.
pub trait RunEnvironmentProvider {
Expand Down Expand Up @@ -73,7 +59,7 @@ pub trait RunEnvironmentProvider {
) -> Result<UploadMetadata> {
let run_environment_metadata = self.get_run_environment_metadata()?;

let commit_hash = get_commit_hash(&run_environment_metadata.repository_root_path)?;
let commit_hash = self.get_commit_hash(&run_environment_metadata.repository_root_path)?;

Ok(UploadMetadata {
version: Some(LATEST_UPLOAD_METADATA_VERSION),
Expand All @@ -94,6 +80,25 @@ pub trait RunEnvironmentProvider {
run_part: self.get_run_provider_run_part(),
})
}

/// Returns the HEAD commit hash of the repository at the given path.
fn get_commit_hash(&self, repository_root_path: &str) -> Result<String> {
get_commit_hash_default_impl(repository_root_path)
}
}

fn get_commit_hash_default_impl(repository_root_path: &str) -> Result<String> {
let repo = Repository::open(repository_root_path).context(format!(
"Failed to open repository at path: {repository_root_path}"
))?;

let commit_hash = repo
.head()
.and_then(|head| head.peel_to_commit())
.context("Failed to get HEAD commit")?
.id()
.to_string();
Ok(commit_hash)
}

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

#[test]
fn test_get_commit_hash() {
let commit_hash = get_commit_hash(env!("CARGO_MANIFEST_DIR")).unwrap();
let commit_hash = get_commit_hash_default_impl(env!("CARGO_MANIFEST_DIR")).unwrap();
// ensure that the commit hash is correct, thus it has 40 characters
assert_eq!(commit_hash.len(), 40);
}
Expand Down