Skip to content

Commit 2c3355f

Browse files
committed
Merge branch 'ci-commits-to-projects' into 'master'
Make Ci::Commits belong to Project instead of Ci::Project See merge request !1455
2 parents bdf4668 + 0d877d9 commit 2c3355f

39 files changed

+209
-156
lines changed

app/models/ci/build.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class Build < ActiveRecord::Base
3030
LAZY_ATTRIBUTES = ['trace']
3131

3232
belongs_to :commit, class_name: 'Ci::Commit'
33-
belongs_to :project, class_name: 'Ci::Project'
3433
belongs_to :runner, class_name: 'Ci::Runner'
3534
belongs_to :trigger_request, class_name: 'Ci::TriggerRequest'
3635

@@ -80,7 +79,6 @@ def retry(build)
8079
new_build.commands = build.commands
8180
new_build.tag_list = build.tag_list
8281
new_build.commit_id = build.commit_id
83-
new_build.project_id = build.project_id
8482
new_build.name = build.name
8583
new_build.allow_failure = build.allow_failure
8684
new_build.stage = build.stage
@@ -137,7 +135,7 @@ def retry(build)
137135
state :canceled, value: 'canceled'
138136
end
139137

140-
delegate :sha, :short_sha, :before_sha, :ref,
138+
delegate :sha, :short_sha, :before_sha, :ref, :project,
141139
to: :commit, prefix: false
142140

143141
def trace_html
@@ -188,7 +186,7 @@ def project
188186
end
189187

190188
def project_id
191-
commit.project_id
189+
commit.project.id
192190
end
193191

194192
def project_name

app/models/ci/commit.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
module Ci
1919
class Commit < ActiveRecord::Base
2020
extend Ci::Model
21-
22-
belongs_to :project, class_name: 'Ci::Project'
21+
22+
belongs_to :gl_project, class_name: '::Project', foreign_key: :gl_project_id
2323
has_many :builds, dependent: :destroy, class_name: 'Ci::Build'
2424
has_many :trigger_requests, dependent: :destroy, class_name: 'Ci::TriggerRequest'
2525

@@ -36,6 +36,14 @@ def to_param
3636
sha
3737
end
3838

39+
def project
40+
@project ||= gl_project.ensure_gitlab_ci_project
41+
end
42+
43+
def project_id
44+
project.id
45+
end
46+
3947
def last_build
4048
builds.order(:id).last
4149
end
@@ -111,15 +119,14 @@ def create_builds_for_stage(stage, trigger_request)
111119
builds_attrs = config_processor.builds_for_stage_and_ref(stage, ref, tag)
112120
builds_attrs.map do |build_attrs|
113121
builds.create!({
114-
project: project,
115-
name: build_attrs[:name],
116-
commands: build_attrs[:script],
117-
tag_list: build_attrs[:tags],
118-
options: build_attrs[:options],
119-
allow_failure: build_attrs[:allow_failure],
120-
stage: build_attrs[:stage],
121-
trigger_request: trigger_request,
122-
})
122+
name: build_attrs[:name],
123+
commands: build_attrs[:script],
124+
tag_list: build_attrs[:tags],
125+
options: build_attrs[:options],
126+
allow_failure: build_attrs[:allow_failure],
127+
stage: build_attrs[:stage],
128+
trigger_request: trigger_request,
129+
})
123130
end
124131
end
125132

app/models/ci/project.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,12 @@ class Project < ActiveRecord::Base
3333

3434
belongs_to :gl_project, class_name: '::Project', foreign_key: :gitlab_id
3535

36-
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'
37-
has_many :builds, through: :commits, dependent: :destroy, class_name: 'Ci::Build'
3836
has_many :runner_projects, dependent: :destroy, class_name: 'Ci::RunnerProject'
3937
has_many :runners, through: :runner_projects, class_name: 'Ci::Runner'
4038
has_many :web_hooks, dependent: :destroy, class_name: 'Ci::WebHook'
4139
has_many :events, dependent: :destroy, class_name: 'Ci::Event'
4240
has_many :variables, dependent: :destroy, class_name: 'Ci::Variable'
4341
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger'
44-
has_one :last_commit, -> { order 'ci_commits.created_at DESC' }, class_name: 'Ci::Commit'
4542

