refactor: renamed codebase to "targets"

This commit is contained in:
Jef Roosens 2022-06-14 22:25:40 +02:00 committed by Jef Roosens
parent faec08f846
commit 102a7f8899
Signed by untrusted user: Jef Roosens
GPG key ID: B580B976584B5F30
19 changed files with 205 additions and 191 deletions

View file

@ -10,7 +10,7 @@ import os
import util
import models { BuildLog, BuildLogFilter }
// v1_get_logs returns all build logs in the database. A 'repo' query param can
// v1_get_logs returns all build logs in the database. A 'target' query param can
// optionally be added to limit the list of build logs to that repository.
['/api/v1/logs'; get]
fn (mut app App) v1_get_logs() web.Result {
@ -47,7 +47,7 @@ fn (mut app App) v1_get_log_content(id int) web.Result {
log := app.db.get_build_log(id) or { return app.not_found() }
file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.repo_id.str(), log.arch,
full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), log.arch,
file_name)
return app.file(full_path)
@ -96,15 +96,15 @@ fn (mut app App) v1_post_log() web.Result {
arch := app.query['arch']
repo_id := app.query['repo'].int()
target_id := app.query['target'].int()
if !app.db.git_repo_exists(repo_id) {
return app.json(http.Status.bad_request, new_response('Unknown Git repo.'))
if !app.db.target_exists(target_id) {
return app.json(http.Status.bad_request, new_response('Unknown target.'))
}
// Store log in db
log := BuildLog{
repo_id: repo_id
target_id: target_id
start_time: start_time
end_time: end_time
arch: arch
@ -113,7 +113,7 @@ fn (mut app App) v1_post_log() web.Result {
app.db.add_build_log(log)
repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, repo_id.str(), arch)
repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, target_id.str(), arch)
// Create the logs directory of it doesn't exist
if !os.exists(repo_logs_dir) {

View file

@ -4,36 +4,36 @@ import web
import net.http
import response { new_data_response, new_response }
import db
import models { GitRepo, GitRepoArch, GitRepoFilter }
import models { Target, TargetArch, TargetFilter }
// v1_get_targets returns the current list of repos.
// v1_get_targets returns the current list of targets.
['/api/v1/targets'; get]
fn (mut app App) v1_get_targets() web.Result {
if !app.is_authorized() {
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
filter := models.from_params<GitRepoFilter>(app.query) or {
filter := models.from_params<TargetFilter>(app.query) or {
return app.json(http.Status.bad_request, new_response('Invalid query parameters.'))
}
repos := app.db.get_git_repos(filter)
repos := app.db.get_targets(filter)
return app.json(http.Status.ok, new_data_response(repos))
}
// v1_get_single_target returns the information for a single repo.
// v1_get_single_target returns the information for a single target.
['/api/v1/targets/:id'; get]
fn (mut app App) v1_get_single_target(id int) web.Result {
if !app.is_authorized() {
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
repo := app.db.get_git_repo(id) or { return app.not_found() }
repo := app.db.get_target(id) or { return app.not_found() }
return app.json(http.Status.ok, new_data_response(repo))
}
// v1_post_target creates a new repo from the provided query string.
// v1_post_target creates a new target from the provided query string.
['/api/v1/targets'; post]
fn (mut app App) v1_post_target() web.Result {
if !app.is_authorized() {
@ -48,40 +48,40 @@ fn (mut app App) v1_post_target() web.Result {
params['arch'] = app.conf.default_arch
}
new_repo := models.from_params<GitRepo>(params) or {
new_repo := models.from_params<Target>(params) or {
return app.json(http.Status.bad_request, new_response(err.msg()))
}
app.db.add_git_repo(new_repo)
app.db.add_target(new_repo)
return app.json(http.Status.ok, new_response('Repo added successfully.'))
}
// v1_delete_target removes a given repo from the server's list.
// v1_delete_target removes a given target from the server's list.
['/api/v1/targets/:id'; delete]
fn (mut app App) v1_delete_target(id int) web.Result {
if !app.is_authorized() {
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
app.db.delete_git_repo(id)
app.db.delete_target(id)
return app.json(http.Status.ok, new_response('Repo removed successfully.'))
}
// v1_patch_target updates a repo's data with the given query params.
// v1_patch_target updates a target's data with the given query params.
['/api/v1/targets/:id'; patch]
fn (mut app App) v1_patch_target(id int) web.Result {
if !app.is_authorized() {
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
app.db.update_git_repo(id, app.query)
app.db.update_target(id, app.query)
if 'arch' in app.query {
arch_objs := app.query['arch'].split(',').map(GitRepoArch{ value: it })
arch_objs := app.query['arch'].split(',').map(TargetArch{ value: it })
app.db.update_git_repo_archs(id, arch_objs)
app.db.update_target_archs(id, arch_objs)
}
return app.json(http.Status.ok, new_response('Repo updated successfully.'))