diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 11:10:13 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 11:10:13 +0000 |
commit | 0ea3fcec397b69815975647f5e2aa5fe944a8486 (patch) | |
tree | 7979381b89d26011bcf9bdc989a40fcc2f1ed4ff /app/serializers | |
parent | 72123183a20411a36d607d70b12d57c484394c8e (diff) | |
download | gitlab-ce-0ea3fcec397b69815975647f5e2aa5fe944a8486.tar.gz |
Add latest changes from gitlab-org/gitlab@15-1-stable-eev15.1.0-rc42
Diffstat (limited to 'app/serializers')
24 files changed, 242 insertions, 176 deletions
diff --git a/app/serializers/analytics_issue_entity.rb b/app/serializers/analytics_issue_entity.rb index a0d6d120a48..2e3e32faef6 100644 --- a/app/serializers/analytics_issue_entity.rb +++ b/app/serializers/analytics_issue_entity.rb @@ -30,6 +30,10 @@ class AnalyticsIssueEntity < Grape::Entity url_to(:namespace_project_issue, object) end + expose :end_event_timestamp do |object| + object[:end_event_timestamp] && interval_in_words(object[:end_event_timestamp]) + end + private def url_to(route, object) diff --git a/app/serializers/deploy_key_entity.rb b/app/serializers/deploy_key_entity.rb deleted file mode 100644 index 486189b84ca..00000000000 --- a/app/serializers/deploy_key_entity.rb +++ /dev/null @@ -1,39 +0,0 @@ -# frozen_string_literal: true - -class DeployKeyEntity < Grape::Entity - expose :id - expose :user_id - expose :title - expose :fingerprint - expose :fingerprint_sha256 - expose :destroyed_when_orphaned?, as: :destroyed_when_orphaned - expose :almost_orphaned?, as: :almost_orphaned - expose :created_at - expose :updated_at - expose :deploy_keys_projects, using: DeployKeysProjectEntity do |deploy_key| - deploy_key.deploy_keys_projects.select do |deploy_key_project| - !deploy_key_project.project&.pending_delete? && (allowed_to_read_project?(deploy_key_project.project) || options[:user].admin?) - end - end - expose :can_edit - expose :user, as: :owner, using: ::API::Entities::UserBasic, if: -> (_, opts) { can_read_owner?(opts) } - - private - - def can_edit - Ability.allowed?(options[:user], :update_deploy_key, object) || - Ability.allowed?(options[:user], :update_deploy_keys_project, object.deploy_keys_project_for(options[:project])) - end - - def can_read_owner?(opts) - opts[:with_owner] && Ability.allowed?(options[:user], :read_user, object.user) - end - - def allowed_to_read_project?(project) - if options[:readable_project_ids] - options[:readable_project_ids].include?(project.id) - else - Ability.allowed?(options[:user], :read_project, project) - end - end -end diff --git a/app/serializers/deploy_key_serializer.rb b/app/serializers/deploy_key_serializer.rb deleted file mode 100644 index a1cd98b631b..00000000000 --- a/app/serializers/deploy_key_serializer.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -class DeployKeySerializer < BaseSerializer - entity DeployKeyEntity -end diff --git a/app/serializers/deploy_keys/basic_deploy_key_entity.rb b/app/serializers/deploy_keys/basic_deploy_key_entity.rb new file mode 100644 index 00000000000..9184bc5f0ce --- /dev/null +++ b/app/serializers/deploy_keys/basic_deploy_key_entity.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module DeployKeys + class BasicDeployKeyEntity < Grape::Entity + expose :id + expose :user_id + expose :title + expose :fingerprint + expose :fingerprint_sha256 + expose :destroyed_when_orphaned?, as: :destroyed_when_orphaned + expose :almost_orphaned?, as: :almost_orphaned + expose :created_at + expose :updated_at + expose :can_edit + expose :user, as: :owner, using: ::API::Entities::UserBasic, if: -> (_, opts) { can_read_owner?(opts) } + + private + + def can_edit + Ability.allowed?(options[:user], :update_deploy_key, object) || + Ability.allowed?(options[:user], :update_deploy_keys_project, object.deploy_keys_project_for(options[:project])) + end + + def can_read_owner?(opts) + opts[:with_owner] && Ability.allowed?(options[:user], :read_user, object.user) + end + end +end diff --git a/app/serializers/deploy_keys/basic_deploy_key_serializer.rb b/app/serializers/deploy_keys/basic_deploy_key_serializer.rb new file mode 100644 index 00000000000..699f3baac78 --- /dev/null +++ b/app/serializers/deploy_keys/basic_deploy_key_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module DeployKeys + class BasicDeployKeySerializer < BaseSerializer + entity BasicDeployKeyEntity + end +end diff --git a/app/serializers/deploy_keys/deploy_key_entity.rb b/app/serializers/deploy_keys/deploy_key_entity.rb new file mode 100644 index 00000000000..79f386d1529 --- /dev/null +++ b/app/serializers/deploy_keys/deploy_key_entity.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module DeployKeys + class DeployKeyEntity < BasicDeployKeyEntity + expose :deploy_keys_projects, using: DeployKeysProjectEntity do |deploy_key| + deploy_key.deploy_keys_projects.select do |deploy_key_project| + !deploy_key_project.project&.pending_delete? && + (allowed_to_read_project?(deploy_key_project.project) || options[:user].can_admin_all_resources?) + end + end + + private + + def allowed_to_read_project?(project) + if options[:readable_project_ids] + options[:readable_project_ids].include?(project.id) + else + Ability.allowed?(options[:user], :read_project, project) + end + end + end +end diff --git a/app/serializers/deploy_keys/deploy_key_serializer.rb b/app/serializers/deploy_keys/deploy_key_serializer.rb new file mode 100644 index 00000000000..b00ef65696f --- /dev/null +++ b/app/serializers/deploy_keys/deploy_key_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module DeployKeys + class DeployKeySerializer < BaseSerializer + entity DeployKeyEntity + end +end diff --git a/app/serializers/diff_file_entity.rb b/app/serializers/diff_file_entity.rb index ef856ee0116..9f8628fe849 100644 --- a/app/serializers/diff_file_entity.rb +++ b/app/serializers/diff_file_entity.rb @@ -56,8 +56,7 @@ class DiffFileEntity < DiffFileBaseEntity # Used for inline diffs expose :highlighted_diff_lines, using: DiffLineEntity, if: -> (diff_file, options) { inline_diff_view?(options) && diff_file.text? } do |diff_file| - file = conflict_file(options, diff_file) || diff_file - file.diff_lines_for_serializer + highlighted_diff_lines_for(diff_file, options) end expose :is_fully_expanded do |diff_file| @@ -89,6 +88,15 @@ class DiffFileEntity < DiffFileBaseEntity # If nothing is present, inline will be the default. options.fetch(:diff_view, :inline).to_sym end + + def highlighted_diff_lines_for(diff_file, options) + file = conflict_file(options, diff_file) || diff_file + + file.diff_lines_for_serializer + rescue Gitlab::Git::Conflict::Parser::UnmergeableFile + # Fallback to diff_file as it means that conflict lines can't be parsed due to limit + diff_file.diff_lines_for_serializer + end end DiffFileEntity.prepend_mod diff --git a/app/serializers/environment_serializer.rb b/app/serializers/environment_serializer.rb index b13140efea7..3f236fa55df 100644 --- a/app/serializers/environment_serializer.rb +++ b/app/serializers/environment_serializer.rb @@ -52,19 +52,30 @@ class EnvironmentSerializer < BaseSerializer end def batch_load(resource) + temp_deployment_associations = deployment_associations + resource = resource.preload(environment_associations.except(:last_deployment, :upcoming_deployment)) + if ::Feature.enabled?(:batch_load_environment_last_deployment_group, resource.first&.project) + temp_deployment_associations[:deployable][:pipeline][:latest_successful_builds] = [] + end + Preloaders::Environments::DeploymentPreloader.new(resource) - .execute_with_union(:last_deployment, deployment_associations) + .execute_with_union(:last_deployment, temp_deployment_associations) Preloaders::Environments::DeploymentPreloader.new(resource) - .execute_with_union(:upcoming_deployment, deployment_associations) + .execute_with_union(:upcoming_deployment, temp_deployment_associations) resource.to_a.tap do |environments| environments.each do |environment| # Batch loading the commits of the deployments environment.last_deployment&.commit&.try(:lazy_author) environment.upcoming_deployment&.commit&.try(:lazy_author) + + if ::Feature.enabled?(:batch_load_environment_last_deployment_group, environment.project) + # Batch loading last_deployment_group which is called later by environment.stop_actions + environment.last_deployment_group + end end end end @@ -89,10 +100,11 @@ class EnvironmentSerializer < BaseSerializer user: [], metadata: [], pipeline: { - manual_actions: [], - scheduled_actions: [] + manual_actions: [:metadata, :deployment], + scheduled_actions: [:metadata] }, - project: project_associations + project: project_associations, + deployment: [] } } end diff --git a/app/serializers/integrations/event_entity.rb b/app/serializers/integrations/event_entity.rb new file mode 100644 index 00000000000..170f660f334 --- /dev/null +++ b/app/serializers/integrations/event_entity.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module Integrations + class EventEntity < Grape::Entity + include RequestAwareEntity + + expose :title do |event| + IntegrationsHelper.integration_event_title(event) + end + + expose :event_field_name, as: :name + + expose :value do |event| + integration[event_field_name] + end + + expose :description do |event| + IntegrationsHelper.integration_event_description(integration, event) + end + + expose :field, if: ->(_, _) { event_field } do + expose :name do |event| + event_field[:name] + end + expose :value do |event| + integration.public_send(event_field[:name]) # rubocop:disable GitlabSecurity/PublicSend + end + end + + private + + alias_method :event, :object + + def event_field_name + IntegrationsHelper.integration_event_field_name(event) + end + + def event_field + @event_field ||= integration.event_field(event) + end + + def integration + request.integration + end + end +end diff --git a/app/serializers/integrations/event_serializer.rb b/app/serializers/integrations/event_serializer.rb new file mode 100644 index 00000000000..fab7f9d459f --- /dev/null +++ b/app/serializers/integrations/event_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Integrations + class EventSerializer < BaseSerializer + entity Integrations::EventEntity + end +end diff --git a/app/serializers/integrations/field_entity.rb b/app/serializers/integrations/field_entity.rb new file mode 100644 index 00000000000..697b53a737e --- /dev/null +++ b/app/serializers/integrations/field_entity.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module Integrations + class FieldEntity < Grape::Entity + include RequestAwareEntity + include Gitlab::Utils::StrongMemoize + + expose :section, :type, :name, :placeholder, :required, :choices, :checkbox_label + + expose :title do |field| + non_empty_password?(field) ? field[:non_empty_password_title] : field[:title] + end + + expose :help do |field| + non_empty_password?(field) ? field[:non_empty_password_help] : field[:help] + end + + expose :value do |field| + value = value_for(field) + + if non_empty_password?(field) + 'true' + elsif field[:type] == 'checkbox' + ActiveRecord::Type::Boolean.new.deserialize(value).to_s + else + value + end + end + + private + + def integration + request.integration + end + + def value_for(field) + strong_memoize(:value_for) do + # field[:name] is not user input and so can assume is safe + integration.public_send(field[:name]) # rubocop:disable GitlabSecurity/PublicSend + end + end + + def non_empty_password?(field) + strong_memoize(:non_empty_password) do + field[:type] == 'password' && value_for(field).present? + end + end + end +end diff --git a/app/serializers/integrations/field_serializer.rb b/app/serializers/integrations/field_serializer.rb new file mode 100644 index 00000000000..c8f9823e997 --- /dev/null +++ b/app/serializers/integrations/field_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Integrations + class FieldSerializer < BaseSerializer + entity Integrations::FieldEntity + end +end diff --git a/app/serializers/issue_board_entity.rb b/app/serializers/issue_board_entity.rb index bcad28d6aad..ebd0f037160 100644 --- a/app/serializers/issue_board_entity.rb +++ b/app/serializers/issue_board_entity.rb @@ -3,6 +3,10 @@ class IssueBoardEntity < Grape::Entity include RequestAwareEntity + format_with(:upcase) do |item| + item.try(:upcase) + end + expose :id expose :iid expose :title @@ -51,6 +55,11 @@ class IssueBoardEntity < Grape::Entity expose :assignable_labels_endpoint, if: -> (issue) { issue.project } do |issue| project_labels_path(issue.project, format: :json, include_ancestor_groups: true) end + + expose :issue_type, + as: :type, + format_with: :upcase, + documentation: { type: "String", desc: "One of #{::WorkItems::Type.base_types.keys.map(&:upcase)}" } end IssueBoardEntity.prepend_mod_with('IssueBoardEntity') diff --git a/app/serializers/issue_entity.rb b/app/serializers/issue_entity.rb index 852a2e62b7d..eba2c49bc2e 100644 --- a/app/serializers/issue_entity.rb +++ b/app/serializers/issue_entity.rb @@ -3,6 +3,10 @@ class IssueEntity < IssuableEntity include TimeTrackableEntity + format_with(:upcase) do |item| + item.try(:upcase) + end + expose :state expose :milestone_id expose :updated_by_id @@ -75,6 +79,11 @@ class IssueEntity < IssuableEntity expose :issue_email_participants do |issue| issue.issue_email_participants.map { |x| { email: x.email } } end + + expose :issue_type, + as: :type, + format_with: :upcase, + documentation: { type: "String", desc: "One of #{::WorkItems::Type.base_types.keys.map(&:upcase)}" } end IssueEntity.prepend_mod_with('IssueEntity') diff --git a/app/serializers/linked_issue_entity.rb b/app/serializers/linked_issue_entity.rb index 769e3ed7310..4a28213fbac 100644 --- a/app/serializers/linked_issue_entity.rb +++ b/app/serializers/linked_issue_entity.rb @@ -3,6 +3,10 @@ class LinkedIssueEntity < Grape::Entity include RequestAwareEntity + format_with(:upcase) do |item| + item.try(:upcase) + end + expose :id, :confidential, :title expose :assignees, using: UserEntity @@ -21,6 +25,11 @@ class LinkedIssueEntity < Grape::Entity Gitlab::UrlBuilder.build(link, only_path: true) end + expose :issue_type, + as: :type, + format_with: :upcase, + documentation: { type: "String", desc: "One of #{::WorkItems::Type.base_types.keys.map(&:upcase)}" } + expose :relation_path expose :due_date, :created_at, :closed_at diff --git a/app/serializers/merge_request_poll_cached_widget_entity.rb b/app/serializers/merge_request_poll_cached_widget_entity.rb index 9d001d18aa6..0c5af67bcda 100644 --- a/app/serializers/merge_request_poll_cached_widget_entity.rb +++ b/app/serializers/merge_request_poll_cached_widget_entity.rb @@ -18,7 +18,8 @@ class MergeRequestPollCachedWidgetEntity < IssuableEntity expose :rebase_in_progress?, as: :rebase_in_progress expose :commits_count expose :merge_ongoing?, as: :merge_ongoing - expose :work_in_progress?, as: :work_in_progress + expose :draft?, as: :draft + expose :draft?, as: :work_in_progress expose :cannot_be_merged?, as: :has_conflicts expose :can_be_merged?, as: :can_be_merged expose :remove_source_branch?, as: :remove_source_branch diff --git a/app/serializers/merge_request_poll_widget_entity.rb b/app/serializers/merge_request_poll_widget_entity.rb index 12998d70a22..fc1534a88aa 100644 --- a/app/serializers/merge_request_poll_widget_entity.rb +++ b/app/serializers/merge_request_poll_widget_entity.rb @@ -33,13 +33,17 @@ class MergeRequestPollWidgetEntity < Grape::Entity # Booleans expose :mergeable_discussions_state?, as: :mergeable_discussions_state do |merge_request| - # This avoids calling MergeRequest#mergeable_discussions_state without - # considering the state of the MR first. If a MR isn't mergeable, we can - # safely short-circuit it. - if merge_request.mergeable_state?(skip_ci_check: true, skip_discussions_check: true) + if Feature.enabled?(:change_response_code_merge_status, merge_request.project) merge_request.mergeable_discussions_state? else - false + # This avoids calling MergeRequest#mergeable_discussions_state without + # considering the state of the MR first. If a MR isn't mergeable, we can + # safely short-circuit it. + if merge_request.mergeable_state?(skip_ci_check: true, skip_discussions_check: true) + merge_request.mergeable_discussions_state? + else + false + end end end diff --git a/app/serializers/merge_request_widget_entity.rb b/app/serializers/merge_request_widget_entity.rb index 5bf91ed0a51..cf984207ad1 100644 --- a/app/serializers/merge_request_widget_entity.rb +++ b/app/serializers/merge_request_widget_entity.rb @@ -128,20 +128,6 @@ class MergeRequestWidgetEntity < Grape::Entity end end - expose :codeclimate, if: -> (mr, _) { head_pipeline_downloadable_path_for_report_type(:codequality) } do - expose :head_path do |merge_request| - head_pipeline_downloadable_path_for_report_type(:codequality) - end - - expose :base_path do |merge_request| - if use_merge_base_with_merged_results? - merge_base_pipeline_downloadable_path_for_report_type(:codequality) - else - base_pipeline_downloadable_path_for_report_type(:codequality) - end - end - end - expose :security_reports_docs_path do |merge_request| help_page_path('user/application_security/index.md', anchor: 'view-security-scan-information-in-merge-requests') end diff --git a/app/serializers/prometheus_alert_entity.rb b/app/serializers/prometheus_alert_entity.rb index 92905d2b389..fb25889e4db 100644 --- a/app/serializers/prometheus_alert_entity.rb +++ b/app/serializers/prometheus_alert_entity.rb @@ -13,10 +13,6 @@ class PrometheusAlertEntity < Grape::Entity prometheus_alert.computed_operator end - expose :alert_path do |prometheus_alert| - project_prometheus_alert_path(prometheus_alert.project, prometheus_alert.prometheus_metric_id, environment_id: prometheus_alert.environment.id, format: :json) - end - private alias_method :prometheus_alert, :object diff --git a/app/serializers/service_event_entity.rb b/app/serializers/service_event_entity.rb deleted file mode 100644 index 49a4944b2b0..00000000000 --- a/app/serializers/service_event_entity.rb +++ /dev/null @@ -1,44 +0,0 @@ -# frozen_string_literal: true - -class ServiceEventEntity < Grape::Entity - include RequestAwareEntity - - expose :title do |event| - IntegrationsHelper.integration_event_title(event) - end - - expose :event_field_name, as: :name - - expose :value do |event| - integration[event_field_name] - end - - expose :description do |event| - IntegrationsHelper.integration_event_description(integration, event) - end - - expose :field, if: -> (_, _) { event_field } do - expose :name do |event| - event_field[:name] - end - expose :value do |event| - integration.public_send(event_field[:name]) # rubocop:disable GitlabSecurity/PublicSend - end - end - - private - - alias_method :event, :object - - def event_field_name - IntegrationsHelper.integration_event_field_name(event) - end - - def event_field - @event_field ||= integration.event_field(event) - end - - def integration - request.service - end -end diff --git a/app/serializers/service_event_serializer.rb b/app/serializers/service_event_serializer.rb deleted file mode 100644 index 7f5fe36e571..00000000000 --- a/app/serializers/service_event_serializer.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -class ServiceEventSerializer < BaseSerializer - entity ServiceEventEntity -end diff --git a/app/serializers/service_field_entity.rb b/app/serializers/service_field_entity.rb deleted file mode 100644 index b13f2c0e217..00000000000 --- a/app/serializers/service_field_entity.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -class ServiceFieldEntity < Grape::Entity - include RequestAwareEntity - include Gitlab::Utils::StrongMemoize - - expose :section, :type, :name, :placeholder, :required, :choices, :checkbox_label - - expose :title do |field| - non_empty_password?(field) ? field[:non_empty_password_title] : field[:title] - end - - expose :help do |field| - non_empty_password?(field) ? field[:non_empty_password_help] : field[:help] - end - - expose :value do |field| - value = value_for(field) - - if non_empty_password?(field) - 'true' - elsif field[:type] == 'checkbox' - ActiveRecord::Type::Boolean.new.deserialize(value).to_s - else - value - end - end - - private - - def service - request.service - end - - def value_for(field) - strong_memoize(:value_for) do - # field[:name] is not user input and so can assume is safe - service.public_send(field[:name]) # rubocop:disable GitlabSecurity/PublicSend - end - end - - def non_empty_password?(field) - strong_memoize(:non_empty_password) do - field[:type] == 'password' && value_for(field).present? - end - end -end diff --git a/app/serializers/service_field_serializer.rb b/app/serializers/service_field_serializer.rb deleted file mode 100644 index 120d0f5820e..00000000000 --- a/app/serializers/service_field_serializer.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -class ServiceFieldSerializer < BaseSerializer - entity ServiceFieldEntity -end |