14_pipelines.rb 4.29 KB
Newer Older
Grzegorz Bizon's avatar
Grzegorz Bizon committed
1
class Gitlab::Seeder::Builds
2
  STAGES = %w[build test deploy notify]
3 4 5 6 7 8 9
  BUILDS = [
    { name: 'build:linux', stage: 'build', status: :success },
    { name: 'build:osx', stage: 'build', status: :success },
    { name: 'rspec:linux', stage: 'test', status: :success },
    { name: 'rspec:windows', stage: 'test', status: :success },
    { name: 'rspec:windows', stage: 'test', status: :success },
    { name: 'rspec:osx', stage: 'test', status_event: :success },
10 11 12 13 14 15
    { name: 'spinach:linux', stage: 'test', status: :success },
    { name: 'spinach:osx', stage: 'test', status: :failed, allow_failure: true},
    { name: 'env:alpha', stage: 'deploy', environment: 'alpha', status: :pending },
    { name: 'env:beta', stage: 'deploy', environment: 'beta', status: :running },
    { name: 'env:gamma', stage: 'deploy', environment: 'gamma', status: :canceled },
    { name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success },
16 17
    { name: 'production', stage: 'deploy', environment: 'production', when: 'manual', status: :skipped },
    { name: 'slack', stage: 'notify', when: 'manual', status: :created },
18
  ]
Z.J. van de Weg's avatar
Z.J. van de Weg committed
19

Grzegorz Bizon's avatar
Grzegorz Bizon committed
20 21 22
  def initialize(project)
    @project = project
  end
23

Grzegorz Bizon's avatar
Grzegorz Bizon committed
24
  def seed!
25
    pipelines.each do |pipeline|
26
      begin
27
        BUILDS.each { |opts| build_create!(pipeline, opts) }
28
        commit_status_create!(pipeline, name: 'jenkins', stage: 'test', status: :success)
29 30 31
        print '.'
      rescue ActiveRecord::RecordInvalid
        print 'F'
32 33
      ensure
        pipeline.build_updated
34 35
      end
    end
36
  end
Grzegorz Bizon's avatar
Grzegorz Bizon committed
37

38 39
  private

40
  def pipelines
41
    create_master_pipelines + create_merge_request_pipelines
42 43
  end

44
  def create_master_pipelines
45
    @project.repository.commits('master', limit: 4).map do |commit|
46 47
      create_pipeline!(@project, 'master', commit)
    end
Grzegorz Bizon's avatar
Grzegorz Bizon committed
48 49 50 51
  rescue
    []
  end

52
  def create_merge_request_pipelines
53
    pipelines = @project.merge_requests.first(3).map do |merge_request|
54 55 56
      project = merge_request.source_project
      branch = merge_request.source_branch

57
      merge_request.commits.last(4).map do |commit|
58 59 60 61 62
        create_pipeline!(project, branch, commit)
      end
    end

    pipelines.flatten
63 64 65 66 67
  rescue
    []
  end


68 69
  def create_pipeline!(project, ref, commit)
    project.pipelines.create(sha: commit.id, ref: ref)
70 71
  end

72
  def build_create!(pipeline, opts = {})
73 74
    attributes = job_attributes(pipeline, opts)
      .merge(commands: '$ build command')
75

76
    Ci::Build.create!(attributes).tap do |build|
77 78 79 80 81
      setup_artifacts(build)
      setup_build_log(build)
      build.save
    end
  end
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  def setup_artifacts(build)
    return unless %w[build test].include?(build.stage)

    artifacts_cache_file(artifacts_archive_path) do |file|
      build.artifacts_file = file
    end

    artifacts_cache_file(artifacts_metadata_path) do |file|
      build.artifacts_metadata = file
    end
  end

  def setup_build_log(build)
    ##
    # We need to set build trace after saving a build (id required)
    # That is why we need `#tap` method instead of passing block
    # directly to `Ci::Build#create!`.
    #
    if %w(running success failed).include?(build.status)
      build.trace = FFaker::Lorem.paragraphs(6).join("\n\n")
103 104
    end
  end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
105

106
  def commit_status_create!(pipeline, opts = {})
107 108
    attributes = job_attributes(pipeline, opts)

Z.J. van de Weg's avatar
Z.J. van de Weg committed
109
    GenericCommitStatus.create!(attributes)
110
  end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
111

112
  def job_attributes(pipeline, opts)
113
    { name: 'test build', stage: 'test', stage_idx: stage_index(opts[:stage]),
Z.J. van de Weg's avatar
Z.J. van de Weg committed
114
      ref: 'master', tag: false, user: build_user, project: @project, pipeline: pipeline,
115 116 117
      created_at: Time.now, updated_at: Time.now
    }.merge(opts)
  end
118

Grzegorz Bizon's avatar
Grzegorz Bizon committed
119 120 121 122 123
  def build_user
    @project.team.users.sample
  end

  def build_status
124
    Ci::Build::AVAILABLE_STATUSES.sample
Grzegorz Bizon's avatar
Grzegorz Bizon committed
125 126
  end

127 128 129 130
  def stage_index(stage)
    STAGES.index(stage) || 0
  end

131
  def artifacts_archive_path
132
    Rails.root + 'spec/fixtures/ci_build_artifacts.zip'
Grzegorz Bizon's avatar
Grzegorz Bizon committed
133 134
  end

135 136 137 138 139 140 141 142 143 144 145
  def artifacts_metadata_path
    Rails.root + 'spec/fixtures/ci_build_artifacts_metadata.gz'
  end

  def artifacts_cache_file(file_path)
    cache_path = file_path.to_s.gsub('ci_', "p#{@project.id}_")

    FileUtils.copy(file_path, cache_path)
    File.open(cache_path) do |file|
      yield file
    end
Grzegorz Bizon's avatar
Grzegorz Bizon committed
146 147 148 149
  end
end

Gitlab::Seeder.quiet do
150
  Project.all.sample(5).each do |project|
Grzegorz Bizon's avatar
Grzegorz Bizon committed
151 152 153
    project_builds = Gitlab::Seeder::Builds.new(project)
    project_builds.seed!
  end
154
end