1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::collections::BTreeMap;
use std::path::{Component, Path, PathBuf};
use anyhow::{Context, Result, ensure};
use ctl_core::input::{FieldPath, Input};
use garde::Validate;
use serde::Deserialize;
use crate::publisher_spec::PublisherSpec;
mod artifact_table;
mod assets;
mod deserialize;
mod package;
mod sections;
mod validate;
mod validate_assets;
pub use artifact_table::ArtifactTable;
pub use assets::{
AssetFormat, AssetTarget, Assets, Attest, Generated, Job, Packslip, Runner, Vendored,
VendoredTarget, read_packslip_manifest,
};
pub use package::{
CommandField, Current, DriverSpec, PackageSpec, Prerelease, Publish, PublishOptions,
stock_driver,
};
#[cfg(test)]
use sections::ARITY;
pub use sections::{
Changelog, DIR, FILE, NAME_PLACEHOLDER, Occurrences, PLACEHOLDER, Pin, PinPattern, Prepare,
Proposal, ReleaseCheck, Tags, Templates, root_of,
};
use validate::{
a_marker_names_no_driver, a_prerelease_is_lowercase_letters, a_workflow_file_name,
each_release_check_once, every_pattern_bounds_the_version, every_pattern_is_listed,
every_pattern_says_where_the_version_goes, no_unknown_package_keys, no_version_label,
none_is_not_a_publisher, owners_are_logins, package_owners_are_logins,
publish_options_fit_the_publisher, python_names_its_publisher, release_only_names_no_registry,
};
use validate_assets::{
every_key_is_one_plain_name, every_tarball_is_its_own, packslip_needs_assets,
something_to_release,
};
/// The whole file, and the context every rule in it is checked against. A
/// pattern is only unused, and an id only unknown, relative to the rest of the
/// config. `Config::parse` reads the shape with serde and then validates it
/// once, here, so a complaint names the field that has to change.
///
/// A section verctl does not know stops the load instead of being dropped.
/// Ignoring `pattern:` for `patterns:` would ship a release with the pins it
/// declares never rewritten.
#[derive(Debug, Clone, Deserialize, Default, Validate)]
#[serde(deny_unknown_fields)]
#[garde(context(Self as config))]
#[garde(allow_unvalidated)]
#[non_exhaustive]
pub struct Config {
/// Custom drivers by name. A stock name (`cargo`, `npm`, `pyproject`,
/// `go`, `version`) is overridden field by field.
#[serde(default)]
pub drivers: BTreeMap<String, DriverSpec>,
/// What this repo releases. Omit only when the repo declares `vendored`;
/// a config with neither has nothing to do.
#[serde(default)]
#[garde(
custom(something_to_release),
custom(no_unknown_package_keys),
custom(release_only_names_no_registry),
custom(publish_options_fit_the_publisher),
custom(python_names_its_publisher),
custom(a_marker_names_no_driver),
custom(a_prerelease_is_lowercase_letters),
custom(package_owners_are_logins)
)]
pub packages: Vec<PackageSpec>,
/// The crates.io logins every package's crates must list as owners, which
/// `check registry` proves. A package's own `owners` replaces it. When
/// omitted here and on a package, that package has no owner proof.
#[serde(default)]
#[garde(inner(custom(owners_are_logins)))]
pub owners: Option<Vec<String>>,
/// Whether crates.io crates must disable API-token publishing
/// (`trustpub_only = true`). Defaults to `true`; set `false` here or on a
/// package to allow API-token publishes.
#[serde(default = "Config::default_trusted_publishing_only")]
pub trusted_publishing_only: bool,
/// The file under `.github/workflows/` whose run publishes, which a
/// crates.io trusted publisher entry must name for `check trust` to pass.
/// crates.io matches the entry against the calling workflow that the OIDC
/// token's `workflow_ref` claim names
/// (`crates_io_trustpub/src/github/workflows.rs`), so a reusable workflow
/// it calls does not count. When omitted, it is `release.yml`, the caller
/// file the template renders.
#[serde(default = "Config::default_release_workflow")]
#[garde(custom(a_workflow_file_name))]
pub release_workflow: String,
/// The checks `release` and `release --dry-run` run before the first
/// write, from `registry` and `trust`. Only registry state can change
/// between the merge and the release, so the checks that prove anything
/// else run before the merge and are not listed here. `trust` runs only
/// when `VERCTL_OWNER_TOKEN` contains an owner's token, and is reported as
/// skipped otherwise. When omitted, it is `[registry, trust]`; `[]` runs
/// no check.
#[serde(default = "ReleaseCheck::default_list")]
#[garde(custom(each_release_check_once))]
pub release_checks: Vec<ReleaseCheck>,
/// Publish recipes by name, for a package's `publisher` to select.
#[serde(default)]
#[garde(custom(none_is_not_a_publisher))]
pub publishers: BTreeMap<String, PublisherSpec>,
/// Machines, declared once and named. Entirely repo-owned: verctl ships no
/// built-in names.
#[serde(default)]
#[garde(dive)]
pub runners: BTreeMap<String, Runner>,
/// Release tarball recipe. Omit for libraries.
#[serde(default)]
#[garde(dive, custom(every_tarball_is_its_own))]
pub assets: Option<Assets>,
/// The packslip the Release carries. When it is set, the Release that
/// carries the tarballs also carries `packslip.sigstore.json`, signed by
/// the release workflow. `{}` signs a packslip that lists `assets.bin`
/// and attests the tarballs. Omit to publish no packslip.
#[serde(default)]
#[garde(dive, custom(packslip_needs_assets))]
pub packslip: Option<Packslip>,
/// Asset sets built from someone else's source at its own version, by
/// name. Omit when the repo vendors nothing.
#[serde(default)]
#[garde(custom(every_key_is_one_plain_name), dive)]
pub vendored: BTreeMap<String, Vendored>,
/// PR and push validation. Empty means one `verify` on `ubuntu-26.04`.
#[serde(default)]
pub ci: BTreeMap<String, Job>,
/// Work done after the version bumps.
#[serde(default)]
#[garde(custom(no_version_label))]
pub prepare: Prepare,
/// The Version PR's title and commit message. Omit for
/// `chore(release): version packages` as both.
#[serde(default)]
pub proposal: Proposal,
/// Whose names changelog entries never credit. Omit to list no one.
#[serde(default)]
pub changelog: Changelog,
/// Where served-file templates live. Omit for the convention.
#[serde(default)]
#[garde(dive)]
pub templates: Templates,
/// How release tags are named. Omit for `v{version}`: one tag when every
/// released package shares a version. Put `{name}` in the template for one
/// tag (and one Release) per package.
#[serde(default)]
#[garde(dive)]
pub tags: Tags,
/// Version spellings, named once and listed by the files that carry them,
/// so a spelling three files share is written once.
#[serde(default)]
#[garde(custom(every_pattern_says_where_the_version_goes))]
#[garde(custom(every_pattern_bounds_the_version))]
#[garde(custom(every_pattern_is_listed))]
pub patterns: BTreeMap<String, PinPattern>,
/// Collocated tool pins rewritten when `package` is bumped.
#[serde(default)]
#[garde(dive)]
pub pins: Vec<Pin>,
}
impl Config {
/// Read and validate the file at `path`.
///
/// This parses the schema and checks that any `packslip.manifest` parses
/// as TOML. Callers inside a git checkout should use
/// `verctl_release::forge::load_config` (or call
/// [`Config::verify_packslip_tracked`]) to also verify that
/// `packslip.manifest` is tracked by git.
///
/// # Errors
///
/// Returns an error when the file cannot be read, when its YAML does not
/// match the schema, or when validation fails. Each complaint names the
/// line it comes from.
pub fn load(path: &Path) -> Result<Self> {
let config = Self::from_input(Input::read(path)?)?;
config.packslip_manifest_is_sound(&root_of(path))?;
Ok(config)
}
/// The `packslip.manifest` must be a file git tracks, so the release
/// workflow, which checks out the commit, reads it there, and it must
/// parse as a TOML table. Only `load` checks: `parse` reads text in
/// memory and has no tree to check.
fn packslip_manifest_is_sound(&self, root: &Path) -> Result<()> {
let Some(manifest) = self
.packslip
.as_ref()
.and_then(|packslip| packslip.manifest.as_ref())
else {
return Ok(());
};
read_packslip_manifest(&root.join(manifest), manifest).map(drop)
}
/// Verify that `packslip.manifest`, when configured, is among the paths git
/// tracks.
///
/// # Errors
///
/// Returns an error naming `packslip.manifest` when it is not in `tracked`.
pub fn verify_packslip_tracked(&self, tracked: &[PathBuf]) -> Result<()> {
let Some(manifest) = self
.packslip
.as_ref()
.and_then(|packslip| packslip.manifest.as_ref())
else {
return Ok(());
};
let plain = |path: &Path| {
path.components()
.filter(|part| !matches!(part, Component::CurDir))
.collect::<PathBuf>()
};
let wanted = plain(manifest);
let is_tracked = tracked.iter().any(|known| plain(known) == wanted);
ensure!(
is_tracked,
"packslip.manifest: {} is not a file git tracks; commit it, or remove `packslip.manifest`",
manifest.display()
);
Ok(())
}
/// A config from text in memory, named as the file a repo writes.
///
/// # Errors
///
/// Returns an error when `raw` does not match the schema or fails
/// validation, as described for [`Config::load`].
pub fn parse(raw: &str) -> Result<Self> {
Self::from_input(Input::new(".ctl/ver.yaml", raw))
}
/// The one parse boundary. ctl-core's declared input reads the shape and
/// validates it once against the whole document, placing each complaint
/// on its line. A document is therefore read exactly one way.
///
/// `assets.targets` is the one flattened map whose entries garde dives
/// into, so it is declared here, and a target's problem names the path
/// the file contains.
fn from_input(input: Input) -> Result<Self> {
let input = input.flattened(FieldPath::root().key("assets").key("targets"));
let mut config: Self = input.parse()?;
input.check(&config, &config)?;
config.resolve_patterns();
Ok(config)
}
/// Turn every pin's `patterns` ids into the patterns they name, so no
/// consumer looks an id up again. Validation already proved every name
/// resolves, which is why this cannot fail.
fn resolve_patterns(&mut self) {
let declared = self.patterns.clone();
for pin in &mut self.pins {
pin.patterns = pin
.pattern_ids
.iter()
.filter_map(|id| declared.get(id).cloned())
.collect();
}
}
/// The `release_workflow` a file that sets none gets.
fn default_release_workflow() -> String {
"release.yml".to_owned()
}
/// The `trusted_publishing_only` default when omitted.
const fn default_trusted_publishing_only() -> bool {
true
}
/// Whether `package` enforces crates.io `trustpub_only`: its own override,
/// or else the top-level `trusted_publishing_only` setting (default
/// `true`).
#[must_use]
pub fn trusted_publishing_only_of(&self, package: &PackageSpec) -> bool {
package
.trusted_publishing_only
.unwrap_or(self.trusted_publishing_only)
}
/// The crates.io logins `package` must list as owners: its own `owners`,
/// or else the top-level list. `None` when neither is set, so the package
/// has no owner proof.
#[must_use]
pub fn owners_of<'a>(&'a self, package: &'a PackageSpec) -> Option<&'a [String]> {
package.owners.as_deref().or(self.owners.as_deref())
}
/// The package declared as `name`.
///
/// # Errors
///
/// Returns an error when no declared package has that name.
pub fn find(&self, name: &str) -> Result<&PackageSpec> {
self.packages
.iter()
.find(|package| package.name == name)
.with_context(|| format!("package {name:?} is not in verctl config"))
}
/// The spec for `name`: a stock driver, a declared one, or a declared one
/// over the stock.
#[must_use]
pub fn driver_spec(&self, name: &str) -> Option<DriverSpec> {
match (DriverSpec::stock(name), self.drivers.get(name)) {
(Some(stock), Some(over)) => Some(stock.merge(over.clone())),
(Some(stock), None) => Some(stock),
(None, Some(over)) => Some(over.clone()),
(None, None) => None,
}
}
}
#[cfg(test)]
mod tests;