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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      reload_status!
K
Kamil Trzcinski 已提交
94 95
    end

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

      reload_status!
K
Kamil Trzcinski 已提交
102 103
    end

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

K
Kamil Trzcinski 已提交
111 112 113 114
    def triggered?
      trigger_requests.any?
    end

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

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

126 127 128 129 130 131 132 133
    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 已提交
134 135
    def has_warnings?
      builds.latest.ignored.any?
136 137
    end

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

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

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

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

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

182 183 184 185
    def notes
      Note.for_commit_id(sha)
    end

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

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

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

212 213
    private

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

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

    def keep_around_commits
224
      return unless project
225

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