summaryrefslogtreecommitdiff
path: root/qa/qa/resource/pipeline.rb
blob: 910065d76a8389baca94fd4a7f002811ae7527e1 (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
# frozen_string_literal: true

module QA
  module Resource
    class Pipeline < Base
      attribute :project do
        Resource::Project.fabricate! do |project|
          project.name = 'project-with-pipeline'
        end
      end

      attributes :id,
                 :status,
                 :ref,
                 :sha

      # array in form
      # [
      #   { key: 'UPLOAD_TO_S3', variable_type: 'file', value: true },
      #   { key: 'SOMETHING', variable_type: 'env_var', value: 'yes' }
      # ]
      attribute :variables

      def initialize
        @variables = []
      end

      def fabricate!
        project.visit!

        Page::Project::Menu.perform(&:click_ci_cd_pipelines)
        Page::Project::Pipeline::Index.perform(&:click_run_pipeline_button)
        Page::Project::Pipeline::New.perform(&:click_run_pipeline_button)
      end

      def fabricate_via_api!
        resource_web_url(api_get)
      rescue ResourceNotFoundError
        super
      rescue NoValueError
        super
      end

      def ref
        project.default_branch
      end

      def api_get_path
        "/projects/#{project.id}/pipelines/#{id}"
      end

      def api_post_path
        "/projects/#{project.id}/pipeline"
      end

      def api_post_body
        {
          ref: ref,
          variables: variables
        }
      end

      def pipeline_variables
        response = get(request_url("#{api_get_path}/variables"))

        unless response.code == HTTP_STATUS_OK
          raise ResourceQueryError, "Could not get variables. Request returned (#{response.code}): `#{response}`."
        end

        parse_body(response)
      end

      def has_variable?(key:, value:)
        pipeline_variables.any? { |var| var[:key] == key && var[:value] == value }
      end

      def has_no_variable?(key:, value:)
        !pipeline_variables.any? { |var| var[:key] == key && var[:value] == value }
      end

      def pipeline_bridges
        response = get(request_url("#{api_get_path}/bridges"))

        unless response.code == HTTP_STATUS_OK
          raise ResourceQueryError, "Could not get bridges. Request returned (#{response.code}): `#{response}`."
        end

        parse_body(response)
      end

      def downstream_pipeline_id(bridge_name:)
        result = pipeline_bridges.find { |bridge| bridge[:name] == bridge_name }

        result[:downstream_pipeline][:id]
      end
    end
  end
end