summaryrefslogtreecommitdiff
path: root/db/fixtures/development/19_environments.rb
blob: 0f9188164c9d418ede68a5757c6d2dcdee9ac3be (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
require './spec/support/sidekiq_middleware'

class Gitlab::Seeder::Environments
  def initialize(project)
    @project = project
  end

  def seed!
    @project.create_mock_monitoring_service!(active: true)

    create_master_deployments!('production')
    create_master_deployments!('staging')
    create_merge_request_review_deployments!
  end

  private

  def create_master_deployments!(name)
    @project.repository.commits('master', limit: 4).map do |commit|
      create_deployment!(
        @project,
        name,
        'master',
        commit.id
      )
    end
  end

  def create_merge_request_review_deployments!
    @project
      .merge_requests
      .select { |mr| mr.source_branch.match?(/[a-zA-Z0-9]+/) }
      .sample(4)
      .each do |merge_request|
      next unless merge_request.diff_head_sha

      create_deployment!(
        merge_request.source_project,
        "review/#{merge_request.source_branch.gsub(/[^a-zA-Z0-9]+/, '')}",
        merge_request.source_branch,
        merge_request.diff_head_sha
      )
    end
  end

  def create_deployment!(project, name, ref, sha)
    environment = find_or_create_environment!(project, name)
    environment.deployments.create!(
      project: project,
      ref: ref,
      sha: sha,
      tag: false,
      deployable: find_deployable(project, name)
    )
  end

  def find_or_create_environment!(project, name)
    project.environments.find_or_create_by!(name: name).tap do |environment|
      environment.update(external_url: "https://google.com/#{name}")
    end
  end

  def find_deployable(project, environment)
    project.builds.where(environment: environment).sample
  end
end

Gitlab::Seeder.quiet do
  Project.not_mass_generated.sample(5).each do |project|
    project_environments = Gitlab::Seeder::Environments.new(project)
    project_environments.seed!
  end
end