summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-09-29 11:44:07 +0000
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-09-29 11:44:07 +0000
commit2c3355f9584a1a22d91806b8b49d441f22c0c286 (patch)
tree60d21258783742373ef3cd1fe4a7536b50e3272b /app
parentbdf466858653c3599f4c9bc0c514fb25018f77aa (diff)
parent0d877d91e7556edfcdc29ad77491740da3cc7661 (diff)
downloadgitlab-ce-2c3355f9584a1a22d91806b8b49d441f22c0c286.tar.gz
Merge branch 'ci-commits-to-projects' into 'master'
Make Ci::Commits belong to Project instead of Ci::Project See merge request !1455
Diffstat (limited to 'app')
-rw-r--r--app/models/ci/build.rb6
-rw-r--r--app/models/ci/commit.rb29
-rw-r--r--app/models/ci/project.rb39
-rw-r--r--app/models/ci/runner.rb4
-rw-r--r--app/models/project.rb6
-rw-r--r--app/services/ci/register_build_service.rb6
-rw-r--r--app/views/ci/admin/projects/_project.html.haml2
7 files changed, 58 insertions, 34 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index 8096d4fa5ae..cda4fdd4982 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -30,7 +30,6 @@ module Ci
LAZY_ATTRIBUTES = ['trace']
belongs_to :commit, class_name: 'Ci::Commit'
- belongs_to :project, class_name: 'Ci::Project'
belongs_to :runner, class_name: 'Ci::Runner'
belongs_to :trigger_request, class_name: 'Ci::TriggerRequest'
@@ -80,7 +79,6 @@ module Ci
new_build.commands = build.commands
new_build.tag_list = build.tag_list
new_build.commit_id = build.commit_id
- new_build.project_id = build.project_id
new_build.name = build.name
new_build.allow_failure = build.allow_failure
new_build.stage = build.stage
@@ -137,7 +135,7 @@ module Ci
state :canceled, value: 'canceled'
end
- delegate :sha, :short_sha, :before_sha, :ref,
+ delegate :sha, :short_sha, :before_sha, :ref, :project,
to: :commit, prefix: false
def trace_html
@@ -188,7 +186,7 @@ module Ci
end
def project_id
- commit.project_id
+ commit.project.id
end
def project_name
diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb
index f102d0a7679..6d048779cde 100644
--- a/app/models/ci/commit.rb
+++ b/app/models/ci/commit.rb
@@ -18,8 +18,8 @@
module Ci
class Commit < ActiveRecord::Base
extend Ci::Model
-
- belongs_to :project, class_name: 'Ci::Project'
+
+ belongs_to :gl_project, class_name: '::Project', foreign_key: :gl_project_id
has_many :builds, dependent: :destroy, class_name: 'Ci::Build'
has_many :trigger_requests, dependent: :destroy, class_name: 'Ci::TriggerRequest'
@@ -36,6 +36,14 @@ module Ci
sha
end
+ def project
+ @project ||= gl_project.ensure_gitlab_ci_project
+ end
+
+ def project_id
+ project.id
+ end
+
def last_build
builds.order(:id).last
end
@@ -111,15 +119,14 @@ module Ci
builds_attrs = config_processor.builds_for_stage_and_ref(stage, ref, tag)
builds_attrs.map do |build_attrs|
builds.create!({
- project: project,
- name: build_attrs[:name],
- commands: build_attrs[:script],
- tag_list: build_attrs[:tags],
- options: build_attrs[:options],
- allow_failure: build_attrs[:allow_failure],
- stage: build_attrs[:stage],
- trigger_request: trigger_request,
- })
+ name: build_attrs[:name],
+ commands: build_attrs[:script],
+ tag_list: build_attrs[:tags],
+ options: build_attrs[:options],
+ allow_failure: build_attrs[:allow_failure],
+ stage: build_attrs[:stage],
+ trigger_request: trigger_request,
+ })
end
end
diff --git a/app/models/ci/project.rb b/app/models/ci/project.rb
index 8fb54b90d61..77cce261fc8 100644
--- a/app/models/ci/project.rb
+++ b/app/models/ci/project.rb
@@ -33,15 +33,12 @@ module Ci
belongs_to :gl_project, class_name: '::Project', foreign_key: :gitlab_id
- has_many :commits, ->() { order('CASE WHEN ci_commits.committed_at IS NULL THEN 0 ELSE 1 END', :committed_at, :id) }, dependent: :destroy, class_name: 'Ci::Commit'
- has_many :builds, through: :commits, dependent: :destroy, class_name: 'Ci::Build'
has_many :runner_projects, dependent: :destroy, class_name: 'Ci::RunnerProject'
has_many :runners, through: :runner_projects, class_name: 'Ci::Runner'
has_many :web_hooks, dependent: :destroy, class_name: 'Ci::WebHook'
has_many :events, dependent: :destroy, class_name: 'Ci::Event'
has_many :variables, dependent: :destroy, class_name: 'Ci::Variable'
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger'
- has_one :last_commit, -> { order 'ci_commits.created_at DESC' }, class_name: 'Ci::Commit'
# Project services
has_many :services, dependent: :destroy, class_name: 'Ci::Service'
@@ -55,13 +52,13 @@ module Ci
# Validations
#
validates_presence_of :name, :timeout, :token, :default_ref,
- :path, :ssh_url_to_repo, :gitlab_id
+ :path, :ssh_url_to_repo, :gitlab_id
validates_uniqueness_of :gitlab_id
validates :polling_interval,
- presence: true,
- if: ->(project) { project.always_build.present? }
+ presence: true,
+ if: ->(project) { project.always_build.present? }
scope :public_only, ->() { where(public: true) }
@@ -79,12 +76,12 @@ module Ci
def parse(project)
params = {
- name: project.name_with_namespace,
- gitlab_id: project.id,
- path: project.path_with_namespace,
- default_ref: project.default_branch || 'master',
- ssh_url_to_repo: project.ssh_url_to_repo,
- email_add_pusher: current_application_settings.add_pusher,
+ name: project.name_with_namespace,
+ gitlab_id: project.id,
+ path: project.path_with_namespace,
+ default_ref: project.default_branch || 'master',
+ ssh_url_to_repo: project.ssh_url_to_repo,
+ email_add_pusher: current_application_settings.add_pusher,
email_only_broken_builds: current_application_settings.all_broken_builds,
}
@@ -104,8 +101,8 @@ module Ci
end
def ordered_by_last_commit_date
- last_commit_subquery = "(SELECT project_id, MAX(committed_at) committed_at FROM #{Ci::Commit.table_name} GROUP BY project_id)"
- joins("LEFT JOIN #{last_commit_subquery} AS last_commit ON #{Ci::Project.table_name}.id = last_commit.project_id").
+ last_commit_subquery = "(SELECT gl_project_id, MAX(committed_at) committed_at FROM #{Ci::Commit.table_name} GROUP BY gl_project_id)"
+ joins("LEFT JOIN #{last_commit_subquery} AS last_commit ON #{Ci::Project.table_name}.gitlab_id = last_commit.gl_project_id").
order("CASE WHEN last_commit.committed_at IS NULL THEN 1 ELSE 0 END, last_commit.committed_at DESC")
end
@@ -125,10 +122,14 @@ module Ci
def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank?
+ self.default_ref ||= 'master'
+ self.name ||= gl_project.name_with_namespace
+ self.path ||= gl_project.path_with_namespace
+ self.ssh_url_to_repo ||= gl_project.ssh_url_to_repo
end
def tracked_refs
- @tracked_refs ||= default_ref.split(",").map{|ref| ref.strip}
+ @tracked_refs ||= default_ref.split(",").map { |ref| ref.strip }
end
def valid_token? token
@@ -207,5 +208,13 @@ module Ci
def setup_finished?
commits.any?
end
+
+ def commits
+ gl_project.ci_commits
+ end
+
+ def builds
+ gl_project.ci_builds
+ end
end
end
diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb
index 1e9f78a3748..6838ccfaaab 100644
--- a/app/models/ci/runner.rb
+++ b/app/models/ci/runner.rb
@@ -41,6 +41,10 @@ module Ci
query: "%#{query.try(:downcase)}%")
end
+ def gl_projects_ids
+ projects.select(:gitlab_id)
+ end
+
def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank?
end
diff --git a/app/models/project.rb b/app/models/project.rb
index e912c48467d..953b37e3f7a 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -118,6 +118,8 @@ class Project < ActiveRecord::Base
has_many :deploy_keys, through: :deploy_keys_projects
has_many :users_star_projects, dependent: :destroy
has_many :starrers, through: :users_star_projects, source: :user
+ has_many :ci_commits, ->() { order('CASE WHEN ci_commits.committed_at IS NULL THEN 0 ELSE 1 END', :committed_at, :id) }, dependent: :destroy, class_name: 'Ci::Commit', foreign_key: :gl_project_id
+ has_many :ci_builds, through: :ci_commits, source: :builds, dependent: :destroy, class_name: 'Ci::Build'
has_one :import_data, dependent: :destroy, class_name: "ProjectImportData"
has_one :gitlab_ci_project, dependent: :destroy, class_name: "Ci::Project", foreign_key: :gitlab_id
@@ -745,6 +747,10 @@ class Project < ActiveRecord::Base
gitlab_ci_project.commits.find_by(sha: sha) if gitlab_ci?
end
+ def ensure_gitlab_ci_project
+ gitlab_ci_project || create_gitlab_ci_project
+ end
+
def enable_ci(user)
# Enable service
service = gitlab_ci_service || create_gitlab_ci_service
diff --git a/app/services/ci/register_build_service.rb b/app/services/ci/register_build_service.rb
index 33f1c1e918d..71b61bbe389 100644
--- a/app/services/ci/register_build_service.rb
+++ b/app/services/ci/register_build_service.rb
@@ -8,10 +8,10 @@ module Ci
builds =
if current_runner.shared?
# don't run projects which have not enables shared runners
- builds.includes(:project).where(ci_projects: { shared_runners_enabled: true })
+ builds.joins(commit: { gl_project: :gitlab_ci_project }).where(ci_projects: { shared_runners_enabled: true })
else
# do run projects which are only assigned to this runner
- builds.where(project_id: current_runner.projects)
+ builds.joins(:commit).where(ci_commits: { gl_project_id: current_runner.gl_projects_ids })
end
builds = builds.order('created_at ASC')
@@ -19,7 +19,7 @@ module Ci
build = builds.find do |build|
(build.tag_list - current_runner.tag_list).empty?
end
-
+
if build
# In case when 2 runners try to assign the same build, second runner will be declined
diff --git a/app/views/ci/admin/projects/_project.html.haml b/app/views/ci/admin/projects/_project.html.haml
index c461206c72a..a342d6e1cf0 100644
--- a/app/views/ci/admin/projects/_project.html.haml
+++ b/app/views/ci/admin/projects/_project.html.haml
@@ -1,4 +1,4 @@
-- last_commit = project.last_commit
+- last_commit = project.commits.last
%tr
%td
= project.id