From 2ce0ab9f5198be7cf7c2af3b2a6a2d480639d4a5 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Wed, 8 Dec 2021 11:29:36 -0500 Subject: [PATCH] RunCommand: move JSON generation to its own function --- src/lib/Hydra/Plugin/RunCommand.pm | 114 +++++++++++++++-------------- 1 file changed, 59 insertions(+), 55 deletions(-) diff --git a/src/lib/Hydra/Plugin/RunCommand.pm b/src/lib/Hydra/Plugin/RunCommand.pm index 33295e50..91a3087a 100644 --- a/src/lib/Hydra/Plugin/RunCommand.pm +++ b/src/lib/Hydra/Plugin/RunCommand.pm @@ -38,6 +38,64 @@ sub eventMatches { return 0; } +sub makeJsonPayload { + my ($event, $build) = @_; + my $json = { + event => $event, + build => $build->id, + finished => $build->get_column('finished') ? JSON::true : JSON::false, + timestamp => $build->get_column('timestamp'), + project => $build->get_column('project'), + jobset => $build->get_column('jobset'), + job => $build->get_column('job'), + drvPath => $build->get_column('drvpath'), + startTime => $build->get_column('starttime'), + stopTime => $build->get_column('stoptime'), + buildStatus => $build->get_column('buildstatus'), + nixName => $build->get_column('nixname'), + system => $build->get_column('system'), + homepage => $build->get_column('homepage'), + description => $build->get_column('description'), + license => $build->get_column('license'), + outputs => [], + products => [], + metrics => [], + }; + + for my $output ($build->buildoutputs) { + my $j = { + name => $output->name, + path => $output->path, + }; + push @{$json->{outputs}}, $j; + } + + for my $product ($build->buildproducts) { + my $j = { + productNr => $product->productnr, + type => $product->type, + subtype => $product->subtype, + fileSize => $product->filesize, + sha256hash => $product->sha256hash, + path => $product->path, + name => $product->name, + defaultPath => $product->defaultpath, + }; + push @{$json->{products}}, $j; + } + + for my $metric ($build->buildmetrics) { + my $j = { + name => $metric->name, + unit => $metric->unit, + value => 0 + $metric->value, + }; + push @{$json->{metrics}}, $j; + } + + return $json; +} + sub buildFinished { my ($self, $build, $dependents) = @_; my $event = "buildFinished"; @@ -59,61 +117,7 @@ sub buildFinished { unless (defined $tmp) { $tmp = File::Temp->new(SUFFIX => '.json'); - - my $json = { - event => $event, - build => $build->id, - finished => $build->get_column('finished') ? JSON::true : JSON::false, - timestamp => $build->get_column('timestamp'), - project => $build->get_column('project'), - jobset => $build->get_column('jobset'), - job => $build->get_column('job'), - drvPath => $build->get_column('drvpath'), - startTime => $build->get_column('starttime'), - stopTime => $build->get_column('stoptime'), - buildStatus => $build->get_column('buildstatus'), - nixName => $build->get_column('nixname'), - system => $build->get_column('system'), - homepage => $build->get_column('homepage'), - description => $build->get_column('description'), - license => $build->get_column('license'), - outputs => [], - products => [], - metrics => [], - }; - - for my $output ($build->buildoutputs) { - my $j = { - name => $output->name, - path => $output->path, - }; - push @{$json->{outputs}}, $j; - } - - for my $product ($build->buildproducts) { - my $j = { - productNr => $product->productnr, - type => $product->type, - subtype => $product->subtype, - fileSize => $product->filesize, - sha256hash => $product->sha256hash, - path => $product->path, - name => $product->name, - defaultPath => $product->defaultpath, - }; - push @{$json->{products}}, $j; - } - - for my $metric ($build->buildmetrics) { - my $j = { - name => $metric->name, - unit => $metric->unit, - value => 0 + $metric->value, - }; - push @{$json->{metrics}}, $j; - } - - print $tmp encode_json($json) or die; + print $tmp encode_json(makeJsonPayload($event, $build)) or die; } $ENV{"HYDRA_JSON"} = $tmp->filename;