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

ambee/giterated

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

Repository commit before request and handler

Type: Feature

erremilia - ⁨2⁩ years ago

parent: tbd commit: ⁨144b159

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