| 1 | module main |
| 2 | |
| 3 | import time |
| 4 | |
| 5 | enum CiStatusEnum { |
| 6 | pending = 0 |
| 7 | running = 1 |
| 8 | success = 2 |
| 9 | failure = 3 |
| 10 | cancelled = 4 |
| 11 | } |
| 12 | |
| 13 | fn (s CiStatusEnum) str() string { |
| 14 | return match s { |
| 15 | .pending { 'pending' } |
| 16 | .running { 'running' } |
| 17 | .success { 'success' } |
| 18 | .failure { 'failure' } |
| 19 | .cancelled { 'cancelled' } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn (s CiStatusEnum) css_class() string { |
| 24 | return match s { |
| 25 | .pending { 'ci-pending' } |
| 26 | .running { 'ci-running' } |
| 27 | .success { 'ci-success' } |
| 28 | .failure { 'ci-failure' } |
| 29 | .cancelled { 'ci-cancelled' } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn (s CiStatusEnum) icon() string { |
| 34 | return match s { |
| 35 | .pending { '⏳' } |
| 36 | .running { '🔄' } |
| 37 | .success { '✓' } |
| 38 | .failure { '✗' } |
| 39 | .cancelled { '⊘' } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | struct CiStatus { |
| 44 | id int @[primary; sql: serial] |
| 45 | repo_id int |
| 46 | commit_hash string |
| 47 | branch string |
| 48 | status CiStatusEnum |
| 49 | ci_run_id int |
| 50 | created_at int |
| 51 | updated_at int |
| 52 | } |
| 53 | |
| 54 | fn ci_status_from_string(s string) CiStatusEnum { |
| 55 | return match s { |
| 56 | 'pending' { CiStatusEnum.pending } |
| 57 | 'running' { CiStatusEnum.running } |
| 58 | 'success' { CiStatusEnum.success } |
| 59 | 'failure' { CiStatusEnum.failure } |
| 60 | 'cancelled' { CiStatusEnum.cancelled } |
| 61 | else { CiStatusEnum.pending } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn (mut app App) find_ci_status_for_commit(repo_id int, commit_hash string) ?CiStatus { |
| 66 | results := sql app.db { |
| 67 | select from CiStatus where repo_id == repo_id && commit_hash == commit_hash order by id desc limit 1 |
| 68 | } or { return none } |
| 69 | if results.len == 0 { |
| 70 | return none |
| 71 | } |
| 72 | return results[0] |
| 73 | } |
| 74 | |
| 75 | fn (mut app App) find_ci_status_for_branch(repo_id int, branch string) ?CiStatus { |
| 76 | results := sql app.db { |
| 77 | select from CiStatus where repo_id == repo_id && branch == branch order by id desc limit 1 |
| 78 | } or { return none } |
| 79 | if results.len == 0 { |
| 80 | return none |
| 81 | } |
| 82 | return results[0] |
| 83 | } |
| 84 | |
| 85 | fn (mut app App) find_ci_runs_for_repo(repo_id int) []CiStatus { |
| 86 | return sql app.db { |
| 87 | select from CiStatus where repo_id == repo_id order by id desc |
| 88 | } or { []CiStatus{} } |
| 89 | } |
| 90 | |
| 91 | fn (mut app App) add_ci_status(ci CiStatus) ! { |
| 92 | sql app.db { |
| 93 | insert ci into CiStatus |
| 94 | }! |
| 95 | } |
| 96 | |
| 97 | fn (mut app App) update_ci_status(repo_id int, commit_hash string, status CiStatusEnum) ! { |
| 98 | updated := int(time.now().unix()) |
| 99 | sql app.db { |
| 100 | update CiStatus set status = status, updated_at = updated where repo_id == repo_id |
| 101 | && commit_hash == commit_hash |
| 102 | }! |
| 103 | } |
| 104 | |
| 105 | fn (mut app App) upsert_ci_status(repo_id int, commit_hash string, branch string, status CiStatusEnum, ci_run_id int) ! { |
| 106 | existing := app.find_ci_status_for_commit(repo_id, commit_hash) or { |
| 107 | // Insert new |
| 108 | app.add_ci_status(CiStatus{ |
| 109 | repo_id: repo_id |
| 110 | commit_hash: commit_hash |
| 111 | branch: branch |
| 112 | status: status |
| 113 | ci_run_id: ci_run_id |
| 114 | created_at: int(time.now().unix()) |
| 115 | updated_at: int(time.now().unix()) |
| 116 | })! |
| 117 | return |
| 118 | } |
| 119 | // Update existing |
| 120 | id := existing.id |
| 121 | updated := int(time.now().unix()) |
| 122 | sql app.db { |
| 123 | update CiStatus set status = status, ci_run_id = ci_run_id, updated_at = updated |
| 124 | where id == id |
| 125 | }! |
| 126 | } |
| 127 | |
| 128 | fn (mut app App) delete_repo_ci_statuses(repo_id int) ! { |
| 129 | sql app.db { |
| 130 | delete from CiStatus where repo_id == repo_id |
| 131 | }! |
| 132 | } |
| 133 | |
| 134 | fn (ci &CiStatus) relative_time() string { |
| 135 | if ci.updated_at == 0 && ci.created_at == 0 { |
| 136 | return '' |
| 137 | } |
| 138 | t := if ci.updated_at > 0 { ci.updated_at } else { ci.created_at } |
| 139 | return time.unix(t).relative() |
| 140 | } |
| 141 | |