summaryrefslogtreecommitdiff
path: root/spec/factories/ci/pipelines.rb
blob: 9652b0000a97525d152783bd1f9d8bf4fdaadd05 (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
# frozen_string_literal: true

FactoryBot.define do
  factory :ci_empty_pipeline, class: Ci::Pipeline do
    source :push
    ref 'master'
    sha '97de212e80737a608d939f648d959671fb0a0142'
    status 'pending'
    protected false

    project

    factory :ci_pipeline_without_jobs do
      after(:build) do |pipeline|
        pipeline.instance_variable_set(:@ci_yaml_file, YAML.dump({}))
      end
    end

    factory :ci_pipeline_with_one_job do
      after(:build) do |pipeline|
        allow(pipeline).to receive(:ci_yaml_file) do
          pipeline.instance_variable_set(:@ci_yaml_file, YAML.dump({ rspec: { script: "ls" } }))
        end
      end
    end

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

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

    factory :ci_pipeline do
      transient { config nil }

      after(:build) do |pipeline, evaluator|
        if evaluator.config
          pipeline.instance_variable_set(:@ci_yaml_file, YAML.dump(evaluator.config))

          # Populates pipeline with errors
          pipeline.config_processor if evaluator.config
        else
          pipeline.instance_variable_set(:@ci_yaml_file, File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')))
        end
      end

      trait :invalid do
        config(rspec: nil)
        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
        protected true
      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_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
    end
  end
end