Skip to content

Commit cf62693

Browse files
committed
boulder: Write a template monitoring.yaml file with boulder new
1 parent 53fe266 commit cf62693

File tree

7 files changed

+66
-19
lines changed

7 files changed

+66
-19
lines changed

boulder/src/draft.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::{io, path::PathBuf};
88
use fs_err as fs;
99
use itertools::Itertools;
1010
use moss::Dependency;
11+
use serde::Serialize;
12+
use std::io::Write;
1113
use thiserror::Error;
1214
use url::Url;
1315

@@ -20,6 +22,22 @@ mod build;
2022
mod metadata;
2123
mod upstream;
2224

25+
#[derive(Serialize)]
26+
struct Monitoring {
27+
releases: Releases,
28+
security: Security,
29+
}
30+
31+
#[derive(Serialize)]
32+
struct Releases {
33+
id: Option<u16>,
34+
}
35+
36+
#[derive(Serialize)]
37+
struct Security {
38+
cpe: Option<String>,
39+
}
40+
2341
pub struct Drafter {
2442
upstreams: Vec<Url>,
2543
}
@@ -29,6 +47,33 @@ impl Drafter {
2947
Self { upstreams }
3048
}
3149

50+
pub fn write_monitoring(&self) -> Result<String, Error> {
51+
let monitoring_template = Monitoring {
52+
releases: Releases { id: None },
53+
security: Security { cpe: None },
54+
};
55+
56+
let mut yaml_string = serde_yaml::to_string(&monitoring_template).expect("Failed to serialize to YAML");
57+
58+
let id_string = "id: null";
59+
let id_marker = yaml_string.find(id_string).expect("releases id marker not found");
60+
let id_help_text = " # https://release-monitoring.org/ and use the numeric id in the url of project";
61+
62+
// append_str at marker instead of insert
63+
yaml_string.insert_str(id_marker + id_string.len(), id_help_text);
64+
65+
let cpe_string = "cpe: null";
66+
let cpe_marker = yaml_string.find(cpe_string).expect("security cpe marker not found");
67+
let cpe_help_text = " # Use the cpesearch bash macro and insert [ { - vendor: foo, product: bar } ] if found";
68+
69+
yaml_string.insert_str(cpe_marker + cpe_string.len(), cpe_help_text);
70+
71+
let mut file = std::fs::File::create("monitoring.yaml")?;
72+
file.write_all(yaml_string.as_bytes())?;
73+
println!("Wrote template 'monitoring.yaml' file");
74+
Ok(yaml_string)
75+
}
76+
3277
pub fn run(&self) -> Result<String, Error> {
3378
// TODO: Use tempdir
3479
let extract_root = PathBuf::from("/tmp/boulder-new");
@@ -42,7 +87,7 @@ impl Drafter {
4287
// Enumerate all extracted files
4388
let files = util::enumerate_files(&extract_root, |_| true)?
4489
.into_iter()
45-
.map(|path| File {
90+
.map(|path| DrafterFile {
4691
path,
4792
extract_root: &extract_root,
4893
})
@@ -90,6 +135,8 @@ license : UPDATE LICENSE
90135
metadata.upstreams(),
91136
);
92137

138+
self.write_monitoring()?;
139+
93140
Ok(template)
94141
}
95142
}
@@ -104,12 +151,12 @@ fn builddeps(deps: impl IntoIterator<Item = Dependency>) -> String {
104151
}
105152
}
106153

107-
pub struct File<'a> {
154+
pub struct DrafterFile<'a> {
108155
pub path: PathBuf,
109156
pub extract_root: &'a Path,
110157
}
111158

