-
Notifications
You must be signed in to change notification settings - Fork 41
feat: include git commit SHA in PET info response for telemetry regression tracking
#473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,42 @@ | |
| fn main() { | ||
| println!("cargo:rerun-if-env-changed=PET_BUILD_ID"); | ||
| println!("cargo:rerun-if-env-changed=BUILD_BUILDID"); | ||
| println!("cargo:rerun-if-env-changed=PET_COMMIT_SHA"); | ||
| println!("cargo:rerun-if-env-changed=BUILD_SOURCEVERSION"); | ||
| println!("cargo:rerun-if-env-changed=GITHUB_SHA"); | ||
|
|
||
| // Filter empties per-candidate so an explicitly-set-but-empty primary var | ||
| // doesn't short-circuit the fallback chain (e.g., `PET_BUILD_ID=""`). | ||
| if let Some(build_id) = std::env::var("PET_BUILD_ID") | ||
| .ok() | ||
| .or_else(|| std::env::var("BUILD_BUILDID").ok()) | ||
| .filter(|value| !value.is_empty()) | ||
| .or_else(|| { | ||
| std::env::var("BUILD_BUILDID") | ||
| .ok() | ||
| .filter(|value| !value.is_empty()) | ||
| }) | ||
| { | ||
| println!("cargo:rustc-env=PET_BUILD_ID={build_id}"); | ||
| } | ||
|
|
||
| // BUILD_SOURCEVERSION is set by Azure Pipelines; GITHUB_SHA by GitHub Actions. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot generated: [verified]
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acknowledged — treating this as a config check rather than a code change. Shipped PET binaries are built from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acknowledged — treating this as a config check rather than a code change. Shipped PET binaries are built from |
||
| if let Some(commit_sha) = std::env::var("PET_COMMIT_SHA") | ||
| .ok() | ||
| .filter(|value| !value.is_empty()) | ||
| .or_else(|| { | ||
| std::env::var("BUILD_SOURCEVERSION") | ||
| .ok() | ||
| .filter(|value| !value.is_empty()) | ||
| }) | ||
| .or_else(|| { | ||
| std::env::var("GITHUB_SHA") | ||
| .ok() | ||
| .filter(|value| !value.is_empty()) | ||
| }) | ||
| { | ||
| println!("cargo:rustc-env=PET_COMMIT_SHA={commit_sha}"); | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| { | ||
| if std::env::var("CARGO_BIN_NAME").is_err() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -523,6 +523,8 @@ pub struct InfoResponse { | |
| pub pet_version: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub build_id: Option<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub commit_sha: Option<String>, | ||
| } | ||
|
|
||
| impl InfoResponse { | ||
|
|
@@ -532,6 +534,9 @@ impl InfoResponse { | |
| build_id: option_env!("PET_BUILD_ID") | ||
| .filter(|value| !value.is_empty()) | ||
| .map(ToString::to_string), | ||
| commit_sha: option_env!("PET_COMMIT_SHA") | ||
| .filter(|value| !value.is_empty()) | ||
| .map(ToString::to_string), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1537,14 +1542,45 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn test_info_response_uses_package_version_and_optional_build_id() { | ||
| fn test_info_response_uses_package_version_and_optional_build_metadata() { | ||
| let info = InfoResponse::current(); | ||
|
|
||
| assert_eq!(info.pet_version, env!("CARGO_PKG_VERSION")); | ||
| // build_id / commit_sha are populated from env vars set at compile time by CI. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot generated: [verified]
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Added |
||
| // For local dev builds they will be None; assert non-empty only when present. | ||
| assert!(info | ||
| .build_id | ||
| .as_deref() | ||
| .is_none_or(|build_id| !build_id.is_empty())); | ||
| assert!(info | ||
| .commit_sha | ||
| .as_deref() | ||
| .is_none_or(|commit_sha| !commit_sha.is_empty())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_info_response_serializes_camel_case_and_omits_none() { | ||
| // Guards the JSON wire format (camelCase rename + skip_serializing_if) | ||
| // independently of whether CI env vars were set at compile time. | ||
| let json = serde_json::to_value(InfoResponse { | ||
| pet_version: "1.2.3".to_string(), | ||
| build_id: Some("42".to_string()), | ||
| commit_sha: Some("abc123".to_string()), | ||
| }) | ||
| .unwrap(); | ||
| assert_eq!(json["petVersion"], "1.2.3"); | ||
| assert_eq!(json["buildId"], "42"); | ||
| assert_eq!(json["commitSha"], "abc123"); | ||
|
|
||
| let json = serde_json::to_value(InfoResponse { | ||
| pet_version: "1.2.3".to_string(), | ||
| build_id: None, | ||
| commit_sha: None, | ||
| }) | ||
| .unwrap(); | ||
| assert_eq!(json["petVersion"], "1.2.3"); | ||
| assert!(json.get("buildId").is_none()); | ||
| assert!(json.get("commitSha").is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,16 +111,22 @@ fn assert_single_environment( | |
| } | ||
|
|
||
| #[test] | ||
| fn info_reports_pet_version_and_optional_build_id() { | ||
| fn info_reports_pet_version_and_optional_build_metadata() { | ||
| let client = PetJsonRpcClient::spawn().expect("failed to spawn PET server"); | ||
|
|
||
| let info = client.info().expect("info request failed"); | ||
|
|
||
| assert_eq!(info.pet_version, env!("CARGO_PKG_VERSION")); | ||
| // build_id / commit_sha are populated from env vars set at compile time by CI. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot generated: [verified]
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same fix path as the unit-test thread: the new |
||
| // For local dev builds they will be None; assert non-empty only when present. | ||
| assert!(info | ||
| .build_id | ||
| .as_deref() | ||
| .is_none_or(|build_id| !build_id.is_empty())); | ||
| assert!(info | ||
| .commit_sha | ||
| .as_deref() | ||
| .is_none_or(|commit_sha| !commit_sha.is_empty())); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot generated:
The Skeptic identified (structurally confirmed) that an explicitly-set-but-empty
PET_COMMIT_SHAsilently suppresses theBUILD_SOURCEVERSION/GITHUB_SHAfallbacks:or_elseonly fires onNone, soSome("")short-circuits the chain and the trailing.filter()then drops it toNone, losing the SHA entirely. This is concretely triggerable (a GitHub Actions YAML referencing an unset context yields""). While the Advocate notes this mirrors the existingbuild_idlogic and may be unreachable in practice, the fix is cheap — filter each candidate before falling back:.ok().filter(|v| !v.is_empty()).or_else(|| std::env::var("BUILD_SOURCEVERSION").ok().filter(|v| !v.is_empty())).... Apply the same hardening to thebuild_idchain above.[verified]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in 356f4af. Moved the empty-string .filter() inside each candidate so a Some("") falls through to the next env var instead of being swallowed by the trailing filter. Applied the same hardening to the �uild_id chain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in 356f4af. Moved the empty-string
.filter()inside each candidate so aSome(\"\")falls through to the next env var instead of being swallowed by the trailing filter. Applied the same hardening to thebuild_idchain.