summaryrefslogtreecommitdiff
path: root/qa/qa/tools/ci/test_results.rb
blob: 635b69f6ca0d6d8081642c5e20987098717134fa (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

module QA
  module Tools
    module Ci
      class TestResults
        include Helpers

        def initialize(pipeline_name, test_report_job_name, report_path)
          @pipeline_name = pipeline_name
          @test_report_job_name = test_report_job_name
          @report_path = report_path
        end

        # Get test report artifacts from downstream pipeline
        #
        # @param [String] pipeline_name
        # @param [String] test_report_job_name
        # @param [String] report_path
        # @return [void]
        def self.get(pipeline_name, test_report_job_name, report_path)
          new(pipeline_name, test_report_job_name, report_path).download_test_results
        end

        # Download test results from child pipeline
        #
        # @return [void]
        def download_test_results
          logger.info("Fetching test results for '#{pipeline_name}'")

          logger.debug("  fetching pipeline id of '#{pipeline_name}' child pipeline")
          downstream_pipeline_id = api_get("#{pipelines_url(pipeline_id)}/bridges")
            .find { |bridge| bridge[:name] == pipeline_name }
            &.dig(:downstream_pipeline, :id)
          return logger.error("Child pipeline '#{pipeline_name}' not found!") unless downstream_pipeline_id

          logger.debug("  fetching job id of test report job")
          job_id = api_get("#{pipelines_url(downstream_pipeline_id)}/jobs")
            .find { |job| job[:name] == test_report_job_name }
            &.fetch(:id)
          return logger.error("Test report job '#{test_report_job_name}' not found!") unless job_id

          logger.debug("  fetching test results artifact archive")
          response = api_get("/projects/#{project_id}/jobs/#{job_id}/artifacts", raw_response: true)

          logger.info("Extracting test result archive")
          system("unzip", "-o", "-d", report_path, response.file.path)
        end

        private

        attr_reader :pipeline_name, :test_report_job_name, :report_path

        # Base get pipeline url
        #
        # @param [Integer] id
        # @return [String]
        def pipelines_url(id)
          "/projects/#{project_id}/pipelines/#{id}"
        end

        # Current pipeline id
        #
        # @return [String]
        def pipeline_id
          ENV["CI_PIPELINE_ID"]
        end

        # Current project id
        #
        # @return [String]
        def project_id
          ENV["CI_PROJECT_ID"]
        end
      end
    end
  end
end