112-
impl File<'_> {
159+
impl DrafterFile<'_> {
113160
// The depth of a file relative to it's extracted archive
114161
pub fn depth(&self) -> usize {
115162
let relative = self.path.strip_prefix(self.extract_root).unwrap_or(&self.path);
@@ -145,7 +192,7 @@ mod test {
145192
fn test_file_depth() {
146193
let extract_root = Path::new("/tmp/test");
147194

148-
let file = File {
195+
let file = DrafterFile {
149196
path: PathBuf::from("/tmp/test/some_archive/meson.build"),
150197
extract_root,
151198
};

boulder/src/draft/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{fmt, num::NonZeroU64};
66

77
use moss::Dependency;
88

9-
use super::File;
9+
use super::DrafterFile;
1010

1111
mod autotools;
1212
mod cargo;
@@ -60,7 +60,7 @@ impl System {
6060
}
6161
}
6262

63-
fn process(&self, state: &mut State, file: &File) -> Result<(), Error> {
63+
fn process(&self, state: &mut State, file: &DrafterFile) -> Result<(), Error> {
6464
match self {
6565
System::Autotools => autotools::process(state, file),
6666
System::Cargo => cargo::process(state, file),
@@ -126,7 +126,7 @@ pub struct Analysis {
126126

127127
/// Analyze the provided paths to determine which build [`System`]
128128
/// the project uses and any dependencies that are identified
129-
pub fn analyze(files: &[File]) -> Result<Analysis, Error> {
129+
pub fn analyze(files: &[DrafterFile]) -> Result<Analysis, Error> {
130130
let mut dependencies = BTreeSet::new();
131131
let mut confidences = BTreeMap::new();
132132

boulder/src/draft/build/autotools.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use regex::Regex;
88
//
99
// SPDX-License-Identifier: MPL-2.0
1010
use crate::draft::build::{Error, Phases, State};
11-
use crate::draft::File;
11+
use crate::draft::DrafterFile;
1212

1313
pub fn phases() -> Phases {
1414
Phases {
@@ -19,7 +19,7 @@ pub fn phases() -> Phases {
1919
}
2020
}
2121

22-
pub fn process(state: &mut State, file: &File) -> Result<(), Error> {
22+
pub fn process(state: &mut State, file: &DrafterFile) -> Result<(), Error> {
2323
// Depth too great
2424
if file.depth() > 0 {
2525
return Ok(());

boulder/src/draft/build/cargo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44
use crate::draft::build::{Error, Phases, State};
5-
use crate::draft::File;
5+
use crate::draft::DrafterFile;
66

77
pub fn phases() -> Phases {
88
Phases {
@@ -13,7 +13,7 @@ pub fn phases() -> Phases {
1313
}
1414
}
1515

16-
pub fn process(state: &mut State, file: &File) -> Result<(), Error> {
16+
pub fn process(state: &mut State, file: &DrafterFile) -> Result<(), Error> {
1717
if file.file_name() == "Cargo.toml" {
1818
state.increment_confidence(100);
1919
}

boulder/src/draft/build/cmake.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: MPL-2.0
44

55
use crate::draft::build::{Error, Phases, State};
6-
use crate::draft::File;
6+
use crate::draft::DrafterFile;
77

88
pub fn phases() -> Phases {
99
Phases {
@@ -14,7 +14,7 @@ pub fn phases() -> Phases {
1414
}
1515
}
1616

17-
pub fn process(state: &mut State, file: &File) -> Result<(), Error> {
17+
pub fn process(state: &mut State, file: &DrafterFile) -> Result<(), Error> {
1818
// Depth too great
1919
if file.depth() > 0 {
2020
return Ok(());

boulder/src/draft/build/meson.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use moss::{dependency, Dependency};
88
use regex::Regex;
99

1010
use crate::draft::build::{Error, Phases, State};
11-
use crate::draft::File;
11+
use crate::draft::DrafterFile;
1212

1313
pub fn phases() -> Phases {
1414
Phases {
@@ -19,7 +19,7 @@ pub fn phases() -> Phases {
1919
}
2020
}
2121

22-
pub fn process(state: &mut State, file: &File) -> Result<(), Error> {
22+
pub fn process(state: &mut State, file: &DrafterFile) -> Result<(), Error> {
2323
match file.file_name() {
2424
"meson.build" if file.depth() == 0 => {
2525
state.increment_confidence(100);

boulder/src/draft/build/python.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
pub mod pep517 {
66
use crate::draft::build::{Error, Phases, State};
7-
use crate::draft::File;
7+
use crate::draft::DrafterFile;
88

99
pub fn phases() -> Phases {
1010
Phases {
@@ -15,7 +15,7 @@ pub mod pep517 {
1515
}
1616
}
1717

18-
pub fn process(state: &mut State, file: &File) -> Result<(), Error> {
18+
pub fn process(state: &mut State, file: &DrafterFile) -> Result<(), Error> {
1919
match file.file_name() {
2020
"pyproject.toml" | "setup.cfg" => state.increment_confidence(100),
2121
_ => {}
@@ -27,7 +27,7 @@ pub mod pep517 {
2727

2828
pub mod setup_tools {
2929
use crate::draft::build::{Error, Phases, State};
30-
use crate::draft::File;
30+
use crate::draft::DrafterFile;
3131

3232
pub fn phases() -> Phases {
3333
Phases {
@@ -38,7 +38,7 @@ pub mod setup_tools {
3838
}
3939
}
4040

41-
pub fn process(state: &mut State, file: &File) -> Result<(), Error> {
41+
pub fn process(state: &mut State, file: &DrafterFile) -> Result<(), Error> {
4242
if file.file_name() == "setup.py" {
4343
state.increment_confidence(100);
4444
}

0 commit comments

Comments
 (0)