From c60c8d10eab3a5cd896690ec9a459126ccc9b5da Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Fri, 24 Sep 2021 15:52:46 -0400 Subject: [PATCH] add api endpoint: /build//constituents Returns a list of constituent builds --- hydra-api.yaml | 26 +++++++++++++++++ src/lib/Hydra/Controller/Build.pm | 13 +++++++++ t/Controller/Build/constituents.t | 46 +++++++++++++++++++++++++++++++ t/jobs/aggregate.nix | 26 +++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 t/Controller/Build/constituents.t create mode 100644 t/jobs/aggregate.nix diff --git a/hydra-api.yaml b/hydra-api.yaml index 2a454942..656b2c14 100644 --- a/hydra-api.yaml +++ b/hydra-api.yaml @@ -504,6 +504,32 @@ paths: schema: $ref: '#/components/schemas/Error' + /build/{build-id}/constituents: + get: + summary: Retrieves a build's constituent jobs + parameters: + - name: build-id + in: path + description: build identifier + required: true + schema: + type: integer + responses: + '200': + description: build + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Build' + '404': + description: build couldn't be found + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + /eval/{build-id}: get: summary: Retrieves evaluations identified by build id diff --git a/src/lib/Hydra/Controller/Build.pm b/src/lib/Hydra/Controller/Build.pm index 765dcbd7..c3bcd8ac 100644 --- a/src/lib/Hydra/Controller/Build.pm +++ b/src/lib/Hydra/Controller/Build.pm @@ -104,6 +104,19 @@ sub build_GET { $c->stash->{binaryCachePublicUri} = $c->config->{binary_cache_public_uri}; } +sub constituents :Chained('buildChain') :PathPart('constituents') :Args(0) :ActionClass('REST') { } + +sub constituents_GET { + my ($self, $c) = @_; + + my $build = $c->stash->{build}; + + $self->status_ok( + $c, + entity => [$build->constituents_->search({}, {order_by => ["job"]})] + ); +} + sub view_nixlog : Chained('buildChain') PathPart('nixlog') { my ($self, $c, $stepnr, $mode) = @_; diff --git a/t/Controller/Build/constituents.t b/t/Controller/Build/constituents.t new file mode 100644 index 00000000..47a2d560 --- /dev/null +++ b/t/Controller/Build/constituents.t @@ -0,0 +1,46 @@ +use strict; +use Setup; +use JSON qw(decode_json encode_json); +use Data::Dumper; +use URI; +my %ctx = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; +require Hydra::Helper::Nix; + +use Test2::V0; +require Catalyst::Test; +Catalyst::Test->import('Hydra'); +use HTTP::Request::Common; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); + +my $jobset = createBaseJobset("aggregate", "aggregate.nix", $ctx{jobsdir}); + +ok(evalSucceeds($jobset), "Evaluating jobs/aggregate.nix should exit with return code 0"); +is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/aggregate.nix should result in 3 builds"); +for my $build (queuedBuildsForJobset($jobset)) { + ok(runBuild($build), "Build '".$build->job."' from jobs/aggregate.nix should exit with return code 0"); +} + +my $build_redirect = request(GET '/job/tests/aggregate/aggregate/latest-finished'); + +my $url = URI->new($build_redirect->header('location'))->path . "/constituents"; +my $constituents = request(GET $url, + Accept => 'application/json', + ); + +ok($constituents->is_success, "Getting the constituent builds"); +my $data = decode_json($constituents->content); + +my ($buildA) = grep { $_->{nixname} eq "empty-dir-a" } @$data; +my ($buildB) = grep { $_->{nixname} eq "empty-dir-b" } @$data; + +is($buildA->{job}, "a"); +is($buildB->{job}, "b"); + +done_testing; diff --git a/t/jobs/aggregate.nix b/t/jobs/aggregate.nix new file mode 100644 index 00000000..b8025bb6 --- /dev/null +++ b/t/jobs/aggregate.nix @@ -0,0 +1,26 @@ +with import ./config.nix; +{ + a = + mkDerivation { + name = "empty-dir-a"; + builder = ./empty-dir-builder.sh; + }; + + b = + mkDerivation { + name = "empty-dir-b"; + builder = ./empty-dir-builder.sh; + }; + + aggregate = + mkDerivation { + name = "aggregate"; + builder = ./empty-dir-builder.sh; # doesn't matter, just needs to pass a build + + _hydraAggregate = true; + constituents = [ + "a" + "b" + ]; + }; +}