From 0d2a2d8923a418b0bf9660e65c127925a9c9c420 Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Wed, 31 Oct 2018 18:46:33 +0100 Subject: [PATCH] Add json output for the search API endpoint This commit also add a test of this feature. Note the search JSON output doesn't contain any jobs because they can not be exported to JSON yet. The JSON output on a search query matching a build looks like: ``` { "builds": [ { "buildoutputs": { "out": { "path": "/nix/store/wdag3pznrvqk01byk989irg7rq3q2a2c-job" } }, "finished": 0, "releasename": null, "starttime": null, "project": "sample", "buildproducts": {}, "timestamp": 1541007629, "buildstatus": null, "nixname": "job", "drvpath": "/nix/store/n9zqndn7j7nyr6gg3bmxvw26cfmdwv2n-job.drv", "job": "job", "id": 1, "stoptime": null, "priority": 100, "system": "x86_64-linux", "jobsetevals": [ 1 ], "jobset": "default", "buildmetrics": {} } ], "projects": [], "jobsets": [], "buildsdrv": [] } ``` --- src/lib/Hydra/Controller/Root.pm | 5 +++++ tests/api-test.pl | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/Hydra/Controller/Root.pm b/src/lib/Hydra/Controller/Root.pm index 146c37a4..19bb6fa9 100644 --- a/src/lib/Hydra/Controller/Root.pm +++ b/src/lib/Hydra/Controller/Root.pm @@ -419,6 +419,11 @@ sub search :Local Args(0) { $c->stash->{buildsdrv} = [ $c->model('DB::Builds')->search( { "drvpath" => trim($query) }, { order_by => ["id desc"] } ) ]; + + $c->stash->{resource} = { projects => $c->stash->{projects}, + jobsets => $c->stash->{jobsets}, + builds => $c->stash->{builds}, + buildsdrv => $c->stash->{buildsdrv} }; } sub serveLogFile { diff --git a/tests/api-test.pl b/tests/api-test.pl index 3bebd621..b516f736 100644 --- a/tests/api-test.pl +++ b/tests/api-test.pl @@ -1,6 +1,6 @@ use LWP::UserAgent; use JSON; -use Test::Simple tests => 17; +use Test::Simple tests => 19; my $ua = LWP::UserAgent->new; $ua->cookie_jar({}); @@ -65,3 +65,10 @@ my $build = decode_json(request_json({ uri => "/build/" . $evals->[0]->{builds}- ok($build->{job} eq "job", "The build's job name is job"); ok($build->{finished} == 0, "The build isn't finished yet"); ok($build->{buildoutputs}->{out}->{path} =~ /^\/nix\/store\/[a-zA-Z0-9]{32}-job$/, "The build's outpath is in the Nix store and named 'job'"); + + +my $search_project = decode_json(request_json({ uri => "/search/?query=sample" })->content()); +ok($search_project->{projects}[0]->{name} == "sample", "Search for project 'sample' works"); + +my $search_build = decode_json(request_json({ uri => "/search/?query=" . $build->{buildoutputs}->{out}->{path} })->content()); +ok($search_build->{builds}[0]->{buildoutputs}->{out}->{path} == $build->{buildoutputs}->{out}->{path}, "Search for builds work");