diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-11-10 15:32:23 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-11-10 15:32:23 +0100 |
commit | 6f6119b7389ef7b5e13f2800611d5d7a806e41a5 (patch) | |
tree | e5a8b28f3056fc3918ec302dda9e87bb94a51499 /app/serializers | |
parent | 7ae775d7aafa7dd02392d50cd20ccdac75b261e1 (diff) | |
download | gitlab-ce-6f6119b7389ef7b5e13f2800611d5d7a806e41a5.tar.gz |
Support pipelines API
Pass `updated_at` to get only incremental changes since last update
Diffstat (limited to 'app/serializers')
-rw-r--r-- | app/serializers/pipeline_action_entity.rb | 14 | ||||
-rw-r--r-- | app/serializers/pipeline_artifact_entity.rb | 14 | ||||
-rw-r--r-- | app/serializers/pipeline_entity.rb | 84 | ||||
-rw-r--r-- | app/serializers/pipeline_serializer.rb | 3 | ||||
-rw-r--r-- | app/serializers/pipeline_stage_entity.rb | 19 | ||||
-rw-r--r-- | app/serializers/request_aware_entity.rb | 8 |
6 files changed, 142 insertions, 0 deletions
diff --git a/app/serializers/pipeline_action_entity.rb b/app/serializers/pipeline_action_entity.rb new file mode 100644 index 00000000000..d45341f09d2 --- /dev/null +++ b/app/serializers/pipeline_action_entity.rb @@ -0,0 +1,14 @@ +class PipelineActionEntity < Grape::Entity + include RequestAwareEntity + + expose :name do |build| + build.name.humanize + end + + expose :url do |build| + play_namespace_project_build_path( + pipeline.project.namespace, + pipeline.project, + build) + end +end diff --git a/app/serializers/pipeline_artifact_entity.rb b/app/serializers/pipeline_artifact_entity.rb new file mode 100644 index 00000000000..01393dbea2d --- /dev/null +++ b/app/serializers/pipeline_artifact_entity.rb @@ -0,0 +1,14 @@ +class PipelineArtifactEntity < Grape::Entity + include RequestAwareEntity + + expose :name do |build| + build.name + end + + expose :url do |build| + download_namespace_project_build_artifacts_path( + pipeline.project.namespace, + pipeline.project, + build) + end +end diff --git a/app/serializers/pipeline_entity.rb b/app/serializers/pipeline_entity.rb new file mode 100644 index 00000000000..7afce8fd15b --- /dev/null +++ b/app/serializers/pipeline_entity.rb @@ -0,0 +1,84 @@ +class PipelineEntity < Grape::Entity + include RequestAwareEntity + + expose :id + expose :user, if: -> (pipeline, opts) { created?(pipeline, opts) }, using: UserEntity + + expose :status + expose :duration + expose :finished_at + expose :stages_with_statuses, as: :stages, if: -> (pipeline, opts) { updated?(pipeline, opts) }, using: PipelineStageEntity + expose :artifacts, if: -> (pipeline, opts) { updated?(pipeline, opts) }, using: PipelineArtifactEntity + expose :manual_actions, if: -> (pipeline, opts) { updated?(pipeline, opts) }, using: PipelineActionEntity + + expose :flags, if: -> (pipeline, opts) { created?(pipeline, opts) } do + expose :latest?, as: :latest + expose :triggered?, as: :triggered + expose :yaml_errors?, as: :yaml_errors do |pipeline| + pipeline.yaml_errors.present? + end + expose :stuck?, as: :stuck do |pipeline| + pipeline.builds.any?(&:stuck?) + end + end + + expose :ref, if: -> (pipeline, opts) { created?(pipeline, opts) } do + expose :name do |pipeline| + pipeline.ref + end + + expose :ref_url do |pipeline| + namespace_project_tree_url( + pipeline.project.namespace, + pipeline.project, + id: pipeline.ref) + end + + expose :tag? + end + + expose :commit, if: -> (pipeline, opts) { created?(pipeline, opts) } do + expose :short_sha + + expose :sha_url do |pipeline| + namespace_project_commit_path( + pipeline.project.namespace, + pipeline.project, + pipeline.sha) + end + + expose :title do |pipeline| + pipeline.commit.try(:title) + end + + expose :author, using: UserEntity do |pipeline| + pipeline.commit.try(:author) + end + end + + expose :retry_url, if: -> (pipeline, opts) { updated?(pipeline, opts) } do |pipeline| + can?(current_user, :update_pipeline, pipeline.project) && + pipeline.retryable? && + retry_namespace_project_pipeline_path(pipeline.project.namespace, pipeline.project, pipeline.id) + end + + expose :cancel_url, if: -> (pipeline, opts) { updated?(pipeline, opts) } do |pipeline| + can?(current_user, :update_pipeline, pipeline.project) && + pipeline.cancelable? && + cancel_namespace_project_pipeline_path(pipeline.project.namespace, pipeline.project, pipeline.id) + end + + private + + def last_updated(opts) + opts.fetch(:last_updated) + end + + def created?(pipeline, opts) + !last_updated(opts) || pipeline.created_at > last_updated(opts) + end + + def updated?(pipeline, opts) + !last_updated(opts) || pipeline.updated_at > last_updated(opts) + end +end diff --git a/app/serializers/pipeline_serializer.rb b/app/serializers/pipeline_serializer.rb new file mode 100644 index 00000000000..f7abbec7d45 --- /dev/null +++ b/app/serializers/pipeline_serializer.rb @@ -0,0 +1,3 @@ +class PipelineSerializer < BaseSerializer + entity PipelineEntity +end diff --git a/app/serializers/pipeline_stage_entity.rb b/app/serializers/pipeline_stage_entity.rb new file mode 100644 index 00000000000..230ef8a22da --- /dev/null +++ b/app/serializers/pipeline_stage_entity.rb @@ -0,0 +1,19 @@ +class PipelineStageEntity < Grape::Entity + include RequestAwareEntity + + expose :name do |stage| + stage.name + end + + expose :status do |stage| + stage.status || 'not found' + end + + expose :url do |stage| + namespace_project_pipeline_path( + stage.pipeline.project.namespace, + stage.pipeline.project, + stage.pipeline.id, + anchor: stage.name) + end +end diff --git a/app/serializers/request_aware_entity.rb b/app/serializers/request_aware_entity.rb index ff8c1142abc..7a096d9d5a8 100644 --- a/app/serializers/request_aware_entity.rb +++ b/app/serializers/request_aware_entity.rb @@ -8,4 +8,12 @@ module RequestAwareEntity def request @options.fetch(:request) end + + def current_user + @options.fetch(:current_user) + end + + def can?(object, action, subject) + Ability.allowed?(object, action, subject) + end end |