BigW Consortium Gitlab

14_pipelines.rb 4.73 KB
Newer Older
1
class Gitlab::Seeder::Pipelines
2
  STAGES = %w[build test deploy notify]
3 4 5
  BUILDS = [
    { name: 'build:linux', stage: 'build', status: :success },
    { name: 'build:osx', stage: 'build', status: :success },
Kamil Trzcinski committed
6 7 8 9 10 11 12
    { name: 'rspec:linux 0 3', stage: 'test', status: :success },
    { name: 'rspec:linux 1 3', stage: 'test', status: :success },
    { name: 'rspec:linux 2 3', stage: 'test', status: :success },
    { name: 'rspec:windows 0 3', stage: 'test', status: :success },
    { name: 'rspec:windows 1 3', stage: 'test', status: :success },
    { name: 'rspec:windows 2 3', stage: 'test', status: :success },
    { name: 'rspec:windows 2 3', stage: 'test', status: :success },
13
    { name: 'rspec:osx', stage: 'test', status_event: :success },
14 15 16 17 18
    { 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 },
19 20
    { name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success, options: { environment: { on_stop: 'stop staging' } } },
    { name: 'stop staging', stage: 'deploy', environment: 'staging', when: 'manual', status: :skipped },
21 22
    { name: 'production', stage: 'deploy', environment: 'production', when: 'manual', status: :skipped },
    { name: 'slack', stage: 'notify', when: 'manual', status: :created },
23
  ]
Z.J. van de Weg committed
24

25 26 27
  def initialize(project)
    @project = project
  end
28

29
  def seed!
30
    pipelines.each do |pipeline|
31
      begin
32
        BUILDS.each { |opts| build_create!(pipeline, opts) }
33
        commit_status_create!(pipeline, name: 'jenkins', stage: 'test', status: :success)
34 35 36
        print '.'
      rescue ActiveRecord::RecordInvalid
        print 'F'
37
      ensure
38
        pipeline.update_status
39 40
      end
    end
41
  end
42

43 44
  private

45
  def pipelines
46
    create_master_pipelines + create_merge_request_pipelines
47 48
  end

49
  def create_master_pipelines
50
    @project.repository.commits('master', limit: 4).map do |commit|
51 52
      create_pipeline!(@project, 'master', commit)
    end
53 54 55 56
  rescue
    []
  end

57
  def create_merge_request_pipelines
58
    pipelines = @project.merge_requests.first(3).map do |merge_request|
59 60 61
      project = merge_request.source_project
      branch = merge_request.source_branch

62
      merge_request.commits.last(4).map do |commit|
63 64 65 66 67
        create_pipeline!(project, branch, commit)
      end
    end

    pipelines.flatten
68 69 70 71 72
  rescue
    []
  end


73 74
  def create_pipeline!(project, ref, commit)
    project.pipelines.create(sha: commit.id, ref: ref)
75 76
  end

77
  def build_create!(pipeline, opts = {})
78 79
    attributes = job_attributes(pipeline, opts)
      .merge(commands: '$ build command')
80

81
    Ci::Build.create!(attributes).tap do |build|
82 83 84 85
      # We need to set build trace and artifacts after saving a build
      # (id required), that is why we need `#tap` method instead of passing
      # block directly to `Ci::Build#create!`.

86 87 88 89 90
      setup_artifacts(build)
      setup_build_log(build)
      build.save
    end
  end
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  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)
    if %w(running success failed).include?(build.status)
      build.trace = FFaker::Lorem.paragraphs(6).join("\n\n")
107 108
    end
  end
Z.J. van de Weg committed
109

110
  def commit_status_create!(pipeline, opts = {})
111 112
    attributes = job_attributes(pipeline, opts)

Z.J. van de Weg committed
113
    GenericCommitStatus.create!(attributes)
114
  end
Z.J. van de Weg committed
115

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

123 124 125 126 127
  def build_user
    @project.team.users.sample
  end

  def build_status
128
    Ci::Build::AVAILABLE_STATUSES.sample
129 130
  end

131 132 133 134
  def stage_index(stage)
    STAGES.index(stage) || 0
  end

135
  def artifacts_archive_path
136
    Rails.root + 'spec/fixtures/ci_build_artifacts.zip'
137 138
  end

139 140 141 142 143 144 145 146 147 148 149
  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
150 151 152 153
  end
end

Gitlab::Seeder.quiet do
154
  Project.all.sample(5).each do |project|
155
    project_builds = Gitlab::Seeder::Pipelines.new(project)
156 157
    project_builds.seed!
  end
158
end