| 1 | module main |
| 2 | |
| 3 | import veb |
| 4 | import time |
| 5 | import api |
| 6 | |
| 7 | @['/api/v1/:user/:repo_name/:branch_name/commits/count'] |
| 8 | fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_name string, branch_name string) veb.Result { |
| 9 | has_access := app.has_user_repo_read_access_by_repo_name(ctx, ctx.user.id, username, repo_name) |
| 10 | |
| 11 | if !has_access { |
| 12 | return ctx.json_error('Not found') |
| 13 | } |
| 14 | |
| 15 | repo := app.find_repo_by_name_and_username(repo_name, username) or { |
| 16 | return ctx.json_error('Not found') |
| 17 | } |
| 18 | |
| 19 | branch := app.find_repo_branch_by_name(repo.id, branch_name) |
| 20 | count := app.get_repo_commit_count(repo.id, branch.id) |
| 21 | |
| 22 | // app.debug("${branch} ${count}" ) |
| 23 | |
| 24 | return ctx.json(api.ApiCommitCount{ |
| 25 | success: true |
| 26 | result: count |
| 27 | }) |
| 28 | } |
| 29 | |
| 30 | @['/:username/:repo_name/:branch_name/commits/:page'] |
| 31 | pub fn (mut app App) commits(mut ctx Context, username string, repo_name string, branch_name string, page string) veb.Result { |
| 32 | repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } |
| 33 | |
| 34 | page_i := page.int() |
| 35 | branch := app.find_repo_branch_by_name(repo.id, branch_name) |
| 36 | commits_count := app.get_repo_commit_count(repo.id, branch.id) |
| 37 | |
| 38 | offset := commits_per_page * page_i |
| 39 | // FIXME: b_author always false |
| 40 | b_author := false |
| 41 | page_count := calculate_pages(commits_count, commits_per_page) |
| 42 | is_first_page := check_first_page(page_i) |
| 43 | is_last_page := check_last_page(commits_count, offset, commits_per_page) |
| 44 | prev_page, next_page := generate_prev_next_pages(page_i) |
| 45 | |
| 46 | mut commits := app.find_repo_commits_as_page(repo.id, branch.id, offset) |
| 47 | |
| 48 | mut d_commits := map[string][]Commit{} |
| 49 | mut author_avatars := map[int]string{} |
| 50 | mut author_usernames := map[int]string{} |
| 51 | for commit in commits { |
| 52 | date := time.unix(commit.created_at) |
| 53 | author := commit.author_id.str() |
| 54 | date_s := date.custom_format('MMMM D, YYYY') |
| 55 | |
| 56 | if b_author { |
| 57 | if author !in d_commits { |
| 58 | d_commits[author] = []Commit{} |
| 59 | } |
| 60 | d_commits[author] << commit |
| 61 | } else { |
| 62 | if date_s !in d_commits { |
| 63 | d_commits[date_s] = []Commit{} |
| 64 | } |
| 65 | d_commits[date_s] << commit |
| 66 | } |
| 67 | |
| 68 | if commit.author_id != 0 && commit.author_id !in author_avatars { |
| 69 | if user := app.get_user_by_id(commit.author_id) { |
| 70 | author_avatars[commit.author_id] = app.prepare_user_avatar_url(user.avatar) |
| 71 | author_usernames[commit.author_id] = user.username |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $veb.html() |
| 77 | } |
| 78 | |
| 79 | @['/:username/:repo_name/commit/:hash'] |
| 80 | pub fn (mut app App) commit(mut ctx Context, username string, repo_name string, hash string) veb.Result { |
| 81 | repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } |
| 82 | |
| 83 | is_patch_request := hash.ends_with('.patch') |
| 84 | |
| 85 | if is_patch_request { |
| 86 | commit_hash := hash.trim_string_right('.patch') |
| 87 | patch := repo.get_commit_patch(commit_hash) or { return ctx.not_found() } |
| 88 | |
| 89 | return ctx.ok(patch) |
| 90 | } |
| 91 | |
| 92 | patch_url := '/${username}/${repo_name}/commit/${hash}.patch' |
| 93 | commit := app.find_repo_commit_by_hash(repo.id, hash) |
| 94 | raw_diff := repo.git('show --no-color --pretty=format: ${commit.hash}') |
| 95 | file_diffs := parse_unified_diff(raw_diff) |
| 96 | |
| 97 | mut all_adds := 0 |
| 98 | mut all_dels := 0 |
| 99 | for fd in file_diffs { |
| 100 | all_adds += fd.additions |
| 101 | all_dels += fd.deletions |
| 102 | } |
| 103 | |
| 104 | return $veb.html() |
| 105 | } |
| 106 | |