4643
# Project services
4744
has_many :services, dependent: :destroy, class_name: 'Ci::Service'
@@ -55,13 +52,13 @@ class Project < ActiveRecord::Base
5552
# Validations
5653
#
5754
validates_presence_of :name, :timeout, :token, :default_ref,
58-
:path, :ssh_url_to_repo, :gitlab_id
55+
:path, :ssh_url_to_repo, :gitlab_id
5956

6057
validates_uniqueness_of :gitlab_id
6158

6259
validates :polling_interval,
63-
presence: true,
64-
if: ->(project) { project.always_build.present? }
60+
presence: true,
61+
if: ->(project) { project.always_build.present? }
6562

6663
scope :public_only, ->() { where(public: true) }
6764

@@ -79,12 +76,12 @@ def base_build_script
7976

8077
def parse(project)
8178
params = {
82-
name: project.name_with_namespace,
83-
gitlab_id: project.id,
84-
path: project.path_with_namespace,
85-
default_ref: project.default_branch || 'master',
86-
ssh_url_to_repo: project.ssh_url_to_repo,
87-
email_add_pusher: current_application_settings.add_pusher,
79+
name: project.name_with_namespace,
80+
gitlab_id: project.id,
81+
path: project.path_with_namespace,
82+
default_ref: project.default_branch || 'master',
83+
ssh_url_to_repo: project.ssh_url_to_repo,
84+
email_add_pusher: current_application_settings.add_pusher,
8885
email_only_broken_builds: current_application_settings.all_broken_builds,
8986
}
9087

@@ -104,8 +101,8 @@ def unassigned(runner)
104101
end
105102

106103
def ordered_by_last_commit_date
107-
last_commit_subquery = "(SELECT project_id, MAX(committed_at) committed_at FROM #{Ci::Commit.table_name} GROUP BY project_id)"
108-
joins("LEFT JOIN #{last_commit_subquery} AS last_commit ON #{Ci::Project.table_name}.id = last_commit.project_id").
104+
last_commit_subquery = "(SELECT gl_project_id, MAX(committed_at) committed_at FROM #{Ci::Commit.table_name} GROUP BY gl_project_id)"
105+
joins("LEFT JOIN #{last_commit_subquery} AS last_commit ON #{Ci::Project.table_name}.gitlab_id = last_commit.gl_project_id").
109106
order("CASE WHEN last_commit.committed_at IS NULL THEN 1 ELSE 0 END, last_commit.committed_at DESC")
110107
end
111108

@@ -125,10 +122,14 @@ def any_runners?
125122

126123
def set_default_values
127124
self.token = SecureRandom.hex(15) if self.token.blank?
125+
self.default_ref ||= 'master'
126+
self.name ||= gl_project.name_with_namespace
127+
self.path ||= gl_project.path_with_namespace
128+
self.ssh_url_to_repo ||= gl_project.ssh_url_to_repo
128129
end
129130

130131
def tracked_refs
131-
@tracked_refs ||= default_ref.split(",").map{|ref| ref.strip}
132+
@tracked_refs ||= default_ref.split(",").map { |ref| ref.strip }
132133
end
133134

134135
def valid_token? token
@@ -207,5 +208,13 @@ def gitlab_url
207208
def setup_finished?
208209
commits.any?
209210
end
211+
212+
def commits
213+
gl_project.ci_commits
214+
end
215+
216+
def builds
217+
gl_project.ci_builds
218+
end
210219
end
211220
end

app/models/ci/runner.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def self.search(query)
4141
query: "%#{query.try(:downcase)}%")
4242
end
4343

44+
def gl_projects_ids
45+
projects.select(:gitlab_id)
46+
end
47+
4448
def set_default_values
4549
self.token = SecureRandom.hex(15) if self.token.blank?
4650
end

