pipeline.rb 6.7 KB
Newer Older
D
Douwe Maan 已提交
1
module Ci
2
  class Pipeline < ActiveRecord::Base
D
Douwe Maan 已提交
3
    extend Ci::Model
K
Kamil Trzcinski 已提交
4
    include Statuseable
K
WIP  
Kamil Trzcinski 已提交
5

K
Kamil Trzcinski 已提交
6 7
    self.table_name = 'ci_commits'

K
Kamil Trzcinski 已提交
8
    belongs_to :project, class_name: '::Project', foreign_key: :gl_project_id
9 10
    belongs_to :user

11 12
    has_many :statuses, class_name: 'CommitStatus', foreign_key: :commit_id
    has_many :builds, class_name: 'Ci::Build', foreign_key: :commit_id
13
    has_many :trigger_requests, dependent: :destroy, class_name: 'Ci::TriggerRequest', foreign_key: :commit_id
D
Douwe Maan 已提交
14

K
Kamil Trzcinski 已提交
15
    validates_presence_of :sha
K
Kamil Trzcinski 已提交
16
    validates_presence_of :status
D
Douwe Maan 已提交
17 18
    validate :valid_commit_sha

K
Kamil Trzcinski 已提交
19
    # Invalidate object and save if when touched
20
    after_touch :update_state
21
    after_save :keep_around_commits
K
Kamil Trzcinski 已提交
22

23
    # ref can't be HEAD or SHA, can only be branch/tag name
24
    scope :latest_successful_for, ->(ref = default_branch) do
25
      where(ref: ref).success.order(id: :desc).limit(1)
26 27
    end

D
Douwe Maan 已提交
28 29 30 31
    def self.truncate_sha(sha)
      sha[0...8]
    end

K
Kamil Trzcinski 已提交
32
    def self.stages
K
Kamil Trzcinski 已提交
33
      # We use pluck here due to problems with MySQL which doesn't allow LIMIT/OFFSET in queries
34
      CommitStatus.where(pipeline: pluck(:id)).stages
K
Kamil Trzcinski 已提交
35 36
    end

K
Kamil Trzcinski 已提交
37 38
    def project_id
      project.id
K
WIP  
Kamil Trzcinski 已提交
39 40
    end

D
Douwe Maan 已提交
41
    def valid_commit_sha
42
      if self.sha == Gitlab::Git::BLANK_SHA
D
Douwe Maan 已提交
43 44 45 46 47
        self.errors.add(:sha, " cant be 00000000 (branch removal)")
      end
    end

    def git_author_name
48
      commit.try(:author_name)
D
Douwe Maan 已提交
49 50 51
    end

    def git_author_email
52
      commit.try(:author_email)
D
Douwe Maan 已提交
53 54 55
    end

    def git_commit_message
56
      commit.try(:message)
D
Douwe Maan 已提交
57 58
    end

59 60 61 62
    def git_commit_title
      commit.try(:title)
    end

D
Douwe Maan 已提交
63
    def short_sha
64
      Ci::Pipeline.truncate_sha(sha)
D
Douwe Maan 已提交
65 66
    end

67
    def commit
K
Kamil Trzcinski 已提交
68
      @commit ||= project.commit(sha)
D
Douwe Maan 已提交
69 70 71 72
    rescue
      nil
    end

K
Kamil Trzcinski 已提交
73 74 75 76
    def branch?
      !tag?
    end

77 78
    def manual_actions
      builds.latest.manual_actions
79 80
    end

K
Kamil Trzcinski 已提交
81 82
    def retryable?
      builds.latest.any? do |build|
K
Kamil Trzcinski 已提交
83
        build.failed? && build.retryable?
K
Kamil Trzcinski 已提交
84 85 86
      end
    end

87 88 89 90
    def cancelable?
      builds.running_or_pending.any?
    end

K
Kamil Trzcinski 已提交
91 92 93 94
    def cancel_running
      builds.running_or_pending.each(&:cancel)
    end

95 96 97 98
    def retry_failed(user)
      builds.latest.failed.select(&:retryable?).each do |build|
        Ci::Build.retry(build, user)
      end
K
Kamil Trzcinski 已提交
99 100
    end

K
Kamil Trzcinski 已提交
101 102 103 104 105 106 107
    def latest?
      return false unless ref
      commit = project.commit(ref)
      return false unless commit
      commit.sha == sha
    end

K
Kamil Trzcinski 已提交
108 109 110 111
    def triggered?
      trigger_requests.any?
    end

112 113 114 115
    def create_builds(user, trigger_request = nil)
      ##
      # We persist pipeline only if there are builds available
      #
D
Douwe Maan 已提交
116
      return unless config_processor
117

118
      build_builds_for_stages(config_processor.stages, user,
119
                              'success', trigger_request) && save
120 121
    end

122
    def create_next_builds(build)
K
Kamil Trzcinski 已提交
123 124
      return unless config_processor

125
      # don't create other builds if this one is retried
K
Kamil Trzcinski 已提交
126
      latest_builds = builds.latest
