/hash takes query parameters, add hash search form

This commit is contained in:
Reynir Björnsson 2021-02-02 17:23:57 +01:00
parent 03fe06be87
commit 1c5de383ea
2 changed files with 34 additions and 17 deletions

View file

@ -200,27 +200,28 @@ let routes t =
in
let hash req =
let hash_s = Router.param req "hash" in
let hash = try Some (Hex.to_cstruct (`Hex hash_s))
with Invalid_argument _ -> None
in
match hash with
let hash_hex = Request.query "sha256" req in
match Option.map (fun h -> Hex.to_cstruct (`Hex h)) hash_hex with
| None ->
Log.debug (fun m -> m "Invalid hash hex %S" hash_s);
Log.debug (fun m -> m "sha256 query parameter not provided");
Response.of_plain_text "Bad request\n" ~status:`Bad_request
|> Lwt.return
| Some hash ->
let+ build = Caqti_lwt.Pool.use (Model.build_hash hash) t.pool in
match build with
| Error e ->
Log.warn (fun m -> m "Database error: %a" pp_error e);
Response.of_plain_text "Internal server error\n" ~status:`Internal_server_error
| Ok None ->
Log.debug (fun m -> m "Hash not found: %S" hash_s);
Response.of_plain_text "Artifact not found\n" ~status:`Not_found
| Ok (Some (job_name, build)) ->
Response.redirect_to (Fmt.strf "/job/%s/build/%a/" job_name
Uuidm.pp build.Builder_db.Build.uuid)
(match build with
| Error e ->
Log.warn (fun m -> m "Database error: %a" pp_error e);
Response.of_plain_text "Internal server error\n" ~status:`Internal_server_error
| Ok None ->
Log.debug (fun m -> m "Hash not found: %S" (Request.query_exn "sha256" req));
Response.of_plain_text "Artifact not found\n" ~status:`Not_found
| Ok (Some (job_name, build)) ->
Response.redirect_to (Fmt.strf "/job/%s/build/%a/" job_name
Uuidm.pp build.Builder_db.Build.uuid))
| exception Invalid_argument _ ->
Log.debug (fun m -> m "Invalid hash hex %S" (Request.query_exn "sha256" req));
Response.of_plain_text "Bad request\n" ~status:`Bad_request
|> Lwt.return
in
[
@ -229,7 +230,7 @@ let routes t =
App.get "/job/:job/build/:build/" job_build;
App.get "/job/:job/build/:build/f/**" job_build_file;
App.post "/upload" (authorized t upload);
App.get "/hash/:hash" hash;
App.get "/hash" hash;
]
let add_routes t (app : App.t) =

View file

@ -82,6 +82,22 @@ let toggleable ?(hidden=true) id description content =
let builder jobs =
layout ~title:"Builder Web"
[ h1 [txt "Builder web"];
form ~a:[a_action "/hash"; a_method `Get]
[
label [
txt "Search artifact by SHA256";
br ();
input ~a:[
a_input_type `Search;
a_id "sha256";
a_name "sha256";
] ();
];
input ~a:[
a_input_type `Submit;
a_value "Search";
] ();
];
p [
txtf "We have currently %d jobs."
(List.length jobs);