g1t/crates/contracts/src/repos.rs

217 lines5,729 bytesCodeBlame
1//! The repos service: repository metadata, contents, forks and git access.
2//!
3//! Each `*Args` struct is the argument of the method of the same name,
4//! served at `POST /rpc/<method>`.
5
6use serde::{Deserialize, Serialize};
7
8use crate::{User, Viewer};
9
10#[derive(Clone, Debug, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Repo {
13 pub id: String,
14 /// The owning user's (later, workspace's) name: the first URL segment.
15 pub namespace: String,
16 pub name: String,
17 pub description: Option<String>,
18 pub is_private: bool,
19 pub owner_id: String,
20 pub default_branch: String,
21 /// Set when this repo is an attempt's working copy of another repo.
22 pub fork_of: Option<String>,
23 /// Milliseconds since the epoch.
24 pub created_at: u64,
25}
26
27#[derive(Clone, Debug, Serialize, Deserialize)]
28pub struct RepoPath {
29 pub namespace: String,
30 pub name: String,
31}
32
33#[derive(Clone, Debug, Serialize, Deserialize)]
34pub struct Signature {
35 pub name: String,
36 pub email: String,
37}
38
39#[derive(Clone, Debug, Serialize, Deserialize)]
40#[serde(rename_all = "camelCase")]
41pub struct Commit {
42 pub hash: String,
43 pub tree_hash: String,
44 pub message: String,
45 pub author: Signature,
46 pub parents: Vec<String>,
47 /// Milliseconds since the epoch.
48 pub authored_at: u64,
49}
50
51#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
52#[serde(rename_all = "lowercase")]
53pub enum EntryKind {
54 Tree,
55 Blob,
56 Symlink,
57 Gitlink,
58 Exec,
59}
60
61#[derive(Clone, Debug, Serialize, Deserialize)]
62pub struct TreeEntry {
63 pub name: String,
64 pub hash: String,
65 pub kind: EntryKind,
66}
67
68#[derive(Clone, Debug, Serialize, Deserialize)]
69pub struct Readme {
70 pub name: String,
71 /// Null when the file is binary or too large to show.
72 pub text: Option<String>,
73}
74
75#[derive(Clone, Debug, Serialize, Deserialize)]
76pub struct TreeView {
77 pub repo: Repo,
78 #[serde(rename = "ref")]
79 pub git_ref: String,
80 pub path: String,
81 /// Null when the repo has no commits yet.
82 pub head: Option<Commit>,
83 pub entries: Vec<TreeEntry>,
84 pub readme: Option<Readme>,
85}
86
87#[derive(Clone, Debug, Serialize, Deserialize)]
88pub struct BlobView {
89 pub repo: Repo,
90 #[serde(rename = "ref")]
91 pub git_ref: String,
92 pub path: String,
93 pub size: u64,
94 /// Null when the file is binary or too large to show.
95 pub text: Option<String>,
96}
97
98/// A git remote and a short-lived credential for it.
99#[derive(Clone, Debug, Serialize, Deserialize)]
100pub struct GitAccess {
101 pub remote: String,
102 pub token: String,
103}
104
105#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
106pub enum GitService {
107 #[serde(rename = "git-upload-pack")]
108 UploadPack,
109 #[serde(rename = "git-receive-pack")]
110 ReceivePack,
111}
112
113/// The commit `main` points to after an attempt has landed.
114#[derive(Clone, Debug, Serialize, Deserialize)]
115pub struct Landed {
116 pub commit: String,
117}
118
119/// `get`. Returns `Outcome<Repo>`.
120#[derive(Debug, Serialize, Deserialize)]
121pub struct GetArgs {
122 pub path: RepoPath,
123 pub viewer: Viewer,
124}
125
126/// `get_by_id`. Returns `Outcome<Repo>`.
127#[derive(Debug, Serialize, Deserialize)]
128pub struct GetByIdArgs {
129 pub id: String,
130 pub viewer: Viewer,
131}
132
133/// `list`: repos the viewer may see, newest first. Returns `Vec<Repo>`.
134#[derive(Debug, Default, Serialize, Deserialize)]
135pub struct ListArgs {
136 pub viewer: Viewer,
137 #[serde(default)]
138 pub query: Option<String>,
139 #[serde(default)]
140 pub namespace: Option<String>,
141}
142
143/// `create`. Returns `Outcome<Repo>`.
144#[derive(Debug, Serialize, Deserialize)]
145#[serde(rename_all = "camelCase")]
146pub struct CreateArgs {
147 pub owner: User,
148 pub name: String,
149 #[serde(default)]
150 pub description: Option<String>,
151 #[serde(default)]
152 pub is_private: bool,
153}
154
155/// `tree`. Returns `Outcome<TreeView>`.
156#[derive(Debug, Serialize, Deserialize)]
157#[serde(rename_all = "camelCase")]
158pub struct TreeArgs {
159 pub path: RepoPath,
160 pub viewer: Viewer,
161 /// The default branch when absent.
162 #[serde(default, rename = "ref")]
163 pub git_ref: Option<String>,
164 #[serde(default)]
165 pub tree_path: String,
166}
167
168/// `blob`. Returns `Outcome<BlobView>`.
169#[derive(Debug, Serialize, Deserialize)]
170#[serde(rename_all = "camelCase")]
171pub struct BlobArgs {
172 pub path: RepoPath,
173 pub viewer: Viewer,
174 #[serde(rename = "ref")]
175 pub git_ref: String,
176 pub file_path: String,
177}
178
179/// `log`. Returns `Outcome<Vec<Commit>>`.
180#[derive(Debug, Serialize, Deserialize)]
181pub struct LogArgs {
182 pub path: RepoPath,
183 pub viewer: Viewer,
184 #[serde(default, rename = "ref")]
185 pub git_ref: Option<String>,
186 pub limit: u32,
187}
188
189/// `fork_for_attempt`: a copy-on-write copy of the source repo, hidden from
190/// listings, for one attempt to work in. Returns `Outcome<Repo>`.
191#[derive(Debug, Serialize, Deserialize)]
192#[serde(rename_all = "camelCase")]
193pub struct ForkArgs {
194 pub source_id: String,
195 pub attempt_id: String,
196 pub actor: User,
197}
198
199/// `git_access`: authorizes a git operation and says where to send it.
200/// Pushing to a repo that does not exist creates it in the pusher's own
201/// namespace. Returns `Outcome<GitAccess>`.
202#[derive(Debug, Serialize, Deserialize)]
203pub struct GitAccessArgs {
204 pub path: RepoPath,
205 pub viewer: Viewer,
206 pub service: GitService,
207}
208
209/// `land`: moves the default branch of the repo a fork came from to the
210/// fork's head. Refused with `conflict` when the fork is behind, since
211/// that would discard commits. Returns `Outcome<Landed>`.
212#[derive(Debug, Serialize, Deserialize)]
213#[serde(rename_all = "camelCase")]
214pub struct LandArgs {
215 pub fork_id: String,
216 pub actor: User,
217}