Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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