forked from vlang/gitly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_db.v
351 lines (330 loc) · 9.4 KB
/
repo_db.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) 2019-2020 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
fn (mut app App) create_table(name string, fields []string) {
app.db.exec('create table if not exists `$name` (' + fields.join(',') + ')')
}
fn (mut app App) create_tables() {
app.create_table('Repo', [
'id integer primary key',
"git_dir text default ''",
"name text default ''",
"description text default ''",
'user_id int default 0',
"user_name text default ''",
'primary_branch text default ""',
'is_public int default 0',
'nr_views int default 0',
'nr_commits int default 0',
'nr_open_issues int default 0',
'nr_tags int default 0',
'nr_releases int default 0',
'nr_open_prs int default 0',
'webhook_secret text default ""',
'nr_branches int default 0',
'nr_contributors int default 0',
"created_at int default (strftime('%s', 'now'))",
])
// unix time default now
app.create_table('File', [
'id integer primary key',
"name text default ''",
'repo_id int default 0',
"parent_path text default ''",
"branch text default ''",
'is_dir int default 0',
"last_hash text default ''",
"last_msg text default ''",
'last_time int default 0',
'size int default 0',
'nr_contributors int default 0',
'nr_views int default 0',
'UNIQUE(parent_path, name, repo_id, branch) ON CONFLICT REPLACE',
])
//"created_at int default (strftime('%s', 'now'))"
app.create_table('Issue', [
'id integer primary key',
'author_id int default 0',
'is_pr int default 0',
'repo_id int default 0',
"title text default ''",
"text text default ''",
'created_at integer default 0',
'nr_comments int default 0',
])
// "created_at int default (strftime('%s', 'now'))"
app.create_table('Commit', [
'id integer primary key',
'author_id int default 0',
"author text default ''",
"hash text default ''",
'repo_id int default 0',
"message text default ''",
"created_at int default (strftime('%s', 'now'))",
'UNIQUE(hash, repo_id)',
])
// author text default '' is to to avoid joins
app.create_table('LangStat', [
'id integer primary key',
'repo_id int default 0',
'name text default ""',
'nr_lines int default 0',
'pct int default 0',
'color text default ""',
'UNIQUE(repo_id, name) ON CONFLICT REPLACE',
])
app.create_table('User', [
'id integer primary key',
'name text default ""',
'username text default ""',
'password text default ""',
'avatar text default ""',
'nr_posts integer default 0',
'last_post_time integer default 0',
'nr_namechanges integer default 0',
'last_namechange_time integer default 0',
'is_github int default 0',
'is_blocked int default 0',
'is_registered int default 0',
'is_admin int default 0',
'login_attempts int default 0',
'github_username text default ""',
'UNIQUE(username)',
])
app.create_table('Email', [
'id integer primary key',
'user integer default 0',
'email text default ""',
'UNIQUE(email)',
])
app.create_table('Contributor', [
'id integer primary key',
'user integer default 0',
'repo integer default 0',
'UNIQUE(user, repo)',
])
app.create_table('Tag', [
'id integer primary key',
'name text default ""',
'hash text default ""',
'user_id integer default 0',
'repo_id integer default 0',
'date integer default 0',
'UNIQUE(name, repo_id)',
])
app.create_table('Release', [
'id integer primary key',
'tag_id integer not null',
'repo_id integer not null',
'notes text default ""',
'UNIQUE(tag_id, repo_id)',
])
app.create_table('SshKey', [
'id integer primary key',
'user integer default 0',
'title text default ""',
'sshkey text default ""',
'is_deleted integer default 0',
])
app.create_table('Comment', [
'id integer primary key',
'author_id integer default 0',
'issue_id integer default 0',
'created_at integer default 0',
'text text default ""',
])
app.create_table('Branch', [
'id integer primary key',
'repo_id integer default 0',
'name text default ""',
'author text default ""',
'hash text default ""',
'date integer default 0',
'UNIQUE(repo_id, name)',
])
app.create_table('Visit', [
'id integer primary key',
'repo_id integer default 0',
"url text default ''",
"referer text default ''",
'created_at integer default 0',
])
app.create_table('GitlySettings', [
'id integer primary key',
'oauth_client_id text default ""',
'oauth_client_secret text default ""',
'only_gh_login int default 1',
'repo_storage_path text default "./repos"',
])
app.create_table('Token', [
'id integer primary key',
'user_id integer default 0',
"value text defaut ''",
'ip text default ""',
])
app.create_table('Token2', [
'id integer primary key',
'user_id integer default 0',
"value text defaut ''",
'ip text default ""',
])
app.create_table('SecurityLog', [
'id integer primary key',
'user_id integer default 0',
'kind int default 0',
"ip text default ''",
"arg1 text default ''",
"arg2 text default ''",
"created_at int default (strftime('%s', 'now'))",
])
}
fn (mut app App) update_repo_in_db(repo &Repo) {
id := repo.id
desc := repo.description
nr_views := repo.nr_views
webhook_secret := repo.webhook_secret
nr_tags := repo.nr_tags
is_public := if repo.is_public { 1 } else { 0 }
nr_open_issues := repo.nr_open_issues
nr_open_prs := repo.nr_open_prs
nr_branches := repo.nr_branches
nr_releases := repo.nr_releases
nr_contributors := repo.nr_contributors
nr_commits := repo.nr_commits
sql app.db {
update Repo set description = desc, nr_views = nr_views, is_public = is_public, webhook_secret = webhook_secret,
nr_tags = nr_tags, nr_open_issues = nr_open_issues, nr_open_prs = nr_open_prs, nr_releases = nr_releases,
nr_contributors = nr_contributors, nr_commits = nr_commits, nr_branches = nr_branches
where id == id
}
}
fn (mut app App) find_repo_by_name(user int, name string) ?Repo {
x := sql app.db {
select from Repo where name == name && user_id == user limit 1
}
if x.id == 0 {
return none
}
return x
}
fn (mut app App) nr_user_repos(user_id int) int {
return sql app.db {
select count from Repo where user_id == user_id
}
}
fn (mut app App) find_user_repos(user_id int) []Repo {
return sql app.db {
select from Repo where user_id == user_id
}
}
fn (mut app App) nr_user_public_repos(user_id int) int {
return sql app.db {
select count from Repo where user_id == user_id && is_public == true
}
}
fn (mut app App) find_user_public_repos(user_id int) []Repo {
return sql app.db {
select from Repo where user_id == user_id && is_public == true
}
}
fn (mut app App) find_repo_by_id(repo_id int) Repo {
return sql app.db {
select from Repo where id == repo_id
}
}
fn (mut app App) exists_user_repo(user string, name string) bool {
if user.len == 0 || name.len == 0 {
app.info('User or repo was not found')
return false
}
u := app.find_user_by_username(user) or {
app.info('User was not found')
return false
}
app.repo = app.find_repo_by_name(u.id, name) or {
app.info('Repo was not found')
return false
}
app.repo.lang_stats = app.find_repo_lang_stats(app.repo.id)
app.html_path = app.repo.html_path_to(app.path, app.repo.primary_branch)
return true
}
fn (mut app App) retrieve_repo(id int) Repo {
return app.repo
}
fn (mut app App) inc_repo_views(repo_id int) {
sql app.db {
update Repo set nr_views = nr_views + 1 where id == repo_id
}
}
fn (mut app App) inc_file_views(file_id int) {
sql app.db {
update File set nr_views = nr_views + 1 where id == file_id
}
}
fn (mut app App) inc_repo_issues(repo_id int) {
sql app.db {
update Repo set nr_open_issues = nr_open_issues + 1 where id == repo_id
}
app.repo.nr_open_issues++
}
fn (mut app App) update_repo_nr_commits(repo_id int, nr_commits int) {
sql app.db {
update Repo set nr_commits = nr_commits where id == repo_id
}
app.repo.nr_commits = nr_commits
}
fn (mut app App) update_repo_webhook(repo_id int, webhook string) {
sql app.db {
update Repo set webhook_secret = webhook where id == repo_id
}
}
fn (mut app App) update_repo_nr_contributor(repo_id int, nr_contributors int) {
sql app.db {
update Repo set nr_contributors = nr_contributors where id == repo_id
}
app.repo.nr_contributors = nr_contributors
}
fn (mut app App) insert_repo(repo Repo) {
sql app.db {
insert repo into Repo
}
}
fn (mut app App) delete_repo(id int, path string, name string) {
// Remove repo
sql app.db {
delete from Repo where id == id
}
app.info('Removed repo entry ($id, $name)')
// Remove all commits
sql app.db {
delete from Commit where repo_id == id
}
app.info('Removed repo commits ($id, $name)')
// Remove all issues & prs
app.delete_repo_issues(id)
app.info('Removed repo issues ($id, $name)')
// Remove all branches
app.delete_repo_branches(id)
app.info('Removed repo branches ($id, $name)')
// Remove all releases
app.delete_repo_releases(id)
app.info('Removed repo releases ($id, $name)')
// Remove all files
app.delete_repo_files(id)
app.info('Removed repo files ($id, $name)')
// Remove physical files
app.delete_repo_folder(path)
app.info('Removed repo folder ($id, $name)')
}
fn (mut app App) move_repo_to_user(repo_id int, user_id int, user_name string) {
sql app.db {
update Repo set user_id = user_id, user_name = user_name where id == repo_id
}
}
fn (mut app App) user_has_repo(user_id int, repo_name string) bool {
count := sql app.db {
select count from Repo where user_id == user_id && name == repo_name
}
return count >= 0
}