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.

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