release-kit 0.8.6

A canonical release workflow: a technology-agnostic method, per-technology bindings, and the rk CLI that lands and serves them.
Documentation
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! The setup step table, defined by what each step proves and by the
//! target configuration it applies to.
//!
//! Steps follow `method/02-setup.md`, with reporting intake after the
//! protection inventory. Each step carries a pure predicate over the
//! resolved configuration: a forge step applies only where an adapter
//! exists, the release half applies only to an automatic release whose
//! automation this release carries, and a local step applies wherever its
//! own prerequisites hold. A step that does not apply reports as such
//! with the profile value that decided it, and carries no operator
//! reason; an exclusion is the operator's own statement about a step that
//! does apply.
//!
//! SATISFIES forge-setup:applicability-follows-the-target-configuration

use crate::detect::Forge;

use super::context::Ctx;

/// What a step touches when it applies.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mutates {
    /// The step only reads and reports.
    Nothing,
    /// The step writes forge configuration.
    Forge,
    /// The step writes local repository state in the target.
    Local,
}

/// Why a step does not apply to a target, or `None` where it does.
///
/// The predicate is pure over the resolved configuration: it reads no
/// forge, no disk, and no environment, so `rk setup --list`, a preview, a
/// check, and an apply all answer alike.
pub type Applies = fn(&Ctx) -> Option<String>;

/// Every step applies wherever the run stands: the local ones.
const fn always(_: &Ctx) -> Option<String> {
    None
}

/// A step that calls the forge applies only where the profile names one
/// this release drives.
fn needs_adapter(ctx: &Ctx) -> Option<String> {
    if ctx.has_adapter() {
        return None;
    }
    Some(ctx.declared_forge().map_or_else(
        || "the profile names no forge".to_owned(),
        |name| format!("the profile names the forge {name}, which this release has no adapter for"),
    ))
}

/// A step that serves the release automation applies only to an automatic
/// release at a forge this release drives.
fn needs_release(ctx: &Ctx) -> Option<String> {
    needs_adapter(ctx).or_else(|| {
        (!ctx.automatic_release()).then(|| {
            format!(
                "profile.release.mode is {}, so no release automation is selected",
                ctx.profile.release.mode.as_str()
            )
        })
    })
}

/// The packaging gate reads the release driver's own command, so it
/// applies where an automatic release names one.
fn needs_driver(ctx: &Ctx) -> Option<String> {
    if !ctx.automatic_release() {
        return Some(format!(
            "profile.release.mode is {}, so no package is published from here",
            ctx.profile.release.mode.as_str()
        ));
    }
    ctx.driver().is_none().then(|| {
        "profile.release.driver names no technology, so no packaging command is known".to_owned()
    })
}

/// The private reporting channel pairs with the landed policy, so it
/// applies where the target requested one at a forge this release drives.
fn needs_reporting_policy(ctx: &Ctx) -> Option<String> {
    needs_adapter(ctx).or_else(|| {
        (!ctx.reporting_policy()).then(|| "capabilities.reporting_policy is false".to_owned())
    })
}

/// One step of the setup, in canonical order.
pub struct StepSpec {
    /// The name, which is also the `rk setup step` argument and the script
    /// file name in every forge tree.
    pub name: &'static str,
    /// The `method/02-setup.md` section the step executes.
    pub chapter: &'static str,
    /// What the step touches under apply.
    pub mutates: Mutates,
    /// What the step proves, from the chapter.
    pub proves: &'static str,
    /// Whether the step deletes anything; a destructive step carries its own
    /// refusal beyond `--apply`.
    pub destructive: bool,
    /// Whether a full run skips the step: an optional step applies only
    /// where its condition holds, by name, through `rk setup step`.
    pub optional: bool,
    /// The forges at which the step reaches the forge through its CLI.
    ///
    /// Empty where the step is local work alone. A forge absent from the
    /// list is one this step answers without a call, which `forge-version`
    /// does on GitHub: a rolling service declares no version floor, so
    /// there is nothing to read. A run that reaches the CLI for a step
    /// that never calls it refuses an operator who has no reason to need
    /// one.
    pub forge_cli: &'static [Forge],
    /// Steps that must be observed satisfied before this one applies.
    pub prereqs: &'static [&'static str],
    /// Why this step does not apply to a target, or `None` where it does.
    pub applies: Applies,
}

impl std::fmt::Debug for StepSpec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StepSpec")
            .field("name", &self.name)
            .field("chapter", &self.chapter)
            .field("mutates", &self.mutates)
            .field("destructive", &self.destructive)
            .field("optional", &self.optional)
            .field("prereqs", &self.prereqs)
            .finish_non_exhaustive()
    }
}

