Return 409 conflic if build uuid already exists

This commit is contained in:
Reynir Björnsson 2021-01-22 13:13:09 +01:00 committed by Gitea
parent 7a8f853663
commit 041faadb54
2 changed files with 20 additions and 4 deletions

View file

@ -156,7 +156,19 @@ let routes t =
Log.warn (fun m -> m "Received bad builder ASN.1");
Log.debug (fun m -> m "Parse error: %s" e);
Lwt.return (Response.of_plain_text "Bad request\n" ~status:`Bad_request)
| Ok exec ->
| Ok ((_, uuid, _, _, _, _, _) as exec) ->
Log.info (fun m -> m "Received build %a" pp_exec exec);
let* r = Caqti_lwt.Pool.use (Model.build_exists uuid) t.pool in
match r with
| Error e ->
Log.warn (fun m -> m "Error saving build %a: %a" pp_exec exec pp_error e);
Lwt.return (Response.of_plain_text "Internal server error\n" ~status:`Internal_server_error)
| Ok true ->
Log.warn (fun m -> m "Build with same uuid exists: %a" pp_exec exec);
Lwt.return (Response.of_plain_text
(Fmt.strf "Build with same uuid exists: %a\n" Uuidm.pp uuid)
~status:`Conflict)
| Ok false ->
let* r = Caqti_lwt.Pool.use (Model.add_build t.datadir exec) t.pool in
match r with
| Ok () ->

View file

@ -45,6 +45,10 @@ let build uuid (module Db : CONN) =
Db.find_opt Builder_db.Build.get_by_uuid uuid >>=
not_found
let build_exists uuid (module Db : CONN) =
Db.find_opt Builder_db.Build.get_by_uuid uuid >|=
Option.is_some
let job job (module Db : CONN) =
Db.collect_list Builder_db.Build.get_all_meta_by_name job