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
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
1 change: 0 additions & 1 deletion src/ci_provider/buildkite/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
source: src/ci_provider/buildkite/provider.rs
expression: upload_metadata
expression: provider_metadata
---
{
"ref": "refs/pull/22/merge",
"headRef": "feat/codspeed-runner",
"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/"
Expand Down
1 change: 0 additions & 1 deletion src/ci_provider/github_actions/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
source: src/ci_provider/github_actions/provider.rs
expression: upload_metadata
expression: provider_metadata
---
{
"ref": "refs/pull/22/merge",
"headRef": "fork-owner:feat/codspeed-runner",
"baseRef": "main",
"owner": "my-org",
"repository": "adrien-python-test",
"commitHash": "24809d9fca9ad0808a777bcbd807ecd5ec8a9100",
"event": "pull_request",
"ghData": {
"runId": 6957110437,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
source: src/ci_provider/github_actions/provider.rs
expression: upload_metadata
expression: provider_metadata
---
{
"ref": "refs/pull/22/merge",
"headRef": "feat/codspeed-runner",
"baseRef": "main",
"owner": "my-org",
"repository": "adrien-python-test",
"commitHash": "24809d9fca9ad0808a777bcbd807ecd5ec8a9100",
"event": "pull_request",
"ghData": {
"runId": 6957110437,
Expand Down
1 change: 0 additions & 1 deletion src/ci_provider/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub struct ProviderMetadata {
pub base_ref: Option<String>,
pub owner: String,
pub repository: String,
pub commit_hash: String,
pub event: RunEvent,
pub gh_data: Option<GhData>,
pub repository_root_path: String,
Expand Down
30 changes: 30 additions & 0 deletions src/ci_provider/provider.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use git2::Repository;
use simplelog::SharedLogger;

use crate::config::Config;
Expand All @@ -11,6 +12,20 @@ pub trait CIProviderDetector {
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
.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.
Expand Down Expand Up @@ -58,11 +73,14 @@ pub trait CIProvider {
fn get_upload_metadata(&self, config: &Config, archive_hash: &str) -> Result<UploadMetadata> {
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(),
Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions src/uploader/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/uploader/upload_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -54,7 +54,7 @@ mod tests {
let hash = upload_metadata.get_hash();
assert_eq!(
hash,
"ae0421143ff1cd8ddad4566c0ccecce3f30c25b0be86736d91555ff323ec16ba"
"8beb149c4645c666156e24fe0f68d24a63cec1d7756f35dd17cab1d84528ed7b"
);
assert_json_snapshot!(upload_metadata);
}
Expand Down