g1t/crates/contracts/src/lib.rs
| 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 | |
| 7 | pub mod access; |
| 8 | pub mod accounts; |
| 9 | pub mod actions; |
| 10 | pub mod agents; |
| 11 | pub mod audit; |
| 12 | pub mod billing; |
| 13 | pub mod capture; |
| 14 | pub mod credentials; |
| 15 | pub mod events; |
| 16 | pub mod github; |
| 17 | pub mod guardrails; |
| 18 | pub mod identity; |
| 19 | pub mod integrations; |
| 20 | mod ids; |
| 21 | mod names; |
| 22 | mod outcome; |
| 23 | pub mod projects; |
| 24 | pub mod repos; |
| 25 | pub mod search; |
| 26 | pub mod security; |
| 27 | pub mod time; |
| 28 | pub mod webhooks; |
| 29 | pub mod work; |
| 30 | |
| 31 | pub use ids::new_id; |
| 32 | pub use names::{is_valid_namespace, is_valid_repo_name}; |
| 33 | pub use outcome::{Failure, FailureCode, Outcome}; |
| 34 | |
| 35 | use serde::{Deserialize, Serialize}; |
| 36 | |
| 37 | /// What a member may do in a workspace. |
| 38 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 39 | #[serde(rename_all = "lowercase")] |
| 40 | pub enum Role { |
| 41 | /// Everything a member can, plus managing members. |
| 42 | Owner, |
| 43 | /// Create repositories, push, manage issues and merge pull requests. |
| 44 | Member, |
| 45 | } |
| 46 | |
| 47 | /// One workspace a user belongs to. |
| 48 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 49 | pub struct Membership { |
| 50 | /// The workspace's name in URLs: `g1t.sh/<slug>`. |
| 51 | pub slug: String, |
| 52 | pub role: Role, |
| 53 | /// The workspace's display name, for showing it to people. Set when a |
| 54 | /// user is resolved from credentials; absent on principals made up by |
| 55 | /// a service. |
| 56 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 57 | pub name: Option<String>, |
| 58 | /// The workspace's uploaded icon: the SHA-256 of its bytes, served at |
| 59 | /// `/avatars/<avatar>`. Absent means the generated letter avatar. |
| 60 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 61 | pub avatar: Option<String>, |
| 62 | /// What a member gets on each of the workspace's repositories: the |
| 63 | /// workspace's base permission. Set when a user is resolved from |
| 64 | /// credentials; absent means the default, Write. Owners have Admin |
| 65 | /// whatever it says. See [`access`]. |
| 66 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 67 | pub base_permission: Option<access::BasePermission>, |
| 68 | } |
| 69 | |
| 70 | impl Membership { |
| 71 | /// A plain member of `slug`, as services act inside one workspace. |
| 72 | pub fn member(slug: impl Into<String>) -> Self { |
| 73 | Membership { |
| 74 | slug: slug.into(), |
| 75 | role: Role::Member, |
| 76 | name: None, |
| 77 | avatar: None, |
| 78 | base_permission: None, |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// What a set of credentials resolved to. |
| 84 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 85 | #[serde(rename_all = "lowercase")] |
| 86 | pub enum PrincipalKind { |
| 87 | /// A person's account. |
| 88 | #[default] |
| 89 | User, |
| 90 | /// A workspace, acting through one of its own access tokens. Its `id` |
| 91 | /// is the workspace's, its `username` the workspace's slug, and it is a |
| 92 | /// member of that workspace and no other. |
| 93 | Workspace, |
| 94 | /// A g1t agent at work in a sandbox, acting through a token that lives |
| 95 | /// as long as its run and can do only what that token's scope lists, in |
| 96 | /// one repository. Its `username` is `g1t-agent`. |
| 97 | Agent, |
| 98 | } |
| 99 | |
| 100 | #[derive(Clone, Debug, Default, Serialize, Deserialize)] |
| 101 | pub struct User { |
| 102 | pub id: String, |
| 103 | pub username: String, |
| 104 | #[serde(default)] |
| 105 | pub kind: PrincipalKind, |
| 106 | /// Whether the account's email address has been confirmed. Unverified |
| 107 | /// accounts can sign in but cannot create or change anything. |
| 108 | #[serde(default)] |
| 109 | pub verified: bool, |
| 110 | /// The workspaces this user belongs to. Filled in when a user is |
| 111 | /// resolved from credentials, so any service can authorize from it. |
| 112 | #[serde(default)] |
| 113 | pub workspaces: Vec<Membership>, |
| 114 | /// The person's uploaded avatar: the SHA-256 of its bytes, served at |
| 115 | /// `/avatars/<avatar>`. Absent means the generated letter avatar. |
| 116 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 117 | pub avatar: Option<String>, |
| 118 | /// Set on an agent resolved from its token: who it acts for, with which |
| 119 | /// credential, and what it may do. See [`credentials`]. |
| 120 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 121 | pub acting: Option<Box<credentials::Acting>>, |
| 122 | /// The repositories this user has been given a role on directly, |
| 123 | /// whether or not they belong to its workspace. Filled in with |
| 124 | /// `workspaces`; see [`access`]. |
| 125 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 126 | pub grants: Vec<access::RepoGrant>, |
| 127 | } |
| 128 | |
| 129 | impl User { |
| 130 | pub fn role_in(&self, slug: &str) -> Option<Role> { |
| 131 | self.workspaces |
| 132 | .iter() |
| 133 | .find(|membership| membership.slug == slug) |
| 134 | .map(|membership| membership.role) |
| 135 | } |
| 136 | |
| 137 | pub fn is_member(&self, slug: &str) -> bool { |
| 138 | self.role_in(slug).is_some() |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// Who is asking. Every read and write in every service takes one. |
| 143 | pub type Viewer = Option<User>; |