Skip to content

g1t/crates/rules/src/lib.rs

64 lines2,117 bytesCodeBlame
1//! The rules engine: which rulesets hold for a branch or tag, and whether a
2//! change to it meets them.
3//!
4//! Everything here is pure: the work service (merges, and where rulesets
5//! are kept) and the repos service (pushes, and every other change to a
6//! branch or tag) gather the facts and ask. The types are in
7//! `g1t_contracts::rules`.
8//!
9//! - [`select`]: which rulesets hold in a repository and for a name, who
10//! may bypass them, and the effective rules of one branch.
11//! - [`push`]: judging a push, or a branch created, deleted or renamed.
12//! - [`merge`]: judging a pull request's merge, and what the active rules
13//! ask of g1t's lifecycle and merge queue ([`merge::requirements`]).
14//! - [`content`]: rules about what commits bring, for both.
15//! - [`validate`]: whether a ruleset can be saved.
16//! - [`legacy`]: branch protection as it was, as a ruleset.
17//! - [`report`]: what git and people are told.
18
19pub mod content;
20pub mod glob;
21pub mod legacy;
22pub mod merge;
23pub mod outcome;
24pub mod push;
25pub mod report;
26pub mod select;
27pub mod text;
28pub mod validate;
29pub mod window;
30
31pub use outcome::Judged;
32pub use select::{ActorFacts, RepoFacts, Who};
33
34use g1t_contracts::rules::{Action, NewEvaluation};
35
36/// The evaluations to record for a change: one per ruleset that holds.
37pub fn evaluations(
38 judged: &[Judged],
39 repo_id: &str,
40 workspace: &str,
41 action: Action,
42 actor: &ActorFacts,
43 number: Option<u32>,
44 sha: Option<&str>,
45) -> Vec<NewEvaluation> {
46 judged
47 .iter()
48 .map(|one| NewEvaluation {
49 repo_id: repo_id.to_owned(),
50 workspace: workspace.to_lowercase(),
51 ruleset_id: one.id.clone(),
52 ruleset_name: one.name.clone(),
53 enforcement: one.enforcement,
54 action,
55 git_ref: one.git_ref.clone(),
56 actor: actor.username.clone(),
57 actor_kind: actor.kind.as_str().to_owned(),
58 verdict: one.verdict(),
59 violations: one.violations.clone(),
60 number,
61 sha: sha.map(str::to_owned),
62 })
63 .collect()
64}