summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTomasz Maczukin <tomasz@maczukin.pl>2017-02-16 01:05:44 +0100
committerTomasz Maczukin <tomasz@maczukin.pl>2017-03-02 17:45:45 +0100
commit3eafffcef0f8932bab4e06b465f9b63327e87942 (patch)
treecdb64b4be94007d4b8d0ce380862f21a027267fa /lib
parent3d26a8d0b62f70e02917f7601bc2032fb3aed7e6 (diff)
downloadgitlab-ce-3eafffcef0f8932bab4e06b465f9b63327e87942.tar.gz
Refactor JobRequest response structure
Diffstat (limited to 'lib')
-rw-r--r--lib/api/entities.rb94
-rw-r--r--lib/api/runner.rb6
-rw-r--r--lib/ci/api/builds.rb2
-rw-r--r--lib/gitlab/ci/build/response/image.rb20
-rw-r--r--lib/gitlab/ci/build/response/step.rb46
5 files changed, 137 insertions, 31 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index cb3b409b8d0..7e07154600a 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -698,42 +698,82 @@ module API
expose :active?, as: :active
end
- class ArtifactFile < Grape::Entity
- expose :filename, :size
- end
+ module JobRequest
+ class JobInfo < Grape::Entity
+ expose :name, :stage
+ expose :project_id, :project_name
+ end
+
+ class GitInfo < Grape::Entity
+ expose :repo_url, :ref, :sha, :before_sha
+ expose :ref_type do |model|
+ if model.tag
+ 'tag'
+ else
+ 'branch'
+ end
+ end
+ end
- class JobCredentials < Grape::Entity
- expose :type, :url, :username, :password
- end
+ class RunnerInfo < Grape::Entity
+ expose :timeout
+ end
- class JobResponse < Grape::Entity
- expose :id, :ref, :tag, :sha, :status
- expose :name, :token, :stage
- expose :project_id
- expose :project_name
- expose :artifacts_file, using: ArtifactFile, if: ->(build, _) { build.artifacts? }
- end
+ class Step < Grape::Entity
+ expose :name, :script, :timeout, :condition, :result
+ end
- class RequestJobResponse < JobResponse
- expose :commands
- expose :repo_url
- expose :before_sha
- expose :allow_git_fetch
- expose :token
- expose :artifacts_expire_at, if: ->(build, _) { build.artifacts? }
+ class Image < Grape::Entity
+ expose :name
+ end
- expose :options do |model|
- model.options
+ class Artifacts < Grape::Entity
+ expose :name, :untracked, :paths, :when, :expire_in
end
- expose :timeout do |model|
- model.timeout
+ class Cache < Grape::Entity
+ expose :key, :untracked, :paths
end
- expose :variables
- expose :depends_on_builds, using: JobResponse
+ class Credentials < Grape::Entity
+ expose :type, :url, :username, :password
+ end
- expose :credentials, using: JobCredentials
+ class ArtifactFile < Grape::Entity
+ expose :filename, :size
+ end
+
+ class Dependency < Grape::Entity
+ expose :id, :name
+ expose :artifacts_file, using: ArtifactFile, if: ->(job, _) { job.artifacts? }
+ end
+
+ class Response < Grape::Entity
+ expose :id
+ expose :token
+ expose :allow_git_fetch
+
+ expose :job_info, using: JobInfo do |model|
+ model
+ end
+
+ expose :git_info, using: GitInfo do |model|
+ model
+ end
+
+ expose :runner_info, using: RunnerInfo do |model|
+ model
+ end
+
+ expose :variables
+ expose :steps, using: Step
+ expose :image, using: Image
+ expose :services, using: Image
+ expose :artifacts, using: Artifacts
+ expose :cache, using: Cache
+ expose :credentials, using: Credentials
+ expose :depends_on_builds, as: :dependencies, using: Dependency
+ end
end
end
end
diff --git a/lib/api/runner.rb b/lib/api/runner.rb
index 2e7b96e5169..8372b794739 100644
--- a/lib/api/runner.rb
+++ b/lib/api/runner.rb
@@ -51,7 +51,7 @@ module API
resource :jobs do
desc 'Request a job' do
- success Entities::RequestJobResponse
+ success Entities::JobRequest::Response
end
params do
requires :token, type: String, desc: %q(Runner's authentication token)
@@ -68,13 +68,13 @@ module API
end
new_update = current_runner.ensure_runner_queue_value
- result = ::Ci::RegisterBuildService.new(current_runner).execute
+ result = ::Ci::RegisterJobService.new(current_runner).execute
if result.valid?
if result.build
Gitlab::Metrics.add_event(:build_found,
project: result.build.project.path_with_namespace)
- present result.build, with: Entities::RequestJobResponse
+ present result.build, with: Entities::JobRequest::Response
else
Gitlab::Metrics.add_event(:build_not_found)
header 'X-GitLab-Last-Update', new_update
diff --git a/lib/ci/api/builds.rb b/lib/ci/api/builds.rb
index b51e76d93f2..746e76a1b1f 100644
--- a/lib/ci/api/builds.rb
+++ b/lib/ci/api/builds.rb
@@ -24,7 +24,7 @@ module Ci
new_update = current_runner.ensure_runner_queue_value
- result = Ci::RegisterBuildService.new(current_runner).execute
+ result = Ci::RegisterJobService.new(current_runner).execute
if result.valid?
if result.build
diff --git a/lib/gitlab/ci/build/response/image.rb b/lib/gitlab/ci/build/response/image.rb
new file mode 100644
index 00000000000..342a249aee8
--- /dev/null
+++ b/lib/gitlab/ci/build/response/image.rb
@@ -0,0 +1,20 @@
+module Gitlab
+ module Ci
+ module Build
+ module Response
+ class Image
+ attr_reader :name
+
+ def initialize(image)
+ type = image.class
+ @name = image if type == String
+ end
+
+ def valid?
+ @name != nil
+ end
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/gitlab/ci/build/response/step.rb b/lib/gitlab/ci/build/response/step.rb
new file mode 100644
index 00000000000..7b35852a510
--- /dev/null
+++ b/lib/gitlab/ci/build/response/step.rb
@@ -0,0 +1,46 @@
+module Gitlab
+ module Ci
+ module Build
+ module Response
+ class Step
+ CONDITION_IF_SUCCEEDED = 'run_if_succeeded'
+ CONDITION_ALWAYS = 'run_always'
+
+ RESULT_FAILS_JOB = 'fails_job'
+ RESULT_DOESNT_FAIL_JOB = 'doesnt_fail_job'
+
+ attr_reader :name, :script, :condition, :result, :timeout
+
+ class << self
+ def from_commands(build)
+ self.new(:script,
+ build.commands,
+ build.timeout,
+ CONDITION_IF_SUCCEEDED,
+ RESULT_FAILS_JOB)
+ end
+
+ def from_after_script(build)
+ after_script = build.options[:after_script]
+ return unless after_script
+
+ self.new(:after_script,
+ after_script,
+ build.timeout,
+ CONDITION_ALWAYS,
+ RESULT_DOESNT_FAIL_JOB)
+ end
+ end
+
+ def initialize(name, script, timeout, condition = CONDITION_IF_SUCCEEDED, result = RESULT_FAILS_JOB)
+ @name = name
+ @script = script.split("\n")
+ @timeout = timeout
+ @condition = condition
+ @result = result
+ end
+ end
+ end
+ end
+ end
+end \ No newline at end of file