app/models/project.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def set_last_activity_at
118118
has_many :deploy_keys, through: :deploy_keys_projects
119119
has_many :users_star_projects, dependent: :destroy
120120
has_many :starrers, through: :users_star_projects, source: :user
121+
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
122+
has_many :ci_builds, through: :ci_commits, source: :builds, dependent: :destroy, class_name: 'Ci::Build'
121123

122124
has_one :import_data, dependent: :destroy, class_name: "ProjectImportData"
123125
has_one :gitlab_ci_project, dependent: :destroy, class_name: "Ci::Project", foreign_key: :gitlab_id
@@ -745,6 +747,10 @@ def ci_commit(sha)
745747
gitlab_ci_project.commits.find_by(sha: sha) if gitlab_ci?
746748
end
747749

750+
def ensure_gitlab_ci_project
751+
gitlab_ci_project || create_gitlab_ci_project
752+
end
753+
748754
def enable_ci(user)
749755
# Enable service
750756
service = gitlab_ci_service || create_gitlab_ci_service

app/services/ci/register_build_service.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ def execute(current_runner)
88
builds =
99
if current_runner.shared?
1010
# don't run projects which have not enables shared runners
11-
builds.includes(:project).where(ci_projects: { shared_runners_enabled: true })
11+
builds.joins(commit: { gl_project: :gitlab_ci_project }).where(ci_projects: { shared_runners_enabled: true })
1212
else
1313
# do run projects which are only assigned to this runner
14-
builds.where(project_id: current_runner.projects)
14+
builds.joins(:commit).where(ci_commits: { gl_project_id: current_runner.gl_projects_ids })
1515
end
1616

1717
builds = builds.order('created_at ASC')
1818

1919
build = builds.find do |build|
2020
(build.tag_list - current_runner.tag_list).empty?
2121
end
22-
22+
2323

2424
if build
2525
# In case when 2 runners try to assign the same build, second runner will be declined

app/views/ci/admin/projects/_project.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- last_commit = project.last_commit
1+
- last_commit = project.commits.last
22
%tr
33
%td
44
= project.id
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddProjectIdToCiCommit < ActiveRecord::Migration
2+
def up
3+
add_column :ci_commits, :gl_project_id, :integer
4+
end
5+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class MigrateProjectIdForCiCommits < ActiveRecord::Migration
2+
def up
3+
subquery = 'SELECT gitlab_id FROM ci_projects WHERE ci_projects.id = ci_commits.project_id'
4+
execute("UPDATE ci_commits SET gl_project_id=(#{subquery}) WHERE gl_project_id IS NULL")
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20150920161119) do
14+
ActiveRecord::Schema.define(version: 20150924125436) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -115,9 +115,10 @@
115115
t.text "push_data"
116116
t.datetime "created_at"
117117
t.datetime "updated_at"
118-
t.boolean "tag", default: false
118+
t.boolean "tag", default: false
119119
t.text "yaml_errors"
120120
t.datetime "committed_at"
121+
t.integer "gl_project_id"
121122
end
122123

123124
add_index "ci_commits", ["project_id", "committed_at", "id"], name: "index_ci_commits_on_project_id_and_committed_at_and_id", using: :btree

features/steps/project/commits/commits.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps
104104

105105
step 'commit has ci status' do
106106
@project.enable_ci(@user)
107-
create :ci_commit, project: @project.gitlab_ci_project, sha: sample_commit.id
107+
create :ci_commit, gl_project: @project, sha: sample_commit.id
108108
end
109109

110110
step 'I see commit ci info' do

features/steps/shared/project.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,6 @@ def current_project
204204

205205
step 'project "Shop" has CI build' do
206206
project = Project.find_by(name: "Shop")
207-
create :ci_commit, project: project.gitlab_ci_project, sha: project.commit.sha
207+
create :ci_commit, gl_project: project, sha: project.commit.sha
208208
end
209209
end

spec/controllers/ci/commits_controller_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
require "spec_helper"
22

