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

ambee/giterated

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

Repository diff request and handler

Type: Feature

erremilia - ⁨2⁩ years ago

parent: tbd commit: ⁨502a11c

⁨giterated-models/src/repository/operations.rs⁩ - ⁨6392⁩ 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::{IssueLabel, Repository, RepositoryIssue, RepositoryTreeEntry, RepositoryView, RepositoryFile, RepositoryDiff};
11
12 /// A request to get a repository's information.
13 ///
14 /// # Authentication
15 /// - Instance Authentication
16 /// - Validate request against the `issued_for` public key
17 /// - Validate User token against the user's instance's public key
18 /// # Authorization
19 /// - User Authorization
20 /// - Potential User permissions checks
21 #[derive(Clone, Debug, Serialize, Deserialize)]
22 pub struct RepositoryInfoRequest {
23 pub extra_metadata: bool,
24 pub rev: Option<String>,
25 pub path: Option<String>,
26 }
27
28 impl GiteratedOperation<Repository> for RepositoryInfoRequest {
29 type Success = RepositoryView;
30 type Failure = RepositoryError;
31 }
32
33 /// A request to get a file from a repository using the given id.
34 ///
35 /// # Authentication
36 /// - Instance Authentication
37 /// - Validate request against the `issued_for` public key
38 /// - Validate User token against the user's instance's public key
39 /// # Authorization
40 /// - User Authorization
41 /// - Potential User permissions checks
42 #[derive(Clone, Debug, Serialize, Deserialize)]
43 pub struct RepositoryFileFromIdRequest(pub String);
44
45 impl GiteratedOperation<Repository> for RepositoryFileFromIdRequest {
46 type Success = RepositoryFile;
47 type Failure = RepositoryError;
48 }
49
50 /// A request to get the difference between two repository trees.
51 ///
52 /// # Authentication
53 /// - Instance Authentication
54 /// - Validate request against the `issued_for` public key
55 /// - Validate User token against the user's instance's public key
56 /// # Authorization
57 /// - User Authorization
58 /// - Potential User permissions checks
59 #[derive(Clone, Debug, Serialize, Deserialize)]
60 pub struct RepositoryDiffRequest {
61 pub old_id: String,
62 pub new_id: String,
63 }
64
65 impl GiteratedOperation<Repository> for RepositoryDiffRequest {
66 type Success = RepositoryDiff;
67 type Failure = RepositoryError;
68 }
69
70 #[derive(Clone, Debug, Serialize, Deserialize)]
71 pub struct RepositoryIssuesCountRequest;
72
73 impl GiteratedOperation<Repository> for RepositoryIssuesCountRequest {
74 type Success = u64;
75 type Failure = RepositoryError;
76 }
77
78 /// A request to get a repository's issues count.
79 ///
80 /// # Authentication
81 /// - Instance Authentication
82 /// - Validate request against the `issued_for` public key
83 /// - Validate User token against the user's instance's public key
84 /// # Authorization
85 /// - User Authorization
86 /// - Potential User permissions checks
87 #[derive(Clone, Debug, Serialize, Deserialize)]
88 pub struct RepositoryIssueLabelsRequest;
89
90 impl GiteratedOperation<Repository> for RepositoryIssueLabelsRequest {
91 type Success = Vec<IssueLabel>;
92 type Failure = RepositoryError;
93 }
94
95 /// A request to get a repository's issue labels.
96 ///
97 /// # Authentication
98 /// - Instance Authentication
99 /// - Validate request against the `issued_for` public key
100 /// - Validate User token against the user's instance's public key
101 /// # Authorization
102 /// - User Authorization
103 /// - Potential User permissions checks
104 #[derive(Clone, Debug, Serialize, Deserialize)]
105 pub struct RepositoryIssuesRequest;
106
107 impl GiteratedOperation<Repository> for RepositoryIssuesRequest {
108 type Success = Vec<RepositoryIssue>;
109 type Failure = RepositoryError;
110 }
111
112 /// A request to inspect the tree of a repository.
113 ///
114 /// # Authentication
115 /// - Instance Authentication
116 /// - Validate request against the `issued_for` public key
117 /// - Validate User token against the user's instance's public key
118 /// # Authorization
119 /// - User Authorization
120 /// - Potential User permissions checks
121 #[derive(Clone, Debug, Serialize, Deserialize)]
122 pub struct RepositoryFileInspectRequest {
123 /// Whether to get extra metadata for every entry (file mode, size and last commit made to it).
124 pub extra_metadata: bool,
125 /// Revision of the repository to get (branch, commit id).
126 pub rev: Option<String>,
127 /// If not given a path, it'll default to the base.
128 pub path: Option<String>,
129 }
130
131 impl GiteratedOperation<Repository> for RepositoryFileInspectRequest {
132 type Success = Vec<RepositoryTreeEntry>;
133 type Failure = RepositoryError;
134 }
135
136 impl<B: ObjectBackend + std::fmt::Debug> Object<'_, Repository, B> {
137 pub async fn info(
138 &mut self,
139 extra_metadata: bool,
140 rev: Option<String>,
141 path: Option<String>,
142 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
143 self.request::<RepositoryInfoRequest>(RepositoryInfoRequest {
144 extra_metadata,
145 rev,
146 path,
147 })
148 .await
149 }
150
151 pub async fn file_from_id(
152 &mut self,
153 id: String,
154 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
155 self.request::<RepositoryFileFromIdRequest>(RepositoryFileFromIdRequest(id)).await
156 }
157
158 pub async fn diff(
159 &mut self,
160 old_id: String,
161 new_id: String,
162 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
163 self.request::<RepositoryDiffRequest>(RepositoryDiffRequest { old_id, new_id }).await
164 }
165
166 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {
167 // self.request::<RepositoryIssuesCountRequest>(RepositoryIssuesCountRequest)
168 // .await
169 // }
170
171 pub async fn issue_labels(
172 &mut self,
173 ) -> Result<Vec<IssueLabel>, OperationError<RepositoryError>> {
174 self.request::<RepositoryIssueLabelsRequest>(RepositoryIssueLabelsRequest)
175 .await
176 }
177
178 pub async fn issues(
179 &mut self,
180 ) -> Result<Vec<RepositoryIssue>, OperationError<RepositoryError>> {
181 self.request::<RepositoryIssuesRequest>(RepositoryIssuesRequest)
182 .await
183 }
184
185 pub async fn inspect_files(
186 &mut self,
187 extra_metadata: bool,
188 rev: Option<&str>,
189 path: Option<&str>,
190 ) -> Result<Vec<RepositoryTreeEntry>, OperationError<RepositoryError>> {
191 self.request::<RepositoryFileInspectRequest>(RepositoryFileInspectRequest {
192 extra_metadata,
193 rev: rev.map(|r| r.to_string()),
194 path: path.map(|p| p.to_string()),
195 })
196 .await
197 }
198 }
199