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