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