Skip to content

g1t/crates/contracts/src/lib.rs

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