Add /job/<job>/build/<build>/main-binary redirect endpoint

Fixes #52
This commit is contained in:
Robur 2021-09-08 09:10:30 +00:00
parent 8279bc1c26
commit edcbf73386
5 changed files with 34 additions and 0 deletions

View file

@ -188,6 +188,13 @@ module Build_artifact = struct
Caqti_type.unit Caqti_type.unit
"DROP TABLE IF EXISTS build_artifact" "DROP TABLE IF EXISTS build_artifact"
let get =
Caqti_request.find
(id `build_artifact)
file
{| SELECT filepath, localpath, sha256, size
FROM build_artifact WHERE id = ? |}
let get_by_build = let get_by_build =
Caqti_request.find Caqti_request.find
(Caqti_type.tup2 (id `build) fpath) (Caqti_type.tup2 (id `build) fpath)

View file

@ -99,6 +99,7 @@ module Build_artifact : sig
val rollback : val rollback :
(unit, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t (unit, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t
val get : ([`build_artifact] id, file, [< `Many | `One | `Zero > `One]) Caqti_request.t
val get_by_build : val get_by_build :
([`build] id * Fpath.t, [`build_artifact] id * file, ([`build] id * Fpath.t, [`build_artifact] id * file,
[< `Many | `One | `Zero > `One ]) Caqti_request.t [< `Many | `One | `Zero > `One ]) Caqti_request.t

View file

@ -130,6 +130,25 @@ let add_routes datadir =
|> Lwt_result.ok |> Lwt_result.ok
in in
let redirect_main_binary req =
let job_name = Dream.param "job" req
and build = Dream.param "build" req in
get_uuid build >>= fun uuid ->
Dream.sql req (Model.build uuid)
|> if_error "Error getting job build"
~log:(fun e -> Log.warn (fun m -> m "Error getting job build: %a" pp_error e))
>>= fun (_id, build) ->
match build.Builder_db.Build.main_binary with
| None -> Lwt_result.fail ("Resource not found", `Not_Found)
| Some main_binary ->
Dream.sql req (Model.build_artifact_by_id main_binary)
|> if_error "Error getting main binary" >>= fun main_binary ->
Dream.redirect req
(Fmt.strf "/job/%s/build/%a/f/%a" job_name Uuidm.pp uuid
Fpath.pp main_binary.Builder_db.filepath)
|> Lwt_result.ok
in
let job_build req = let job_build req =
let job_name = Dream.param "job" req let job_name = Dream.param "job" req
and build = Dream.param "build" req in and build = Dream.param "build" req in
@ -302,6 +321,7 @@ let add_routes datadir =
Dream.get "/job/:job/build/latest/**" (w redirect_latest); Dream.get "/job/:job/build/latest/**" (w redirect_latest);
Dream.get "/job/:job/build/:build/" (w job_build); Dream.get "/job/:job/build/:build/" (w job_build);
Dream.get "/job/:job/build/:build/f/**" (w job_build_file); Dream.get "/job/:job/build/:build/f/**" (w job_build_file);
Dream.get "/job/:job/build/:build/main-binary" (w redirect_main_binary);
Dream.get "/hash" (w hash); Dream.get "/hash" (w hash);
Dream.get "/compare/:build_left/:build_right/opam-switch" (w compare_opam); Dream.get "/compare/:build_left/:build_right/opam-switch" (w compare_opam);
Dream.post "/upload" (Authorization.authenticate (w upload)); Dream.post "/upload" (Authorization.authenticate (w upload));

View file

@ -36,6 +36,9 @@ let build_artifact build filepath (module Db : CONN) =
Db.find_opt Builder_db.Build_artifact.get_by_build_uuid (build, filepath) Db.find_opt Builder_db.Build_artifact.get_by_build_uuid (build, filepath)
>>= not_found >|= snd >>= not_found >|= snd
let build_artifact_by_id id (module Db : CONN) =
Db.find Builder_db.Build_artifact.get id
let build_artifact_data datadir file = let build_artifact_data datadir file =
read_file datadir file.Builder_db.localpath read_file datadir file.Builder_db.localpath

View file

@ -12,6 +12,9 @@ val cleanup_staging : Fpath.t -> Caqti_lwt.connection ->
val build_artifact : Uuidm.t -> Fpath.t -> Caqti_lwt.connection -> val build_artifact : Uuidm.t -> Fpath.t -> Caqti_lwt.connection ->
(Builder_db.file, [> error ]) result Lwt.t (Builder_db.file, [> error ]) result Lwt.t
val build_artifact_by_id : [`build_artifact] Builder_db.id -> Caqti_lwt.connection ->
(Builder_db.file, [> Caqti_error.call_or_retrieve ]) result Lwt.t
val build_artifact_data : Fpath.t -> Builder_db.file -> val build_artifact_data : Fpath.t -> Builder_db.file ->
(string, [> error ]) result Lwt.t (string, [> error ]) result Lwt.t