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