Skip to content
290 linesCodeBlameRaw
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 account_deletion;
10pub mod accounts;
11pub mod actions;
12pub mod agents;
13pub mod audit;
14pub mod backups;
15pub mod billing;
16pub mod capture;
17pub mod checks;
18pub mod codeowners;
19pub mod credentials;
20pub mod datasets;
21pub mod deploy_keys;
22pub mod events;
23pub mod folios;
24pub mod github;
25pub mod guardrails;
26pub mod identity;
27pub mod inbox;
28pub mod integrations;
29pub mod members;
30pub mod mirrors;
31mod ids;
32mod names;
33mod outcome;
34pub mod packages;
35pub mod projects;
36pub mod repos;
37pub mod rules;
38pub mod runners;
39pub mod scopes;
40pub mod search;
41pub mod security;
42pub mod teams;
43pub mod security_suite;
44pub mod subscribers;
45pub mod time;
46pub mod tokens;
47pub mod updates;
48pub mod webhooks;
49pub mod work;
50
51pub use ids::new_id;
52pub use names::{
53 Username, aliasable_name, claimable_namespace, claimable_username, is_namespace_shaped, is_reserved_name, is_route_name,
54 is_valid_namespace, is_valid_repo_name,
55};
56pub use outcome::{Failure, FailureCode, Outcome};
57
58use serde::{Deserialize, Serialize};
59
60/// What a member may do in a workspace. A member may also hold
61/// [`members::OrgRole`]s, which add to it.
62#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
63#[serde(rename_all = "lowercase")]
64pub enum Role {
65 /// Everything: Admin on every repository, the workspace's members,
66 /// settings, billing and security.
67 Owner,
68 /// The workspace's base permission on each repository, and what its
69 /// member privileges allow (see [`members::MemberPrivileges`]).
70 Member,
71}
72
73pub use members::{MemberPrivileges, OrgRole};
74
75/// One workspace a user belongs to.
76#[derive(Clone, Debug, Serialize, Deserialize)]
77pub struct Membership {
78 /// The workspace's name in URLs: `g1t.sh/<slug>`.
79 pub slug: String,
80 pub role: Role,
81 /// The workspace's display name, for showing it to people. Set when a
82 /// user is resolved from credentials; absent on principals made up by
83 /// a service.
84 #[serde(default, skip_serializing_if = "Option::is_none")]
85 pub name: Option<String>,
86 /// The workspace's uploaded icon: the SHA-256 of its bytes, served at
87 /// `/avatars/<avatar>`. Absent means the generated letter avatar.
88 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub avatar: Option<String>,
90 /// What a member gets on each of the workspace's repositories: the
91 /// workspace's base permission. Set when a user is resolved from
92 /// credentials; absent means the default, Write. Owners have Admin
93 /// whatever it says. See [`access`].
94 #[serde(default, skip_serializing_if = "Option::is_none")]
95 pub base_permission: Option<access::BasePermission>,
96 /// Who may create the workspace's teams. Set when a user is resolved
97 /// from credentials; absent means the default, any member. See
98 /// [`teams::TeamCreation`].
99 #[serde(default, skip_serializing_if = "Option::is_none")]
100 pub team_creation: Option<teams::TeamCreation>,
101 /// The roles the member holds besides `role`: billing manager,
102 /// security manager. Set when a user is resolved from credentials.
103 #[serde(default, skip_serializing_if = "Vec::is_empty")]
104 pub org_roles: Vec<OrgRole>,
105 /// What the workspace lets members (and repository admins) do. Set
106 /// when a user is resolved from credentials; absent means the
107 /// defaults. See [`members::MemberPrivileges`].
108 #[serde(default, skip_serializing_if = "Option::is_none")]
109 pub privileges: Option<MemberPrivileges>,
110}
111
112impl Membership {
113 /// A plain member of `slug`, as services act inside one workspace.
114 pub fn member(slug: impl Into<String>) -> Self {
115 Membership {
116 slug: slug.into(),
117 role: Role::Member,
118 name: None,
119 avatar: None,
120 base_permission: None,
121 team_creation: None,
122 org_roles: Vec::new(),
123 privileges: None,
124 }
125 }
126
127 /// Whether the member holds `role` besides owner or member.
128 pub fn has(&self, role: OrgRole) -> bool {
129 self.org_roles.contains(&role)
130 }
131}
132
133/// What a set of credentials resolved to.
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
135#[serde(rename_all = "lowercase")]
136pub enum PrincipalKind {
137 /// A person's account.
138 #[default]
139 User,
140 /// A workspace, acting through one of its own access tokens. Its `id`
141 /// is the workspace's, its `username` the workspace's slug, and it is a
142 /// member of that workspace and no other.
143 Workspace,
144 /// A g1t agent at work in a sandbox, acting through a token that lives
145 /// as long as its run and can do only what that token's scope lists, in
146 /// one repository. Its `username` is `g1t`.
147 Agent,
148 /// g1t itself: the platform acting on its own, as when it opens a
149 /// pull request to upgrade a vulnerable dependency or merges from the
150 /// queue. Never resolved from credentials: only services make one,
151 /// with [`User::system`]. Its `username` is `g1t`, which nobody can
152 /// register.
153 System,
154}
155
156/// g1t's own identity, as [`PrincipalKind::System`] work is recorded.
157pub mod system {
158 /// Its id wherever an author or actor id is stored.
159 pub const ID: &str = "g1t";
160 /// Its name, shown as the author of what it does.
161 pub const USERNAME: &str = "g1t";
162 /// The address on the commits it makes, which no mailbox receives.
163 pub const EMAIL: &str = "g1t@users.noreply.g1t.sh";
164 /// Ids that earlier versions stored for g1t's own actions, such as a
165 /// merge its settings made. Read as g1t too.
166 pub const LEGACY_IDS: [&str; 3] = ["g1t_policy", "svc_runner", "g1t_runner"];
167
168 /// Whether `id` is g1t's own.
169 pub fn is_system_id(id: &str) -> bool {
170 id == ID || LEGACY_IDS.contains(&id)
171 }
172}
173
174#[derive(Clone, Debug, Default, Serialize, Deserialize)]
175pub struct User {
176 pub id: String,
177 /// Lowercased: what the person is found, linked and mentioned by.
178 pub username: String,
179 /// The username as its owner wrote it (`Ana`), when that differs from
180 /// `username`: what pages show. Set on the signed-in person and on
181 /// people looked up by name; absent elsewhere, where `username` is shown.
182 #[serde(default, skip_serializing_if = "Option::is_none")]
183 pub display_username: Option<String>,
184 #[serde(default)]
185 pub kind: PrincipalKind,
186 /// Whether the account's email address has been confirmed. Unverified
187 /// accounts can sign in but cannot create or change anything.
188 #[serde(default)]
189 pub verified: bool,
190 /// The workspaces this user belongs to. Filled in when a user is
191 /// resolved from credentials, so any service can authorize from it.
192 #[serde(default)]
193 pub workspaces: Vec<Membership>,
194 /// The person's uploaded avatar: the SHA-256 of its bytes, served at
195 /// `/avatars/<avatar>`. Absent means the generated letter avatar.
196 #[serde(default, skip_serializing_if = "Option::is_none")]
197 pub avatar: Option<String>,
198 /// Set on an agent resolved from its token: who it acts for, with which
199 /// credential, and what it may do. See [`credentials`].
200 #[serde(default, skip_serializing_if = "Option::is_none")]
201 pub acting: Option<Box<credentials::Acting>>,
202 /// The repositories this user has been given a role on directly,
203 /// whether or not they belong to its workspace. Filled in with
204 /// `workspaces`; see [`access`].
205 #[serde(default, skip_serializing_if = "Vec::is_empty")]
206 pub grants: Vec<access::RepoGrant>,
207 /// Set on a user resolved from an access token: its scopes and the
208 /// workspaces or repositories it is limited to. Absent on a signed-in
209 /// session and on an agent (whose `acting` scope applies instead).
210 /// See [`scopes`].
211 #[serde(default, skip_serializing_if = "Option::is_none")]
212 pub token: Option<Box<scopes::TokenAccess>>,
213 /// The workspaces this person belongs to but cannot use until they
214 /// meet its policy, such as turning on two-factor authentication.
215 /// They are left out of `workspaces` and `grants` meanwhile. Set when
216 /// a person is resolved from a session.
217 #[serde(default, skip_serializing_if = "Vec::is_empty")]
218 pub held: Vec<members::PolicyHold>,
219}
220
221impl User {
222 /// g1t itself, acting in `workspace`: what the platform's own work,
223 /// such as security updates, is done and recorded as.
224 pub fn system(workspace: &str) -> User {
225 User {
226 id: system::ID.to_owned(),
227 username: system::USERNAME.to_owned(),
228 kind: PrincipalKind::System,
229 verified: true,
230 workspaces: vec![Membership::member(workspace.to_lowercase())],
231 ..User::default()
232 }
233 }
234
235 /// Whether this is g1t itself.
236 pub fn is_system(&self) -> bool {
237 self.kind == PrincipalKind::System
238 }
239
240 /// Whether this is a person whose account has not confirmed its email
241 /// address. Such an account can only confirm it (or change it, or sign
242 /// out): the site, the API, MCP and git refuse it everything else
243 /// ([`accounts::confirm_email_first`]).
244 pub fn awaits_confirmation(&self) -> bool {
245 self.kind == PrincipalKind::User && !self.verified
246 }
247
248 pub fn role_in(&self, slug: &str) -> Option<Role> {
249 self.workspaces
250 .iter()
251 .find(|membership| membership.slug == slug)
252 .map(|membership| membership.role)
253 }
254
255 pub fn is_member(&self, slug: &str) -> bool {
256 self.role_in(slug).is_some()
257 }
258
259 /// The membership in `slug`, if any.
260 pub fn membership(&self, slug: &str) -> Option<&Membership> {
261 self.workspaces.iter().find(|membership| membership.slug.eq_ignore_ascii_case(slug))
262 }
263
264 /// Whether this is a person who owns `slug`, or holds `role` in it.
265 pub fn owns_or_has(&self, slug: &str, role: OrgRole) -> bool {
266 self.membership(slug)
267 .is_some_and(|membership| membership.role == Role::Owner || membership.has(role))
268 }
269
270 /// Whether the user may manage `slug`'s billing: an owner or a billing
271 /// manager.
272 pub fn manages_billing(&self, slug: &str) -> bool {
273 self.owns_or_has(slug, OrgRole::BillingManager)
274 }
275
276 /// Whether the user may see and manage security across `slug`: an
277 /// owner or a security manager.
278 pub fn manages_security(&self, slug: &str) -> bool {
279 self.owns_or_has(slug, OrgRole::SecurityManager)
280 }
281
282 /// The workspace's member privileges as this user sees them: the
283 /// defaults when the membership does not say.
284 pub fn privileges_in(&self, slug: &str) -> MemberPrivileges {
285 self.membership(slug).and_then(|membership| membership.privileges).unwrap_or_default()
286 }
287}
288
289/// Who is asking. Every read and write in every service takes one.
290pub type Viewer = Option<User>;