pipeline.rb 5.3 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
16
    validates_presence_of :ref
K
Kamil Trzcinski 已提交
17
    validates_presence_of :status
D
Douwe Maan 已提交
18 19
    validate :valid_commit_sha

20
    after_save :keep_around_commits
K
Kamil Trzcinski 已提交
21

22 23
    delegate :stages, to: :statuses

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      reload_status!
K
Kamil Trzcinski 已提交
96 97
    end

98 99 100 101
    def retry_failed(user)
      builds.latest.failed.select(&:retryable?).each do |build|
        Ci::Build.retry(build, user)
      end
102 103

      reload_status!
K
Kamil Trzcinski 已提交
104 105
    end

K
Kamil Trzcinski 已提交
106 107 108 109 110 111 112
    def latest?
      return false unless ref
      commit = project.commit(ref)
      return false unless commit
      commit.sha == sha
    end

K
Kamil Trzcinski 已提交
113 114 115 116
    def triggered?
      trigger_requests.any?
    end

K
Kamil Trzcinski 已提交
117 118
    def retried
      @retried ||= (statuses.order(id: :desc) - statuses.latest)
D
Douwe Maan 已提交
119 120 121
    end

    def coverage
122
      coverage_array = statuses.latest.map(&:coverage).compact
K
Kamil Trzcinski 已提交
123 124
      if coverage_array.size >= 1
        '%.2f' % (coverage_array.reduce(:+) / coverage_array.size)
D
Douwe Maan 已提交
125 126 127
      end
    end

128 129 130 131 132 133 134 135
    def config_builds_attributes
      return [] unless config_processor

      config_processor.
        builds_for_ref(ref, tag?, trigger_requests.first).
        sort_by { |build| build[:stage_idx] }
    end

C
Connor Shea 已提交
136 137
    def has_warnings?
      builds.latest.ignored.any?
138 139
    end

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

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

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

K
Kamil Trzcinski 已提交
167 168 169 170
    def environments
      builds.where.not(environment: nil).success.pluck(:environment).uniq
    end

J
James Lopez 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
    # 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

184 185 186 187
    def notes
      Note.for_commit_id(sha)
    end

188 189 190 191 192
    def process!
      Ci::ProcessPipelineService.new(project, user).execute(self)
      reload_status!
    end

193 194 195 196 197 198
    def predefined_variables
      [
        { key: 'CI_PIPELINE_ID', value: id.to_s, public: true }
      ]
    end

199
    def reload_status!
200
      statuses.reload
201 202 203 204 205 206
      self.status =
        if yaml_errors.blank?
          statuses.latest.status || 'skipped'
        else
          'failed'
        end
207 208 209
      self.started_at = statuses.started_at
      self.finished_at = statuses.finished_at
      self.duration = statuses.latest.duration
210
      save
211
      execute_hooks if status_changed?
212 213
    end

214 215
    private

216 217
    def execute_hooks
      project.execute_hooks(pipeline_data, :pipeline_hooks)
218 219 220 221 222
      project.execute_services(pipeline_data, :pipeline_hooks)
    end

    def pipeline_data
      Gitlab::DataBuilder::PipelineDataBuilder.build(self)
K
Kamil Trzcinski 已提交
223
    end
224 225

    def keep_around_commits
226
      return unless project
227

228 229 230
      project.repository.keep_around(self.sha)
      project.repository.keep_around(self.before_sha)
    end
D
Douwe Maan 已提交
231 232
  end
end