Commit 2eb9a20f authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Enable CI for gitlab when .gitlab-ci.yml is pushed

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 3b6915d8
......@@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date.
v 8.1.0 (unreleased)
- Show CI status on all pages where commits list is rendered
- Automatically enable CI when push .gitlab-ci.yml file to repository
v 8.0.1
- Improve CI migration procedure and documentation
......
......@@ -739,4 +739,23 @@ class Project < ActiveRecord::Base
def ci_commit(sha)
gitlab_ci_project.commits.find_by(sha: sha) if gitlab_ci?
end
def enable_ci(user)
# Enable service
service = gitlab_ci_service || create_gitlab_ci_service
service.active = true
service.save
# Create Ci::Project
params = OpenStruct.new({
id: self.id,
name_with_namespace: self.name_with_namespace,
path_with_namespace: self.path_with_namespace,
web_url: self.web_url,
default_branch: self.default_branch,
ssh_url_to_repo: self.ssh_url_to_repo
})
Ci::CreateProjectService.new.execute(user, params)
end
end
......@@ -72,7 +72,7 @@ class GitlabCiService < CiService
})
ci_project = Ci::Project.find_by!(gitlab_id: project.id)
Ci::CreateProjectService.new.execute(
current_user,
params,
......
......@@ -55,6 +55,12 @@ class GitPushService
@push_data = build_push_data(oldrev, newrev, ref)
# If CI was disabled but .gitlab-ci.yml file was pushed
# we enable CI automatically
if !project.gitlab_ci? && gitlab_ci_yaml?(newrev)
project.enable_ci(user)
end
EventCreateService.new.push(project, user, @push_data)
project.execute_hooks(@push_data.dup, :push_hooks)
project.execute_services(@push_data.dup, :push_hooks)
......@@ -143,4 +149,8 @@ class GitPushService
def commit_user(commit)
commit.author || user
end
def gitlab_ci_yaml?(sha)
@project.repository.blob_at(sha, '.gitlab-ci.yml')
end
end
......@@ -411,4 +411,14 @@ describe Project do
it { expect(project.ci_commit(commit.sha)).to eq(commit) }
end
describe :enable_ci do
let(:project) { create :project }
let(:user) { create :user }
before { project.enable_ci(user) }
it { expect(project.gitlab_ci?).to be_truthy }
it { expect(project.gitlab_ci_project).to be_a(Ci::Project) }
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