Skip to content

Commit

Permalink
Avoid double serialization of environment strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Feb 17, 2025
1 parent ae6d93c commit 8ccaf48
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/ci/citool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct GithubActionsJob {
/// prefix (PR/try/auto).
full_name: String,
os: String,
env: BTreeMap<String, String>,
env: BTreeMap<String, serde_json::Value>,
#[serde(flatten)]
extra_keys: BTreeMap<String, serde_json::Value>,
}
Expand Down Expand Up @@ -165,15 +165,12 @@ fn skip_jobs(jobs: Vec<Job>, channel: &str) -> Vec<Job> {
.collect()
}

fn to_string_map(map: &BTreeMap<String, Value>) -> BTreeMap<String, String> {
map.iter()
fn yaml_map_to_json(map: &BTreeMap<String, Value>) -> BTreeMap<String, serde_json::Value> {
map.into_iter()
.map(|(key, value)| {
(
key.clone(),
serde_yaml::to_string(value)
.expect("Cannot serialize YAML value to string")
.trim()
.to_string(),
serde_json::to_value(&value).expect("Cannot convert map value from YAML to JSON"),
)
})
.collect()
Expand Down Expand Up @@ -222,26 +219,16 @@ fn calculate_jobs(
let jobs = jobs
.into_iter()
.map(|job| {
let mut env: BTreeMap<String, String> = to_string_map(base_env);
env.extend(to_string_map(&job.env));
let mut env: BTreeMap<String, serde_json::Value> = yaml_map_to_json(base_env);
env.extend(yaml_map_to_json(&job.env));
let full_name = format!("{prefix} - {}", job.name);

GithubActionsJob {
name: job.name,
full_name,
os: job.os,
env,
extra_keys: job
.extra_keys
.into_iter()
.map(|(key, value)| {
(
key,
serde_json::to_value(&value)
.expect("Cannot convert extra key value from YAML to JSON"),
)
})
.collect(),
extra_keys: yaml_map_to_json(&job.extra_keys),
}
})
.collect();
Expand Down Expand Up @@ -314,7 +301,15 @@ fn run_workflow_locally(db: JobDatabase, job_type: JobType, name: String) -> any
custom_env.insert("DEPLOY".to_string(), "1".to_string());
}
}
custom_env.extend(to_string_map(&job.env));
custom_env.extend(job.env.iter().map(|(key, value)| {
let value = match value {
Value::Bool(value) => value.to_string(),
Value::Number(value) => value.to_string(),
Value::String(value) => value.clone(),
_ => panic!("Unexpected type for environment variable {key} Only bool/number/string is supported.")
};
(key.clone(), value)
}));

let mut cmd = Command::new(Path::new(DOCKER_DIRECTORY).join("run.sh"));
cmd.arg(job.image());
Expand Down

0 comments on commit 8ccaf48

Please sign in to comment.