/// The fifteen steps, with reporting intake last.
///
/// `package-check`, `branch-reminder`, and `forge-version` belong to no
/// forge tree โ€” the first reads its command from the technology binding,
/// the second writes an embedded hook body into the target's own git
/// directory, and the third is one read-only API call the observer already
/// makes โ€” which makes them the three steps outside the parity rule.
pub const STEPS: [StepSpec; 15] = [
    StepSpec {
        name: "package-check",
        chapter: "ยง0",
        mutates: Mutates::Nothing,
        proves: "the package is publishable with no credentials, and carries the reporting policy and none of release-kit's own files where the binding can list it",
        destructive: false,
        optional: false,
        forge_cli: &[],
        prereqs: &[],
        applies: needs_driver,
    },
    StepSpec {
        name: "default-branch",
        chapter: "ยง1",
        mutates: Mutates::Forge,
        proves: "the trunk is the default branch",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_adapter,
    },
    StepSpec {
        name: "single-trunk",
        chapter: "ยง1",
        mutates: Mutates::Forge,
        proves: "no long-lived branch besides the trunk remains",
        destructive: true,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &["default-branch"],
        applies: needs_adapter,
    },
    StepSpec {
        name: "merge-cleanup",
        chapter: "ยง1",
        mutates: Mutates::Forge,
        proves: "the forge deletes a branch when its merge lands",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &["default-branch"],
        applies: needs_adapter,
    },
    StepSpec {
        name: "branch-reminder",
        chapter: "ยง1",
        mutates: Mutates::Local,
        proves: "a pull reminds the operator when a merged branch lingers locally",
        destructive: false,
        optional: false,
        forge_cli: &[],
        prereqs: &[],
        applies: always,
    },
    StepSpec {
        name: "ci-permissions",
        chapter: "ยง2",
        mutates: Mutates::Forge,
        proves: "CI may write and open requests",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_release,
    },
    StepSpec {
        name: "install-bot",
        chapter: "ยง2",
        mutates: Mutates::Forge,
        proves: "the bot identity can act on this project",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_release,
    },
    StepSpec {
        name: "bot-secrets",
        chapter: "ยง2",
        mutates: Mutates::Forge,
        proves: "the bot credentials are stored on the project",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_release,
    },
    StepSpec {
        name: "forge-version",
        chapter: "ยง3",
        mutates: Mutates::Nothing,
        proves: "the forge meets the convention's minimum version",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Gitlab],
        prereqs: &[],
        applies: needs_adapter,
    },
    StepSpec {
        name: "protect-trunk",
        chapter: "ยง3",
        mutates: Mutates::Forge,
        proves: "the trunk takes no direct push, merges only by squash with the request's title and body as the message, and requires the named check",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &["default-branch", "forge-version"],
        applies: needs_adapter,
    },
    StepSpec {
        name: "protect-tags",
        chapter: "ยง3",
        mutates: Mutates::Forge,
        proves: "v* is protected as far as the forge allows",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_adapter,
    },
    StepSpec {
        name: "protect-release-lines",
        chapter: "ยง3",
        mutates: Mutates::Forge,
        proves: "release/* cannot be force-pushed or deleted",
        destructive: false,
        optional: true,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_release,
    },
    StepSpec {
        name: "auto-merge",
        chapter: "ยง3",
        mutates: Mutates::Forge,
        proves: "a request may merge itself once its required checks pass",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &["default-branch"],
        applies: needs_release,
    },
    StepSpec {
        name: "protections-check",
        chapter: "ยง3",
        mutates: Mutates::Nothing,
        proves: "exactly the owned protections, with those rules",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_adapter,
    },
    StepSpec {
        name: "private-vulnerability-reporting",
        chapter: "ยง3",
        mutates: Mutates::Forge,
        proves: "a vulnerability report has a private intake path, with the forge's limits named",
        destructive: false,
        optional: false,
        forge_cli: &[Forge::Github, Forge::Gitlab],
        prereqs: &[],
        applies: needs_reporting_policy,
    },
];

/// Look one step up by name.
#[must_use]
pub fn spec(name: &str) -> Option<&'static StepSpec> {
    STEPS.iter().find(|step| step.name == name)
}

#[cfg(test)]
mod tests {
    use super::{Ctx, STEPS, spec};
    use crate::profile::ReleaseMode;

    /// One resolved configuration to judge the table against: the forge
    /// the profile declares, whether this release has an adapter for it,
    /// and the release mode.
    fn ctx(forge: Option<&str>, mode: ReleaseMode) -> Ctx {
        let mut ctx = Ctx::for_tests(
            camino::Utf8PathBuf::from("/tmp/target"),
            "acme/widget".to_owned(),
            crate::detect::Forge::Github,
            std::path::PathBuf::from("gh"),
            Some("rust"),
        );
        ctx.declared_forge = forge.map(str::to_owned);
        ctx.forge = forge.and_then(crate::detect::Forge::parse);
        ctx.profile.forge = ctx.declared_forge.clone();
        ctx.profile.release.mode = mode;
        if mode != ReleaseMode::Automatic {
            ctx.profile.release.driver = None;
            ctx.profile.release.style = None;
            ctx.profile.release.line_prefix = None;
        }
        ctx
    }

