Commit 048dd5e4 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'merge-ci-data' into 'master'

Use data stored in projects instead of ci_projects

- It delegates name, path, gitlab_url, ssh_url_to_repo
- Remove ability to set this parameters using CI API

This fixes GitLab project rename, namespace change, repository rename, etc.

/cc @dzaporozhets 

See merge request !1404
parents 247385a7 ad765353
...@@ -12,7 +12,10 @@ module Ci ...@@ -12,7 +12,10 @@ module Ci
def show def show
@builds = @runner.builds.order('id DESC').first(30) @builds = @runner.builds.order('id DESC').first(30)
@projects = Ci::Project.all @projects = Ci::Project.all
@projects = @projects.search(params[:search]) if params[:search].present? if params[:search].present?
@gl_projects = ::Project.search(params[:search])
@projects = @projects.where(gitlab_id: @gl_projects.select(:id))
end
@projects = @projects.where("ci_projects.id NOT IN (?)", @runner.projects.pluck(:id)) if @runner.projects.any? @projects = @projects.where("ci_projects.id NOT IN (?)", @runner.projects.pluck(:id)) if @runner.projects.any?
@projects = @projects.page(params[:page]).per(30) @projects = @projects.page(params[:page]).per(30)
end end
......
...@@ -48,11 +48,12 @@ module Ci ...@@ -48,11 +48,12 @@ module Ci
accepts_nested_attributes_for :variables, allow_destroy: true accepts_nested_attributes_for :variables, allow_destroy: true
delegate :name_with_namespace, :path_with_namespace, :web_url, :http_url_to_repo, :ssh_url_to_repo, to: :gl_project
# #
# Validations # Validations
# #
validates_presence_of :name, :timeout, :token, :default_ref, validates_presence_of :timeout, :token, :default_ref, :gitlab_id
:path, :ssh_url_to_repo, :gitlab_id
validates_uniqueness_of :gitlab_id validates_uniqueness_of :gitlab_id
...@@ -60,8 +61,6 @@ module Ci ...@@ -60,8 +61,6 @@ module Ci
presence: true, presence: true,
if: ->(project) { project.always_build.present? } if: ->(project) { project.always_build.present? }
scope :public_only, ->() { where(public: true) }
before_validation :set_default_values before_validation :set_default_values
class << self class << self
...@@ -76,11 +75,8 @@ module Ci ...@@ -76,11 +75,8 @@ module Ci
def parse(project) def parse(project)
params = { params = {
name: project.name_with_namespace,
gitlab_id: project.id, gitlab_id: project.id,
path: project.path_with_namespace,
default_ref: project.default_branch || 'master', default_ref: project.default_branch || 'master',
ssh_url_to_repo: project.ssh_url_to_repo,
email_add_pusher: current_application_settings.add_pusher, email_add_pusher: current_application_settings.add_pusher,
email_only_broken_builds: current_application_settings.all_broken_builds, email_only_broken_builds: current_application_settings.all_broken_builds,
} }
...@@ -105,11 +101,18 @@ module Ci ...@@ -105,11 +101,18 @@ module Ci
joins("LEFT JOIN #{last_commit_subquery} AS last_commit ON #{Ci::Project.table_name}.gitlab_id = last_commit.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") order("CASE WHEN last_commit.committed_at IS NULL THEN 1 ELSE 0 END, last_commit.committed_at DESC")
end end
end
def name
name_with_namespace
end
def search(query) def path
where("LOWER(#{Ci::Project.table_name}.name) LIKE :query", path_with_namespace
query: "%#{query.try(:downcase)}%")
end end
def gitlab_url
web_url
end end
def any_runners? def any_runners?
...@@ -123,9 +126,6 @@ module Ci ...@@ -123,9 +126,6 @@ module Ci
def set_default_values def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank? self.token = SecureRandom.hex(15) if self.token.blank?
self.default_ref ||= 'master' 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 end
def tracked_refs def tracked_refs
...@@ -169,7 +169,7 @@ module Ci ...@@ -169,7 +169,7 @@ module Ci
# using http and basic auth # using http and basic auth
def repo_url_with_auth def repo_url_with_auth
auth = "gitlab-ci-token:#{token}@" auth = "gitlab-ci-token:#{token}@"
url = gitlab_url + ".git" url = http_url_to_repo + ".git"
url.sub(/^https?:\/\//) do |prefix| url.sub(/^https?:\/\//) do |prefix|
prefix + auth prefix + auth
end end
...@@ -201,10 +201,6 @@ module Ci ...@@ -201,10 +201,6 @@ module Ci
end end
end end
def gitlab_url
File.join(Gitlab.config.gitlab.url, path)
end
def setup_finished? def setup_finished?
commits.any? commits.any?
end end
......
...@@ -70,11 +70,7 @@ class GitlabCiService < CiService ...@@ -70,11 +70,7 @@ class GitlabCiService < CiService
def fork_registration(new_project, current_user) def fork_registration(new_project, current_user)
params = OpenStruct.new({ params = OpenStruct.new({
id: new_project.id, id: new_project.id,
name_with_namespace: new_project.name_with_namespace, default_branch: new_project.default_branch
path_with_namespace: new_project.path_with_namespace,
web_url: new_project.web_url,
default_branch: new_project.default_branch,
ssh_url_to_repo: new_project.ssh_url_to_repo
}) })
ci_project = Ci::Project.find_by!(gitlab_id: project.id) ci_project = Ci::Project.find_by!(gitlab_id: project.id)
......
class ChangeNameOfCiProjects < ActiveRecord::Migration
def up
change_column_null :ci_projects, :name, true
end
def down
change_column_null :ci_projects, :name, false
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150924125436) do ActiveRecord::Schema.define(version: 20150930095736) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
...@@ -158,7 +158,7 @@ ActiveRecord::Schema.define(version: 20150924125436) do ...@@ -158,7 +158,7 @@ ActiveRecord::Schema.define(version: 20150924125436) do
add_index "ci_jobs", ["project_id"], name: "index_ci_jobs_on_project_id", using: :btree add_index "ci_jobs", ["project_id"], name: "index_ci_jobs_on_project_id", using: :btree
create_table "ci_projects", force: true do |t| create_table "ci_projects", force: true do |t|
t.string "name", null: false t.string "name"
t.integer "timeout", default: 3600, null: false t.integer "timeout", default: 3600, null: false
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
......
...@@ -100,8 +100,6 @@ Parameters: ...@@ -100,8 +100,6 @@ Parameters:
* `name` (required) - The name of the project * `name` (required) - The name of the project
* `gitlab_id` (required) - The ID of the project on the Gitlab instance * `gitlab_id` (required) - The ID of the project on the Gitlab instance
* `path` (required) - The gitlab project path
* `ssh_url_to_repo` (required) - The gitlab SSH url to the repo
* `default_ref` (optional) - The branch to run on (default to `master`) * `default_ref` (optional) - The branch to run on (default to `master`)
### Update Project ### Update Project
...@@ -114,9 +112,6 @@ authenticated user has access to. ...@@ -114,9 +112,6 @@ authenticated user has access to.
Parameters: Parameters:
* `name` - The name of the project * `name` - The name of the project
* `gitlab_id` - The ID of the project on the Gitlab instance
* `path` - The gitlab project path
* `ssh_url_to_repo` - The gitlab SSH url to the repo
* `default_ref` - The branch to run on (default to `master`) * `default_ref` - The branch to run on (default to `master`)
### Remove Project ### Remove Project
......
...@@ -75,23 +75,17 @@ module Ci ...@@ -75,23 +75,17 @@ module Ci
# Create Gitlab CI project using Gitlab project info # Create Gitlab CI project using Gitlab project info
# #
# Parameters: # Parameters:
# name (required) - The name of the project
# gitlab_id (required) - The gitlab id of the project # gitlab_id (required) - The gitlab id of the project
# path (required) - The gitlab project path, ex. randx/six
# ssh_url_to_repo (required) - The gitlab ssh url to the repo
# default_ref - The branch to run against (defaults to `master`) # default_ref - The branch to run against (defaults to `master`)
# Example Request: # Example Request:
# POST /projects # POST /projects
post do post do
required_attributes! [:name, :gitlab_id, :ssh_url_to_repo] required_attributes! [:gitlab_id]
filtered_params = { filtered_params = {
name: params[:name],
gitlab_id: params[:gitlab_id], gitlab_id: params[:gitlab_id],
# we accept gitlab_url for backward compatibility for a while (added to 7.11) # we accept gitlab_url for backward compatibility for a while (added to 7.11)
path: params[:path] || params[:gitlab_url].sub(/.*\/(.*\/.*)$/, '\1'), default_ref: params[:default_ref] || 'master'
default_ref: params[:default_ref] || 'master',
ssh_url_to_repo: params[:ssh_url_to_repo]
} }
project = Ci::Project.new(filtered_params) project = Ci::Project.new(filtered_params)
...@@ -109,10 +103,6 @@ module Ci ...@@ -109,10 +103,6 @@ module Ci
# #
# Parameters: # Parameters:
# id (required) - The ID of a project # id (required) - The ID of a project
# name - The name of the project
# gitlab_id - The gitlab id of the project
# path - The gitlab project path, ex. randx/six
# ssh_url_to_repo - The gitlab ssh url to the repo
# default_ref - The branch to run against (defaults to `master`) # default_ref - The branch to run against (defaults to `master`)
# Example Request: # Example Request:
# PUT /projects/:id # PUT /projects/:id
...@@ -121,12 +111,7 @@ module Ci ...@@ -121,12 +111,7 @@ module Ci
unauthorized! unless can?(current_user, :admin_project, project.gl_project) unauthorized! unless can?(current_user, :admin_project, project.gl_project)
attrs = attributes_for_keys [:name, :gitlab_id, :path, :gitlab_url, :default_ref, :ssh_url_to_repo] attrs = attributes_for_keys [:default_ref]
# we accept gitlab_url for backward compatibility for a while (added to 7.11)
if attrs[:gitlab_url] && !attrs[:path]
attrs[:path] = attrs[:gitlab_url].sub(/.*\/(.*\/.*)$/, '\1')
end
if project.update_attributes(attrs) if project.update_attributes(attrs)
present project, with: Entities::Project present project, with: Entities::Project
......
...@@ -29,20 +29,8 @@ ...@@ -29,20 +29,8 @@
FactoryGirl.define do FactoryGirl.define do
factory :ci_project_without_token, class: Ci::Project do factory :ci_project_without_token, class: Ci::Project do
sequence :name do |n|
"GitLab / gitlab-shell#{n}"
end
default_ref 'master' default_ref 'master'
sequence :path do |n|
"gitlab/gitlab-shell#{n}"
end
sequence :ssh_url_to_repo do |n|
"git@demo.gitlab.com:gitlab/gitlab-shell#{n}.git"
end
gl_project factory: :empty_project gl_project factory: :empty_project
factory :ci_project do factory :ci_project do
......
...@@ -2,8 +2,7 @@ require 'spec_helper' ...@@ -2,8 +2,7 @@ require 'spec_helper'
describe "Admin Runners" do describe "Admin Runners" do
before do before do
skip_ci_admin_auth login_as :admin
login_as :user
end end
describe "Runners page" do describe "Runners page" do
...@@ -20,16 +19,16 @@ describe "Admin Runners" do ...@@ -20,16 +19,16 @@ describe "Admin Runners" do
describe 'search' do describe 'search' do
before do before do
FactoryGirl.create :ci_runner, description: 'foo' FactoryGirl.create :ci_runner, description: 'runner-foo'
FactoryGirl.create :ci_runner, description: 'bar' FactoryGirl.create :ci_runner, description: 'runner-bar'
search_form = find('#runners-search') search_form = find('#runners-search')
search_form.fill_in 'search', with: 'foo' search_form.fill_in 'search', with: 'runner-foo'
search_form.click_button 'Search' search_form.click_button 'Search'
end end
it { expect(page).to have_content("foo") } it { expect(page).to have_content("runner-foo") }
it { expect(page).not_to have_content("bar") } it { expect(page).not_to have_content("runner-bar") }
end end
end end
...@@ -37,8 +36,8 @@ describe "Admin Runners" do ...@@ -37,8 +36,8 @@ describe "Admin Runners" do
let(:runner) { FactoryGirl.create :ci_runner } let(:runner) { FactoryGirl.create :ci_runner }
before do before do
FactoryGirl.create(:ci_project, name: "foo") @project1 = FactoryGirl.create(:ci_project)
FactoryGirl.create(:ci_project, name: "bar") @project2 = FactoryGirl.create(:ci_project)
visit ci_admin_runner_path(runner) visit ci_admin_runner_path(runner)
end end
...@@ -47,19 +46,19 @@ describe "Admin Runners" do ...@@ -47,19 +46,19 @@ describe "Admin Runners" do
end end
describe 'projects' do describe 'projects' do
it { expect(page).to have_content("foo") } it { expect(page).to have_content(@project1.name_with_namespace) }
it { expect(page).to have_content("bar") } it { expect(page).to have_content(@project2.name_with_namespace) }
end end
describe 'search' do describe 'search' do
before do before do
search_form = find('#runner-projects-search') search_form = find('#runner-projects-search')
search_form.fill_in 'search', with: 'foo' search_form.fill_in 'search', with: @project1.gl_project.name
search_form.click_button 'Search' search_form.click_button 'Search'
end end
it { expect(page).to have_content("foo") } it { expect(page).to have_content(@project1.name_with_namespace) }
it { expect(page).not_to have_content("bar") } it { expect(page).not_to have_content(@project2.name_with_namespace) }
end end
end end
end end
...@@ -29,7 +29,8 @@ require 'spec_helper' ...@@ -29,7 +29,8 @@ require 'spec_helper'
describe Ci::Project do describe Ci::Project do
let(:gl_project) { FactoryGirl.create :empty_project } let(:gl_project) { FactoryGirl.create :empty_project }
subject { FactoryGirl.create :ci_project, gl_project: gl_project } let(:project) { FactoryGirl.create :ci_project, gl_project: gl_project }
subject { project }
it { is_expected.to have_many(:runner_projects) } it { is_expected.to have_many(:runner_projects) }
it { is_expected.to have_many(:runners) } it { is_expected.to have_many(:runners) }
...@@ -40,6 +41,7 @@ describe Ci::Project do ...@@ -40,6 +41,7 @@ describe Ci::Project do
it { is_expected.to have_many(:services) } it { is_expected.to have_many(:services) }
it { is_expected.to validate_presence_of :timeout } it { is_expected.to validate_presence_of :timeout }
it { is_expected.to validate_presence_of :gitlab_id }
describe 'before_validation' do describe 'before_validation' do
it 'should set an random token if none provided' do it 'should set an random token if none provided' do
...@@ -53,6 +55,66 @@ describe Ci::Project do ...@@ -53,6 +55,66 @@ describe Ci::Project do
end end
end end
describe :name_with_namespace do
subject { project.name_with_namespace }
it { is_expected.to eq(project.name) }
it { is_expected.to eq(gl_project.name_with_namespace) }
end
describe :path_with_namespace do
subject { project.path_with_namespace }
it { is_expected.to eq(project.path) }
it { is_expected.to eq(gl_project.path_with_namespace) }
end
describe :path_with_namespace do
subject { project.web_url }
it { is_expected.to eq(gl_project.web_url) }
end
describe :web_url do
subject { project.web_url }
it { is_expected.to eq(project.gitlab_url) }
it { is_expected.to eq(gl_project.web_url) }
end
describe :http_url_to_repo do
subject { project.http_url_to_repo }
it { is_expected.to eq(gl_project.http_url_to_repo) }
end
describe :ssh_url_to_repo do
subject { project.ssh_url_to_repo }
it { is_expected.to eq(gl_project.ssh_url_to_repo) }
end
describe :commits do
subject { project.commits }
before do
FactoryGirl.create :ci_commit, committed_at: 1.hour.ago, gl_project: gl_project
end
it { is_expected.to eq(gl_project.ci_commits) }
end
describe :builds do
subject { project.builds }
before do
commit = FactoryGirl.create :ci_commit, committed_at: 1.hour.ago, gl_project: gl_project
FactoryGirl.create :ci_build, commit: commit
end
it { is_expected.to eq(gl_project.ci_builds) }
end
describe "ordered_by_last_commit_date" do describe "ordered_by_last_commit_date" do
it "returns ordered projects" do it "returns ordered projects" do
newest_project = FactoryGirl.create :empty_project newest_project = FactoryGirl.create :empty_project
...@@ -174,13 +236,6 @@ describe Ci::Project do ...@@ -174,13 +236,6 @@ describe Ci::Project do
it { is_expected.to include(project.gitlab_url[7..-1]) } it { is_expected.to include(project.gitlab_url[7..-1]) }
end end
describe :search do
let!(:project) { FactoryGirl.create(:ci_project, name: "foo") }
it { expect(Ci::Project.search('fo')).to include(project) }
it { expect(Ci::Project.search('bar')).to be_empty }
end
describe :any_runners do describe :any_runners do
it "there are no runners available" do it "there are no runners available" do
project = FactoryGirl.create(:ci_project) project = FactoryGirl.create(:ci_project)
......
...@@ -134,7 +134,7 @@ describe Ci::API::API do ...@@ -134,7 +134,7 @@ describe Ci::API::API do
describe "PUT /projects/:id" do describe "PUT /projects/:id" do
let!(:project) { FactoryGirl.create(:ci_project) } let!(:project) { FactoryGirl.create(:ci_project) }
let!(:project_info) { { name: "An updated name!" } } let!(:project_info) { { default_ref: "develop" } }
before do before do
options.merge!(project_info) options.merge!(project_info)
...@@ -144,7 +144,7 @@ describe Ci::API::API do ...@@ -144,7 +144,7 @@ describe Ci::API::API do
project.gl_project.team << [user, :master] project.gl_project.team << [user, :master]
put ci_api("/projects/#{project.id}"), options put ci_api("/projects/#{project.id}"), options
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response["name"]).to eq(project_info[:name]) expect(json_response["default_ref"]).to eq(project_info[:default_ref])
end end
it "fails to update a non-existing project" do it "fails to update a non-existing project" do
...@@ -181,12 +181,10 @@ describe Ci::API::API do ...@@ -181,12 +181,10 @@ describe Ci::API::API do
end end
describe "POST /projects" do describe "POST /projects" do
let(:gl_project) { FactoryGirl.create :empty_project }
let(:project_info) do let(:project_info) do
{ {
name: "My project", gitlab_id: gl_project.id
gitlab_id: 1,
path: "testing/testing",
ssh_url_to_repo: "ssh://example.com/testing/testing.git"
} }
end end
...@@ -200,7 +198,7 @@ describe Ci::API::API do ...@@ -200,7 +198,7 @@ describe Ci::API::API do
it "should create a project with valid data" do it "should create a project with valid data" do
post ci_api("/projects"), options post ci_api("/projects"), options
expect(response.status).to eq(201) expect(response.status).to eq(201)
expect(json_response['name']).to eq(project_info[:name]) expect(json_response['name']).to eq(gl_project.name_with_namespace)
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Ci::EventService do describe Ci::EventService do
let(:project) { FactoryGirl.create :ci_project, name: "GitLab / gitlab-shell" } let(:project) { FactoryGirl.create :ci_project }
let(:user) { double(username: "root", id: 1) } let(:user) { double(username: "root", id: 1) }
before do before do
...@@ -12,7 +12,7 @@ describe Ci::EventService do ...@@ -12,7 +12,7 @@ describe Ci::EventService do
it "creates event" do it "creates event" do
Ci::EventService.new.remove_project(user, project) Ci::EventService.new.remove_project(user, project)
expect(Ci::Event.admin.last.description).to eq("Project \"GitLab / gitlab-shell\" has been removed by root") expect(Ci::Event.admin.last.description).to eq("Project \"#{project.name_with_namespace}\" has been removed by root")
end end
end end
...@@ -20,7 +20,7 @@ describe Ci::EventService do ...@@ -20,7 +20,7 @@ describe Ci::EventService do
it "creates event" do it "creates event" do
Ci::EventService.new.create_project(user, project) Ci::EventService.new.create_project(user, project)
expect(Ci::Event.admin.last.description).to eq("Project \"GitLab / gitlab-shell\" has been created by root") expect(Ci::Event.admin.last.description).to eq("Project \"#{project.name_with_namespace}\" has been created by root")
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment