summaryrefslogtreecommitdiff
path: root/app/controllers/projects/pipelines/tests_controller.rb
blob: 20bdb4d3af18a107d614bb58cec18cf197194b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

module Projects
  module Pipelines
    class TestsController < Projects::Pipelines::ApplicationController
      urgency :low, [:show, :summary]

      before_action :authorize_read_build!
      before_action :builds, only: [:show]
      before_action :validate_test_reports!, only: [:show]

      feature_category :code_testing

      def summary
        respond_to do |format|
          format.json do
            render json: TestReportSummarySerializer
              .new(project: project, current_user: @current_user)
              .represent(pipeline.test_report_summary)
          end
        end
      end

      def show
        respond_to do |format|
          format.json do
            render json: TestSuiteSerializer
              .new(project: project, current_user: @current_user)
              .represent(test_suite, details: true)
          end
        end
      end

      private

      def validate_test_reports!
        unless pipeline.has_test_reports?
          render json: { errors: 'Test report artifacts have expired' }, status: :not_found
        end
      end

      def builds
        @builds ||= pipeline.latest_builds.id_in(build_ids).presence || render_404
      end

      def build_ids
        return [] unless params[:build_ids]

        params[:build_ids].split(",")
      end

      def test_suite
        suite = builds.sum do |build|
          build.collect_test_reports!(Gitlab::Ci::Reports::TestReports.new)
        end

        Gitlab::Ci::Reports::TestFailureHistory.new(suite.failed.values, project).load!

        suite
      end
    end
  end
end