33
describe Ci::CommitsController do
4-
before do
5-
@project = FactoryGirl.create :ci_project
6-
end
7-
84
describe "GET /status" do
95
it "returns status of commit" do
10-
commit = FactoryGirl.create :ci_commit, project: @project
11-
get :status, id: commit.sha, ref_id: commit.ref, project_id: @project.id
6+
commit = FactoryGirl.create :ci_commit
7+
get :status, id: commit.sha, ref_id: commit.ref, project_id: commit.project.id
128

139
expect(response).to be_success
1410
expect(response.code).to eq('200')
1511
JSON.parse(response.body)["status"] == "pending"
1612
end
1713

1814
it "returns not_found status" do
19-
commit = FactoryGirl.create :ci_commit, project: @project
20-
get :status, id: commit.sha, ref_id: "deploy", project_id: @project.id
15+
commit = FactoryGirl.create :ci_commit
16+
get :status, id: commit.sha, ref_id: "deploy", project_id: commit.project.id
2117

2218
expect(response).to be_success
2319
expect(response.code).to eq('200')

spec/factories/ci/commits.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
}
5252
end
5353

54+
gl_project factory: :empty_project
55+
5456
factory :ci_commit_without_jobs do
5557
after(:create) do |commit, evaluator|
5658
commit.push_data[:ci_yaml_file] = YAML.dump({})

spec/factories/ci/projects.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"[email protected]:gitlab/gitlab-shell#{n}.git"
4444
end
4545

46-
gl_project factory: :project
46+
gl_project factory: :empty_project
4747

4848
factory :ci_project do
4949
token 'iPWx6WM4lhHNedGfBpPJNP'

spec/features/ci/admin/builds_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
require 'spec_helper'
22

33
describe "Admin Builds" do
4-
let(:project) { FactoryGirl.create :ci_project }
5-
let(:commit) { FactoryGirl.create :ci_commit, project: project }
4+
let(:commit) { FactoryGirl.create :ci_commit }
65
let(:build) { FactoryGirl.create :ci_build, commit: commit }
76

87
before do

spec/features/ci/builds_spec.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
describe "Builds" do
44
context :private_project do
55
before do
6-
@project = FactoryGirl.create :ci_project
7-
@commit = FactoryGirl.create :ci_commit, project: @project
6+
@commit = FactoryGirl.create :ci_commit
87
@build = FactoryGirl.create :ci_build, commit: @commit
98
login_as :user
10-
@project.gl_project.team << [@user, :master]
9+
@commit.project.gl_project.team << [@user, :master]
1110
end
1211

1312
describe "GET /:project/builds/:id" do
1413
before do
15-
visit ci_project_build_path(@project, @build)
14+
visit ci_project_build_path(@commit.project, @build)
1615
end
1716

1817
it { expect(page).to have_content @commit.sha[0..7] }
@@ -23,7 +22,7 @@
2322
describe "GET /:project/builds/:id/cancel" do
2423
before do
2524
@build.run!
26-
visit cancel_ci_project_build_path(@project, @build)
25+
visit cancel_ci_project_build_path(@commit.project, @build)
2726
end
2827

2928
it { expect(page).to have_content 'canceled' }
@@ -33,7 +32,7 @@
3332
describe "POST /:project/builds/:id/retry" do
3433
before do
3534
@build.cancel!
36-
visit ci_project_build_path(@project, @build)
35+
visit ci_project_build_path(@commit.project, @build)
3736
click_link 'Retry'
3837
end
3938

@@ -45,13 +44,15 @@
4544
context :public_project do
4645
describe "Show page public accessible" do
4746
before do
48-
@project = FactoryGirl.create :ci_public_project
49-
@commit = FactoryGirl.create :ci_commit, project: @project
47+
@commit = FactoryGirl.create :ci_commit
48+
@commit.project.public = true
49+
@commit.project.save
50+
5051
@runner = FactoryGirl.create :ci_specific_runner
5152
@build = FactoryGirl.create :ci_build, commit: @commit, runner: @runner
5253

5354
stub_gitlab_calls
54-
visit ci_project_build_path(@project, @build)
55+
visit ci_project_build_path(@commit.project, @build)
5556
end
5657

5758
it { expect(page).to have_content @commit.sha[0..7] }

0 commit comments

Comments
 (0)