Skip to content

Commit 8ccaf48

Browse files
committed
Avoid double serialization of environment strings
1 parent ae6d93c commit 8ccaf48

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/ci/citool/src/main.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct GithubActionsJob {
9292
/// prefix (PR/try/auto).
9393
full_name: String,
9494
os: String,
95-
env: BTreeMap<String, String>,
95+
env: BTreeMap<String, serde_json::Value>,
9696
#[serde(flatten)]
9797
extra_keys: BTreeMap<String, serde_json::Value>,
9898
}
@@ -165,15 +165,12 @@ fn skip_jobs(jobs: Vec<Job>, channel: &str) -> Vec<Job> {
165165
.collect()
166166
}
167167

168-
fn to_string_map(map: &BTreeMap<String, Value>) -> BTreeMap<String, String> {
169-
map.iter()
168+
fn yaml_map_to_json(map: &BTreeMap<String, Value>) -> BTreeMap<String, serde_json::Value> {
169+
map.into_iter()
170170
.map(|(key, value)| {
171171
(
172172
key.clone(),
173-
serde_yaml::to_string(value)
174-
.expect("Cannot serialize YAML value to string")
175-
.trim()
176-
.to_string(),
173+
serde_json::to_value(&value).expect("Cannot convert map value from YAML to JSON"),
177174
)
178175
})
179176
.collect()
@@ -222,26 +219,16 @@ fn calculate_jobs(
222219
let jobs = jobs
223220
.into_iter()
224221
.map(|job| {
225-
let mut env: BTreeMap<String, String> = to_string_map(base_env);
226-
env.extend(to_string_map(&job.env));
222+
let mut env: BTreeMap<String, serde_json::Value> = yaml_map_to_json(base_env);
223+
env.extend(yaml_map_to_json(&job.env));
227224
let full_name = format!("{prefix} - {}", job.name);
228225

229226
GithubActionsJob {
230227
name: job.name,
231228
full_name,
232229
os: job.os,
233230
env,
234-
extra_keys: job
235-
.extra_keys
236-
.into_iter()
237-
.map(|(key, value)| {
238-
(
239-
key,
240-
serde_json::to_value(&value)
241-
.expect("Cannot convert extra key value from YAML to JSON"),
242-
)
243-
})
244-
.collect(),
231+
extra_keys: yaml_map_to_json(&job.extra_keys),
245232
}
246233
})
247234
.collect();
@@ -314,7 +301,15 @@ fn run_workflow_locally(db: JobDatabase, job_type: JobType, name: String) -> any
314301
custom_env.insert("DEPLOY".to_string(), "1".to_string());
315302
}
316303
}
317-
custom_env.extend(to_string_map(&job.env));
304+
custom_env.extend(job.env.iter().map(|(key, value)| {
305+
let value = match value {
306+
Value::Bool(value) => value.to_string(),
307+
Value::Number(value) => value.to_string(),
308+
Value::String(value) => value.clone(),
309+
_ => panic!("Unexpected type for environment variable {key} Only bool/number/string is supported.")
310+
};
311+
(key.clone(), value)
312+
}));
318313

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

0 commit comments

Comments
 (0)