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

ambee/giterated

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

Fixes and cleanup

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨555bd26

⁨giterated-models/src/repository/operations.rs⁩ - ⁨7257⁩ 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, RepositoryDiff, RepositoryFile, RepositoryIssue,
12 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 the difference between two repository trees.
54 ///
55 /// # Authentication
56 /// - Instance Authentication
57 /// - Validate request against the `issued_for` public key
58 /// - Validate User token against the user's instance's public key
59 /// # Authorization
60 /// - User Authorization
61 /// - Potential User permissions checks
62 #[derive(Clone, Debug, Serialize, Deserialize)]
63 pub struct RepositoryDiffRequest {
64 pub old_id: String,
65 pub new_id: String,
66 }
67
68 impl GiteratedOperation<Repository> for RepositoryDiffRequest {
69 type Success = RepositoryDiff;
70 type Failure = RepositoryError;
71 }
72
73 /// A request to get the commit before the one with the passed id
74 ///
75 /// # Authentication
76 /// - Instance Authentication
77 /// - Validate request against the `issued_for` public key
78 /// - Validate User token against the user's instance's public key
79 /// # Authorization
80 /// - User Authorization
81 /// - Potential User permissions checks
82 #[derive(Clone, Debug, Serialize, Deserialize)]
83 pub struct RepositoryCommitBeforeRequest(pub String);
84
85 impl GiteratedOperation<Repository> for RepositoryCommitBeforeRequest {
86 type Success = Commit;
87 type Failure = RepositoryError;
88 }
89
90 #[derive(Clone, Debug, Serialize, Deserialize)]
91 pub struct RepositoryIssuesCountRequest;
92
93 impl GiteratedOperation<Repository> for RepositoryIssuesCountRequest {
94 type Success = u64;
95 type Failure = RepositoryError;
96 }
97
98 /// A request to get a repository's issues count.
99 ///
100 /// # Authentication
101 /// - Instance Authentication
102 /// - Validate request against the `issued_for` public key
103 /// - Validate User token against the user's instance's public key
104 /// # Authorization
105 /// - User Authorization
106 /// - Potential User permissions checks
107 #[derive(Clone, Debug, Serialize, Deserialize)]
108 pub struct RepositoryIssueLabelsRequest;
109
110 impl GiteratedOperation<Repository> for RepositoryIssueLabelsRequest {
111 type Success = Vec<IssueLabel>;
112 type Failure = RepositoryError;
113 }
114
115 /// A request to get a repository's issue labels.
116 ///
117 /// # Authentication
118 /// - Instance Authentication
119 /// - Validate request against the `issued_for` public key
120 /// - Validate User token against the user's instance's public key
121 /// # Authorization
122 /// - User Authorization
123 /// - Potential User permissions checks
124 #[derive(Clone, Debug, Serialize, Deserialize)]
125 pub struct RepositoryIssuesRequest;
126
127 impl GiteratedOperation<Repository> for RepositoryIssuesRequest {
128 type Success = Vec<RepositoryIssue>;
129 type Failure = RepositoryError;
130 }
131
132 /// A request to inspect the tree of a repository.
133 ///
134 /// # Authentication
135 /// - Instance Authentication
136 /// - Validate request against the `issued_for` public key
137 /// - Validate User token against the user's instance's public key
138 /// # Authorization
139 /// - User Authorization
140 /// - Potential User permissions checks
141 #[derive(Clone, Debug, Serialize, Deserialize)]
142 pub struct RepositoryFileInspectRequest {
143 /// Whether to get extra metadata for every entry (file mode, size and last commit made to it).
144 pub extra_metadata: bool,
145 /// Revision of the repository to get (branch, commit id).
146 pub rev: Option<String>,
147 /// If not given a path, it'll default to the base.
148 pub path: Option<String>,
149 }
150
151 impl GiteratedOperation<Repository> for RepositoryFileInspectRequest {
152 type Success = Vec<RepositoryTreeEntry>;
153 type Failure = RepositoryError;
154 }
155
156 impl<B: ObjectBackend + std::fmt::Debug> Object<'_, Repository, B> {
157 pub async fn info(
158 &mut self,
159 extra_metadata: bool,
160 rev: Option<String>,
161 path: Option<String>,
162 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
163 self.request::<RepositoryInfoRequest>(RepositoryInfoRequest {
164 extra_metadata,
165 rev,
166 path,
167 })
168 .await
169 }
170
171 pub async fn file_from_id(
172 &mut self,
173 id: String,
174 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
175 self.request::<RepositoryFileFromIdRequest>(RepositoryFileFromIdRequest(id))
176 .await
177 }
178
179 pub async fn diff(
180 &mut self,
181 old_id: String,
182 new_id: String,
183 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
184 self.request::<RepositoryDiffRequest>(RepositoryDiffRequest { old_id, new_id })
185 .await
186 }
187
188 pub async fn commit_before(
189 &mut self,
190 id: String,
191 ) -> Result<Commit, OperationError<RepositoryError>> {
192 self.request::<RepositoryCommitBeforeRequest>(RepositoryCommitBeforeRequest(id))
193 .await
194 }
195
196 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {
197 // self.request::<RepositoryIssuesCountRequest>(RepositoryIssuesCountRequest)
198 // .await
199 // }
200
201 pub async fn issue_labels(
202 &mut self,
203 ) -> Result<Vec<IssueLabel>, OperationError<RepositoryError>> {
204 self.request::<RepositoryIssueLabelsRequest>(RepositoryIssueLabelsRequest)
205 .await
206 }
207
208 pub async fn issues(
209 &mut self,
210 ) -> Result<Vec<RepositoryIssue>, OperationError<RepositoryError>> {
211 self.request::<RepositoryIssuesRequest>(RepositoryIssuesRequest)
212 .await
213 }
214
215 pub async fn inspect_files(
216 &mut self,
217 extra_metadata: bool,
218 rev: Option<&str>,
219 path: Option<&str>,
220 ) -> Result<Vec<RepositoryTreeEntry>, OperationError<RepositoryError>> {
221 self.request::<RepositoryFileInspectRequest>(RepositoryFileInspectRequest {
222 extra_metadata,
223 rev: rev.map(|r| r.to_string()),
224 path: path.map(|p| p.to_string()),
225 })
226 .await
227 }
228 }
229