Skip to content

g1t/crates/contracts/src/lib.rs

211 lines7,579 bytesCodeBlame
1//! Types and service interfaces shared by every g1t service.
2//!
3//! Each service has a module here holding the data it exchanges and the
4//! arguments of each of its methods. Services and their callers depend on
5//! this crate, never on each other's code.
6
7pub mod about;
8pub mod access;
9pub mod accounts;
10pub mod actions;
11pub mod agents;
12pub mod audit;
13pub mod backups;
14pub mod billing;
15pub mod capture;
16pub mod codeowners;
17pub mod credentials;
18pub mod events;
19pub mod github;
20pub mod guardrails;
21pub mod identity;
22pub mod inbox;
23pub mod integrations;
24mod ids;
25mod names;
26mod outcome;
27pub mod packages;
28pub mod projects;
29pub mod repos;
30pub mod rules;
31pub mod runners;
32pub mod scopes;
33pub mod search;
34pub mod security;
35pub mod teams;
36pub mod security_suite;
37pub mod time;
38pub mod updates;
39pub mod webhooks;
40pub mod work;
41
42pub use ids::new_id;
43pub use names::{
44 aliasable_name, claimable_namespace, is_namespace_shaped, is_reserved_name, is_route_name, is_valid_namespace,
45 is_valid_repo_name,
46};
47pub use outcome::{Failure, FailureCode, Outcome};
48
49use serde::{Deserialize, Serialize};
50
51/// What a member may do in a workspace.
52#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
53#[serde(rename_all = "lowercase")]
54pub enum Role {
55 /// Everything a member can, plus managing members.
56 Owner,
57 /// Create repositories, push, manage issues and merge pull requests.
58 Member,
59}
60
61/// One workspace a user belongs to.
62#[derive(Clone, Debug, Serialize, Deserialize)]
63pub struct Membership {
64 /// The workspace's name in URLs: `g1t.sh/<slug>`.
65 pub slug: String,
66 pub role: Role,
67 /// The workspace's display name, for showing it to people. Set when a
68 /// user is resolved from credentials; absent on principals made up by
69 /// a service.
70 #[serde(default, skip_serializing_if = "Option::is_none")]
71 pub name: Option<String>,
72 /// The workspace's uploaded icon: the SHA-256 of its bytes, served at
73 /// `/avatars/<avatar>`. Absent means the generated letter avatar.
74 #[serde(default, skip_serializing_if = "Option::is_none")]
75 pub avatar: Option<String>,
76 /// What a member gets on each of the workspace's repositories: the
77 /// workspace's base permission. Set when a user is resolved from
78 /// credentials; absent means the default, Write. Owners have Admin
79 /// whatever it says. See [`access`].
80 #[serde(default, skip_serializing_if = "Option::is_none")]
81 pub base_permission: Option<access::BasePermission>,
82 /// Who may create the workspace's teams. Set when a user is resolved
83 /// from credentials; absent means the default, any member. See
84 /// [`teams::TeamCreation`].
85 #[serde(default, skip_serializing_if = "Option::is_none")]
86 pub team_creation: Option<teams::TeamCreation>,
87}
88
89impl Membership {
90 /// A plain member of `slug`, as services act inside one workspace.
91 pub fn member(slug: impl Into<String>) -> Self {
92 Membership {
93 slug: slug.into(),
94 role: Role::Member,
95 name: None,
96 avatar: None,
97 base_permission: None,
98 team_creation: None,
99 }
100 }
101}
102
103/// What a set of credentials resolved to.
104#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
105#[serde(rename_all = "lowercase")]
106pub enum PrincipalKind {
107 /// A person's account.
108 #[default]
109 User,
110 /// A workspace, acting through one of its own access tokens. Its `id`
111 /// is the workspace's, its `username` the workspace's slug, and it is a
112 /// member of that workspace and no other.
113 Workspace,
114 /// A g1t agent at work in a sandbox, acting through a token that lives
115 /// as long as its run and can do only what that token's scope lists, in
116 /// one repository. Its `username` is `g1t`.
117 Agent,
118 /// g1t itself: the platform acting on its own, as when it opens a
119 /// pull request to upgrade a vulnerable dependency or merges from the
120 /// queue. Never resolved from credentials: only services make one,
121 /// with [`User::system`]. Its `username` is `g1t`, which nobody can
122 /// register.
123 System,
124}
125
126/// g1t's own identity, as [`PrincipalKind::System`] work is recorded.
127pub mod system {
128 /// Its id wherever an author or actor id is stored.
129 pub const ID: &str = "g1t";
130 /// Its name, shown as the author of what it does.
131 pub const USERNAME: &str = "g1t";
132 /// The address on the commits it makes, which no mailbox receives.
133 pub const EMAIL: &str = "g1t@users.noreply.g1t.sh";
134 /// Ids that earlier versions stored for g1t's own actions, such as a
135 /// merge its settings made. Read as g1t too.
136 pub const LEGACY_IDS: [&str; 3] = ["g1t_policy", "svc_runner", "g1t_runner"];
137
138 /// Whether `id` is g1t's own.
139 pub fn is_system_id(id: &str) -> bool {
140 id == ID || LEGACY_IDS.contains(&id)
141 }
142}
143
144#[derive(Clone, Debug, Default, Serialize, Deserialize)]
145pub struct User {
146 pub id: String,
147 pub username: String,
148 #[serde(default)]
149 pub kind: PrincipalKind,
150 /// Whether the account's email address has been confirmed. Unverified
151 /// accounts can sign in but cannot create or change anything.
152 #[serde(default)]
153 pub verified: bool,
154 /// The workspaces this user belongs to. Filled in when a user is
155 /// resolved from credentials, so any service can authorize from it.
156 #[serde(default)]
157 pub workspaces: Vec<Membership>,
158 /// The person's uploaded avatar: the SHA-256 of its bytes, served at
159 /// `/avatars/<avatar>`. Absent means the generated letter avatar.
160 #[serde(default, skip_serializing_if = "Option::is_none")]
161 pub avatar: Option<String>,
162 /// Set on an agent resolved from its token: who it acts for, with which
163 /// credential, and what it may do. See [`credentials`].
164 #[serde(default, skip_serializing_if = "Option::is_none")]
165 pub acting: Option<Box<credentials::Acting>>,
166 /// The repositories this user has been given a role on directly,
167 /// whether or not they belong to its workspace. Filled in with
168 /// `workspaces`; see [`access`].
169 #[serde(default, skip_serializing_if = "Vec::is_empty")]
170 pub grants: Vec<access::RepoGrant>,
171 /// Set on a user resolved from an access token: its scopes and the
172 /// workspaces or repositories it is limited to. Absent on a signed-in
173 /// session and on an agent (whose `acting` scope applies instead).
174 /// See [`scopes`].
175 #[serde(default, skip_serializing_if = "Option::is_none")]
176 pub token: Option<Box<scopes::TokenAccess>>,
177}
178
179impl User {
180 /// g1t itself, acting in `workspace`: what the platform's own work,
181 /// such as security updates, is done and recorded as.
182 pub fn system(workspace: &str) -> User {
183 User {
184 id: system::ID.to_owned(),
185 username: system::USERNAME.to_owned(),
186 kind: PrincipalKind::System,
187 verified: true,
188 workspaces: vec![Membership::member(workspace.to_lowercase())],
189 ..User::default()
190 }
191 }
192
193 /// Whether this is g1t itself.
194 pub fn is_system(&self) -> bool {
195 self.kind == PrincipalKind::System
196 }
197
198 pub fn role_in(&self, slug: &str) -> Option<Role> {
199 self.workspaces
200 .iter()
201 .find(|membership| membership.slug == slug)
202 .map(|membership| membership.role)
203 }
204
205 pub fn is_member(&self, slug: &str) -> bool {
206 self.role_in(slug).is_some()
207 }
208}
209
210/// Who is asking. Every read and write in every service takes one.
211pub type Viewer = Option<User>;