summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-09-23 13:11:40 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-09-23 13:11:40 +0200
commit2eb9a20f36054d99e52342aae1b018206e7791a3 (patch)
tree818c1602c82a63de6c1078d41a02a971e6adb5e0
parent3b6915d8910296296676e32129138c50bb1b0c5c (diff)
downloadgitlab-ce-2eb9a20f36054d99e52342aae1b018206e7791a3.tar.gz
Enable CI for gitlab when .gitlab-ci.yml is pushed
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--CHANGELOG1
-rw-r--r--app/models/project.rb19
-rw-r--r--app/models/project_services/gitlab_ci_service.rb2
-rw-r--r--app/services/git_push_service.rb10
-rw-r--r--spec/models/project_spec.rb10
5 files changed, 41 insertions, 1 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 80998016563..4ee55016576 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -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
diff --git a/app/models/project.rb b/app/models/project.rb
index 72120885105..c5c94cbfba2 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -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
diff --git a/app/models/project_services/gitlab_ci_service.rb b/app/models/project_services/gitlab_ci_service.rb
index 9e2b3bcd873..ec3bd44024f 100644
--- a/app/models/project_services/gitlab_ci_service.rb
+++ b/app/models/project_services/gitlab_ci_service.rb
@@ -72,7 +72,7 @@ class GitlabCiService < CiService
})
ci_project = Ci::Project.find_by!(gitlab_id: project.id)
-
+
Ci::CreateProjectService.new.execute(
current_user,
params,
diff --git a/app/services/git_push_service.rb b/app/services/git_push_service.rb
index 0a73244774a..fb4a6dd8742 100644
--- a/app/services/git_push_service.rb
+++ b/app/services/git_push_service.rb
@@ -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
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index ba57e31f7fe..9e7b6f5cb30 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -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