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