JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Make old operations use new types

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨b22dd12

⁨giterated-models/src/repository/operations.rs⁩ - ⁨17509⁩ bytes
Raw
1 use serde::{Deserialize, Serialize};
2
3 use crate::{
4 error::{OperationError, RepositoryError},
5 issue::{IssueInfoBasic, IssueTag},
6 object::Object,
7 object_backend::ObjectBackend,
8 operation::GiteratedOperation,
9 };
10
11 use super::{
12 Commit, Repository, RepositoryBranch, RepositoryBranchFilter, RepositoryDiff, RepositoryFile,
13 RepositoryStatistics, RepositoryTag, RepositoryTreeEntry, RepositoryView,
14 };
15
16 /// A request to get a repository's information.
17 ///
18 /// # Authentication
19 /// - Instance Authentication
20 /// - Validate request against the `issued_for` public key
21 /// - Validate User token against the user's instance's public key
22 /// # Authorization
23 /// - User Authorization
24 /// - Potential User permissions checks
25 #[derive(Clone, Debug, Serialize, Deserialize)]
26 pub struct RepositoryInfoRequest {
27 pub extra_metadata: bool,
28 pub rev: Option<String>,
29 pub path: Option<String>,
30 }
31
32 impl GiteratedOperation<Repository> for RepositoryInfoRequest {
33 type Success = RepositoryView;
34 type Failure = RepositoryError;
35 }
36
37 /// A request to get a file from a repository using the given id.
38 ///
39 /// # Authentication
40 /// - Instance Authentication
41 /// - Validate request against the `issued_for` public key
42 /// - Validate User token against the user's instance's public key
43 /// # Authorization
44 /// - User Authorization
45 /// - Potential User permissions checks
46 #[derive(Clone, Debug, Serialize, Deserialize)]
47 pub struct RepositoryFileFromIdRequest(pub String);
48
49 impl GiteratedOperation<Repository> for RepositoryFileFromIdRequest {
50 type Success = RepositoryFile;
51 type Failure = RepositoryError;
52 }
53
54 /// A request to get a file from a repository using the given relative path.
55 /// Also returns the commit id of the rev.
56 ///
57 /// # Authentication
58 /// - Instance Authentication
59 /// - Validate request against the `issued_for` public key
60 /// - Validate User token against the user's instance's public key
61 /// # Authorization
62 /// - User Authorization
63 /// - Potential User permissions checks
64 #[derive(Clone, Debug, Serialize, Deserialize)]
65 pub struct RepositoryFileFromPathRequest {
66 pub rev: Option<String>,
67 pub path: String,
68 }
69
70 impl GiteratedOperation<Repository> for RepositoryFileFromPathRequest {
71 type Success = (RepositoryFile, String);
72 type Failure = RepositoryError;
73 }
74
75 /// A request to get the last commit made to a file, using a starting point and the path.
76 ///
77 /// # Authentication
78 /// - Instance Authentication
79 /// - Validate request against the `issued_for` public key
80 /// - Validate User token against the user's instance's public key
81 /// # Authorization
82 /// - User Authorization
83 /// - Potential User permissions checks
84 #[derive(Clone, Debug, Serialize, Deserialize)]
85 pub struct RepositoryLastCommitOfFileRequest {
86 /// ID of commit to start at
87 pub start_commit: String,
88 // Path of the file
89 pub path: String,
90 }
91
92 impl GiteratedOperation<Repository> for RepositoryLastCommitOfFileRequest {
93 type Success = Commit;
94 type Failure = RepositoryError;
95 }
96
97 /// A request to get the commit by the specified id.
98 ///
99 /// # Authentication
100 /// - Instance Authentication
101 /// - Validate request against the `issued_for` public key
102 /// - Validate User token against the user's instance's public key
103 /// # Authorization
104 /// - User Authorization
105 /// - Potential User permissions checks
106 #[derive(Clone, Debug, Serialize, Deserialize)]
107 pub struct RepositoryCommitFromIdRequest(pub String);
108
109 impl GiteratedOperation<Repository> for RepositoryCommitFromIdRequest {
110 type Success = Commit;
111 type Failure = RepositoryError;
112 }
113
114 /// A request to get the difference between two repository trees.
115 ///
116 /// # Authentication
117 /// - Instance Authentication
118 /// - Validate request against the `issued_for` public key
119 /// - Validate User token against the user's instance's public key
120 /// # Authorization
121 /// - User Authorization
122 /// - Potential User permissions checks
123 #[derive(Clone, Debug, Serialize, Deserialize)]
124 pub struct RepositoryDiffRequest {
125 pub old_id: String,
126 pub new_id: String,
127 }
128
129 impl GiteratedOperation<Repository> for RepositoryDiffRequest {
130 type Success = RepositoryDiff;
131 type Failure = RepositoryError;
132 }
133
134 /// A request to get the difference between two repository trees as a unified git patch.
135 ///
136 /// # Authentication
137 /// - Instance Authentication
138 /// - Validate request against the `issued_for` public key
139 /// - Validate User token against the user's instance's public key
140 /// # Authorization
141 /// - User Authorization
142 /// - Potential User permissions checks
143 #[derive(Clone, Debug, Serialize, Deserialize)]
144 pub struct RepositoryDiffPatchRequest {
145 pub old_id: String,
146 pub new_id: String,
147 }
148
149 impl GiteratedOperation<Repository> for RepositoryDiffPatchRequest {
150 type Success = String;
151 type Failure = RepositoryError;
152 }
153
154 /// A request to get the commit before the one with the passed id
155 ///
156 /// # Authentication
157 /// - Instance Authentication
158 /// - Validate request against the `issued_for` public key
159 /// - Validate User token against the user's instance's public key
160 /// # Authorization
161 /// - User Authorization
162 /// - Potential User permissions checks
163 #[derive(Clone, Debug, Serialize, Deserialize)]
164 pub struct RepositoryCommitBeforeRequest(pub String);
165
166 impl GiteratedOperation<Repository> for RepositoryCommitBeforeRequest {
167 type Success = Commit;
168 type Failure = RepositoryError;
169 }
170
171 #[derive(Clone, Debug, Serialize, Deserialize)]
172 pub struct RepositoryIssuesCountRequest;
173
174 impl GiteratedOperation<Repository> for RepositoryIssuesCountRequest {
175 type Success = u64;
176 type Failure = RepositoryError;
177 }
178
179 /// A request to get a repository's issue tags.
180 ///
181 /// # Authentication
182 /// - Instance Authentication
183 /// - Validate request against the `issued_for` public key
184 /// - Validate User token against the user's instance's public key
185 /// # Authorization
186 /// - User Authorization
187 /// - Potential User permissions checks
188 #[derive(Clone, Debug, Serialize, Deserialize)]
189 pub struct RepositoryIssueTagsRequest;
190
191 impl GiteratedOperation<Repository> for RepositoryIssueTagsRequest {
192 type Success = Vec<IssueTag>;
193 type Failure = RepositoryError;
194 }
195
196 /// A request to get a repository's issues.
197 ///
198 /// # Authentication
199 /// - Instance Authentication
200 /// - Validate request against the `issued_for` public key
201 /// - Validate User token against the user's instance's public key
202 /// # Authorization
203 /// - User Authorization
204 /// - Potential User permissions checks
205 #[derive(Clone, Debug, Serialize, Deserialize)]
206 pub struct RepositoryIssuesRequest;
207
208 impl GiteratedOperation<Repository> for RepositoryIssuesRequest {
209 type Success = Vec<IssueInfoBasic>;
210 type Failure = RepositoryError;
211 }
212
213 /// A request to inspect the tree of a repository.
214 ///
215 /// # Authentication
216 /// - Instance Authentication
217 /// - Validate request against the `issued_for` public key
218 /// - Validate User token against the user's instance's public key
219 /// # Authorization
220 /// - User Authorization
221 /// - Potential User permissions checks
222 #[derive(Clone, Debug, Serialize, Deserialize)]
223 pub struct RepositoryFileInspectRequest {
224 /// Whether to get extra metadata for every entry (file mode, size and last commit made to it).
225 pub extra_metadata: bool,
226 /// Revision of the repository to get (branch, commit id).
227 pub rev: Option<String>,
228 /// If not given a path, it'll default to the base.
229 pub path: Option<String>,
230 }
231
232 impl GiteratedOperation<Repository> for RepositoryFileInspectRequest {
233 type Success = Vec<RepositoryTreeEntry>;
234 type Failure = RepositoryError;
235 }
236
237 /// A request to get the statistics of repository.
238 ///
239 /// # Authentication
240 /// - Instance Authentication
241 /// - Validate request against the `issued_for` public key
242 /// - Validate User token against the user's instance's public key
243 /// # Authorization
244 /// - User Authorization
245 /// - Potential User permissions checks
246 #[derive(Clone, Debug, Serialize, Deserialize)]
247 pub struct RepositoryStatisticsRequest {
248 /// Revision of the repository to get (branch, commit id).
249 pub rev: Option<String>,
250 }
251
252 impl GiteratedOperation<Repository> for RepositoryStatisticsRequest {
253 type Success = RepositoryStatistics;
254 type Failure = RepositoryError;
255 }
256
257 /// A request to get a list of branches in the repository.
258 /// Also returns the total amount of branches after the filter is applied.
259 ///
260 /// Optional search parameter that'll search through the filtered branches
261 ///
262 /// Skips over references with invalid UTF-8 names.
263 ///
264 /// # Authentication
265 /// - Instance Authentication
266 /// - Validate request against the `issued_for` public key
267 /// - Validate User token against the user's instance's public key
268 /// # Authorization
269 /// - User Authorization
270 /// - Potential User permissions checks
271 #[derive(Clone, Debug, Serialize, Deserialize)]
272 pub struct RepositoryBranchesRequest {
273 pub filter: RepositoryBranchFilter,
274 pub range: (usize, usize),
275 // pub sort: Option<???>
276 pub search: Option<String>,
277 }
278
279 impl GiteratedOperation<Repository> for RepositoryBranchesRequest {
280 type Success = (Vec<RepositoryBranch>, usize);
281 type Failure = RepositoryError;
282 }
283
284 /// A request to get a specific branch using its name
285 ///
286 /// # Authentication
287 /// - Instance Authentication
288 /// - Validate request against the `issued_for` public key
289 /// - Validate User token against the user's instance's public key
290 /// # Authorization
291 /// - User Authorization
292 /// - Potential User permissions checks
293 #[derive(Clone, Debug, Serialize, Deserialize)]
294 pub struct RepositoryBranchRequest {
295 pub name: String,
296 }
297
298 impl GiteratedOperation<Repository> for RepositoryBranchRequest {
299 type Success = RepositoryBranch;
300 type Failure = RepositoryError;
301 }
302
303 /// A request to get a list of tags in the repository.
304 /// Also returns the total amount of tags.
305 ///
306 /// Optional search parameter that'll search through the tags
307 ///
308 /// # Authentication
309 /// - Instance Authentication
310 /// - Validate request against the `issued_for` public key
311 /// - Validate User token against the user's instance's public key
312 /// # Authorization
313 /// - User Authorization
314 /// - Potential User permissions checks
315 #[derive(Clone, Debug, Serialize, Deserialize)]
316 pub struct RepositoryTagsRequest {
317 // pub filter: Option<???>,
318 pub range: (usize, usize),
319 // pub sort: Option<???>,
320 pub search: Option<String>,
321 }
322
323 impl GiteratedOperation<Repository> for RepositoryTagsRequest {
324 type Success = (Vec<RepositoryTag>, usize);
325 type Failure = RepositoryError;
326 }
327
328 /// A request to get a single tag by name.
329 ///
330 /// # Authentication
331 /// - Instance Authentication
332 /// - Validate request against the `issued_for` public key
333 /// - Validate User token against the user's instance's public key
334 /// # Authorization
335 /// - User Authorization
336 /// - Potential User permissions checks
337 #[derive(Clone, Debug, Serialize, Deserialize)]
338 pub struct RepositoryTagRequest {
339 pub name: String,
340 }
341
342 impl GiteratedOperation<Repository> for RepositoryTagRequest {
343 type Success = RepositoryTag;
344 type Failure = RepositoryError;
345 }
346
347 impl<S: Clone + Send + Sync, B: ObjectBackend<S> + std::fmt::Debug> Object<'_, S, Repository, B> {
348 pub async fn info(
349 &mut self,
350 extra_metadata: bool,
351 rev: Option<String>,
352 path: Option<String>,
353 operation_state: &S,
354 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
355 self.request::<RepositoryInfoRequest>(
356 RepositoryInfoRequest {
357 extra_metadata,
358 rev,
359 path,
360 },
361 operation_state,
362 )
363 .await
364 }
365
366 pub async fn file_from_id(
367 &mut self,
368 id: String,
369 operation_state: &S,
370 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
371 self.request::<RepositoryFileFromIdRequest>(
372 RepositoryFileFromIdRequest(id),
373 operation_state,
374 )
375 .await
376 }
377
378 pub async fn file_from_path(
379 &mut self,
380 rev: Option<String>,
381 path: String,
382 operation_state: &S,
383 ) -> Result<(RepositoryFile, String), OperationError<RepositoryError>> {
384 self.request::<RepositoryFileFromPathRequest>(
385 RepositoryFileFromPathRequest { rev, path },
386 operation_state,
387 )
388 .await
389 }
390
391 pub async fn last_commit_of_file(
392 &mut self,
393 start_commit: String,
394 path: String,
395 operation_state: &S,
396 ) -> Result<Commit, OperationError<RepositoryError>> {
397 self.request::<RepositoryLastCommitOfFileRequest>(
398 RepositoryLastCommitOfFileRequest { start_commit, path },
399 operation_state,
400 )
401 .await
402 }
403
404 pub async fn commit_by_id(
405 &mut self,
406 id: String,
407 operation_state: &S,
408 ) -> Result<Commit, OperationError<RepositoryError>> {
409 self.request::<RepositoryCommitFromIdRequest>(
410 RepositoryCommitFromIdRequest(id),
411 operation_state,
412 )
413 .await
414 }
415
416 pub async fn diff(
417 &mut self,
418 old_id: String,
419 new_id: String,
420 operation_state: &S,
421 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
422 self.request::<RepositoryDiffRequest>(
423 RepositoryDiffRequest { old_id, new_id },
424 operation_state,
425 )
426 .await
427 }
428
429 pub async fn diff_patch(
430 &mut self,
431 old_id: String,
432 new_id: String,
433 operation_state: &S,
434 ) -> Result<String, OperationError<RepositoryError>> {
435 self.request::<RepositoryDiffPatchRequest>(
436 RepositoryDiffPatchRequest { old_id, new_id },
437 operation_state,
438 )
439 .await
440 }
441
442 pub async fn commit_before(
443 &mut self,
444 id: String,
445 operation_state: &S,
446 ) -> Result<Commit, OperationError<RepositoryError>> {
447 self.request::<RepositoryCommitBeforeRequest>(
448 RepositoryCommitBeforeRequest(id),
449 operation_state,
450 )
451 .await
452 }
453
454 pub async fn statistics(
455 &mut self,
456 rev: Option<String>,
457 operation_state: &S,
458 ) -> Result<RepositoryStatistics, OperationError<RepositoryError>> {
459 self.request::<RepositoryStatisticsRequest>(
460 RepositoryStatisticsRequest { rev },
461 operation_state,
462 )
463 .await
464 }
465
466 pub async fn branches(
467 &mut self,
468 filter: RepositoryBranchFilter,
469 range_start: usize,
470 range_end: usize,
471 search: Option<String>,
472 operation_state: &S,
473 ) -> Result<(Vec<RepositoryBranch>, usize), OperationError<RepositoryError>> {
474 self.request::<RepositoryBranchesRequest>(
475 RepositoryBranchesRequest {
476 filter,
477 range: (range_start, range_end),
478 search,
479 },
480 operation_state,
481 )
482 .await
483 }
484
485 pub async fn branch(
486 &mut self,
487 name: &str,
488 operation_state: &S,
489 ) -> Result<RepositoryBranch, OperationError<RepositoryError>> {
490 self.request::<RepositoryBranchRequest>(
491 RepositoryBranchRequest {
492 name: name.to_string(),
493 },
494 operation_state,
495 )
496 .await
497 }
498
499 pub async fn tags(
500 &mut self,
501 range_start: usize,
502 range_end: usize,
503 search: Option<String>,
504 operation_state: &S,
505 ) -> Result<(Vec<RepositoryTag>, usize), OperationError<RepositoryError>> {
506 self.request::<RepositoryTagsRequest>(
507 RepositoryTagsRequest {
508 range: (range_start, range_end),
509 search,
510 },
511 operation_state,
512 )
513 .await
514 }
515
516 pub async fn tag(
517 &mut self,
518 name: &str,
519 operation_state: &S,
520 ) -> Result<RepositoryTag, OperationError<RepositoryError>> {
521 self.request::<RepositoryTagRequest>(
522 RepositoryTagRequest {
523 name: name.to_string(),
524 },
525 operation_state,
526 )
527 .await
528 }
529
530 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {
531 // self.request::<RepositoryIssuesCountRequest>(RepositoryIssuesCountRequest)
532 // .await
533 // }
534
535 pub async fn issue_tags(
536 &mut self,
537 operation_state: &S,
538 ) -> Result<Vec<IssueTag>, OperationError<RepositoryError>> {
539 self.request::<RepositoryIssueTagsRequest>(RepositoryIssueTagsRequest, operation_state)
540 .await
541 }
542
543 pub async fn issues(
544 &mut self,
545 operation_state: &S,
546 ) -> Result<Vec<IssueInfoBasic>, OperationError<RepositoryError>> {
547 self.request::<RepositoryIssuesRequest>(RepositoryIssuesRequest, operation_state)
548 .await
549 }
550
551 pub async fn inspect_files(
552 &mut self,
553 extra_metadata: bool,
554 rev: Option<&str>,
555 path: Option<&str>,
556 operation_state: &S,
557 ) -> Result<Vec<RepositoryTreeEntry>, OperationError<RepositoryError>> {
558 self.request::<RepositoryFileInspectRequest>(
559 RepositoryFileInspectRequest {
560 extra_metadata,
561 rev: rev.map(|r| r.to_string()),
562 path: path.map(|p| p.to_string()),
563 },
564 operation_state,
565 )
566 .await
567 }
568 }
569