127
      return unless latest_builds.exists?(build.id)
K
Kamil Trzcinski 已提交
128

129 130 131 132 133
      # get list of stages after this build
      next_stages = config_processor.stages.drop_while { |stage| stage != build.stage }
      next_stages.delete(build.stage)

      # get status for all prior builds
K
Kamil Trzcinski 已提交
134 135
      prior_builds = latest_builds.where.not(stage: next_stages)
      prior_status = prior_builds.status
136

137 138 139 140
      # build builds for next stage that has builds available
      # and save pipeline if we have builds
      build_builds_for_stages(next_stages, build.user, prior_status,
                              build.trigger_request) && save
D
Douwe Maan 已提交
141 142
    end

K
Kamil Trzcinski 已提交
143 144
    def retried
      @retried ||= (statuses.order(id: :desc) - statuses.latest)
D
Douwe Maan 已提交
145 146 147
    end

    def coverage
148
      coverage_array = statuses.latest.map(&:coverage).compact
K
Kamil Trzcinski 已提交
149 150
      if coverage_array.size >= 1
        '%.2f' % (coverage_array.reduce(:+) / coverage_array.size)
D
Douwe Maan 已提交
151 152 153
      end
    end

C
Connor Shea 已提交
154 155
    def has_warnings?
      builds.latest.ignored.any?
156 157
    end

D
Douwe Maan 已提交
158
    def config_processor
159
      return nil unless ci_yaml_file
160 161 162 163 164
      return @config_processor if defined?(@config_processor)

      @config_processor ||= begin
        Ci::GitlabCiYamlProcessor.new(ci_yaml_file, project.path_with_namespace)
      rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e
165
        self.yaml_errors = e.message
166 167
        nil
      rescue
168
        self.yaml_errors = 'Undefined error'
169 170
        nil
      end
D
Douwe Maan 已提交
171 172
    end

K
Kamil Trzcinski 已提交
173
    def ci_yaml_file
174 175
      return @ci_yaml_file if defined?(@ci_yaml_file)

176 177 178 179
      @ci_yaml_file ||= begin
        blob = project.repository.blob_at(sha, '.gitlab-ci.yml')
        blob.load_all_data!(project.repository)
        blob.data
180 181
      rescue
        nil
182
      end
K
Kamil Trzcinski 已提交
183 184
    end

D
Douwe Maan 已提交
185
    def skip_ci?
S
Simon Welsh 已提交
186
      git_commit_message =~ /\[(ci skip|skip ci)\]/i if git_commit_message
D
Douwe Maan 已提交
187 188
    end

K
Kamil Trzcinski 已提交
189 190 191 192
    def environments
      builds.where.not(environment: nil).success.pluck(:environment).uniq
    end

J
James Lopez 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205
    # Manually set the notes for a Ci::Pipeline
    # There is no ActiveRecord relation between Ci::Pipeline and notes
    # as they are related to a commit sha. This method helps importing
    # them using the +Gitlab::ImportExport::RelationFactory+ class.
    def notes=(notes)
      notes.each do |note|
        note[:id] = nil
        note[:commit_id] = sha
        note[:noteable_id] = self['id']
        note.save!
      end
    end

206 207 208 209
    def notes
      Note.for_commit_id(sha)
    end

210 211 212 213 214 215
    def predefined_variables
      [
        { key: 'CI_PIPELINE_ID', value: id.to_s, public: true }
      ]
    end

216 217
    private

218 219 220 221 222 223
    def build_builds_for_stages(stages, user, status, trigger_request)
      ##
      # Note that `Array#any?` implements a short circuit evaluation, so we
      # build builds only for the first stage that has builds available.
      #
      stages.any? do |stage|
224 225 226
        CreateBuildsService.new(self).
          execute(stage, user, status, trigger_request).
          any?(&:active?)
227
      end
228 229
    end

230
    def update_state
231
      last_status = status
232 233 234 235 236 237 238 239 240 241 242

      if update_state_from_commit_statuses
        execute_hooks if last_status != status && !skip_ci?
        true
      else
        false
      end
    end

    def update_state_from_commit_statuses
      statuses.reload
243 244
      self.status = if yaml_errors.blank?
                      statuses.latest.status || 'skipped'
245
                    else
246
                      'failed'
247
                    end
248 249 250
      self.started_at = statuses.started_at
      self.finished_at = statuses.finished_at
      self.duration = statuses.latest.duration
251
      save
252 253 254 255 256 257
    end

    def execute_hooks
      pipeline_data = Gitlab::DataBuilder::PipelineDataBuilder.build(self)
      project.execute_hooks(pipeline_data, :pipeline_hooks)
      project.execute_services(pipeline_data.dup, :pipeline_hooks)
K
Kamil Trzcinski 已提交
258
    end
259 260

    def keep_around_commits
261
      return unless project
262

263 264 265
      project.repository.keep_around(self.sha)
      project.repository.keep_around(self.before_sha)
    end
D
Douwe Maan 已提交
266 267
  end
end