flagon-io/g1t

public

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/crates/contracts/src/lib.rs

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