summaryrefslogtreecommitdiff
path: root/spec/factories/ci/pipelines.rb
blob: 14bd0ab1bc62093c3cece8762f85d4f741f625a6 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# frozen_string_literal: true

FactoryBot.define do
  # TODO: we can remove this factory in favour of :ci_pipeline
  factory :ci_empty_pipeline, class: 'Ci::Pipeline' do
    source { :push }
    ref { 'master' }
    sha { 'b83d6e391c22777fca1ed3012fce84f633d7fed0' }
    status { 'pending' }
    add_attribute(:protected) { false }

    project

    # Persist merge request head_pipeline_id
    # on pipeline factories to avoid circular references
    transient { head_pipeline_of { nil } }

    transient { child_of { nil } }

    after(:build) do |pipeline, evaluator|
      if evaluator.child_of
        pipeline.project = evaluator.child_of.project
        pipeline.source = :parent_pipeline
      end
    end

    after(:create) do |pipeline, evaluator|
      merge_request = evaluator.head_pipeline_of
      merge_request&.update!(head_pipeline: pipeline)

      if evaluator.child_of
        bridge = create(:ci_bridge, pipeline: evaluator.child_of)
        create(:ci_sources_pipeline,
          source_job: bridge,
          pipeline: pipeline)
      end
    end

    factory :ci_pipeline do
      transient { ci_ref_presence { true } }

      before(:create) do |pipeline, evaluator|
        pipeline.ensure_ci_ref! if evaluator.ci_ref_presence && pipeline.ci_ref_id.nil?
      end

      trait :invalid do
        status { :failed }
        yaml_errors { 'invalid YAML' }
        failure_reason { :config_error }
      end

      trait :created do
        status { :created }
      end

      trait :preparing do
        status { :preparing }
      end

      trait :blocked do
        status { :manual }
      end

      trait :scheduled do
        status { :scheduled }
      end

      trait :success do
        status { :success }
      end

      trait :running do
        status { :running }
      end

      trait :failed do
        status { :failed }
      end

      trait :protected do
        add_attribute(:protected) { true }
      end

      trait :with_report_results do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :report_results, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_codequality_report do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :codequality_report, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_test_reports do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :test_reports, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_test_reports_attachment do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :test_reports_with_attachment, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_broken_test_reports do
        status { :success }

        after(:build) do |pipeline, _evaluator|
          pipeline.builds << build(:ci_build, :broken_test_reports, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_test_reports_with_three_failures do
        status { :success }

        after(:build) do |pipeline, _evaluator|
          pipeline.builds << build(:ci_build, :test_reports_with_three_failures, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_accessibility_reports do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :accessibility_reports, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_coverage_reports do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :coverage_reports, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_coverage_report_artifact do
        after(:build) do |pipeline, evaluator|
          pipeline.pipeline_artifacts << build(:ci_pipeline_artifact, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_terraform_reports do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :terraform_reports, pipeline: pipeline, project: pipeline.project)
          pipeline.builds << build(:ci_build, :terraform_reports, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :with_exposed_artifacts do
        status { :success }

        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, :artifacts,
            pipeline: pipeline,
            project: pipeline.project,
            options: { artifacts: { expose_as: 'the artifact', paths: ['ci_artifacts.txt'] } })
        end
      end

      trait :with_job do
        after(:build) do |pipeline, evaluator|
          pipeline.builds << build(:ci_build, pipeline: pipeline, project: pipeline.project)
        end
      end

      trait :auto_devops_source do
        config_source { Ci::Pipeline.config_sources[:auto_devops_source] }
      end

      trait :repository_source do
        config_source { Ci::Pipeline.config_sources[:repository_source] }
      end

      trait :detached_merge_request_pipeline do
        merge_request

        source { :merge_request_event }
        project { merge_request.source_project }
        sha { merge_request.source_branch_sha }
        ref { merge_request.ref_path }
      end

      trait :legacy_detached_merge_request_pipeline do
        detached_merge_request_pipeline

        ref { merge_request.source_branch }
      end

      trait :merged_result_pipeline do
        detached_merge_request_pipeline

        sha { 'test-merge-sha'}
        ref { merge_request.merge_ref_path }
        source_sha { merge_request.source_branch_sha }
        target_sha { merge_request.target_branch_sha }
      end

      trait :webide do
        source { :webide }
        config_source { :webide_source }
      end
    end
  end
end