pipeline.rb 5.8 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 54
    end

    def short_sha
55
      Ci::Pipeline.truncate_sha(sha)
D
Douwe Maan 已提交
56 57
    end

58
    def commit
K
Kamil Trzcinski 已提交
59
      @commit ||= project.commit(sha)
D
Douwe Maan 已提交
60 61 62 63
    rescue
      nil
    end

K
Kamil Trzcinski 已提交
64 65 66 67 68 69
    def branch?
      !tag?
    end

    def retryable?
      builds.latest.any? do |build|
K
Kamil Trzcinski 已提交
70
        build.failed? && build.retryable?
K
Kamil Trzcinski 已提交
71 72 73
      end
    end

74 75 76 77
    def cancelable?
      builds.running_or_pending.any?
    end

K
Kamil Trzcinski 已提交
78 79 80 81
    def cancel_running
      builds.running_or_pending.each(&:cancel)
    end

82 83 84 85
    def retry_failed(user)
      builds.latest.failed.select(&:retryable?).each do |build|
        Ci::Build.retry(build, user)
      end
K
Kamil Trzcinski 已提交
86 87
    end

K
Kamil Trzcinski 已提交
88 89 90 91 92 93 94
    def latest?
      return false unless ref
      commit = project.commit(ref)
      return false unless commit
      commit.sha == sha
    end

K
Kamil Trzcinski 已提交
95 96 97 98
    def triggered?
      trigger_requests.any?
    end

99 100 101 102
    def create_builds(user, trigger_request = nil)
      ##
      # We persist pipeline only if there are builds available
      #
D
Douwe Maan 已提交
103
      return unless config_processor
104

105
      build_builds_for_stages(config_processor.stages, user,
106
                              'success', trigger_request) && save
107 108
    end

109
    def create_next_builds(build)
K
Kamil Trzcinski 已提交
110 111
      return unless config_processor

112
      # don't create other builds if this one is retried
K
Kamil Trzcinski 已提交
113
      latest_builds = builds.latest
114
      return unless latest_builds.exists?(build.id)
K
Kamil Trzcinski 已提交
115

116 117 118 119 120
      # 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 已提交
121 122
      prior_builds = latest_builds.where.not(stage: next_stages)
      prior_status = prior_builds.status
123

124 125 126 127
      # 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 已提交
128 129
    end

K
Kamil Trzcinski 已提交
130 131
    def retried
      @retried ||= (statuses.order(id: :desc) - statuses.latest)
D
Douwe Maan 已提交
132 133 134
    end

    def coverage
135
      coverage_array = statuses.latest.map(&:coverage).compact
K
Kamil Trzcinski 已提交
136 137
      if coverage_array.size >= 1
        '%.2f' % (coverage_array.reduce(:+) / coverage_array.size)
D
Douwe Maan 已提交
138 139 140 141
      end
    end

    def config_processor
142
      return nil unless ci_yaml_file
143 144 145 146 147
      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
148
        self.yaml_errors = e.message
149 150
        nil
      rescue
151
        self.yaml_errors = 'Undefined error'
152 153
        nil
      end
D
Douwe Maan 已提交
154 155
    end

K
Kamil Trzcinski 已提交
156
    def ci_yaml_file
157 158
      return @ci_yaml_file if defined?(@ci_yaml_file)

159 160 161 162
      @ci_yaml_file ||= begin
        blob = project.repository.blob_at(sha, '.gitlab-ci.yml')
        blob.load_all_data!(project.repository)
        blob.data
163 164
      rescue
        nil
165
      end
K
Kamil Trzcinski 已提交
166 167
    end

D
Douwe Maan 已提交
168
    def skip_ci?
S
Simon Welsh 已提交
169
      git_commit_message =~ /\[(ci skip|skip ci)\]/i if git_commit_message
D
Douwe Maan 已提交
170 171
    end

K
Kamil Trzcinski 已提交
172 173 174 175
    def environments
      builds.where.not(environment: nil).success.pluck(:environment).uniq
    end

J
James Lopez 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
    # 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

189 190 191 192
    def notes
      Note.for_commit_id(sha)
    end

193 194
    private

195 196 197 198 199 200 201 202 203
    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
204 205
    end

206
    def update_state
207 208 209
      statuses.reload
      self.status = if yaml_errors.blank?
                      statuses.latest.status || 'skipped'
210
                    else
211
                      'failed'
212
                    end
213 214 215
      self.started_at = statuses.started_at
      self.finished_at = statuses.finished_at
      self.duration = statuses.latest.duration
K
Kamil Trzcinski 已提交
216 217
      save
    end
218 219

    def keep_around_commits
220 221
      return unless project
      
222 223 224
      project.repository.keep_around(self.sha)
      project.repository.keep_around(self.before_sha)
    end
D
Douwe Maan 已提交
225 226
  end
end