| 1 | // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by a GPL license that can be found in the LICENSE file. |
| 3 | module main |
| 4 | |
| 5 | import time |
| 6 | |
| 7 | struct Tag { |
| 8 | id int @[primary; sql: serial] |
| 9 | repo_id int @[unique: 'tag'] |
| 10 | mut: |
| 11 | name string @[unique: 'tag'] |
| 12 | hash string |
| 13 | message string |
| 14 | user_id int |
| 15 | created_at int |
| 16 | } |
| 17 | |
| 18 | fn (mut app App) fetch_tags(repo Repo) ! { |
| 19 | tags_output := |
| 20 | repo.git('tag --format="%(refname:lstrip=2)${log_field_separator}%(objectname)${log_field_separator}%(subject)${log_field_separator}%(authoremail)${log_field_separator}%(creatordate:rfc)"') |
| 21 | |
| 22 | for tag_output in tags_output.split_into_lines() { |
| 23 | tag_parts := tag_output.split(log_field_separator) |
| 24 | tag_name := tag_parts[0] |
| 25 | commit_hash := tag_parts[1] |
| 26 | commit_message := tag_parts[2] |
| 27 | author_email := tag_parts[3] |
| 28 | commit_date := time.parse_rfc2822(tag_parts[4]) or { |
| 29 | app.info('Error: ${err}') |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | user := app.get_user_by_email(author_email) or { |
| 34 | User{ |
| 35 | username: author_email |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | app.insert_tag_into_db(repo.id, tag_name, commit_hash, commit_message, user.id, |
| 40 | int(commit_date.unix()))! |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn (mut app App) insert_tag_into_db(repo_id int, tag_name string, commit_hash string, commit_message string, user_id int, date int) ! { |
| 45 | tags := sql app.db { |
| 46 | select from Tag where repo_id == repo_id && name == tag_name limit 1 |
| 47 | } or { []Tag{} } |
| 48 | |
| 49 | if tags.len != 0 { |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | new_tag := Tag{ |
| 54 | repo_id: repo_id |
| 55 | name: tag_name |
| 56 | hash: commit_hash |
| 57 | message: commit_message |
| 58 | user_id: user_id |
| 59 | created_at: date |
| 60 | } |
| 61 | |
| 62 | sql app.db { |
| 63 | insert new_tag into Tag |
| 64 | }! |
| 65 | } |
| 66 | |
| 67 | fn (mut app App) get_all_repo_tags(repo_id int) []Tag { |
| 68 | return sql app.db { |
| 69 | select from Tag where repo_id == repo_id order by created_at desc |
| 70 | } or { [] } |
| 71 | } |
| 72 | |