Add API for manipulating settings requests
parent: tbd commit: b5e5847
1 | use Error; |
2 | use async_trait; |
3 | use StreamExt; |
4 | use ObjectType; |
5 | use |
6 | |
7 | RepositoryCreateRequest, RepositoryCreateResponse, RepositoryFileInspectRequest, |
8 | RepositoryFileInspectionResponse, RepositoryInfoRequest, RepositoryIssueLabelsRequest, |
9 | RepositoryIssueLabelsResponse, RepositoryIssuesCountRequest, RepositoryIssuesCountResponse, |
10 | RepositoryIssuesRequest, RepositoryIssuesResponse, |
11 | , |
12 | |
13 | , | Instance
14 | |
15 | Commit, Repository, RepositoryObjectType, RepositorySummary, RepositoryTreeEntry, |
16 | RepositoryView, RepositoryVisibility, |
17 | , |
18 | , | User
19 | , |
20 | ; |
21 | use ; |
22 | use ; |
23 | use Error; |
24 | |
25 | use ; |
26 | |
27 | // TODO: Handle this |
28 | //region database structures |
29 | |
30 | /// Repository in the database |
31 | |
32 | |
33 | #[sqlx(try_from = "String")] |
34 | pub owner_user: User, |
35 | pub name: String, |
36 | pub description: , |
37 | pub visibility: RepositoryVisibility, |
38 | pub default_branch: String, |
39 | |
40 | |
41 | |
42 | // Separate function because "Private" will be expanded later |
43 | /// Checks if the user is allowed to view this repository |
44 | |
45 | !matches! |
46 | || |
47 | && Some == user |
48 | |
49 | |
50 | // This is in it's own function because I assume I'll have to add logic to this later |
51 | |
52 | &self, |
53 | repository_directory: &str, |
54 | |
55 | match open |
56 | "{}/{}/{}/{}" |
57 | repository_directory, self.owner_user.instance.url, self.owner_user.username, self.name |
58 | ) |
59 | Ok => Ok, |
60 | Err => |
61 | let err = FailedOpeningFromDisk; |
62 | error!; |
63 | |
64 | Err |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | //endregion |
71 | |
72 | |
73 | |
74 | |
75 | FailedCreatingRepository, |
76 | |
77 | FailedInsertingIntoDatabase, |
78 | |
79 | RepositoryNotFound , |
80 | |
81 | RepositoryAlreadyExists , |
82 | |
83 | CouldNotDeleteFromDisk, |
84 | |
85 | FailedDeletingFromDatabase, |
86 | |
87 | FailedOpeningFromDisk, |
88 | |
89 | RefNotFound, |
90 | |
91 | PathNotFound, |
92 | |
93 | LastCommitNotFound, |
94 | |
95 | |
96 | |
97 | pub pg_pool: PgPool, |
98 | pub repository_folder: String, |
99 | pub instance: Instance, |
100 | |
101 | |
102 | |
103 | |
104 | pg_pool: &PgPool, |
105 | repository_folder: &str, |
106 | instance: impl , |
107 | |
108 | Self |
109 | pg_pool: pg_pool.clone, |
110 | repository_folder: repository_folder.to_string, |
111 | instance: instance.to_owned, |
112 | |
113 | |
114 | |
115 | pub async |
116 | &self, |
117 | user: &User, |
118 | repository_name: &str, |
119 | |
120 | if let Ok = query_as! |
121 | r#"SELECT owner_user, name, description, visibility as "visibility: _", default_branch FROM repositories WHERE owner_user = $1 AND name = $2"#, |
122 | user.to_string, repository_name |
123 | .fetch_one |
124 | .await |
125 | Ok |
126 | else |
127 | Err |
128 | owner_user: user.to_string, |
129 | name: repository_name.to_string, |
130 | |
131 | |
132 | |
133 | |
134 | pub async |
135 | &self, |
136 | user: &User, |
137 | repository_name: &str, |
138 | |
139 | if let Err = remove_dir_all |
140 | "{}/{}/{}/{}" |
141 | self.repository_folder, user.instance.url, user.username, repository_name |
142 | ) |
143 | let err = CouldNotDeleteFromDisk; |
144 | error! |
145 | "Couldn't delete repository from disk, this is bad! {:?}", |
146 | err |
147 | ; |
148 | |
149 | return Err; |
150 | |
151 | |
152 | // Delete the repository from the database |
153 | match query! |
154 | "DELETE FROM repositories WHERE owner_user = $1 AND name = $2", |
155 | user.to_string, |
156 | repository_name |
157 | |
158 | .execute |
159 | .await |
160 | |
161 | Ok => Ok, |
162 | Err => Err, |
163 | |
164 | |
165 | |
166 | // TODO: Find where this fits |
167 | // TODO: Cache this and general repository tree and invalidate select files on push |
168 | // TODO: Find better and faster technique for this |
169 | |
170 | path: &str, |
171 | git: & Repository, |
172 | start_commit: & Commit, |
173 | |
174 | let mut revwalk = git.revwalk?; |
175 | revwalk.set_sorting?; |
176 | revwalk.push?; |
177 | |
178 | for oid in revwalk |
179 | let oid = oid?; |
180 | let commit = git.find_commit?; |
181 | |
182 | // Merge commits have 2 or more parents |
183 | // Commits with 0 parents are handled different because we can't diff against them |
184 | if commit.parent_count == 0 |
185 | return Ok; |
186 | else if commit.parent_count == 1 |
187 | let tree = commit.tree?; |
188 | let last_tree = commit.parent?.tree?; |
189 | |
190 | // Get the diff between the current tree and the last one |
191 | let diff = git.diff_tree_to_tree?; |
192 | |
193 | for dd in diff.deltas |
194 | // Get the path of the current file we're diffing against |
195 | let current_path = dd.new_file .path .unwrap; |
196 | |
197 | // Path or directory |
198 | if current_path.eq || current_path.starts_with |
199 | return Ok; |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | Err? |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | async |
212 | &mut self, |
213 | _user: &User, |
214 | request: &RepositoryCreateRequest, |
215 | |
216 | // Check if repository already exists in the database |
217 | if let Ok = self |
218 | .find_by_owner_user_name |
219 | .await |
220 | |
221 | let err = RepositoryAlreadyExists |
222 | owner_user: repository.owner_user.to_string, |
223 | name: repository.name, |
224 | ; |
225 | error!; |
226 | |
227 | return Err; |
228 | |
229 | |
230 | // Insert the repository into the database |
231 | let _ = match query_as! |
232 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5) RETURNING owner_user, name, description, visibility as "visibility: _", default_branch"#, |
233 | request.owner.to_string, request.name, request.description, request.visibility as _, "master" |
234 | .fetch_one |
235 | .await |
236 | Ok => repository, |
237 | Err => |
238 | let err = FailedInsertingIntoDatabase; |
239 | error!; |
240 | |
241 | return Err; |
242 | |
243 | ; |
244 | |
245 | // Create bare (server side) repository on disk |
246 | match init_bare |
247 | "{}/{}/{}/{}" |
248 | self.repository_folder, |
249 | request.owner.instance.url, |
250 | request.owner.username, |
251 | request.name |
252 | ) |
253 | Ok => |
254 | debug! |
255 | "Created new repository with the name {}/{}/{}", |
256 | request.owner.instance.url, request.owner.username, request.name |
257 | ; |
258 | Ok |
259 | |
260 | Err => |
261 | let err = FailedCreatingRepository; |
262 | error!; |
263 | |
264 | // Delete repository from database |
265 | self.delete_by_owner_user_name |
266 | .await?; |
267 | |
268 | // ??? |
269 | Err |
270 | |
271 | |
272 | |
273 | |
274 | async |
275 | &mut self, |
276 | requester: , |
277 | request: &RepositoryInfoRequest, |
278 | |
279 | let repository = match self |
280 | .find_by_owner_user_name |
281 | // &request.owner.instance.url, |
282 | &request.repository.owner, |
283 | &request.repository.name, |
284 | |
285 | .await |
286 | |
287 | Ok => repository, |
288 | Err => return Err, |
289 | ; |
290 | |
291 | if let Some = requester |
292 | if !repository.can_user_view_repository |
293 | return Err |
294 | owner_user: request.repository.owner.to_string, |
295 | name: request.repository.name.clone, |
296 | |
297 | .into; |
298 | |
299 | else if matches! |
300 | info!; |
301 | // Unauthenticated users can never view private repositories |
302 | |
303 | return Err |
304 | owner_user: request.repository.owner.to_string, |
305 | name: request.repository.name.clone, |
306 | |
307 | .into; |
308 | |
309 | |
310 | let git = match repository.open_git2_repository |
311 | Ok => git, |
312 | Err => return Err, |
313 | ; |
314 | |
315 | let rev_name = match &request.rev |
316 | None => |
317 | if let Ok = git.head |
318 | head.name .unwrap .to_string |
319 | else |
320 | // Nothing in database, render empty tree. |
321 | return Ok |
322 | name: repository.name, |
323 | owner: request.repository.owner.clone, |
324 | description: repository.description, |
325 | visibility: repository.visibility, |
326 | default_branch: repository.default_branch, |
327 | latest_commit: None, |
328 | tree_rev: None, |
329 | tree: vec!, |
330 | ; |
331 | |
332 | |
333 | Some => |
334 | // Find the reference, otherwise return GitBackendError |
335 | match git |
336 | .find_reference |
337 | .map_err |
338 | |
339 | Ok => reference.name .unwrap .to_string, |
340 | Err => return Err, |
341 | |
342 | |
343 | ; |
344 | |
345 | // Get the git object as a commit |
346 | let rev = match git |
347 | .revparse_single |
348 | .map_err |
349 | |
350 | Ok => rev, |
351 | Err => return Err, |
352 | ; |
353 | let commit = rev.as_commit .unwrap; |
354 | |
355 | // this is stupid |
356 | let mut current_path = rev_name.replace; |
357 | |
358 | // Get the commit tree |
359 | let git_tree = if let Some = &request.path |
360 | // Add it to our full path string |
361 | current_path.push_str; |
362 | // Get the specified path, return an error if it wasn't found. |
363 | let entry = match commit |
364 | .tree |
365 | .unwrap |
366 | .get_path |
367 | .map_err |
368 | |
369 | Ok => entry, |
370 | Err => return Err, |
371 | ; |
372 | // Turn the entry into a git tree |
373 | entry.to_object .unwrap .as_tree .unwrap .clone |
374 | else |
375 | commit.tree .unwrap |
376 | ; |
377 | |
378 | // Iterate over the git tree and collect it into our own tree types |
379 | let mut tree = git_tree |
380 | .iter |
381 | .map |
382 | let object_type = match entry.kind .unwrap |
383 | => Tree, | Tree
384 | => Blob, | Blob
385 | _ => unreachable!, |
386 | ; |
387 | let mut tree_entry = |
388 | ; | new
389 | |
390 | if request.extra_metadata |
391 | // Get the file size if It's a blob |
392 | let object = entry.to_object .unwrap; |
393 | if let Some = object.as_blob |
394 | tree_entry.size = Some; |
395 | |
396 | |
397 | // Could possibly be done better |
398 | let path = if let Some = current_path.split_once |
399 | format! |
400 | else |
401 | entry.name .unwrap .to_string |
402 | ; |
403 | |
404 | // Get the last commit made to the entry |
405 | if let Ok = |
406 | get_last_commit_of_file |
407 | |
408 | tree_entry.last_commit = Some; |
409 | |
410 | |
411 | |
412 | tree_entry |
413 | |
414 | .; |
415 | |
416 | // Sort the tree alphabetically and with tree first |
417 | tree.sort_unstable_by_key; |
418 | tree.sort_unstable_by_key |
419 | Reverse |
420 | ; |
421 | |
422 | Ok |
423 | name: repository.name, |
424 | owner: request.repository.owner.clone, |
425 | description: repository.description, |
426 | visibility: repository.visibility, |
427 | default_branch: repository.default_branch, |
428 | latest_commit: None, |
429 | tree_rev: Some, |
430 | tree, |
431 | |
432 | |
433 | |
434 | async |
435 | &mut self, |
436 | _requester: , |
437 | _request: &RepositoryFileInspectRequest, |
438 | |
439 | todo! |
440 | |
441 | |
442 | async |
443 | &mut self, |
444 | requester: , |
445 | user: &User, |
446 | |
447 | let mut repositories = query_as! |
448 | GitRepository, |
449 | r#"SELECT visibility as "visibility: _", owner_user, name, description, default_branch FROM repositories WHERE owner_user = $1"#, |
450 | user.to_string |
451 | |
452 | .fetch_many; |
453 | |
454 | let mut result = vec!; |
455 | |
456 | while let Some = repositories.next .await |
457 | // Check if the requesting user is allowed to see the repository |
458 | if ! |
459 | repository.visibility, |
460 | | Private | Unlisted
461 | && Some != requester |
462 | |
463 | result.push |
464 | repository: Repository |
465 | owner: repository.owner_user.clone, |
466 | name: repository.name, |
467 | instance: self.instance.clone, |
468 | , |
469 | owner: repository.owner_user.clone, |
470 | visibility: repository.visibility, |
471 | description: repository.description, |
472 | // TODO |
473 | last_commit: None, |
474 | ; |
475 | |
476 | |
477 | |
478 | Ok |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | &mut self, |
485 | _requester: , |
486 | _request: &RepositoryIssuesCountRequest, |
487 | |
488 | todo! |
489 | |
490 | |
491 | |
492 | &mut self, |
493 | _requester: , |
494 | _request: &RepositoryIssueLabelsRequest, |
495 | |
496 | todo! |
497 | |
498 | |
499 | |
500 | &mut self, |
501 | _requester: , |
502 | _request: &RepositoryIssuesRequest, |
503 | |
504 | todo! |
505 | |
506 | |
507 |