g1t/crates/contracts/src/repos.rs

298 lines8,107 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 slug of the workspace that owns it: 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 /// RFC 3339.
24 pub created_at: String,
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 /// RFC 3339.
48 pub authored_at: String,
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 result of landing an attempt.
114#[derive(Clone, Debug, Serialize, Deserialize)]
115pub struct Landed {
116 /// The commit the branch points to now.
117 pub commit: String,
118 /// The commit it pointed to before, if it had one. Comparing against
119 /// this shows what the attempt changed.
120 pub previous: Option<String>,
121}
122
123/// `get`. Returns `Outcome<Repo>`.
124#[derive(Debug, Serialize, Deserialize)]
125pub struct GetArgs {
126 pub path: RepoPath,
127 pub viewer: Viewer,
128}
129
130/// `get_by_id`. Returns `Outcome<Repo>`.
131#[derive(Debug, Serialize, Deserialize)]
132pub struct GetByIdArgs {
133 pub id: String,
134 pub viewer: Viewer,
135}
136
137/// `list`: repos the viewer may see, newest first. Returns `Vec<Repo>`.
138#[derive(Debug, Default, Serialize, Deserialize)]
139#[serde(rename_all = "camelCase")]
140pub struct ListArgs {
141 pub viewer: Viewer,
142 #[serde(default)]
143 pub query: Option<String>,
144 /// Only repos in this workspace.
145 #[serde(default)]
146 pub namespace: Option<String>,
147 /// Only repos in workspaces the viewer belongs to.
148 #[serde(default)]
149 pub member_only: bool,
150}
151
152/// `create`. Returns `Outcome<Repo>`.
153#[derive(Debug, Serialize, Deserialize)]
154#[serde(rename_all = "camelCase")]
155pub struct CreateArgs {
156 /// Who is creating it; they must belong to the workspace.
157 pub owner: User,
158 /// The workspace it is created in.
159 pub namespace: String,
160 pub name: String,
161 #[serde(default)]
162 pub description: Option<String>,
163 #[serde(default)]
164 pub is_private: bool,
165}
166
167/// `tree`. Returns `Outcome<TreeView>`.
168#[derive(Debug, Serialize, Deserialize)]
169#[serde(rename_all = "camelCase")]
170pub struct TreeArgs {
171 pub path: RepoPath,
172 pub viewer: Viewer,
173 /// The default branch when absent.
174 #[serde(default, rename = "ref")]
175 pub git_ref: Option<String>,
176 #[serde(default)]
177 pub tree_path: String,
178}
179
180/// `blob`. Returns `Outcome<BlobView>`.
181#[derive(Debug, Serialize, Deserialize)]
182#[serde(rename_all = "camelCase")]
183pub struct BlobArgs {
184 pub path: RepoPath,
185 pub viewer: Viewer,
186 #[serde(rename = "ref")]
187 pub git_ref: String,
188 pub file_path: String,
189}
190
191/// `log`. Returns `Outcome<Vec<Commit>>`.
192#[derive(Debug, Serialize, Deserialize)]
193pub struct LogArgs {
194 pub path: RepoPath,
195 pub viewer: Viewer,
196 #[serde(default, rename = "ref")]
197 pub git_ref: Option<String>,
198 pub limit: u32,
199}
200
201/// `fork_for_attempt`: a copy-on-write copy of the source repo, hidden from
202/// listings, for one attempt to work in. Returns `Outcome<Repo>`.
203#[derive(Debug, Serialize, Deserialize)]
204#[serde(rename_all = "camelCase")]
205pub struct ForkArgs {
206 pub source_id: String,
207 pub attempt_id: String,
208 pub actor: User,
209}
210
211/// `git_access`: authorizes a git operation and says where to send it.
212/// Pushing to a repo that does not exist creates it in the pusher's own
213/// namespace. Returns `Outcome<GitAccess>`.
214#[derive(Debug, Serialize, Deserialize)]
215pub struct GitAccessArgs {
216 pub path: RepoPath,
217 pub viewer: Viewer,
218 pub service: GitService,
219}
220
221/// `land`: moves the default branch of the repo a fork came from to the
222/// fork's head. Refused with `conflict` when the fork is behind, since
223/// that would discard commits. Returns `Outcome<Landed>`.
224#[derive(Debug, Serialize, Deserialize)]
225#[serde(rename_all = "camelCase")]
226pub struct LandArgs {
227 pub fork_id: String,
228 pub actor: User,
229}
230
231#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
232#[serde(rename_all = "lowercase")]
233pub enum FileStatus {
234 Added,
235 Modified,
236 Deleted,
237}
238
239#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
240#[serde(rename_all = "lowercase")]
241pub enum LineKind {
242 /// Unchanged, shown for context.
243 Context,
244 Add,
245 Delete,
246}
247
248#[derive(Clone, Debug, Serialize, Deserialize)]
249pub struct DiffLine {
250 pub kind: LineKind,
251 /// Line number in the old file; absent for added lines.
252 pub old: Option<u32>,
253 /// Line number in the new file; absent for deleted lines.
254 pub new: Option<u32>,
255 pub text: String,
256}
257
258/// A run of changed lines with their surrounding context.
259#[derive(Clone, Debug, Serialize, Deserialize)]
260pub struct Hunk {
261 pub lines: Vec<DiffLine>,
262}
263
264#[derive(Clone, Debug, Serialize, Deserialize)]
265pub struct FileDiff {
266 pub path: String,
267 pub status: FileStatus,
268 pub additions: u32,
269 pub deletions: u32,
270 /// True when the file is binary or too large, so no lines are shown.
271 pub binary: bool,
272 pub hunks: Vec<Hunk>,
273}
274
275/// What changed between two commits.
276#[derive(Clone, Debug, Serialize, Deserialize)]
277pub struct Comparison {
278 /// Null when the head has no earlier commit to compare against.
279 pub base: Option<String>,
280 pub head: String,
281 pub files: Vec<FileDiff>,
282 /// True when the change was too large to return in full.
283 pub truncated: bool,
284}
285
286/// `compare`: what a repository's head changes.
287///
288/// With no `base`, an attempt's fork is compared against the point where it
289/// and the repository it came from last agreed, and any other repository
290/// against its head's parent. Returns `Outcome<Comparison>`.
291#[derive(Debug, Serialize, Deserialize)]
292#[serde(rename_all = "camelCase")]
293pub struct CompareArgs {
294 pub repo_id: String,
295 pub viewer: Viewer,
296 #[serde(default)]
297 pub base: Option<String>,
298}