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