Skip to content

Commit 8ee2d7b

Browse files
committed
Rework the build-info format to be JSON
External services like GH Actions can read JSON but *not* YAML, and the previous format couldn't simply be written "as JSON" because it relied on having structs as keys. This tweaks the format to be JSON-friendly and uses actual JSON files.
1 parent 0a098a8 commit 8ee2d7b

File tree

10 files changed

+63
-72
lines changed

10 files changed

+63
-72
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ open-build-service-mock = { git = "https://github.com/collabora/open-build-servi
2121
# open-build-service-mock = { path = "../open-build-service-rs/open-build-service-api" }
2222
rstest = "0.26"
2323
serde = "1.0"
24+
serde_json = "1.0.140"
2425
serde_yaml = "0.9"
2526
shell-words = "1.1"
2627
tempfile = "3.20"

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ In order to connect to OBS, three variables must be set (generally within the
8282
```bash
8383
dput PROJECT DSC_FILE
8484
[--branch-to BRANCHED_PROJECT]
85-
[--build-info-out BUILD_INFO_FILE=build-info.yml]
85+
[--build-info-out BUILD_INFO_FILE=build-info.json]
8686
[--rebuild-if-unchanged]
8787
```
8888

@@ -92,7 +92,7 @@ within, will be removed.
9292

9393
Metadata information on the uploaded revision, such as the revision number,
9494
project name, and package name, will be saved into the file specified by
95-
`--build-info-out` (default is `build-info.yml`). This file is **required** by
95+
`--build-info-out` (default is `build-info.json`). This file is **required** by
9696
the `generate-monitor` and `prune` steps. Do note that, if `--branch-to` is
9797
given, the file will be written *immediately* after the branch takes place (i.e.
9898
before the upload); that way, if the upload fails, the branched project can still
@@ -109,7 +109,7 @@ testing builds on MRs; you can create an OBS branch named after the MR's Git
109109
branch, and then builds can take place there without interfering with your main
110110
projects.
111111
112-
##### `--build-info-out BUILD_INFO_FILE=build-info.yml`
112+
##### `--build-info-out BUILD_INFO_FILE=build-info.json`
113113
114114
Changes the filename that the build info will be written to.
115115
@@ -130,7 +130,7 @@ operation, there will *always* be a change to upload.
130130
generate-monitor RUNNER_TAG
131131
[--rules RULES]
132132
[--download-build-results-to BUILD_RESULTS_DIR]
133-
[--build-info BUILD_INFO_FILE=build-info.yml]
133+
[--build-info BUILD_INFO_FILE=build-info.json]
134134
[--pipeline-out PIPELINE_FILE=obs.yml]
135135
[--job-prefix MONITOR_JOB_PREFIX=obs]
136136
[--job-timeout MONITOR_JOB_TIMEOUT]
@@ -199,7 +199,7 @@ dput-and-generate:
199199
After a monitoring job completes, download the build results from OBS to the
200200
given `BUILD_RESULTS_DIR`, and upload it as a GitLab build artifact..
201201

202-
##### `--build-info BUILD_INFO_FILE=build-info.yml`
202+
##### `--build-info BUILD_INFO_FILE=build-info.json`
203203

204204
Specifies the name of the build info file to read. In particular, if a different
205205
build info filename was used with `dput` via
@@ -233,7 +233,7 @@ Changes the filename each monitoring job will save the build log into.
233233

234234
```bash
235235
prune
236-
[--build-info BUILD_INFO_FILE=build-info.yml]
236+
[--build-info BUILD_INFO_FILE=build-info.json]
237237
[--ignore-missing-build-info]
238238
[--only-if-job-unsuccessful]
239239
```
@@ -242,7 +242,7 @@ If a branch occurred, deletes the branched package and, if now empty, project,
242242
using the information from the build info file. (If no branching occurred, this
243243
does nothing.)
244244

245-
##### `--build-info BUILD_INFO_FILE=build-info.yml`
245+
##### `--build-info BUILD_INFO_FILE=build-info.json`
246246

247247
Specifies the name of the build info file to read. In particular, if a different
248248
build info filename was used with `dput` via

obs-commander-tests/src/lib.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,8 @@ pub async fn test_dput<C: TestContext>(
314314

315315
let arch_1 = build_info
316316
.enabled_repos
317-
.get(&RepoArch {
318-
repo: TEST_REPO.to_owned(),
319-
arch: TEST_ARCH_1.to_owned(),
320-
})
317+
.iter()
318+
.find(|e| e.repo_arch.repo == TEST_REPO && e.repo_arch.arch == TEST_ARCH_1)
321319
.unwrap();
322320

323321
if test == DputTest::Rebuild {
@@ -327,10 +325,8 @@ pub async fn test_dput<C: TestContext>(
327325

328326
let arch_2 = build_info
329327
.enabled_repos
330-
.get(&RepoArch {
331-
repo: TEST_REPO.to_owned(),
332-
arch: TEST_ARCH_2.to_owned(),
333-
})
328+
.iter()
329+
.find(|e| e.repo_arch.repo == TEST_REPO && e.repo_arch.arch == TEST_ARCH_2)
334330
.unwrap();
335331
assert_none!(arch_2.prev_endtime_for_commit);
336332
}

obs-commander/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ open-build-service-api.workspace = true
1919
reqwest = "0.12"
2020
rfc822-like = "0.2"
2121
serde.workspace = true
22-
serde_yaml.workspace = true
22+
serde_json.workspace = true
2323
shell-words.workspace = true
2424
tempfile.workspace = true
2525
thiserror.workspace = true

obs-commander/src/actions.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, io::SeekFrom};
1+
use std::io::SeekFrom;
22

33
use camino::{Utf8Path, Utf8PathBuf};
44
use clap::{ArgAction, Parser};
@@ -11,17 +11,14 @@ use tracing::{debug, info, instrument};
1111
use crate::{
1212
artifacts::{ArtifactDirectory, ArtifactReader, ArtifactWriter, MissingArtifactToNone},
1313
binaries::download_binaries,
14-
build_meta::{
15-
BuildHistoryRetrieval, BuildMeta, BuildMetaOptions, CommitBuildInfo, DisabledRepos,
16-
RepoArch,
17-
},
14+
build_meta::{BuildHistoryRetrieval, BuildMeta, BuildMetaOptions, DisabledRepos, EnabledRepo},
1815
monitor::{MonitoredPackage, ObsMonitor, PackageCompletion, PackageMonitoringOptions},
1916
prune::prune_branch,
2017
retry_request,
2118
upload::ObsDscUploader,
2219
};
2320

24-
pub const DEFAULT_BUILD_INFO: &str = "build-info.yml";
21+
pub const DEFAULT_BUILD_INFO: &str = "build-info.json";
2522
pub const DEFAULT_BUILD_LOG: &str = "build.log";
2623

2724
// Our flags can all take explicit values, because it makes it easier to
@@ -154,7 +151,7 @@ pub struct ObsBuildInfo {
154151
pub rev: Option<String>,
155152
pub srcmd5: Option<String>,
156153
pub is_branched: bool,
157-
pub enabled_repos: HashMap<RepoArch, CommitBuildInfo>,
154+
pub enabled_repos: Vec<EnabledRepo>,
158155
}
159156

160157
impl ObsBuildInfo {
@@ -163,7 +160,7 @@ impl ObsBuildInfo {
163160
artifacts
164161
.save_with(path, async |file: &mut ArtifactWriter| {
165162
let data =
166-
serde_yaml::to_string(&self).wrap_err("Failed to serialize build info")?;
163+
serde_json::to_string(&self).wrap_err("Failed to serialize build info")?;
167164
file.write_all(data.as_bytes())
168165
.await
169166
.wrap_err("Failed to write build info file")?;
@@ -216,7 +213,7 @@ impl Actions {
216213
rev: None,
217214
srcmd5: None,
218215
is_branched,
219-
enabled_repos: HashMap::new(),
216+
enabled_repos: vec![],
220217
};
221218
debug!("Saving initial build info: {:?}", build_info);
222219
build_info
@@ -413,7 +410,7 @@ impl Actions {
413410
artifacts.read_string(&args.build_info).await?
414411
};
415412

416-
let build_info: ObsBuildInfo = serde_yaml::from_str(&build_info_data)
413+
let build_info: ObsBuildInfo = serde_json::from_str(&build_info_data)
417414
.wrap_err("Failed to parse provided build info file")?;
418415

419416
if build_info.is_branched {

obs-commander/src/build_meta.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub struct RepoArch {
1414
}
1515

1616
#[derive(Deserialize, Serialize, Debug, Clone)]
17-
pub struct CommitBuildInfo {
17+
pub struct EnabledRepo {
18+
#[serde(flatten)]
19+
pub repo_arch: RepoArch,
1820
pub prev_endtime_for_commit: Option<u64>,
1921
}
2022

@@ -237,26 +239,19 @@ impl BuildMeta {
237239
Ok(())
238240
}
239241

240-
pub fn get_commit_build_info(&self, srcmd5: &str) -> HashMap<RepoArch, CommitBuildInfo> {
241-
let mut repos = HashMap::new();
242-
243-
for (repo, jobhist) in &self.repos {
244-
let prev_endtime_for_commit = jobhist
245-
.jobhist
246-
.iter()
247-
.filter(|e| e.srcmd5 == srcmd5)
248-
.next_back()
249-
.map(|e| e.endtime);
250-
251-
repos.insert(
252-
repo.clone(),
253-
CommitBuildInfo {
254-
prev_endtime_for_commit,
255-
},
256-
);
257-
}
258-
259-
repos
242+
pub fn get_commit_build_info(&self, srcmd5: &str) -> Vec<EnabledRepo> {
243+
self.repos
244+
.iter()
245+
.map(|(repo, jobhist)| EnabledRepo {
246+
repo_arch: repo.clone(),
247+
prev_endtime_for_commit: jobhist
248+
.jobhist
249+
.iter()
250+
.filter(|e| e.srcmd5 == srcmd5)
251+
.next_back()
252+
.map(|e| e.endtime),
253+
})
254+
.collect()
260255
}
261256
}
262257

@@ -360,10 +355,10 @@ mod tests {
360355

361356
let build_info = meta.get_commit_build_info(&srcmd5_1);
362357

363-
let arch_1 = assert_some!(build_info.get(&repo_arch_1));
358+
let arch_1 = assert_some!(build_info.iter().find(|e| e.repo_arch == repo_arch_1));
364359
assert_some_eq!(arch_1.prev_endtime_for_commit, endtime_1);
365360

366-
let arch_2 = assert_some!(build_info.get(&repo_arch_2));
361+
let arch_2 = assert_some!(build_info.iter().find(|e| e.repo_arch == repo_arch_2));
367362
assert_none!(arch_2.prev_endtime_for_commit);
368363

369364
let meta = assert_ok!(
@@ -385,9 +380,9 @@ mod tests {
385380

386381
let build_info = meta.get_commit_build_info(&srcmd5_1);
387382

388-
let arch_1 = assert_some!(build_info.get(&repo_arch_1));
383+
let arch_1 = assert_some!(build_info.iter().find(|e| e.repo_arch == repo_arch_1));
389384
assert_none!(arch_1.prev_endtime_for_commit);
390-
let arch_2 = assert_some!(build_info.get(&repo_arch_2));
385+
let arch_2 = assert_some!(build_info.iter().find(|e| e.repo_arch == repo_arch_2));
391386
assert_none!(arch_2.prev_endtime_for_commit);
392387

393388
assert!(meta.repos.contains_key(&repo_arch_2));
@@ -431,7 +426,7 @@ mod tests {
431426

432427
let build_info = meta.get_commit_build_info(&srcmd5_2);
433428

434-
let arch_1 = assert_some!(build_info.get(&repo_arch_2));
429+
let arch_1 = assert_some!(build_info.iter().find(|e| e.repo_arch == repo_arch_2));
435430
assert_some_eq!(arch_1.prev_endtime_for_commit, endtime_2);
436431

437432
mock.add_job_history(
@@ -465,13 +460,13 @@ mod tests {
465460

466461
let build_info = meta.get_commit_build_info(&srcmd5_1);
467462

468-
let arch_2 = assert_some!(build_info.get(&repo_arch_2));
463+
let arch_2 = assert_some!(build_info.iter().find(|e| e.repo_arch == repo_arch_2));
469464
assert_some_eq!(arch_2.prev_endtime_for_commit, endtime_1);
470465

471466
meta.clear_stored_history();
472467

473468
let build_info = meta.get_commit_build_info(&srcmd5_1);
474-
let arch_2 = assert_some!(build_info.get(&repo_arch_2));
469+
let arch_2 = assert_some!(build_info.iter().find(|e| e.repo_arch == repo_arch_2));
475470
assert_none!(arch_2.prev_endtime_for_commit);
476471

477472
mock.set_package_build_status(

obs-gitlab-runner/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ obs-commander = { path = "../obs-commander" }
1919
obs-commander-test-support = { path = "../obs-commander-test-support" }
2020
open-build-service-api.workspace = true
2121
serde.workspace = true
22+
serde_json.workspace = true
2223
serde_yaml.workspace = true
2324
shellexpand = "3.1"
2425
shell-words.workspace = true

obs-gitlab-runner/src/handler.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl ObsJobHandler {
227227
};
228228

229229
let build_info_data = artifacts.read_string(&args.build_info).await?;
230-
let build_info: ObsBuildInfo = serde_yaml::from_str(&build_info_data)
230+
let build_info: ObsBuildInfo = serde_json::from_str(&build_info_data)
231231
.wrap_err("Failed to parse provided build info file")?;
232232

233233
let pipeline = generate_monitor_pipeline(
@@ -447,7 +447,7 @@ mod tests {
447447
use claims::*;
448448
use gitlab_runner::{GitlabLayer, Runner, RunnerBuilder};
449449
use gitlab_runner_mock::*;
450-
use obs_commander::build_meta::{CommitBuildInfo, RepoArch};
450+
use obs_commander::build_meta::{EnabledRepo, RepoArch};
451451
use obs_commander_test_support::*;
452452
use obs_commander_tests::*;
453453
use rstest::rstest;
@@ -867,10 +867,10 @@ mod tests {
867867

868868
assert_eq!(pipeline_map.len(), build_info.enabled_repos.len());
869869

870-
for repo in build_info.enabled_repos.keys() {
870+
for enabled in &build_info.enabled_repos {
871871
let monitor_job_name = format!(
872872
"{}-{}-{}",
873-
DEFAULT_PIPELINE_JOB_PREFIX, TEST_REPO, &repo.arch
873+
DEFAULT_PIPELINE_JOB_PREFIX, TEST_REPO, &enabled.repo_arch.arch
874874
);
875875

876876
let monitor_map = pipeline_yaml
@@ -944,7 +944,7 @@ mod tests {
944944
context,
945945
dput.clone(),
946946
build_info,
947-
repo,
947+
&enabled.repo_arch,
948948
&script,
949949
success,
950950
dput_test,
@@ -1129,23 +1129,21 @@ mod tests {
11291129
rev: Some("1".to_owned()),
11301130
srcmd5: Some("abc".to_owned()),
11311131
is_branched: false,
1132-
enabled_repos: [(
1133-
RepoArch {
1132+
enabled_repos: vec![EnabledRepo {
1133+
repo_arch: RepoArch {
11341134
repo: TEST_REPO.to_owned(),
11351135
arch: TEST_ARCH_1.to_owned(),
11361136
},
1137-
CommitBuildInfo {
1138-
prev_endtime_for_commit: None,
1139-
},
1140-
)]
1141-
.into(),
1137+
1138+
prev_endtime_for_commit: None,
1139+
}],
11421140
};
11431141

11441142
let build_info = context
11451143
.inject_artifacts(
11461144
[(
11471145
DEFAULT_BUILD_INFO.to_owned(),
1148-
serde_yaml::to_string(&build_info).unwrap().into_bytes(),
1146+
serde_json::to_string(&build_info).unwrap().into_bytes(),
11491147
)]
11501148
.into(),
11511149
)

0 commit comments

Comments
 (0)