    /// The steps one configuration applies, in table order.
    fn applicable(forge: Option<&str>, mode: ReleaseMode) -> Vec<&'static str> {
        let ctx = ctx(forge, mode);
        STEPS
            .iter()
            .filter(|step| (step.applies)(&ctx).is_none())
            .map(|step| step.name)
            .collect()
    }

    /// SATISFIES forge-setup:applicability-follows-the-target-configuration
    /// The matrix across an absent forge, an unknown one, and each driven
    /// one, at each release mode.
    #[test]
    fn the_applicability_matrix_follows_the_profile() {
        // No forge: the local step alone.
        for mode in [
            ReleaseMode::Automatic,
            ReleaseMode::External,
            ReleaseMode::None,
        ] {
            let steps = applicable(None, mode);
            assert_eq!(
                steps,
                if mode == ReleaseMode::Automatic {
                    vec!["package-check", "branch-reminder"]
                } else {
                    vec!["branch-reminder"]
                },
                "no forge, {mode:?}"
            );
        }
        // A forge this release has no adapter for reads like no forge.
        assert_eq!(
            applicable(Some("codeberg"), ReleaseMode::None),
            ["branch-reminder"]
        );

        for forge in ["github", "gitlab"] {
            let automatic = applicable(Some(forge), ReleaseMode::Automatic);
            for step in STEPS.iter().map(|step| step.name) {
                assert!(automatic.contains(&step), "{forge} automatic: {step}");
            }
            // A release-less target keeps the trunk half and the tag
            // protection, and takes none of the release half.
            for mode in [ReleaseMode::External, ReleaseMode::None] {
                let steps = applicable(Some(forge), mode);
                for step in [
                    "default-branch",
                    "single-trunk",
                    "merge-cleanup",
                    "branch-reminder",
                    "forge-version",
                    "protect-trunk",
                    "protect-tags",
                    "protections-check",
                    "private-vulnerability-reporting",
                ] {
                    assert!(steps.contains(&step), "{forge} {mode:?}: {step}");
                }
                for step in [
                    "package-check",
                    "ci-permissions",
                    "install-bot",
                    "bot-secrets",
                    "auto-merge",
                    "protect-release-lines",
                ] {
                    assert!(!steps.contains(&step), "{forge} {mode:?}: {step} applies");
                }
            }
        }
    }

    /// The reporting channel pairs with the landed policy: a target that
    /// requests none is asked for none.
    #[test]
    fn the_reporting_channel_follows_the_requested_policy() {
        let mut ctx = ctx(Some("github"), ReleaseMode::None);
        assert!(
            (spec("private-vulnerability-reporting")
                .expect("the step exists")
                .applies)(&ctx)
            .is_none()
        );
        ctx.capabilities.reporting_policy = false;
        let reason = (spec("private-vulnerability-reporting")
            .expect("the step exists")
            .applies)(&ctx)
        .expect("an unrequested policy names itself");
        assert!(reason.contains("capabilities.reporting_policy"), "{reason}");
    }

    /// The packaging gate reads the release driver's own command, so an
    /// ambiguous observation that named none leaves it out.
    #[test]
    fn the_packaging_gate_needs_a_named_driver() {
        let mut ctx = ctx(Some("github"), ReleaseMode::Automatic);
        ctx.profile.release.driver = None;
        let reason = (spec("package-check").expect("the step exists").applies)(&ctx)
            .expect("a driverless release names itself");
        assert!(reason.contains("profile.release.driver"), "{reason}");
    }

    /// Every prerequisite names a step that exists and comes earlier, so the
    /// full run can never be refused by its own table.
    #[test]
    fn every_prereq_is_an_earlier_step() {
        for (idx, step) in STEPS.iter().enumerate() {
            for prereq in step.prereqs {
                let position = STEPS
                    .iter()
                    .position(|other| other.name == *prereq)
                    .unwrap_or(usize::MAX);
                assert!(
                    position < idx,
                    "{}: prereq {prereq} is not an earlier step",
                    step.name
                );
            }
        }
        let reporting = &STEPS[STEPS.len() - 1];
        assert_eq!(reporting.name, "private-vulnerability-reporting");
        assert_eq!(reporting.chapter, "ยง3");
        assert_eq!(reporting.mutates, super::Mutates::Forge);
        assert!(!reporting.optional && !reporting.destructive);
        assert!(reporting.prereqs.is_empty());
        assert_eq!(STEPS[STEPS.len() - 2].name, "protections-check");
        assert!(spec("package-check").is_some());
        assert!(spec("no-such-step").is_none());
    }
}