summaryrefslogtreecommitdiff
path: root/qa/qa/runtime/allure_report.rb
blob: f92bdf74695eebc05fe7ddecaa52dde9eda4e3c0 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

require 'active_support/core_ext/enumerable'

module QA
  module Runtime
    class AllureReport
      extend QA::Support::API

      class << self
        # Configure allure reports
        #
        # @return [void]
        def configure!
          return unless Env.generate_allure_report?

          configure_allure
          configure_attachments
          configure_rspec
        end

        private

        # Configure allure reporter
        #
        # @return [void]
        def configure_allure
          # Match job names like ee:relative, ce:update etc. and set as execution environment
          env_matcher = /^(?<env>\w{2}:\S+)/

          AllureRspec.configure do |config|
            config.results_directory = ENV['QA_ALLURE_RESULTS_DIRECTORY'] || 'tmp/allure-results'
            config.clean_results_directory = true

            # automatically attach links to testcases and issues
            config.tms_tag = :testcase
            config.link_tms_pattern = '{}'
            config.issue_tag = :issue
            config.link_issue_pattern = '{}'

            config.environment_properties = environment_info if Env.running_in_ci?

            # Set custom environment name to separate same specs executed on different environments
            if Env.running_in_ci? && Env.ci_job_name.match?(env_matcher)
              config.environment = Env.ci_job_name.match(env_matcher).named_captures['env']
            end
          end
        end

        # Set up failure screenshot attachments
        #
        # @return [void]
        def configure_attachments
          Capybara::Screenshot.after_save_screenshot do |path|
            Allure.add_attachment(
              name: 'screenshot',
              source: File.open(path),
              type: Allure::ContentType::PNG,
              test_case: true
            )
          end
          Capybara::Screenshot.after_save_html do |path|
            Allure.add_attachment(
              name: 'html',
              source: File.open(path),
              type: 'text/html',
              test_case: true
            )
          end
        end

        # Configure rspec
        #
        # @return [void]
        def configure_rspec
          RSpec.configure do |config|
            config.add_formatter(QA::Support::Formatters::AllureMetadataFormatter)
            config.add_formatter(AllureRspecFormatter)

            config.append_after do
              Allure.add_attachment(
                name: 'browser.log',
                source: Capybara.current_session.driver.browser.logs.get(:browser).map(&:to_s).join("\n\n"),
                type: Allure::ContentType::TXT,
                test_case: true
              )
            end
          end
        end

        # Gitlab version and revision information
        #
        # @return [Hash]
        def environment_info
          -> do
            return {} unless Env.admin_personal_access_token || Env.personal_access_token

            client = Env.admin_personal_access_token ? API::Client.as_admin : API::Client.new
            response = get(API::Request.new(client, '/metadata').url)

            JSON.parse(response.body, symbolize_names: true).then do |metadata|
              {
                **metadata.slice(:version, :revision),
                kas_version: metadata.dig(:kas, :version)
              }.compact
            end
          rescue StandardError, ArgumentError => e
            Logger.error("Failed to attach version info to allure report: #{e}")
            {}
          end
        end
      end
    end
  end
end