From f64230b45edf07d16446e5ba17977c213937a5f0 Mon Sep 17 00:00:00 2001 From: Jelle Besseling Date: Thu, 24 Dec 2020 15:44:54 +0100 Subject: [PATCH] Add endpoint to generate a shields.io badge --- hydra-api.yaml | 35 +++++++++++++++++++++++++++++++++ src/lib/Hydra/Controller/Job.pm | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/hydra-api.yaml b/hydra-api.yaml index 7da99eb8..6b8096e8 100644 --- a/hydra-api.yaml +++ b/hydra-api.yaml @@ -384,6 +384,41 @@ paths: schema: $ref: '#/components/schemas/Error' + /job/{project-id}/{jobset-id}/{job-id}/shield: + get: + summary: Generate data for a shields.io badge + parameters: + - name: project-id + in: path + description: project identifier + required: true + schema: + type: string + - name: jobset-id + in: path + description: jobset identifier + required: true + schema: + type: string + - name: job-id + in: path + description: job identifier + required: true + schema: + type: string + responses: + '200': + description: see https://shields.io/endpoint on how to use this + content: + application/json: + examples: + shield-success: + value: + color: green + schemaVersion: 1 + label: hydra build + message: passing + /build/{build-id}: get: summary: Retrieves a single build of a jobset by id diff --git a/src/lib/Hydra/Controller/Job.pm b/src/lib/Hydra/Controller/Job.pm index 0487d574..efaa508e 100644 --- a/src/lib/Hydra/Controller/Job.pm +++ b/src/lib/Hydra/Controller/Job.pm @@ -28,6 +28,40 @@ sub job : Chained('/') PathPart('job') CaptureArgs(3) { $c->stash->{project} = $c->stash->{jobset}->project; } +sub shield :Chained('job') PathPart('shield') Args(0) { + my ($self, $c) = @_; + + my $job = $c->stash->{job}; + + my $lastBuild = $c->stash->{jobset}->builds->find( + { job => $job, finished => 1 }, + { order_by => 'id DESC', rows => 1, columns => [@buildListColumns] } + ); + notFound($c, "No latest build for job ‘$job’.") unless defined $lastBuild; + + my $color = + $lastBuild->buildstatus == 0 ? "green" : + $lastBuild->buildstatus == 4 ? "yellow" : + "red"; + my $message = + $lastBuild->buildstatus == 0 ? "passing" : + $lastBuild->buildstatus == 4 ? "cancelled" : + "failing"; + + $c->response->content_type('application/json'); + $c->stash->{'plain'} = { + data => scalar (JSON::Any->objToJson( + { + schemaVersion => 1, + label => "hydra build", + color => $color, + message => $message, + })) + }; + $c->forward('Hydra::View::Plain'); +} + + sub prometheus : Chained('job') PathPart('prometheus') Args(0) { my ($self, $c) = @_; my $prometheus = Net::Prometheus->new;