Class: Ci::Commit

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Model
Includes:
Statuseable
Defined in:
app/models/ci/commit.rb

Constant Summary

Constants included from Statuseable

Statuseable::AVAILABLE_STATUSES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

model_name, table_name_prefix

Methods included from Statuseable

#active?, #complete?, #started?

Class Method Details

.stagesObject


46
47
48
# File 'app/models/ci/commit.rb', line 46

def self.stages
  CommitStatus.where(commit: all).stages
end

.truncate_sha(sha) ⇒ Object


42
43
44
# File 'app/models/ci/commit.rb', line 42

def self.truncate_sha(sha)
  sha[0...8]
end

Instance Method Details

#branch?Boolean

Returns:

  • (Boolean)

82
83
84
# File 'app/models/ci/commit.rb', line 82

def branch?
  !tag?
end

#ci_yaml_fileObject


146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/ci/commit.rb', line 146

def ci_yaml_file
  return @ci_yaml_file if defined?(@ci_yaml_file)

  @ci_yaml_file ||= begin
    blob = project.repository.blob_at(sha, '.gitlab-ci.yml')
    blob.load_all_data!(project.repository)
    blob.data
  rescue
    nil
  end
end

#commit_dataObject


76
77
78
79
80
# File 'app/models/ci/commit.rb', line 76

def commit_data
  @commit ||= project.commit(sha)
rescue
  nil
end

#config_processorObject


131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/ci/commit.rb', line 131

def config_processor
  return nil unless ci_yaml_file
  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
    save_yaml_error(e.message)
    nil
  rescue
    save_yaml_error("Undefined error")
    nil
  end
end

#coverageObject


124
125
126
127
128
129
# File 'app/models/ci/commit.rb', line 124

def coverage
  coverage_array = statuses.latest.map(&:coverage).compact
  if coverage_array.size >= 1
    '%.2f' % (coverage_array.reduce(:+) / coverage_array.size)
  end
end

#create_builds(user, trigger_request = nil) ⇒ Object


92
93
94
95
96
97
# File 'app/models/ci/commit.rb', line 92

def create_builds(user, trigger_request = nil)
  return unless config_processor
  config_processor.stages.any? do |stage|
    CreateBuildsService.new(self).execute(stage, user, 'success', trigger_request).present?
  end
end

#create_next_builds(build) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/ci/commit.rb', line 99

def create_next_builds(build)
  return unless config_processor

  # don't create other builds if this one is retried
  latest_builds = builds.latest
  return unless latest_builds.exists?(build.id)

  # 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
  prior_builds = latest_builds.where.not(stage: next_stages)
  prior_status = prior_builds.status

  # create builds for next stages based
  next_stages.any? do |stage|
    CreateBuildsService.new(self).execute(stage, build.user, prior_status, build.trigger_request).present?
  end
end

#git_author_emailObject


64
65
66
# File 'app/models/ci/commit.rb', line 64

def git_author_email
  commit_data.author_email if commit_data
end

#git_author_nameObject


60
61
62
# File 'app/models/ci/commit.rb', line 60

def git_author_name
  commit_data.author_name if commit_data
end

#git_commit_messageObject


68
69
70
# File 'app/models/ci/commit.rb', line 68

def git_commit_message
  commit_data.message if commit_data
end

#project_idObject


50
51
52
# File 'app/models/ci/commit.rb', line 50

def project_id
  project.id
end

#retriedObject


120
121
122
# File 'app/models/ci/commit.rb', line 120

def retried
  @retried ||= (statuses.order(id: :desc) - statuses.latest)
end

#retryable?Boolean

Returns:

  • (Boolean)

86
87
88
89
90
# File 'app/models/ci/commit.rb', line 86

def retryable?
  builds.latest.any? do |build|
    build.failed? && build.retryable?
  end
end

#short_shaObject


72
73
74
# File 'app/models/ci/commit.rb', line 72

def short_sha
  Ci::Commit.truncate_sha(sha)
end

#skip_ci?Boolean

Returns:

  • (Boolean)

158
159
160
# File 'app/models/ci/commit.rb', line 158

def skip_ci?
  git_commit_message =~ /(\[ci skip\])/ if git_commit_message
end

#valid_commit_shaObject


54
55
56
57
58
# File 'app/models/ci/commit.rb', line 54

def valid_commit_sha
  if self.sha == Gitlab::Git::BLANK_SHA
    self.errors.add(:sha, " cant be 00000000 (branch removal)")
  end
end