summaryrefslogtreecommitdiff
path: root/app/services/ci
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-08-25 18:42:46 -0700
committerDouwe Maan <douwe@gitlab.com>2015-08-25 18:42:46 -0700
commit046b28312704f3131e72dcd2dbdacc5264d4aa62 (patch)
treea8c2b14a6e1db3b662fee2c79af70d9fcb643c2e /app/services/ci
parente449426a4e7d15cdd582d4f136add52cbfb5e04e (diff)
downloadgitlab-ce-046b28312704f3131e72dcd2dbdacc5264d4aa62.tar.gz
Groundwork for merging CI into CE
Diffstat (limited to 'app/services/ci')
-rw-r--r--app/services/ci/create_commit_service.rb50
-rw-r--r--app/services/ci/create_project_service.rb35
-rw-r--r--app/services/ci/create_trigger_request_service.rb17
-rw-r--r--app/services/ci/event_service.rb31
-rw-r--r--app/services/ci/image_for_build_service.rb31
-rw-r--r--app/services/ci/register_build_service.rb40
-rw-r--r--app/services/ci/test_hook_service.rb7
-rw-r--r--app/services/ci/web_hook_service.rb36
8 files changed, 247 insertions, 0 deletions
diff --git a/app/services/ci/create_commit_service.rb b/app/services/ci/create_commit_service.rb
new file mode 100644
index 00000000000..0a1abf89a95
--- /dev/null
+++ b/app/services/ci/create_commit_service.rb
@@ -0,0 +1,50 @@
+module Ci
+ class CreateCommitService
+ def execute(project, params)
+ before_sha = params[:before]
+ sha = params[:checkout_sha] || params[:after]
+ origin_ref = params[:ref]
+
+ unless origin_ref && sha.present?
+ return false
+ end
+
+ ref = origin_ref.gsub(/\Arefs\/(tags|heads)\//, '')
+
+ # Skip branch removal
+ if sha == Ci::Git::BLANK_SHA
+ return false
+ end
+
+ commit = project.commits.find_by_sha_and_ref(sha, ref)
+
+ # Create commit if not exists yet
+ unless commit
+ data = {
+ ref: ref,
+ sha: sha,
+ tag: origin_ref.start_with?('refs/tags/'),
+ before_sha: before_sha,
+ push_data: {
+ before: before_sha,
+ after: sha,
+ ref: ref,
+ user_name: params[:user_name],
+ user_email: params[:user_email],
+ repository: params[:repository],
+ commits: params[:commits],
+ total_commits_count: params[:total_commits_count],
+ ci_yaml_file: params[:ci_yaml_file]
+ }
+ }
+
+ commit = project.commits.create(data)
+ end
+
+ commit.update_committed!
+ commit.create_builds unless commit.builds.any?
+
+ commit
+ end
+ end
+end
diff --git a/app/services/ci/create_project_service.rb b/app/services/ci/create_project_service.rb
new file mode 100644
index 00000000000..049ac2e9181
--- /dev/null
+++ b/app/services/ci/create_project_service.rb
@@ -0,0 +1,35 @@
+module Ci
+ class CreateProjectService
+ include Gitlab::Application.routes.url_helpers
+
+ def execute(current_user, params, project_route, forked_project = nil)
+ @project = Ci::Project.parse(params)
+
+ Ci::Project.transaction do
+ @project.save!
+
+ data = {
+ token: @project.token,
+ project_url: project_route.gsub(":project_id", @project.id.to_s),
+ }
+
+ unless Ci::Network.new.enable_ci(@project.gitlab_id, data, current_user.authenticate_options)
+ raise ActiveRecord::Rollback
+ end
+ end
+
+ if forked_project
+ # Copy settings
+ settings = forked_project.attributes.select do |attr_name, value|
+ ["public", "shared_runners_enabled", "allow_git_fetch"].include? attr_name
+ end
+
+ @project.update(settings)
+ end
+
+ Ci::EventService.new.create_project(current_user, @project)
+
+ @project
+ end
+ end
+end
diff --git a/app/services/ci/create_trigger_request_service.rb b/app/services/ci/create_trigger_request_service.rb
new file mode 100644
index 00000000000..9bad09f2f54
--- /dev/null
+++ b/app/services/ci/create_trigger_request_service.rb
@@ -0,0 +1,17 @@
+module Ci
+ class CreateTriggerRequestService
+ def execute(project, trigger, ref, variables = nil)
+ commit = project.commits.where(ref: ref).last
+ return unless commit
+
+ trigger_request = trigger.trigger_requests.create!(
+ commit: commit,
+ variables: variables
+ )
+
+ if commit.create_builds(trigger_request)
+ trigger_request
+ end
+ end
+ end
+end
diff --git a/app/services/ci/event_service.rb b/app/services/ci/event_service.rb
new file mode 100644
index 00000000000..3f4e02dd26c
--- /dev/null
+++ b/app/services/ci/event_service.rb
@@ -0,0 +1,31 @@
+module Ci
+ class EventService
+ def remove_project(user, project)
+ create(
+ description: "Project \"#{project.name}\" has been removed by #{user.username}",
+ user_id: user.id,
+ is_admin: true
+ )
+ end
+
+ def create_project(user, project)
+ create(
+ description: "Project \"#{project.name}\" has been created by #{user.username}",
+ user_id: user.id,
+ is_admin: true
+ )
+ end
+
+ def change_project_settings(user, project)
+ create(
+ project_id: project.id,
+ user_id: user.id,
+ description: "User \"#{user.username}\" updated projects settings"
+ )
+ end
+
+ def create(*args)
+ Ci::Event.create!(*args)
+ end
+ end
+end
diff --git a/app/services/ci/image_for_build_service.rb b/app/services/ci/image_for_build_service.rb
new file mode 100644
index 00000000000..b95835ba093
--- /dev/null
+++ b/app/services/ci/image_for_build_service.rb
@@ -0,0 +1,31 @@
+module Ci
+ class ImageForBuildService
+ def execute(project, params)
+ image_name =
+ if params[:sha]
+ commit = project.commits.find_by(sha: params[:sha])
+ image_for_commit(commit)
+ elsif params[:ref]
+ commit = project.last_commit_for_ref(params[:ref])
+ image_for_commit(commit)
+ else
+ 'build-unknown.svg'
+ end
+
+ image_path = Rails.root.join('public/ci', image_name)
+
+ OpenStruct.new(
+ path: image_path,
+ name: image_name
+ )
+ end
+
+ private
+
+ def image_for_commit(commit)
+ return 'build-unknown.svg' unless commit
+
+ 'build-' + commit.status + ".svg"
+ end
+ end
+end
diff --git a/app/services/ci/register_build_service.rb b/app/services/ci/register_build_service.rb
new file mode 100644
index 00000000000..7e0b58a5dc9
--- /dev/null
+++ b/app/services/ci/register_build_service.rb
@@ -0,0 +1,40 @@
+module Ci
+ # This class responsible for assigning
+ # proper pending build to runner on runner API request
+ class RegisterBuildService
+ def execute(current_runner)
+ builds = Ci::Build.pending.unstarted
+
+ builds =
+ if current_runner.shared?
+ # don't run projects which have not enables shared runners
+ builds.includes(:project).where(projects: { shared_runners_enabled: true })
+ else
+ # do run projects which are only assigned to this runner
+ builds.where(project_id: current_runner.projects)
+ end
+
+ builds = builds.order('created_at ASC')
+
+ 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
+ # with StateMachine::InvalidTransition in run! method.
+ build.with_lock do
+ build.runner_id = current_runner.id
+ build.save!
+ build.run!
+ end
+ end
+
+ build
+
+ rescue StateMachine::InvalidTransition
+ nil
+ end
+ end
+end
diff --git a/app/services/ci/test_hook_service.rb b/app/services/ci/test_hook_service.rb
new file mode 100644
index 00000000000..3a17596aaeb
--- /dev/null
+++ b/app/services/ci/test_hook_service.rb
@@ -0,0 +1,7 @@
+module Ci
+ class TestHookService
+ def execute(hook, current_user)
+ Ci::WebHookService.new.build_end(hook.project.commits.last.last_build)
+ end
+ end
+end
diff --git a/app/services/ci/web_hook_service.rb b/app/services/ci/web_hook_service.rb
new file mode 100644
index 00000000000..87984b20fa1
--- /dev/null
+++ b/app/services/ci/web_hook_service.rb
@@ -0,0 +1,36 @@
+module Ci
+ class WebHookService
+ def build_end(build)
+ execute_hooks(build.project, build_data(build))
+ end
+
+ def execute_hooks(project, data)
+ project.web_hooks.each do |web_hook|
+ async_execute_hook(web_hook, data)
+ end
+ end
+
+ def async_execute_hook(hook, data)
+ Sidekiq::Client.enqueue(Ci::WebHookWorker, hook.id, data)
+ end
+
+ def build_data(build)
+ project = build.project
+ data = {}
+ data.merge!({
+ build_id: build.id,
+ build_name: build.name,
+ build_status: build.status,
+ build_started_at: build.started_at,
+ build_finished_at: build.finished_at,
+ project_id: project.id,
+ project_name: project.name,
+ gitlab_url: project.gitlab_url,
+ ref: build.ref,
+ sha: build.sha,
+ before_sha: build.before_sha,
+ push_data: build.commit.push_data
+ })
+ end
+ end
+end