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

ambee/giterated

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

Add total amount of branches after filter

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨c29b3b8

⁨giterated-models/src/repository/operations.rs⁩ - ⁨14187⁩ 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, RepositoryTreeEntry, RepositoryView,
13 };
14
15 /// A request to get a repository's information.
16 ///
17 /// # Authentication
18 /// - Instance Authentication
19 /// - Validate request against the `issued_for` public key
20 /// - Validate User token against the user's instance's public key
21 /// # Authorization
22 /// - User Authorization
23 /// - Potential User permissions checks
24 #[derive(Clone, Debug, Serialize, Deserialize)]
25 pub struct RepositoryInfoRequest {
26 pub extra_metadata: bool,
27 pub rev: Option<String>,
28 pub path: Option<String>,
29 }
30
31 impl GiteratedOperation<Repository> for RepositoryInfoRequest {
32 type Success = RepositoryView;
33 type Failure = RepositoryError;
34 }
35
36 /// A request to get a file from a repository using the given id.
37 ///
38 /// # Authentication
39 /// - Instance Authentication
40 /// - Validate request against the `issued_for` public key
41 /// - Validate User token against the user's instance's public key
42 /// # Authorization
43 /// - User Authorization
44 /// - Potential User permissions checks
45 #[derive(Clone, Debug, Serialize, Deserialize)]
46 pub struct RepositoryFileFromIdRequest(pub String);
47
48 impl GiteratedOperation<Repository> for RepositoryFileFromIdRequest {
49 type Success = RepositoryFile;
50 type Failure = RepositoryError;
51 }
52
53 /// A request to get a file from a repository using the given relative path.
54 /// Also returns the commit id of the rev.
55 ///
56 /// # Authentication
57 /// - Instance Authentication
58 /// - Validate request against the `issued_for` public key
59 /// - Validate User token against the user's instance's public key
60 /// # Authorization
61 /// - User Authorization
62 /// - Potential User permissions checks
63 #[derive(Clone, Debug, Serialize, Deserialize)]
64 pub struct RepositoryFileFromPathRequest {
65 pub rev: Option<String>,
66 pub path: String,
67 }
68
69 impl GiteratedOperation<Repository> for RepositoryFileFromPathRequest {
70 type Success = (RepositoryFile, String);
71 type Failure = RepositoryError;
72 }
73
74 /// A request to get the last commit made to a file, using a starting point and the path.
75 ///
76 /// # Authentication
77 /// - Instance Authentication
78 /// - Validate request against the `issued_for` public key
79 /// - Validate User token against the user's instance's public key
80 /// # Authorization
81 /// - User Authorization
82 /// - Potential User permissions checks
83 #[derive(Clone, Debug, Serialize, Deserialize)]
84 pub struct RepositoryLastCommitOfFileRequest {
85 /// ID of commit to start at
86 pub start_commit: String,
87 // Path of the file
88 pub path: String,
89 }
90
91 impl GiteratedOperation<Repository> for RepositoryLastCommitOfFileRequest {
92 type Success = Commit;
93 type Failure = RepositoryError;
94 }
95
96 /// A request to get the commit by the specified id.
97 ///
98 /// # Authentication
99 /// - Instance Authentication
100 /// - Validate request against the `issued_for` public key
101 /// - Validate User token against the user's instance's public key
102 /// # Authorization
103 /// - User Authorization
104 /// - Potential User permissions checks
105 #[derive(Clone, Debug, Serialize, Deserialize)]
106 pub struct RepositoryCommitFromIdRequest(pub String);
107
108 impl GiteratedOperation<Repository> for RepositoryCommitFromIdRequest {
109 type Success = Commit;
110 type Failure = RepositoryError;
111 }
112
113 /// A request to get the difference between two repository trees.
114 ///
115 /// # Authentication
116 /// - Instance Authentication
117 /// - Validate request against the `issued_for` public key
118 /// - Validate User token against the user's instance's public key
119 /// # Authorization
120 /// - User Authorization
121 /// - Potential User permissions checks
122 #[derive(Clone, Debug, Serialize, Deserialize)]
123 pub struct RepositoryDiffRequest {
124 pub old_id: String,
125 pub new_id: String,
126 }
127
128 impl GiteratedOperation<Repository> for RepositoryDiffRequest {
129 type Success = RepositoryDiff;
130 type Failure = RepositoryError;
131 }
132
133 /// A request to get the difference between two repository trees as a unified git patch.
134 ///
135 /// # Authentication
136 /// - Instance Authentication
137 /// - Validate request against the `issued_for` public key
138 /// - Validate User token against the user's instance's public key
139 /// # Authorization
140 /// - User Authorization
141 /// - Potential User permissions checks
142 #[derive(Clone, Debug, Serialize, Deserialize)]
143 pub struct RepositoryDiffPatchRequest {
144 pub old_id: String,
145 pub new_id: String,
146 }
147
148 impl GiteratedOperation<Repository> for RepositoryDiffPatchRequest {
149 type Success = String;
150 type Failure = RepositoryError;
151 }
152
153 /// A request to get the commit before the one with the passed id
154 ///
155 /// # Authentication
156 /// - Instance Authentication
157 /// - Validate request against the `issued_for` public key
158 /// - Validate User token against the user's instance's public key
159 /// # Authorization
160 /// - User Authorization
161 /// - Potential User permissions checks
162 #[derive(Clone, Debug, Serialize, Deserialize)]
163 pub struct RepositoryCommitBeforeRequest(pub String);
164
165 impl GiteratedOperation<Repository> for RepositoryCommitBeforeRequest {
166 type Success = Commit;
167 type Failure = RepositoryError;
168 }
169
170 #[derive(Clone, Debug, Serialize, Deserialize)]
171 pub struct RepositoryIssuesCountRequest;
172
173 impl GiteratedOperation<Repository> for RepositoryIssuesCountRequest {
174 type Success = u64;
175 type Failure = RepositoryError;
176 }
177
178 /// A request to get a repository's issues count.
179 ///
180 /// # Authentication
181 /// - Instance Authentication
182 /// - Validate request against the `issued_for` public key
183 /// - Validate User token against the user's instance's public key
184 /// # Authorization
185 /// - User Authorization
186 /// - Potential User permissions checks
187 #[derive(Clone, Debug, Serialize, Deserialize)]
188 pub struct RepositoryIssueLabelsRequest;
189
190 impl GiteratedOperation<Repository> for RepositoryIssueLabelsRequest {
191 type Success = Vec<IssueLabel>;
192 type Failure = RepositoryError;
193 }
194
195 /// A request to get a repository's issue labels.
196 ///
197 /// # Authentication
198 /// - Instance Authentication
199 /// - Validate request against the `issued_for` public key
200 /// - Validate User token against the user's instance's public key
201 /// # Authorization
202 /// - User Authorization
203 /// - Potential User permissions checks
204 #[derive(Clone, Debug, Serialize, Deserialize)]
205 pub struct RepositoryIssuesRequest;
206
207 impl GiteratedOperation<Repository> for RepositoryIssuesRequest {
208 type Success = Vec<RepositoryIssue>;
209 type Failure = RepositoryError;
210 }
211
212 /// A request to inspect the tree of a repository.
213 ///
214 /// # Authentication
215 /// - Instance Authentication
216 /// - Validate request against the `issued_for` public key
217 /// - Validate User token against the user's instance's public key
218 /// # Authorization
219 /// - User Authorization
220 /// - Potential User permissions checks
221 #[derive(Clone, Debug, Serialize, Deserialize)]
222 pub struct RepositoryFileInspectRequest {
223 /// Whether to get extra metadata for every entry (file mode, size and last commit made to it).
224 pub extra_metadata: bool,
225 /// Revision of the repository to get (branch, commit id).
226 pub rev: Option<String>,
227 /// If not given a path, it'll default to the base.
228 pub path: Option<String>,
229 }
230
231 impl GiteratedOperation<Repository> for RepositoryFileInspectRequest {
232 type Success = Vec<RepositoryTreeEntry>;
233 type Failure = RepositoryError;
234 }
235
236 /// A request to get the statistics of repository.
237 ///
238 /// # Authentication
239 /// - Instance Authentication
240 /// - Validate request against the `issued_for` public key
241 /// - Validate User token against the user's instance's public key
242 /// # Authorization
243 /// - User Authorization
244 /// - Potential User permissions checks
245 #[derive(Clone, Debug, Serialize, Deserialize)]
246 pub struct RepositoryStatisticsRequest {
247 /// Revision of the repository to get (branch, commit id).
248 pub rev: Option<String>,
249 }
250
251 impl GiteratedOperation<Repository> for RepositoryStatisticsRequest {
252 type Success = RepositoryStatistics;
253 type Failure = RepositoryError;
254 }
255
256 /// A request to get a list of branches in the repository.
257 /// Also returns the total amount of branches after the filter is applied.
258 /// Skips over references with invalid UTF-8 names.
259 ///
260 /// # Authentication
261 /// - Instance Authentication
262 /// - Validate request against the `issued_for` public key
263 /// - Validate User token against the user's instance's public key
264 /// # Authorization
265 /// - User Authorization
266 /// - Potential User permissions checks
267 #[derive(Clone, Debug, Serialize, Deserialize)]
268 pub struct RepositoryBranchesRequest {
269 pub filter: RepositoryBranchFilter,
270 pub range: (usize, usize),
271 }
272
273 impl GiteratedOperation<Repository> for RepositoryBranchesRequest {
274 type Success = (Vec<RepositoryBranch>, usize);
275 type Failure = RepositoryError;
276 }
277
278 impl<S: Clone + Send + Sync, B: ObjectBackend<S> + std::fmt::Debug> Object<'_, S, Repository, B> {
279 pub async fn info(
280 &mut self,
281 extra_metadata: bool,
282 rev: Option<String>,
283 path: Option<String>,
284 operation_state: &S,
285 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
286 self.request::<RepositoryInfoRequest>(
287 RepositoryInfoRequest {
288 extra_metadata,
289 rev,
290 path,
291 },
292 operation_state,
293 )
294 .await
295 }
296
297 pub async fn file_from_id(
298 &mut self,
299 id: String,
300 operation_state: &S,
301 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
302 self.request::<RepositoryFileFromIdRequest>(
303 RepositoryFileFromIdRequest(id),
304 operation_state,
305 )
306 .await
307 }
308
309 pub async fn file_from_path(
310 &mut self,
311 rev: Option<String>,
312 path: String,
313 operation_state: &S,
314 ) -> Result<(RepositoryFile, String), OperationError<RepositoryError>> {
315 self.request::<RepositoryFileFromPathRequest>(
316 RepositoryFileFromPathRequest { rev, path },
317 operation_state,
318 )
319 .await
320 }
321
322 pub async fn last_commit_of_file(
323 &mut self,
324 start_commit: String,
325 path: String,
326 operation_state: &S,
327 ) -> Result<Commit, OperationError<RepositoryError>> {
328 self.request::<RepositoryLastCommitOfFileRequest>(
329 RepositoryLastCommitOfFileRequest { start_commit, path },
330 operation_state,
331 )
332 .await
333 }
334
335 pub async fn commit_by_id(
336 &mut self,
337 id: String,
338 operation_state: &S,
339 ) -> Result<Commit, OperationError<RepositoryError>> {
340 self.request::<RepositoryCommitFromIdRequest>(
341 RepositoryCommitFromIdRequest(id),
342 operation_state,
343 )
344 .await
345 }
346
347 pub async fn diff(
348 &mut self,
349 old_id: String,
350 new_id: String,
351 operation_state: &S,
352 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
353 self.request::<RepositoryDiffRequest>(
354 RepositoryDiffRequest { old_id, new_id },
355 operation_state,
356 )
357 .await
358 }
359
360 pub async fn diff_patch(
361 &mut self,
362 old_id: String,
363 new_id: String,
364 operation_state: &S,
365 ) -> Result<String, OperationError<RepositoryError>> {
366 self.request::<RepositoryDiffPatchRequest>(
367 RepositoryDiffPatchRequest { old_id, new_id },
368 operation_state,
369 )
370 .await
371 }
372
373 pub async fn commit_before(
374 &mut self,
375 id: String,
376 operation_state: &S,
377 ) -> Result<Commit, OperationError<RepositoryError>> {
378 self.request::<RepositoryCommitBeforeRequest>(
379 RepositoryCommitBeforeRequest(id),
380 operation_state,
381 )
382 .await
383 }
384
385 pub async fn statistics(
386 &mut self,
387 rev: Option<String>,
388 operation_state: &S,
389 ) -> Result<RepositoryStatistics, OperationError<RepositoryError>> {
390 self.request::<RepositoryStatisticsRequest>(
391 RepositoryStatisticsRequest { rev },
392 operation_state,
393 )
394 .await
395 }
396
397 pub async fn branches(
398 &mut self,
399 filter: RepositoryBranchFilter,
400 range_start: usize,
401 range_end: usize,
402 operation_state: &S,
403 ) -> Result<(Vec<RepositoryBranch>, usize), OperationError<RepositoryError>> {
404 self.request::<RepositoryBranchesRequest>(
405 RepositoryBranchesRequest {
406 filter,
407 range: (range_start, range_end),
408 },
409 operation_state,
410 )
411 .await
412 }
413
414 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {
415 // self.request::<RepositoryIssuesCountRequest>(RepositoryIssuesCountRequest)
416 // .await
417 // }
418
419 pub async fn issue_labels(
420 &mut self,
421 operation_state: &S,
422 ) -> Result<Vec<IssueLabel>, OperationError<RepositoryError>> {
423 self.request::<RepositoryIssueLabelsRequest>(RepositoryIssueLabelsRequest, operation_state)
424 .await
425 }
426
427 pub async fn issues(
428 &mut self,
429 operation_state: &S,
430 ) -> Result<Vec<RepositoryIssue>, OperationError<RepositoryError>> {
431 self.request::<RepositoryIssuesRequest>(RepositoryIssuesRequest, operation_state)
432 .await
433 }
434
435 pub async fn inspect_files(
436 &mut self,
437 extra_metadata: bool,
438 rev: Option<&str>,
439 path: Option<&str>,
440 operation_state: &S,
441 ) -> Result<Vec<RepositoryTreeEntry>, OperationError<RepositoryError>> {
442 self.request::<RepositoryFileInspectRequest>(
443 RepositoryFileInspectRequest {
444 extra_metadata,
445 rev: rev.map(|r| r.to_string()),
446 path: path.map(|p| p.to_string()),
447 },
448 operation_state,
449 )
450 .await
451 }
452 }
453