Compare commits

...

2 Commits

4 changed files with 35 additions and 14 deletions

View File

@ -49,6 +49,13 @@ pub fn new_job_queue(default_schedule CronExpression, default_base_image string)
}
}
// insert_all executes insert for each architecture of the given Target.
pub fn (mut q BuildJobQueue) insert_all(target Target) ! {
for arch in target.arch {
q.insert(target, arch.value)!
}
}
// insert a new target's job into the queue for the given architecture. This
// job will then be endlessly rescheduled after being pop'ed, unless removed
// explicitely.

View File

@ -84,6 +84,8 @@ pub fn (db &VieterDb) add_build_log(log BuildLog) int {
insert log into BuildLog
}
// Here, this does work because a log doesn't contain any foreign keys,
// meaning the ORM only has to do a single add
inserted_id := db.conn.last_id() as int
return inserted_id

View File

@ -38,14 +38,17 @@ pub fn (db &VieterDb) get_target(target_id int) ?Target {
}
// add_target inserts the given target into the database.
pub fn (db &VieterDb) add_target(repo Target) int {
pub fn (db &VieterDb) add_target(target Target) int {
sql db.conn {
insert repo into Target
insert target into Target
}
inserted_id := db.conn.last_id() as int
// ID of inserted target is the largest id
inserted_target := sql db.conn {
select from Target order by id desc limit 1
}
return inserted_id
return inserted_target.id
}
// delete_target deletes the target with the given id from the database.

View File

@ -12,17 +12,17 @@ fn (mut app App) v1_get_targets() web.Result {
filter := models.from_params<TargetFilter>(app.query) or {
return app.json(http.Status.bad_request, new_response('Invalid query parameters.'))
}
repos := app.db.get_targets(filter)
targets := app.db.get_targets(filter)
return app.json(.ok, new_data_response(repos))
return app.json(.ok, new_data_response(targets))
}
// v1_get_single_target returns the information for a single target.
['/api/v1/targets/:id'; auth; get]
fn (mut app App) v1_get_single_target(id int) web.Result {
repo := app.db.get_target(id) or { return app.not_found() }
target := app.db.get_target(id) or { return app.not_found() }
return app.json(.ok, new_data_response(repo))
return app.json(.ok, new_data_response(target))
}
// v1_post_target creates a new target from the provided query string.
@ -30,22 +30,27 @@ fn (mut app App) v1_get_single_target(id int) web.Result {
fn (mut app App) v1_post_target() web.Result {
mut params := app.query.clone()
// If a repo is created without specifying the arch, we assume it's meant
// If a target is created without specifying the arch, we assume it's meant
// for the default architecture.
if 'arch' !in params || params['arch'] == '' {
params['arch'] = app.conf.default_arch
}
new_repo := models.from_params<Target>(params) or {
mut new_target := models.from_params<Target>(params) or {
return app.json(http.Status.bad_request, new_response(err.msg()))
}
// Ensure someone doesn't submit an invalid kind
if new_repo.kind !in models.valid_kinds {
if new_target.kind !in models.valid_kinds {
return app.json(http.Status.bad_request, new_response('Invalid kind.'))
}
id := app.db.add_target(new_repo)
id := app.db.add_target(new_target)
new_target.id = id
// Add the target to the job queue
// TODO return better error here if it's the cron schedule that's incorrect
app.job_queue.insert_all(new_target) or { return app.status(.internal_server_error) }
return app.json(.ok, new_data_response(id))
}
@ -54,6 +59,7 @@ fn (mut app App) v1_post_target() web.Result {
['/api/v1/targets/:id'; auth; delete]
fn (mut app App) v1_delete_target(id int) web.Result {
app.db.delete_target(id)
app.job_queue.invalidate(id)
return app.json(.ok, new_response(''))
}
@ -69,7 +75,10 @@ fn (mut app App) v1_patch_target(id int) web.Result {
app.db.update_target_archs(id, arch_objs)
}
repo := app.db.get_target(id) or { return app.status(.internal_server_error) }
target := app.db.get_target(id) or { return app.status(.internal_server_error) }
return app.json(.ok, new_data_response(repo))
app.job_queue.invalidate(id)
app.job_queue.insert_all(target) or { return app.status(.internal_server_error) }
return app.json(.ok, new_data_response(target))
}