Skip to content
214 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 accounts;
10pub mod actions;
11pub mod agents;
12pub mod audit;
13pub mod backups;
14pub mod billing;
15pub mod capture;
16pub mod checks;
17pub mod codeowners;
18pub mod credentials;
19pub mod events;
20pub mod fine_grained;
21pub mod github;
22pub mod guardrails;
23pub mod identity;
24pub mod inbox;
25pub mod integrations;
26mod ids;
27mod names;
28mod outcome;
29pub mod packages;
30pub mod projects;
31pub mod repos;
32pub mod rules;
33pub mod runners;
34pub mod scopes;
35pub mod search;
36pub mod security;
37pub mod teams;
38pub mod security_suite;
39pub mod time;
40pub mod tokens;
41pub mod updates;
42pub mod webhooks;
43pub mod work;
44
45pub use ids::new_id;
46pub use names::{
47 aliasable_name, claimable_namespace, is_namespace_shaped, is_reserved_name, is_route_name, is_valid_namespace,
48 is_valid_repo_name,
49};
50pub use outcome::{Failure, FailureCode, Outcome};
51
52use serde::{Deserialize, Serialize};
53
54/// What a member may do in a workspace.
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
56#[serde(rename_all = "lowercase")]
57pub enum Role {
58 /// Everything a member can, plus managing members.
59 Owner,
60 /// Create repositories, push, manage issues and merge pull requests.
61 Member,
62}
63
64/// One workspace a user belongs to.
65#[derive(Clone, Debug, Serialize, Deserialize)]
66pub struct Membership {
67 /// The workspace's name in URLs: `g1t.sh/<slug>`.
68 pub slug: String,
69 pub role: Role,
70 /// The workspace's display name, for showing it to people. Set when a
71 /// user is resolved from credentials; absent on principals made up by
72 /// a service.
73 #[serde(default, skip_serializing_if = "Option::is_none")]
74 pub name: Option<String>,
75 /// The workspace's uploaded icon: the SHA-256 of its bytes, served at
76 /// `/avatars/<avatar>`. Absent means the generated letter avatar.
77 #[serde(default, skip_serializing_if = "Option::is_none")]
78 pub avatar: Option<String>,
79 /// What a member gets on each of the workspace's repositories: the
80 /// workspace's base permission. Set when a user is resolved from
81 /// credentials; absent means the default, Write. Owners have Admin
82 /// whatever it says. See [`access`].
83 #[serde(default, skip_serializing_if = "Option::is_none")]
84 pub base_permission: Option<access::BasePermission>,
85 /// Who may create the workspace's teams. Set when a user is resolved
86 /// from credentials; absent means the default, any member. See
87 /// [`teams::TeamCreation`].
88 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub team_creation: Option<teams::TeamCreation>,
90}
91
92impl Membership {
93 /// A plain member of `slug`, as services act inside one workspace.
94 pub fn member(slug: impl Into<String>) -> Self {
95 Membership {
96 slug: slug.into(),
97 role: Role::Member,
98 name: None,
99 avatar: None,
100 base_permission: None,
101 team_creation: None,
102 }
103 }
104}
105
106/// What a set of credentials resolved to.
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
108#[serde(rename_all = "lowercase")]
109pub enum PrincipalKind {
110 /// A person's account.
111 #[default]
112 User,
113 /// A workspace, acting through one of its own access tokens. Its `id`
114 /// is the workspace's, its `username` the workspace's slug, and it is a
115 /// member of that workspace and no other.
116 Workspace,
117 /// A g1t agent at work in a sandbox, acting through a token that lives
118 /// as long as its run and can do only what that token's scope lists, in
119 /// one repository. Its `username` is `g1t`.
120 Agent,
121 /// g1t itself: the platform acting on its own, as when it opens a
122 /// pull request to upgrade a vulnerable dependency or merges from the
123 /// queue. Never resolved from credentials: only services make one,
124 /// with [`User::system`]. Its `username` is `g1t`, which nobody can
125 /// register.
126 System,
127}
128
129/// g1t's own identity, as [`PrincipalKind::System`] work is recorded.
130pub mod system {
131 /// Its id wherever an author or actor id is stored.
132 pub const ID: &str = "g1t";
133 /// Its name, shown as the author of what it does.
134 pub const USERNAME: &str = "g1t";
135 /// The address on the commits it makes, which no mailbox receives.
136 pub const EMAIL: &str = "g1t@users.noreply.g1t.sh";
137 /// Ids that earlier versions stored for g1t's own actions, such as a
138 /// merge its settings made. Read as g1t too.
139 pub const LEGACY_IDS: [&str; 3] = ["g1t_policy", "svc_runner", "g1t_runner"];
140
141 /// Whether `id` is g1t's own.
142 pub fn is_system_id(id: &str) -> bool {
143 id == ID || LEGACY_IDS.contains(&id)
144 }
145}
146
147#[derive(Clone, Debug, Default, Serialize, Deserialize)]
148pub struct User {
149 pub id: String,
150 pub username: String,
151 #[serde(default)]
152 pub kind: PrincipalKind,
153 /// Whether the account's email address has been confirmed. Unverified
154 /// accounts can sign in but cannot create or change anything.
155 #[serde(default)]
156 pub verified: bool,
157 /// The workspaces this user belongs to. Filled in when a user is
158 /// resolved from credentials, so any service can authorize from it.
159 #[serde(default)]
160 pub workspaces: Vec<Membership>,
161 /// The person's uploaded avatar: the SHA-256 of its bytes, served at
162 /// `/avatars/<avatar>`. Absent means the generated letter avatar.
163 #[serde(default, skip_serializing_if = "Option::is_none")]
164 pub avatar: Option<String>,
165 /// Set on an agent resolved from its token: who it acts for, with which
166 /// credential, and what it may do. See [`credentials`].
167 #[serde(default, skip_serializing_if = "Option::is_none")]
168 pub acting: Option<Box<credentials::Acting>>,
169 /// The repositories this user has been given a role on directly,
170 /// whether or not they belong to its workspace. Filled in with
171 /// `workspaces`; see [`access`].
172 #[serde(default, skip_serializing_if = "Vec::is_empty")]
173 pub grants: Vec<access::RepoGrant>,
174 /// Set on a user resolved from an access token: its scopes and the
175 /// workspaces or repositories it is limited to. Absent on a signed-in
176 /// session and on an agent (whose `acting` scope applies instead).
177 /// See [`scopes`].
178 #[serde(default, skip_serializing_if = "Option::is_none")]
179 pub token: Option<Box<scopes::TokenAccess>>,
180}
181
182impl User {
183 /// g1t itself, acting in `workspace`: what the platform's own work,
184 /// such as security updates, is done and recorded as.
185 pub fn system(workspace: &str) -> User {
186 User {
187 id: system::ID.to_owned(),
188 username: system::USERNAME.to_owned(),
189 kind: PrincipalKind::System,
190 verified: true,
191 workspaces: vec![Membership::member(workspace.to_lowercase())],
192 ..User::default()
193 }
194 }
195
196 /// Whether this is g1t itself.
197 pub fn is_system(&self) -> bool {
198 self.kind == PrincipalKind::System
199 }
200
201 pub fn role_in(&self, slug: &str) -> Option<Role> {
202 self.workspaces
203 .iter()
204 .find(|membership| membership.slug == slug)
205 .map(|membership| membership.role)
206 }
207
208 pub fn is_member(&self, slug: &str) -> bool {
209 self.role_in(slug).is_some()
210 }
211}
212
213/// Who is asking. Every read and write in every service takes one.
214pub type Viewer = Option<User>;