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.

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