Skip to content
282 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 fine_grained;
23pub mod github;
24pub mod guardrails;
25pub mod identity;
26pub mod inbox;
27pub mod integrations;
28pub mod members;
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 aliasable_name, claimable_namespace, is_namespace_shaped, is_reserved_name, is_route_name, is_valid_namespace,
52 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 pub username: String,
176 #[serde(default)]
177 pub kind: PrincipalKind,
178 /// Whether the account's email address has been confirmed. Unverified
179 /// accounts can sign in but cannot create or change anything.
180 #[serde(default)]
181 pub verified: bool,
182 /// The workspaces this user belongs to. Filled in when a user is
183 /// resolved from credentials, so any service can authorize from it.
184 #[serde(default)]
185 pub workspaces: Vec<Membership>,
186 /// The person's uploaded avatar: the SHA-256 of its bytes, served at
187 /// `/avatars/<avatar>`. Absent means the generated letter avatar.
188 #[serde(default, skip_serializing_if = "Option::is_none")]
189 pub avatar: Option<String>,
190 /// Set on an agent resolved from its token: who it acts for, with which
191 /// credential, and what it may do. See [`credentials`].
192 #[serde(default, skip_serializing_if = "Option::is_none")]
193 pub acting: Option<Box<credentials::Acting>>,
194 /// The repositories this user has been given a role on directly,
195 /// whether or not they belong to its workspace. Filled in with
196 /// `workspaces`; see [`access`].
197 #[serde(default, skip_serializing_if = "Vec::is_empty")]
198 pub grants: Vec<access::RepoGrant>,
199 /// Set on a user resolved from an access token: its scopes and the
200 /// workspaces or repositories it is limited to. Absent on a signed-in
201 /// session and on an agent (whose `acting` scope applies instead).
202 /// See [`scopes`].
203 #[serde(default, skip_serializing_if = "Option::is_none")]
204 pub token: Option<Box<scopes::TokenAccess>>,
205 /// The workspaces this person belongs to but cannot use until they
206 /// meet its policy, such as turning on two-factor authentication.
207 /// They are left out of `workspaces` and `grants` meanwhile. Set when
208 /// a person is resolved from a session.
209 #[serde(default, skip_serializing_if = "Vec::is_empty")]
210 pub held: Vec<members::PolicyHold>,
211}
212
213impl User {
214 /// g1t itself, acting in `workspace`: what the platform's own work,
215 /// such as security updates, is done and recorded as.
216 pub fn system(workspace: &str) -> User {
217 User {
218 id: system::ID.to_owned(),
219 username: system::USERNAME.to_owned(),
220 kind: PrincipalKind::System,
221 verified: true,
222 workspaces: vec![Membership::member(workspace.to_lowercase())],
223 ..User::default()
224 }
225 }
226
227 /// Whether this is g1t itself.
228 pub fn is_system(&self) -> bool {
229 self.kind == PrincipalKind::System
230 }
231
232 /// Whether this is a person whose account has not confirmed its email
233 /// address. Such an account can only confirm it (or change it, or sign
234 /// out): the site, the API, MCP and git refuse it everything else
235 /// ([`accounts::confirm_email_first`]).
236 pub fn awaits_confirmation(&self) -> bool {
237 self.kind == PrincipalKind::User && !self.verified
238 }
239
240 pub fn role_in(&self, slug: &str) -> Option<Role> {
241 self.workspaces
242 .iter()
243 .find(|membership| membership.slug == slug)
244 .map(|membership| membership.role)
245 }
246
247 pub fn is_member(&self, slug: &str) -> bool {
248 self.role_in(slug).is_some()
249 }
250
251 /// The membership in `slug`, if any.
252 pub fn membership(&self, slug: &str) -> Option<&Membership> {
253 self.workspaces.iter().find(|membership| membership.slug.eq_ignore_ascii_case(slug))
254 }
255
256 /// Whether this is a person who owns `slug`, or holds `role` in it.
257 pub fn owns_or_has(&self, slug: &str, role: OrgRole) -> bool {
258 self.membership(slug)
259 .is_some_and(|membership| membership.role == Role::Owner || membership.has(role))
260 }
261
262 /// Whether the user may manage `slug`'s billing: an owner or a billing
263 /// manager.
264 pub fn manages_billing(&self, slug: &str) -> bool {
265 self.owns_or_has(slug, OrgRole::BillingManager)
266 }
267
268 /// Whether the user may see and manage security across `slug`: an
269 /// owner or a security manager.
270 pub fn manages_security(&self, slug: &str) -> bool {
271 self.owns_or_has(slug, OrgRole::SecurityManager)
272 }
273
274 /// The workspace's member privileges as this user sees them: the
275 /// defaults when the membership does not say.
276 pub fn privileges_in(&self, slug: &str) -> MemberPrivileges {
277 self.membership(slug).and_then(|membership| membership.privileges).unwrap_or_default()
278 }
279}
280
281/// Who is asking. Every read and write in every service takes one.
282pub type Viewer = Option<User>;