From 434817d79e4d1967aea27380759012c059962b14 Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Mon, 22 Apr 2024 19:15:26 -0400 Subject: [PATCH] feat: use current checked out commit hash in UploadMetadata --- Cargo.lock | 66 +++++++++++++++++++ Cargo.toml | 1 + src/ci_provider/buildkite/provider.rs | 1 - ...tests__pull_request_provider_metadata.snap | 3 +- src/ci_provider/github_actions/provider.rs | 1 - ...__fork_pull_request_provider_metadata.snap | 3 +- ...tests__pull_request_provider_metadata.snap | 3 +- src/ci_provider/interfaces.rs | 1 - src/ci_provider/provider.rs | 30 +++++++++ src/uploader/interfaces.rs | 1 + ...ad_metadata__tests__get_metadata_hash.snap | 2 +- src/uploader/upload_metadata.rs | 4 +- 12 files changed, 104 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c4713dd..de6faaff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,6 +185,7 @@ version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ + "jobserver", "libc", ] @@ -254,6 +255,7 @@ dependencies = [ "async-compression", "base64", "clap", + "git2", "insta", "itertools", "lazy_static", @@ -569,6 +571,21 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" +[[package]] +name = "git2" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "232e6a7bfe35766bf715e55a88b39a700596c0ccfd88cd3680b4cdb40d66ef70" +dependencies = [ + "bitflags 2.4.1", + "libc", + "libgit2-sys", + "log", + "openssl-probe", + "openssl-sys", + "url", +] + [[package]] name = "h2" version = "0.3.21" @@ -785,6 +802,15 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "jobserver" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b099aaa34a9751c5bf0878add70444e1ed2dd73f347be99003d4577277de6e" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.65" @@ -806,6 +832,46 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libgit2-sys" +version = "0.16.2+1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee4126d8b4ee5c9d9ea891dd875cfdc1e9d0950437179104b183d7d8a74d24e8" +dependencies = [ + "cc", + "libc", + "libssh2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", +] + +[[package]] +name = "libssh2-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libz-sys" +version = "1.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linked-hash-map" version = "0.5.6" diff --git a/Cargo.toml b/Cargo.toml index 8b4dfa6a..e63ef7bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ base64 = "0.21.0" async-compression = { version = "0.4.5", features = ["tokio", "gzip"] } simplelog = { version = "0.12.1", default-features = false } tempfile = "3.10.0" +git2 = "0.18.3" [dev-dependencies] temp-env = { version = "0.3.6", features = ["async_closure"] } diff --git a/src/ci_provider/buildkite/provider.rs b/src/ci_provider/buildkite/provider.rs index cd935cfc..56a33439 100644 --- a/src/ci_provider/buildkite/provider.rs +++ b/src/ci_provider/buildkite/provider.rs @@ -142,7 +142,6 @@ impl CIProvider for BuildkiteProvider { Ok(ProviderMetadata { base_ref: self.base_ref.clone(), head_ref: self.head_ref.clone(), - commit_hash: self.commit_hash.clone(), event: self.event.clone(), owner: self.owner.clone(), repository: self.repository.clone(), diff --git a/src/ci_provider/buildkite/snapshots/codspeed_runner__ci_provider__buildkite__provider__tests__pull_request_provider_metadata.snap b/src/ci_provider/buildkite/snapshots/codspeed_runner__ci_provider__buildkite__provider__tests__pull_request_provider_metadata.snap index 93474df0..dc325f93 100644 --- a/src/ci_provider/buildkite/snapshots/codspeed_runner__ci_provider__buildkite__provider__tests__pull_request_provider_metadata.snap +++ b/src/ci_provider/buildkite/snapshots/codspeed_runner__ci_provider__buildkite__provider__tests__pull_request_provider_metadata.snap @@ -1,6 +1,6 @@ --- source: src/ci_provider/buildkite/provider.rs -expression: upload_metadata +expression: provider_metadata --- { "ref": "refs/pull/22/merge", @@ -8,7 +8,6 @@ expression: upload_metadata "baseRef": "main", "owner": "my-org", "repository": "adrien-python-test", - "commitHash": "abc123", "event": "pull_request", "ghData": null, "repositoryRootPath": "/buildkite/builds/7b10eca7600b-1/my-org/buildkite-test/" diff --git a/src/ci_provider/github_actions/provider.rs b/src/ci_provider/github_actions/provider.rs index efc9d329..a405f365 100644 --- a/src/ci_provider/github_actions/provider.rs +++ b/src/ci_provider/github_actions/provider.rs @@ -132,7 +132,6 @@ impl CIProvider for GitHubActionsProvider { Ok(ProviderMetadata { base_ref: self.base_ref.clone(), head_ref: self.head_ref.clone(), - commit_hash: self.commit_hash.clone(), event: self.event.clone(), gh_data: Some(self.gh_data.clone()), owner: self.owner.clone(), diff --git a/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__fork_pull_request_provider_metadata.snap b/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__fork_pull_request_provider_metadata.snap index 9110971f..8b8d6ef3 100644 --- a/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__fork_pull_request_provider_metadata.snap +++ b/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__fork_pull_request_provider_metadata.snap @@ -1,6 +1,6 @@ --- source: src/ci_provider/github_actions/provider.rs -expression: upload_metadata +expression: provider_metadata --- { "ref": "refs/pull/22/merge", @@ -8,7 +8,6 @@ expression: upload_metadata "baseRef": "main", "owner": "my-org", "repository": "adrien-python-test", - "commitHash": "24809d9fca9ad0808a777bcbd807ecd5ec8a9100", "event": "pull_request", "ghData": { "runId": 6957110437, diff --git a/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__pull_request_provider_metadata.snap b/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__pull_request_provider_metadata.snap index e1312cf1..9ca01187 100644 --- a/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__pull_request_provider_metadata.snap +++ b/src/ci_provider/github_actions/snapshots/codspeed_runner__ci_provider__github_actions__provider__tests__pull_request_provider_metadata.snap @@ -1,6 +1,6 @@ --- source: src/ci_provider/github_actions/provider.rs -expression: upload_metadata +expression: provider_metadata --- { "ref": "refs/pull/22/merge", @@ -8,7 +8,6 @@ expression: upload_metadata "baseRef": "main", "owner": "my-org", "repository": "adrien-python-test", - "commitHash": "24809d9fca9ad0808a777bcbd807ecd5ec8a9100", "event": "pull_request", "ghData": { "runId": 6957110437, diff --git a/src/ci_provider/interfaces.rs b/src/ci_provider/interfaces.rs index cf0a21d2..d9fb7291 100644 --- a/src/ci_provider/interfaces.rs +++ b/src/ci_provider/interfaces.rs @@ -9,7 +9,6 @@ pub struct ProviderMetadata { pub base_ref: Option, pub owner: String, pub repository: String, - pub commit_hash: String, pub event: RunEvent, pub gh_data: Option, pub repository_root_path: String, diff --git a/src/ci_provider/provider.rs b/src/ci_provider/provider.rs index 4f411bde..32b2636a 100644 --- a/src/ci_provider/provider.rs +++ b/src/ci_provider/provider.rs @@ -1,3 +1,4 @@ +use git2::Repository; use simplelog::SharedLogger; use crate::config::Config; @@ -11,6 +12,20 @@ pub trait CIProviderDetector { fn detect() -> bool; } +fn get_commit_hash(repository_root_path: &str) -> Result { + let repo = Repository::open(repository_root_path).context(format!( + "Failed to open repository at path: {}", + repository_root_path + ))?; + + let commit_hash = repo + .revparse_single("HEAD") + .context("Failed to get HEAD commit")? + .id() + .to_string(); + Ok(commit_hash) +} + /// `CIProvider` is a trait that defines the necessary methods for a continuous integration provider. pub trait CIProvider { /// Returns the logger for the CI provider. @@ -58,11 +73,14 @@ pub trait CIProvider { fn get_upload_metadata(&self, config: &Config, archive_hash: &str) -> Result { let provider_metadata = self.get_provider_metadata()?; + let commit_hash = get_commit_hash(&provider_metadata.repository_root_path)?; + Ok(UploadMetadata { version: Some(2), tokenless: config.token.is_none(), provider_metadata, profile_md5: archive_hash.into(), + commit_hash, runner: Runner { name: "codspeed-runner".into(), version: crate::VERSION.into(), @@ -72,3 +90,15 @@ pub trait CIProvider { }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_commit_hash() { + let commit_hash = get_commit_hash(env!("CARGO_MANIFEST_DIR")).unwrap(); + // ensure that the commit hash is correct, thus it has 40 characters + assert_eq!(commit_hash.len(), 40); + } +} diff --git a/src/uploader/interfaces.rs b/src/uploader/interfaces.rs index 05218493..3de19023 100644 --- a/src/uploader/interfaces.rs +++ b/src/uploader/interfaces.rs @@ -10,6 +10,7 @@ pub struct UploadMetadata { pub profile_md5: String, pub runner: Runner, pub platform: String, + pub commit_hash: String, #[serde(flatten)] pub provider_metadata: ProviderMetadata, } diff --git a/src/uploader/snapshots/codspeed_runner__uploader__upload_metadata__tests__get_metadata_hash.snap b/src/uploader/snapshots/codspeed_runner__uploader__upload_metadata__tests__get_metadata_hash.snap index 23e0bff6..7d7e6f4a 100644 --- a/src/uploader/snapshots/codspeed_runner__uploader__upload_metadata__tests__get_metadata_hash.snap +++ b/src/uploader/snapshots/codspeed_runner__uploader__upload_metadata__tests__get_metadata_hash.snap @@ -14,12 +14,12 @@ expression: upload_metadata ] }, "platform": "github-actions", + "commitHash": "5bd77cb0da72bef094893ed45fb793ff16ecfbe3", "ref": "refs/pull/29/merge", "headRef": "chore/native-action-runner", "baseRef": "main", "owner": "CodSpeedHQ", "repository": "codspeed-node", - "commitHash": "ea4005444338762d85163c8e8787387e2ba97fb6", "event": "pull_request", "ghData": { "runId": 7044765741, diff --git a/src/uploader/upload_metadata.rs b/src/uploader/upload_metadata.rs index 7166c6b8..425f0d85 100644 --- a/src/uploader/upload_metadata.rs +++ b/src/uploader/upload_metadata.rs @@ -31,13 +31,13 @@ mod tests { instruments: vec![InstrumentNames::MongoDB], }, platform: "github-actions".into(), + commit_hash: "5bd77cb0da72bef094893ed45fb793ff16ecfbe3".into(), provider_metadata: ProviderMetadata { ref_: "refs/pull/29/merge".into(), head_ref: Some("chore/native-action-runner".into()), base_ref: Some("main".into()), owner: "CodSpeedHQ".into(), repository: "codspeed-node".into(), - commit_hash: "ea4005444338762d85163c8e8787387e2ba97fb6".into(), event: RunEvent::PullRequest, gh_data: Some(GhData { run_id: 7044765741, @@ -54,7 +54,7 @@ mod tests { let hash = upload_metadata.get_hash(); assert_eq!( hash, - "ae0421143ff1cd8ddad4566c0ccecce3f30c25b0be86736d91555ff323ec16ba" + "8beb149c4645c666156e24fe0f68d24a63cec1d7756f35dd17cab1d84528ed7b" ); assert_json_snapshot!(upload_metadata); }