Skip to content

Fix tests #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/branch.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string,
select from Branch where repo_id == repository_id && name == branch_name limit 1
} or { []Branch{} }

// app.debug("branches: ${branches}")

if branches.len != 0 {
branch := branches.first()
app.update_branch(branch.id, author, hash, date)!
Expand All @@ -66,6 +68,8 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string,
date: date
}

app.debug("inserting branch: ${new_branch}")

sql app.db {
insert new_branch into Branch
}!
Expand Down
3 changes: 3 additions & 0 deletions src/commit.v
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s
select from Commit where repo_id == repo_id && branch_id == branch_id && hash == last_hash limit 1
} or { []Commit{} }

// $dbg;

if commits.len > 0 {
return
}
Expand All @@ -100,6 +102,7 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s
message: message
}

// $dbg;
sql app.db {
insert new_commit into Commit
}!
Expand Down
9 changes: 6 additions & 3 deletions src/commit_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import time
import api

@['/api/v1/:user/:repo_name/:branch_name/commits/count']
fn (mut app App) handle_commits_count(username string, repo_name string, branch_name string) veb.Result {
fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_name string, branch_name string) veb.Result {
has_access := app.has_user_repo_read_access_by_repo_name(ctx, ctx.user.id, username,
repo_name)

Expand All @@ -18,17 +18,20 @@ fn (mut app App) handle_commits_count(username string, repo_name string, branch_
return ctx.json_error('Not found')
}


branch := app.find_repo_branch_by_name(repo.id, branch_name)
count := app.get_repo_commit_count(repo.id, branch.id)

// app.debug("${branch} ${count}" )

return ctx.json(api.ApiCommitCount{
success: true
result: count
})
}

@['/:username/:repo_name/:branch_name/commits/:page']
pub fn (mut app App) commits(username string, repo_name string, branch_name string, page int) veb.Result {
pub fn (mut app App) commits(mut ctx Context, username string, repo_name string, branch_name string, page int) veb.Result {
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }

branch := app.find_repo_branch_by_name(repo.id, branch_name)
Expand Down Expand Up @@ -72,7 +75,7 @@ pub fn (mut app App) commits(username string, repo_name string, branch_name stri
}

@['/:username/:repo_name/commit/:hash']
pub fn (mut app App) commit(username string, repo_name string, hash string) veb.Result {
pub fn (mut app App) commit(mut ctx Context, username string, repo_name string, hash string) veb.Result {
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }

is_patch_request := hash.ends_with('.patch')
Expand Down
15 changes: 13 additions & 2 deletions src/repo.v
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ fn (mut app App) increment_repo_issues(repo_id int) ! {
}!
}

fn (mut app App) get_count_repo() int {
return sql app.db {
select count from Repo
} or {0}
}

fn (mut app App) add_repo(repo Repo) ! {
sql app.db {
insert repo into Repo
Expand Down Expand Up @@ -288,17 +294,19 @@ fn (mut app App) update_repo_from_fs(mut repo Repo) ! {
app.info('Repo updated')
}

// fn (mut app App) update_repo_branch_from_fs(mut ctx Context, mut repo Repo, branch_name string) ! {
fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) ! {
repo_id := repo.id
branch := app.find_repo_branch_by_name(repo.id, branch_name)

if branch.id == 0 {
return
}
// $dbg;

data := repo.git('--no-pager log ${branch_name} --abbrev-commit --abbrev=7 --pretty="%h${log_field_separator}%aE${log_field_separator}%cD${log_field_separator}%s${log_field_separator}%aN"')
println('DATA=')
println(data)
// println('DATA=')
// println(data)

for line in data.split_into_lines() {
args := line.split(log_field_separator)
Expand All @@ -323,6 +331,8 @@ fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) !
commit_author_id = user.id
}

// $dbg;

app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author,
commit_author_id, commit_message, int(commit_date.unix()))!
}
Expand Down Expand Up @@ -397,6 +407,7 @@ fn (mut app App) update_repo_branch_data(mut repo Repo, branch_name string) ! {
commit_author_id = user.id
}

// $dbg;
app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author,
commit_author_id, commit_message, int(commit_date.unix()))!
}
Expand Down
9 changes: 7 additions & 2 deletions src/repo_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,11 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str
}
}
repo_path := os.join_path(app.config.repo_storage_path, ctx.user.username, name)
id := app.get_count_repo() + 1
mut new_repo := &Repo{
git_repo: git.new_repo(repo_path)
name: name
id: id
description: description
git_dir: repo_path
user_id: ctx.user.id
Expand All @@ -240,7 +242,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str
os.mkdir(new_repo.git_dir) or { panic(err) }
new_repo.git('init --bare')
} else {
println('GO CLONING:')
app.debug("cloning")
// t := time.now()

spawn app.foo(mut new_repo)
Expand All @@ -256,8 +258,11 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str
return ctx.redirect('/new')
}
repo_id := new_repo2.id
// $dbg;
// primary_branch := git.get_repository_primary_branch(repo_path)
primary_branch := new_repo2.primary_branch
// app.debug("new_repo2: ${new_repo2}")

app.update_repo_primary_branch(repo_id, primary_branch) or {
ctx.error('There was an error while adding the repo')
return app.new(mut ctx)
Expand All @@ -284,7 +289,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str

pub fn (mut app App) foo(mut new_repo Repo) {
new_repo.clone()
println('CLONING DONE')
app.debug("cloning done")
app.update_repo_from_fs(mut new_repo) or {}
// git.clone(valid_clone_url, repo_path)
}
Expand Down
5 changes: 5 additions & 0 deletions tests/first_run.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ fn main() {
assert get_repo_branch_count(token, test_username, 'test1') == 0

test_create_repo(token, 'test2', test_github_repo_url)
// wait while repo is cloning
time.sleep(3 * time.second)
// get repo
assert get_repo_commit_count(token, test_username, 'test2', test_github_repo_primary_branch) > 0
assert get_repo_issue_count(token, test_username, 'test2') == 0
assert get_repo_branch_count(token, test_username, 'test2') > 0
ilog("all tests passed!")

after()!
}
Expand Down Expand Up @@ -230,6 +234,7 @@ fn get_repo_commit_count(token string, username string, repo_name string, branch
response_json := json.decode(api.ApiCommitCount, response.body) or {
exit_with_message(err.str())
}
dump(response_json.result)

return response_json.result
}
Expand Down
Loading