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