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

ambee/giterated

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

Basic tag search and range

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨112ca96

⁨giterated-models/src/repository/operations.rs⁩ - ⁨16616⁩ bytes
Raw
1 use serde::{Deserialize, Serialize};
2
3 use crate::{
4 error::{OperationError, RepositoryError},
5 object::Object,
6 object_backend::ObjectBackend,
7 operation::GiteratedOperation,
8 };
9
10 use super::{
11 Commit, IssueLabel, Repository, RepositoryBranch, RepositoryBranchFilter, RepositoryDiff,
12 RepositoryFile, RepositoryIssue, RepositoryStatistics, RepositoryTag, RepositoryTreeEntry,
13 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 issues count.
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 RepositoryIssueLabelsRequest;
190
191 impl GiteratedOperation<Repository> for RepositoryIssueLabelsRequest {
192 type Success = Vec<IssueLabel>;
193 type Failure = RepositoryError;
194 }
195
196 /// A request to get a repository's issue labels.
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<RepositoryIssue>;
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 impl<S: Clone + Send + Sync, B: ObjectBackend<S> + std::fmt::Debug> Object<'_, S, Repository, B> {
329 pub async fn info(
330 &mut self,
331 extra_metadata: bool,
332 rev: Option<String>,
333 path: Option<String>,
334 operation_state: &S,
335 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
336 self.request::<RepositoryInfoRequest>(
337 RepositoryInfoRequest {
338 extra_metadata,
339 rev,
340 path,
341 },
342 operation_state,
343 )
344 .await
345 }
346
347 pub async fn file_from_id(
348 &mut self,
349 id: String,
350 operation_state: &S,
351 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
352 self.request::<RepositoryFileFromIdRequest>(
353 RepositoryFileFromIdRequest(id),
354 operation_state,
355 )
356 .await
357 }
358
359 pub async fn file_from_path(
360 &mut self,
361 rev: Option<String>,
362 path: String,
363 operation_state: &S,
364 ) -> Result<(RepositoryFile, String), OperationError<RepositoryError>> {
365 self.request::<RepositoryFileFromPathRequest>(
366 RepositoryFileFromPathRequest { rev, path },
367 operation_state,
368 )
369 .await
370 }
371
372 pub async fn last_commit_of_file(
373 &mut self,
374 start_commit: String,
375 path: String,
376 operation_state: &S,
377 ) -> Result<Commit, OperationError<RepositoryError>> {
378 self.request::<RepositoryLastCommitOfFileRequest>(
379 RepositoryLastCommitOfFileRequest { start_commit, path },
380 operation_state,
381 )
382 .await
383 }
384
385 pub async fn commit_by_id(
386 &mut self,
387 id: String,
388 operation_state: &S,
389 ) -> Result<Commit, OperationError<RepositoryError>> {
390 self.request::<RepositoryCommitFromIdRequest>(
391 RepositoryCommitFromIdRequest(id),
392 operation_state,
393 )
394 .await
395 }
396
397 pub async fn diff(
398 &mut self,
399 old_id: String,
400 new_id: String,
401 operation_state: &S,
402 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
403 self.request::<RepositoryDiffRequest>(
404 RepositoryDiffRequest { old_id, new_id },
405 operation_state,
406 )
407 .await
408 }
409
410 pub async fn diff_patch(
411 &mut self,
412 old_id: String,
413 new_id: String,
414 operation_state: &S,
415 ) -> Result<String, OperationError<RepositoryError>> {
416 self.request::<RepositoryDiffPatchRequest>(
417 RepositoryDiffPatchRequest { old_id, new_id },
418 operation_state,
419 )
420 .await
421 }
422
423 pub async fn commit_before(
424 &mut self,
425 id: String,
426 operation_state: &S,
427 ) -> Result<Commit, OperationError<RepositoryError>> {
428 self.request::<RepositoryCommitBeforeRequest>(
429 RepositoryCommitBeforeRequest(id),
430 operation_state,
431 )
432 .await
433 }
434
435 pub async fn statistics(
436 &mut self,
437 rev: Option<String>,
438 operation_state: &S,
439 ) -> Result<RepositoryStatistics, OperationError<RepositoryError>> {
440 self.request::<RepositoryStatisticsRequest>(
441 RepositoryStatisticsRequest { rev },
442 operation_state,
443 )
444 .await
445 }
446
447 pub async fn branches(
448 &mut self,
449 filter: RepositoryBranchFilter,
450 range_start: usize,
451 range_end: usize,
452 search: Option<String>,
453 operation_state: &S,
454 ) -> Result<(Vec<RepositoryBranch>, usize), OperationError<RepositoryError>> {
455 self.request::<RepositoryBranchesRequest>(
456 RepositoryBranchesRequest {
457 filter,
458 range: (range_start, range_end),
459 search,
460 },
461 operation_state,
462 )
463 .await
464 }
465
466 pub async fn branch(
467 &mut self,
468 name: &str,
469 operation_state: &S,
470 ) -> Result<RepositoryBranch, OperationError<RepositoryError>> {
471 self.request::<RepositoryBranchRequest>(
472 RepositoryBranchRequest {
473 name: name.to_string(),
474 },
475 operation_state,
476 )
477 .await
478 }
479
480 pub async fn tags(
481 &mut self,
482 range_start: usize,
483 range_end: usize,
484 search: Option<String>,
485 operation_state: &S,
486 ) -> Result<(Vec<RepositoryTag>, usize), OperationError<RepositoryError>> {
487 self.request::<RepositoryTagsRequest>(
488 RepositoryTagsRequest {
489 range: (range_start, range_end),
490 search,
491 },
492 operation_state,
493 )
494 .await
495 }
496
497 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {
498 // self.request::<RepositoryIssuesCountRequest>(RepositoryIssuesCountRequest)
499 // .await
500 // }
501
502 pub async fn issue_labels(
503 &mut self,
504 operation_state: &S,
505 ) -> Result<Vec<IssueLabel>, OperationError<RepositoryError>> {
506 self.request::<RepositoryIssueLabelsRequest>(RepositoryIssueLabelsRequest, operation_state)
507 .await
508 }
509
510 pub async fn issues(
511 &mut self,
512 operation_state: &S,
513 ) -> Result<Vec<RepositoryIssue>, OperationError<RepositoryError>> {
514 self.request::<RepositoryIssuesRequest>(RepositoryIssuesRequest, operation_state)
515 .await
516 }
517
518 pub async fn inspect_files(
519 &mut self,
520 extra_metadata: bool,
521 rev: Option<&str>,
522 path: Option<&str>,
523 operation_state: &S,
524 ) -> Result<Vec<RepositoryTreeEntry>, OperationError<RepositoryError>> {
525 self.request::<RepositoryFileInspectRequest>(
526 RepositoryFileInspectRequest {
527 extra_metadata,
528 rev: rev.map(|r| r.to_string()),
529 path: path.map(|p| p.to_string()),
530 },
531 operation_state,
532 )
533 .await
534 }
535 }
536