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
2 changes: 2 additions & 0 deletions bundle/src/bundle_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ impl From<BundleMetaV0_6_2> for BundleMetaV0_5_29 {
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct BundleMetaDebugProps {
pub command_line: String,
#[serde(default)]
pub trunk_envs: HashMap<String, String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
Expand Down
1 change: 1 addition & 0 deletions bundle/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ mod tests {
bundle_upload_id_v2: String::with_capacity(0),
debug_props: BundleMetaDebugProps {
command_line: String::with_capacity(0),
trunk_envs: HashMap::new(),
},
variant: Some("variant".to_string()),
base_props: BundleMetaBaseProps {
Expand Down
54 changes: 53 additions & 1 deletion cli/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bundle::{
QuarantineBulkTestStatus, TestCollectionProps, bin_parse,
};
use codeowners::{CodeOwners, OwnersSource};
use constants::{ENVS_TO_GET, TRUNK_PR_NUMBER_ENV};
use constants::{ENVS_TO_GET, TRUNK_API_TOKEN_ENV, TRUNK_ENVS_TO_CAPTURE, TRUNK_PR_NUMBER_ENV};
use context::{
bazel_bep::{
binary_parser::BazelBepBinParser,
Expand Down Expand Up @@ -67,12 +67,22 @@ lazy_static! {
// This function is used to gather debug properties for the bundle meta.
// It will trigger EXC_BAD_ACCESS on arm64-darwin builds when compiled under cdylib
pub fn gather_debug_props(args: Vec<String>, token: String) -> BundleMetaDebugProps {
let trunk_envs: HashMap<String, String> = TRUNK_ENVS_TO_CAPTURE
.iter()
.filter_map(|&env_var| {
env::var(env_var)
.map(|env_var_value| (env_var.to_string(), env_var_value))
.ok()
})
.collect();

BundleMetaDebugProps {
command_line: COMMAND_REGEX
.replace(&args.join(" "), "")
.replace(&token, "")
.trim()
.to_string(),
trunk_envs,
}
}

Expand Down Expand Up @@ -952,6 +962,8 @@ fn parse_num_tests(file_sets: &[FileSet]) -> usize {

#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::env;

use bundle::BundleMetaDebugProps;
#[cfg(target_os = "macos")]
Expand All @@ -976,6 +988,7 @@ mod tests {
upload_args.clone(),
BundleMetaDebugProps {
command_line: "test".to_string(),
trunk_envs: HashMap::new(),
},
)
.unwrap();
Expand All @@ -988,6 +1001,7 @@ mod tests {
upload_args.clone(),
BundleMetaDebugProps {
command_line: "test".to_string(),
trunk_envs: HashMap::new(),
},
)
.unwrap();
Expand All @@ -1000,6 +1014,7 @@ mod tests {
upload_args,
BundleMetaDebugProps {
command_line: "test".to_string(),
trunk_envs: HashMap::new(),
},
)
.unwrap();
Expand Down Expand Up @@ -1115,4 +1130,41 @@ mod tests {
let debug_props = super::gather_debug_props(args, "token".to_string());
assert_eq!(debug_props.command_line, "trunk flakytests");
}

#[test]
fn test_gather_debug_props_trunk_envs() {
unsafe {
env::set_var("TRUNK_REPO_URL", "https://github.com/example/repo");
env::set_var("TRUNK_API_TOKEN", "supersecret");
env::set_var("TRUNK_VARIANT", "my-variant");
}

let args: Vec<String> = vec!["trunk".into(), "upload".into()];
let debug_props = super::gather_debug_props(args, "irrelevant".to_string());

// TRUNK_REPO_URL is in the allowlist, should be captured
assert_eq!(
debug_props
.trunk_envs
.get("TRUNK_REPO_URL")
.map(String::as_str),
Some("https://github.com/example/repo")
);
// TRUNK_API_TOKEN is intentionally excluded from the allowlist
assert!(!debug_props.trunk_envs.contains_key("TRUNK_API_TOKEN"));
// TRUNK_VARIANT is in the allowlist, should be captured
assert_eq!(
debug_props
.trunk_envs
.get("TRUNK_VARIANT")
.map(String::as_str),
Some("my-variant")
);

unsafe {
env::remove_var("TRUNK_REPO_URL");
env::remove_var("TRUNK_API_TOKEN");
env::remove_var("TRUNK_VARIANT");
}
}
}
29 changes: 29 additions & 0 deletions constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ pub const TRUNK_TEST_PROCESS_EXIT_CODE_ENV: &str = "TRUNK_TEST_PROCESS_EXIT_CODE
pub const TRUNK_VALIDATION_REPORT_ENV: &str = "TRUNK_VALIDATION_REPORT";
pub const TRUNK_SHOW_FAILURE_MESSAGES_ENV: &str = "TRUNK_SHOW_FAILURE_MESSAGES";
pub const TRUNK_DEBUG_ENV: &str = "TRUNK_DEBUG";

// TRUNK_* environment variables to capture in bundle metadata for debugging.
// TRUNK_API_TOKEN_ENV is intentionally omitted.
pub const TRUNK_ENVS_TO_CAPTURE: &[&str] = &[
TRUNK_PUBLIC_API_ADDRESS_ENV,
TRUNK_API_CLIENT_RETRY_COUNT_ENV,
TRUNK_ORG_URL_SLUG_ENV,
TRUNK_TEST_COLLECTION_SHORT_ID_ENV,
TRUNK_REPO_ROOT_ENV,
TRUNK_REPO_URL_ENV,
TRUNK_REPO_HEAD_SHA_ENV,
TRUNK_REPO_HEAD_BRANCH_ENV,
TRUNK_REPO_HEAD_COMMIT_EPOCH_ENV,
TRUNK_REPO_HEAD_AUTHOR_NAME_ENV,
TRUNK_PR_NUMBER_ENV,
TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS_ENV,
TRUNK_CODEOWNERS_PATH_ENV,
TRUNK_CODEOWNERS_TYPE_ENV,
TRUNK_VARIANT_ENV,
TRUNK_USE_UNCLONED_REPO_ENV,
TRUNK_DISABLE_QUARANTINING_ENV,
TRUNK_ALLOW_EMPTY_TEST_RESULTS_ENV,
TRUNK_DRY_RUN_ENV,
TRUNK_TEST_PROCESS_EXIT_CODE_ENV,
TRUNK_VALIDATION_REPORT_ENV,
TRUNK_SHOW_FAILURE_MESSAGES_ENV,
TRUNK_DEBUG_ENV,
];

pub const ENVS_TO_GET: &[&str] = &[
"CI",
"GIT_BRANCH",
Expand Down
4 changes: 4 additions & 0 deletions context-js/tests/parse_compressed_bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ const VERSION_TESTS = [
num_tests: faker.number.int(100),
num_files: faker.number.int(100),
command_line: "trunk-analytics-cli upload --token=***",
trunk_envs: {},
},
{
schema: "V0_7_7",
Expand All @@ -210,6 +211,7 @@ const VERSION_TESTS = [
bundle_upload_id_v2: "SOME ID",
variant: null,
internal_bundled_file: null,
trunk_envs: {},
},
{
schema: "V0_7_8",
Expand All @@ -221,6 +223,7 @@ const VERSION_TESTS = [
variant: null,
internal_bundled_file: null,
failed_tests: [],
trunk_envs: {},
},
] as const satisfies VersionedBundle[];

Expand Down Expand Up @@ -377,6 +380,7 @@ describe("context-js", () => {
bundle_upload_id_v2: "SOME ID",
variant: "some-variant",
internal_bundled_file: null,
trunk_envs: {},
} as const satisfies VersionedBundle;
const metaInfoJson = JSON.stringify(
versionedBundle,
Expand Down
1 change: 1 addition & 0 deletions test_report/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
variant: variant.clone().unwrap_or_default(),
});
let test_report = TestReportProto {
// trunk-ignore(clippy/deprecated)

Check notice on line 164 in test_report/src/report.rs

View workflow job for this annotation

GitHub Actions / Trunk Check

trunk(ignore-does-nothing)

[new] trunk-ignore(clippy/deprecated) is not suppressing a lint issue
uploader_metadata: test_result.uploader_metadata.clone(),
test_results: vec![test_result],
};
Expand Down Expand Up @@ -234,7 +234,7 @@
let repo_root = self.get_repo_root();
let command_string = self.0.borrow().command.clone();
let sentry_layer = sentry_tracing::layer().event_mapper(move |event, context| {
// trunk-ignore(clippy/match_ref_pats)

Check notice on line 237 in test_report/src/report.rs

View workflow job for this annotation

GitHub Actions / Trunk Check

trunk(ignore-does-nothing)

[new] trunk-ignore(clippy/match_ref_pats) is not suppressing a lint issue
match event.metadata().level() {
&tracing::Level::ERROR => {
let mut event = sentry_tracing::event_from_event(event, context);
Expand Down Expand Up @@ -554,7 +554,7 @@
let test_report = &mut self.0.borrow_mut().test_report;
// legacy: update the variant in all test results
for test_result in &mut test_report.test_results {
// trunk-ignore(clippy/deprecated)

Check notice on line 557 in test_report/src/report.rs

View workflow job for this annotation

GitHub Actions / Trunk Check

trunk(ignore-does-nothing)

[new] trunk-ignore(clippy/deprecated) is not suppressing a lint issue
if let Some(uploader_metadata) = &mut test_result.uploader_metadata {
uploader_metadata.variant = variant.clone();
}
Expand Down Expand Up @@ -636,6 +636,7 @@
.and_then(|v| v.parse::<usize>().ok());
let debug_props = BundleMetaDebugProps {
command_line: self.0.borrow().command.clone(),
trunk_envs: HashMap::new(),
};
let test_run_result = trunk_analytics_cli::test_command::TestRunResult {
command: self.0.borrow().command.clone(),
Expand Down Expand Up @@ -779,7 +780,7 @@
nanos: finished_at_date_time.timestamp_subsec_nanos() as i32,
};
test.finished_at = Some(test_finished_at);
// trunk-ignore(clippy/deprecated)

Check notice on line 783 in test_report/src/report.rs

View workflow job for this annotation

GitHub Actions / Trunk Check

trunk(ignore-does-nothing)

[new] trunk-ignore(clippy/deprecated) is not suppressing a lint issue
test.status_output_message = failure_text.clone();
if status != Status::Success {
test.test_output = Some(TestOutput {
Expand Down
Loading