pipeline.rb 5.9 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

D
Douwe Maan 已提交
23 24 25 26
    def self.truncate_sha(sha)
      sha[0...8]
    end

K
Kamil Trzcinski 已提交
27
    def self.stages
K
Kamil Trzcinski 已提交
28
      # We use pluck here due to problems with MySQL which doesn't allow LIMIT/OFFSET in queries
29
      CommitStatus.where(pipeline: pluck(:id)).stages
K
Kamil Trzcinski 已提交
30 31
    end

K
Kamil Trzcinski 已提交
32 33
    def project_id
      project.id
K
WIP  
Kamil Trzcinski 已提交
34 35
    end

D
Douwe Maan 已提交
36
    def valid_commit_sha
37
      if self.sha == Gitlab::Git::BLANK_SHA
D
Douwe Maan 已提交
38 39 40 41 42
        self.errors.add(:sha, " cant be 00000000 (branch removal)")
      end
    end

    def git_author_name
43
      commit.try(:author_name)
D
Douwe Maan 已提交
44 45 46
    end

    def git_author_email
47
      commit.try(:author_email)
D
Douwe Maan 已提交
48 49 50
    end

    def git_commit_message
51
      commit.try(:message)
D
Douwe Maan 已提交
52 53
    end

54 55 56 57
    def git_commit_title
      commit.try(:title)
    end

D
Douwe Maan 已提交
58
    def short_sha
59
      Ci::Pipeline.truncate_sha(sha)
D
Douwe Maan 已提交
60 61
    end

62
    def commit
K
Kamil Trzcinski 已提交
63
      @commit ||= project.commit(sha)
D
Douwe Maan 已提交
64 65 66 67
    rescue
      nil
    end

K
Kamil Trzcinski 已提交
68 69 70 71
    def branch?
      !tag?
    end

72 73
    def manual_actions
      builds.latest.manual_actions
74 75
    end

K
Kamil Trzcinski 已提交
76 77
    def retryable?
      builds.latest.any? do |build|
K
Kamil Trzcinski 已提交
78
        build.failed? && build.retryable?
K
Kamil Trzcinski 已提交
79 80 81
      end
    end

82 83 84 85
    def cancelable?
      builds.running_or_pending.any?
    end

K
Kamil Trzcinski 已提交
86 87 88 89
    def cancel_running
      builds.running_or_pending.each(&:cancel)
    end

90 91 92 93
    def retry_failed(user)
      builds.latest.failed.select(&:retryable?).each do |build|
        Ci::Build.retry(build, user)
      end
K
Kamil Trzcinski 已提交
94 95
    end

K
Kamil Trzcinski 已提交
96 97 98 99 100 101 102
    def latest?
      return false unless ref
      commit = project.commit(ref)
      return false unless commit
      commit.sha == sha
    end

K
Kamil Trzcinski 已提交
103 104 105 106
    def triggered?
      trigger_requests.any?
    end

107 108 109 110
    def create_builds(user, trigger_request = nil)
      ##
      # We persist pipeline only if there are builds available
      #
D
Douwe Maan 已提交
111
      return unless config_processor
112

113
      build_builds_for_stages(config_processor.stages, user,
114
                              'success', trigger_request) && save
115 116
    end

117
    def create_next_builds(build)
K
Kamil Trzcinski 已提交
118 119
      return unless config_processor

120
      # don't create other builds if this one is retried
K
Kamil Trzcinski 已提交
121
      latest_builds = builds.latest
122
      return unless latest_builds.exists?(build.id)
K
Kamil Trzcinski 已提交
123

124 125 126 127 128
      # 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 已提交
129 130
      prior_builds = latest_builds.where.not(stage: next_stages)
      prior_status = prior_builds.status
131

132 133 134 135
      # 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 已提交
136 137
    end

K
Kamil Trzcinski 已提交
138 139
    def retried
      @retried ||= (statuses.order(id: :desc) - statuses.latest)
D
Douwe Maan 已提交
140 141 142
    end

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

    def config_processor
150
      return nil unless ci_yaml_file
151 152 153 154 155
      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
156
        self.yaml_errors = e.message
157 158
        nil
      rescue
159
        self.yaml_errors = 'Undefined error'
160 161
        nil
      end
D
Douwe Maan 已提交
162 163
    end

K
Kamil Trzcinski 已提交
164
    def ci_yaml_file
165 166
      return @ci_yaml_file if defined?(@ci_yaml_file)

167 168 169 170
      @ci_yaml_file ||= begin
        blob = project.repository.blob_at(sha, '.gitlab-ci.yml')
        blob.load_all_data!(project.repository)
        blob.data
171 172
      rescue
        nil
173
      end
K
Kamil Trzcinski 已提交
174 175
    end

D
Douwe Maan 已提交
176
    def skip_ci?
S
Simon Welsh 已提交
177
      git_commit_message =~ /\[(ci skip|skip ci)\]/i if git_commit_message
D
Douwe Maan 已提交
178 179
    end

K
Kamil Trzcinski 已提交
180 181 182 183
    def environments
      builds.where.not(environment: nil).success.pluck(:environment).uniq
    end

J
James Lopez 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
    # 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

197 198 199 200
    def notes
      Note.for_commit_id(sha)
    end

201 202
    private

203 204 205 206 207 208 209 210 211
    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|
        CreateBuildsService.new(self)
          .execute(stage, user, status, trigger_request).present?
      end
212 213
    end

214
    def update_state
215 216 217
      statuses.reload
      self.status = if yaml_errors.blank?
                      statuses.latest.status || 'skipped'
218
                    else
219
                      'failed'
220
                    end
221 222 223
      self.started_at = statuses.started_at
      self.finished_at = statuses.finished_at
      self.duration = statuses.latest.duration
K
Kamil Trzcinski 已提交
224 225
      save
    end
226 227

    def keep_around_commits
228 229
      return unless project
      
230 231 232
      project.repository.keep_around(self.sha)
      project.repository.keep_around(self.before_sha)
    end
D
Douwe Maan 已提交
233 234
  end
end