RunCommand: move JSON generation to its own function

This commit is contained in:
Graham Christensen 2021-12-08 11:29:36 -05:00
parent bb91f96381
commit 2ce0ab9f51

View file

@ -38,28 +38,8 @@ sub eventMatches {
return 0; return 0;
} }
sub buildFinished { sub makeJsonPayload {
my ($self, $build, $dependents) = @_; my ($event, $build) = @_;
my $event = "buildFinished";
my $cfg = $self->{config}->{runcommand};
my @config = defined $cfg ? ref $cfg eq "ARRAY" ? @$cfg : ($cfg) : ();
my $tmp;
foreach my $conf (@config) {
next unless eventMatches($conf, $event);
next unless configSectionMatches(
$conf->{job} // "*:*:*",
$build->get_column('project'),
$build->get_column('jobset'),
$build->get_column('job'));
my $command = $conf->{command} // die "<runcommand> section lacks a 'command' option";
unless (defined $tmp) {
$tmp = File::Temp->new(SUFFIX => '.json');
my $json = { my $json = {
event => $event, event => $event,
build => $build->id, build => $build->id,
@ -113,7 +93,31 @@ sub buildFinished {
push @{$json->{metrics}}, $j; push @{$json->{metrics}}, $j;
} }
print $tmp encode_json($json) or die; return $json;
}
sub buildFinished {
my ($self, $build, $dependents) = @_;
my $event = "buildFinished";
my $cfg = $self->{config}->{runcommand};
my @config = defined $cfg ? ref $cfg eq "ARRAY" ? @$cfg : ($cfg) : ();
my $tmp;
foreach my $conf (@config) {
next unless eventMatches($conf, $event);
next unless configSectionMatches(
$conf->{job} // "*:*:*",
$build->get_column('project'),
$build->get_column('jobset'),
$build->get_column('job'));
my $command = $conf->{command} // die "<runcommand> section lacks a 'command' option";
unless (defined $tmp) {
$tmp = File::Temp->new(SUFFIX => '.json');
print $tmp encode_json(makeJsonPayload($event, $build)) or die;
} }
$ENV{"HYDRA_JSON"} = $tmp->filename; $ENV{"HYDRA_JSON"} = $tmp->filename;