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/pipeline_entity.rb | |
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/pipeline_entity.rb')
-rw-r--r-- | app/serializers/pipeline_entity.rb | 84 |
1 files changed, 84 insertions, 0 deletions
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 |