From 7f4236ca894a7cf59c17e65204607a73bcf01d25 Mon Sep 17 00:00:00 2001 From: Jeroen Muis <muis.jeroen@gmail.com> Date: Tue, 27 Jul 2021 10:54:50 +0000 Subject: [PATCH] Add a test report summary resource to the GitLab pipeline API (v4) --- doc/api/pipelines.md | 53 ++++++++++++++++++++++++++ lib/api/ci/pipelines.rb | 13 +++++++ spec/requests/api/ci/pipelines_spec.rb | 39 +++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/doc/api/pipelines.md b/doc/api/pipelines.md index 7d433923865df..4b361da6070c6 100644 --- a/doc/api/pipelines.md +++ b/doc/api/pipelines.md @@ -207,6 +207,59 @@ Sample response: } ``` +### Get a pipeline's test report summary + +> Introduced in [GitLab 14.2](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/65471) + +NOTE: +This API route is part of the [Unit test report](../ci/unit_test_reports.md) feature. + +```plaintext +GET /projects/:id/pipelines/:pipeline_id/test_report_summary +``` + +| Attribute | Type | Required | Description | +|------------|---------|----------|---------------------| +| `id` | integer/string | yes | The ID or [URL-encoded path of the project](index.md#namespaced-path-encoding) owned by the authenticated user | +| `pipeline_id` | integer | yes | The ID of a pipeline | + +Sample request: + +```shell +curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/pipelines/46/test_report_summary" +``` + +Sample response: + +```json +{ + "total": { + "time": 1904, + "count": 3363, + "success": 3351, + "failed": 0, + "skipped": 12, + "error": 0, + "suite_error": null + }, + "test_suites": [ + { + "name": "test", + "total_time": 1904, + "total_count": 3363, + "success_count": 3351, + "failed_count": 0, + "skipped_count": 12, + "error_count": 0, + "build_ids": [ + 66004 + ], + "suite_error": null + } + ] +} +``` + ## Create a new pipeline ```plaintext diff --git a/lib/api/ci/pipelines.rb b/lib/api/ci/pipelines.rb index 19222ef200bf7..78264b9fedbdf 100644 --- a/lib/api/ci/pipelines.rb +++ b/lib/api/ci/pipelines.rb @@ -187,6 +187,19 @@ class Pipelines < ::API::Base present pipeline.test_reports, with: TestReportEntity, details: true end + desc 'Gets the test report summary for a given pipeline' do + detail 'This feature was introduced in GitLab 14.2' + success TestReportSummaryEntity + end + params do + requires :pipeline_id, type: Integer, desc: 'The pipeline ID' + end + get ':id/pipelines/:pipeline_id/test_report_summary' do + authorize! :read_build, pipeline + + present pipeline.test_report_summary, with: TestReportSummaryEntity + end + desc 'Deletes a pipeline' do detail 'This feature was introduced in GitLab 11.6' http_codes [[204, 'Pipeline was deleted'], [403, 'Forbidden']] diff --git a/spec/requests/api/ci/pipelines_spec.rb b/spec/requests/api/ci/pipelines_spec.rb index eb6c08618443f..6c0a1b2502f15 100644 --- a/spec/requests/api/ci/pipelines_spec.rb +++ b/spec/requests/api/ci/pipelines_spec.rb @@ -1150,4 +1150,43 @@ def expect_variables(variables, expected_variables) end end end + + describe 'GET /projects/:id/pipelines/:pipeline_id/test_report_summary' do + subject { get api("/projects/#{project.id}/pipelines/#{pipeline.id}/test_report_summary", current_user) } + + context 'authorized user' do + let(:current_user) { user } + + let(:pipeline) { create(:ci_pipeline, project: project) } + + context 'when pipeline does not have a test report summary' do + it 'returns an empty test report summary' do + subject + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response['total']['count']).to eq(0) + end + end + + context 'when pipeline has a test report summary' do + let(:pipeline) { create(:ci_pipeline, :with_report_results, project: project) } + + it 'returns the test report summary' do + subject + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response['total']['count']).to eq(2) + end + end + end + + context 'unauthorized user' do + it 'does not return project pipelines' do + get api("/projects/#{project.id}/pipelines/#{pipeline.id}/test_report_summary", non_member) + + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['message']).to eq '404 Project Not Found' + end + end + end end -- GitLab