diff options
Diffstat (limited to 'app/serializers')
-rw-r--r-- | app/serializers/base_discussion_entity.rb | 55 | ||||
-rw-r--r-- | app/serializers/diff_file_base_entity.rb | 2 | ||||
-rw-r--r-- | app/serializers/diff_file_entity.rb | 16 | ||||
-rw-r--r-- | app/serializers/diffs_entity.rb | 6 | ||||
-rw-r--r-- | app/serializers/discussion_entity.rb | 48 | ||||
-rw-r--r-- | app/serializers/environment_entity.rb | 5 | ||||
-rw-r--r-- | app/serializers/merge_request_widget_entity.rb | 17 | ||||
-rw-r--r-- | app/serializers/move_to_project_entity.rb | 1 | ||||
-rw-r--r-- | app/serializers/note_entity.rb | 18 | ||||
-rw-r--r-- | app/serializers/paginated_diff_entity.rb | 15 | ||||
-rw-r--r-- | app/serializers/test_case_entity.rb | 1 | ||||
-rw-r--r-- | app/serializers/test_suite_comparer_entity.rb | 27 |
12 files changed, 121 insertions, 90 deletions
diff --git a/app/serializers/base_discussion_entity.rb b/app/serializers/base_discussion_entity.rb new file mode 100644 index 00000000000..5ca4d1d6cc9 --- /dev/null +++ b/app/serializers/base_discussion_entity.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +class BaseDiscussionEntity < Grape::Entity + include RequestAwareEntity + include NotesHelper + + expose :id + expose :reply_id + expose :project_id + expose :commit_id + + expose :confidential?, as: :confidential + expose :diff_discussion?, as: :diff_discussion + expose :expanded?, as: :expanded + expose :for_commit?, as: :for_commit + expose :individual_note?, as: :individual_note + expose :resolvable?, as: :resolvable + + expose :truncated_diff_lines, using: DiffLineEntity, if: -> (d, _) { d.diff_discussion? && d.on_text? && (d.expanded? || render_truncated_diff_lines?) } + + with_options if: -> (d, _) { d.diff_discussion? } do + expose :active?, as: :active + expose :line_code + expose :diff_file, using: DiscussionDiffFileEntity + end + + with_options if: -> (d, _) { d.diff_discussion? && !d.legacy_diff_discussion? } do + expose :position + expose :original_position + end + + expose :discussion_path do |discussion| + discussion_path(discussion) + end + + with_options if: -> (d, _) { d.resolvable? } do + expose :resolve_path do |discussion| + resolve_project_merge_request_discussion_path(discussion.project, discussion.noteable, discussion.id) + end + + expose :resolve_with_issue_path do |discussion| + new_project_issue_path(discussion.project, merge_request_to_resolve_discussions_of: discussion.noteable.iid, discussion_to_resolve: discussion.id) + end + end + + expose :truncated_diff_lines_path, if: -> (d, _) { !d.expanded? && !render_truncated_diff_lines? } do |discussion| + project_merge_request_discussion_path(discussion.project, discussion.noteable, discussion) + end + + private + + def render_truncated_diff_lines? + options.fetch(:render_truncated_diff_lines, false) + end +end diff --git a/app/serializers/diff_file_base_entity.rb b/app/serializers/diff_file_base_entity.rb index 596f5d686da..5036f28184c 100644 --- a/app/serializers/diff_file_base_entity.rb +++ b/app/serializers/diff_file_base_entity.rb @@ -48,7 +48,7 @@ class DiffFileBaseEntity < Grape::Entity next unless has_edit_path?(merge_request) - gitlab_ide_merge_request_path(merge_request) + ide_merge_request_path(merge_request, diff_file.new_path) end expose :old_path_html do |diff_file| diff --git a/app/serializers/diff_file_entity.rb b/app/serializers/diff_file_entity.rb index e3fefbb46b6..9865af1e116 100644 --- a/app/serializers/diff_file_entity.rb +++ b/app/serializers/diff_file_entity.rb @@ -3,6 +3,7 @@ class DiffFileEntity < DiffFileBaseEntity include CommitsHelper include IconsHelper + include Gitlab::Utils::StrongMemoize expose :added_lines expose :removed_lines @@ -54,11 +55,16 @@ class DiffFileEntity < DiffFileBaseEntity # Used for inline diffs expose :highlighted_diff_lines, using: DiffLineEntity, if: -> (diff_file, options) { inline_diff_view?(options, diff_file) && diff_file.text? } do |diff_file| - diff_file.diff_lines_for_serializer + file = conflict_file(options, diff_file) || diff_file + file.diff_lines_for_serializer end expose :is_fully_expanded do |diff_file| - diff_file.fully_expanded? + if conflict_file(options, diff_file) + false + else + diff_file.fully_expanded? + end end # Used for parallel diffs @@ -79,4 +85,10 @@ class DiffFileEntity < DiffFileBaseEntity # If nothing is present, inline will be the default. options.fetch(:diff_view, :inline).to_sym == :inline end + + def conflict_file(options, diff_file) + strong_memoize(:conflict_file) do + options[:conflicts] && options[:conflicts][diff_file.new_path] + end + end end diff --git a/app/serializers/diffs_entity.rb b/app/serializers/diffs_entity.rb index 0b4f21c55f4..f573bbe8385 100644 --- a/app/serializers/diffs_entity.rb +++ b/app/serializers/diffs_entity.rb @@ -71,7 +71,7 @@ class DiffsEntity < Grape::Entity submodule_links = Gitlab::SubmoduleLinks.new(merge_request.project.repository) DiffFileEntity.represent(diffs.diff_files, - options.merge(submodule_links: submodule_links, code_navigation_path: code_navigation_path(diffs))) + options.merge(submodule_links: submodule_links, code_navigation_path: code_navigation_path(diffs), conflicts: conflicts)) end expose :merge_request_diffs, using: MergeRequestDiffEntity, if: -> (_, options) { options[:merge_request_diffs]&.any? } do |diffs| @@ -88,10 +88,6 @@ class DiffsEntity < Grape::Entity private - def code_navigation_path(diffs) - Gitlab::CodeNavigationPath.new(merge_request.project, diffs.diff_refs&.head_sha) - end - def commit_ids @commit_ids ||= merge_request.recent_commits.map(&:id) end diff --git a/app/serializers/discussion_entity.rb b/app/serializers/discussion_entity.rb index 497471699b2..bcf6b331192 100644 --- a/app/serializers/discussion_entity.rb +++ b/app/serializers/discussion_entity.rb @@ -1,23 +1,8 @@ # frozen_string_literal: true -class DiscussionEntity < Grape::Entity - include RequestAwareEntity - include NotesHelper - - expose :id, :reply_id - expose :position, if: -> (d, _) { d.diff_discussion? && !d.legacy_diff_discussion? } - expose :original_position, if: -> (d, _) { d.diff_discussion? && !d.legacy_diff_discussion? } - expose :line_code, if: -> (d, _) { d.diff_discussion? } - expose :expanded?, as: :expanded - expose :active?, as: :active, if: -> (d, _) { d.diff_discussion? } - expose :project_id - +class DiscussionEntity < BaseDiscussionEntity expose :notes do |discussion, opts| - request.note_entity.represent(discussion.notes, opts) - end - - expose :discussion_path do |discussion| - discussion_path(discussion) + request.note_entity.represent(discussion.notes, opts.merge(with_base_discussion: false)) end expose :positions, if: -> (d, _) { display_merge_ref_discussions?(d) } do |discussion| @@ -28,42 +13,13 @@ class DiscussionEntity < Grape::Entity discussion.diff_note_positions.map(&:line_code) end - expose :individual_note?, as: :individual_note - expose :resolvable do |discussion| - discussion.resolvable? - end - expose :resolved?, as: :resolved expose :resolved_by_push?, as: :resolved_by_push expose :resolved_by, using: NoteUserEntity expose :resolved_at - expose :resolve_path, if: -> (d, _) { d.resolvable? } do |discussion| - resolve_project_merge_request_discussion_path(discussion.project, discussion.noteable, discussion.id) - end - expose :resolve_with_issue_path, if: -> (d, _) { d.resolvable? } do |discussion| - new_project_issue_path(discussion.project, merge_request_to_resolve_discussions_of: discussion.noteable.iid, discussion_to_resolve: discussion.id) - end - - expose :diff_file, using: DiscussionDiffFileEntity, if: -> (d, _) { d.diff_discussion? } - - expose :diff_discussion?, as: :diff_discussion - - expose :truncated_diff_lines_path, if: -> (d, _) { !d.expanded? && !render_truncated_diff_lines? } do |discussion| - project_merge_request_discussion_path(discussion.project, discussion.noteable, discussion) - end - - expose :truncated_diff_lines, using: DiffLineEntity, if: -> (d, _) { d.diff_discussion? && d.on_text? && (d.expanded? || render_truncated_diff_lines?) } - - expose :for_commit?, as: :for_commit - expose :commit_id - expose :confidential?, as: :confidential private - def render_truncated_diff_lines? - options[:render_truncated_diff_lines] - end - def current_user request.current_user end diff --git a/app/serializers/environment_entity.rb b/app/serializers/environment_entity.rb index 7da5910a75b..0bd9c602bf5 100644 --- a/app/serializers/environment_entity.rb +++ b/app/serializers/environment_entity.rb @@ -4,6 +4,11 @@ class EnvironmentEntity < Grape::Entity include RequestAwareEntity expose :id + + expose :global_id do |environment| + environment.to_global_id.to_s + end + expose :name expose :state expose :external_url diff --git a/app/serializers/merge_request_widget_entity.rb b/app/serializers/merge_request_widget_entity.rb index 44cbcfc5044..e46b269ea35 100644 --- a/app/serializers/merge_request_widget_entity.rb +++ b/app/serializers/merge_request_widget_entity.rb @@ -67,15 +67,15 @@ class MergeRequestWidgetEntity < Grape::Entity ) end - expose :user_callouts_path, if: -> (_, opts) { opts[:experiment_enabled] == :suggest_pipeline } do |_merge_request| + expose :user_callouts_path, if: -> (*) { Feature.enabled?(:suggest_pipeline, default_enabled: true) } do |_merge_request| user_callouts_path end - expose :suggest_pipeline_feature_id, if: -> (_, opts) { opts[:experiment_enabled] == :suggest_pipeline } do |_merge_request| + expose :suggest_pipeline_feature_id, if: -> (*) { Feature.enabled?(:suggest_pipeline, default_enabled: true) } do |_merge_request| SUGGEST_PIPELINE end - expose :is_dismissed_suggest_pipeline, if: -> (_, opts) { opts[:experiment_enabled] == :suggest_pipeline } do |_merge_request| + expose :is_dismissed_suggest_pipeline, if: -> (*) { Feature.enabled?(:suggest_pipeline, default_enabled: true) } do |_merge_request| current_user && current_user.dismissed_callout?(feature_name: SUGGEST_PIPELINE) end @@ -129,7 +129,7 @@ class MergeRequestWidgetEntity < Grape::Entity end expose :security_reports_docs_path do |merge_request| - help_page_path('user/application_security/sast/index.md', anchor: 'reports-json-format') + help_page_path('user/application_security/index.md', anchor: 'viewing-security-scan-information-in-merge-requests') end private @@ -151,6 +151,10 @@ class MergeRequestWidgetEntity < Grape::Entity can?(current_user, :create_pipeline, merge_request.source_project) end + def use_merge_base_with_merged_results? + object.actual_head_pipeline&.merge_request_event_type == :merged_result + end + def head_pipeline_downloadable_path_for_report_type(file_type) object.head_pipeline&.present(current_user: current_user) &.downloadable_path_for_report_type(file_type) @@ -161,11 +165,6 @@ class MergeRequestWidgetEntity < Grape::Entity &.downloadable_path_for_report_type(file_type) end - def use_merge_base_with_merged_results? - Feature.enabled?(:merge_base_pipelines, object.target_project) && - object.actual_head_pipeline&.merge_request_event_type == :merged_result - end - def merge_base_pipeline_downloadable_path_for_report_type(file_type) object.merge_base_pipeline&.present(current_user: current_user) &.downloadable_path_for_report_type(file_type) diff --git a/app/serializers/move_to_project_entity.rb b/app/serializers/move_to_project_entity.rb index dac1124b0b3..fb1d1a64abd 100644 --- a/app/serializers/move_to_project_entity.rb +++ b/app/serializers/move_to_project_entity.rb @@ -3,4 +3,5 @@ class MoveToProjectEntity < Grape::Entity expose :id expose :name_with_namespace + expose :full_path end diff --git a/app/serializers/note_entity.rb b/app/serializers/note_entity.rb index ef305195e22..9a96778786b 100644 --- a/app/serializers/note_entity.rb +++ b/app/serializers/note_entity.rb @@ -34,6 +34,10 @@ class NoteEntity < API::Entities::Note expose :can_resolve do |note| note.resolvable? && can?(current_user, :resolve_note, note) end + + expose :can_resolve_discussion do |note| + note.discussion.resolvable? && note.discussion.can_resolve?(current_user) + end end expose :suggestions, using: SuggestionEntity @@ -77,11 +81,25 @@ class NoteEntity < API::Entities::Note expose :cached_markdown_version + # Correctly rendering a note requires some background information about any + # discussion it is part of. This is essential for the notes endpoint, but + # optional for the discussions endpoint, which will include the discussion + # along with the note + expose :discussion, as: :base_discussion, using: BaseDiscussionEntity, if: -> (_, _) { with_base_discussion? } + private + def discussion + @discussion ||= object.to_discussion(request.noteable) + end + def current_user request.current_user end + + def with_base_discussion? + options.fetch(:with_base_discussion, true) + end end NoteEntity.prepend_if_ee('EE::NoteEntity') diff --git a/app/serializers/paginated_diff_entity.rb b/app/serializers/paginated_diff_entity.rb index f24571f7d7d..fe59686278c 100644 --- a/app/serializers/paginated_diff_entity.rb +++ b/app/serializers/paginated_diff_entity.rb @@ -7,12 +7,19 @@ # class PaginatedDiffEntity < Grape::Entity include RequestAwareEntity + include DiffHelper expose :diff_files do |diffs, options| submodule_links = Gitlab::SubmoduleLinks.new(merge_request.project.repository) - DiffFileEntity.represent(diffs.diff_files, - options.merge(submodule_links: submodule_links, code_navigation_path: code_navigation_path(diffs))) + DiffFileEntity.represent( + diffs.diff_files, + options.merge( + submodule_links: submodule_links, + code_navigation_path: code_navigation_path(diffs), + conflicts: conflicts + ) + ) end expose :pagination do @@ -36,10 +43,6 @@ class PaginatedDiffEntity < Grape::Entity private - def code_navigation_path(diffs) - Gitlab::CodeNavigationPath.new(merge_request.project, diffs.diff_refs&.head_sha) - end - %i[current_page next_page total_pages].each do |method| define_method method do pagination_data[method] diff --git a/app/serializers/test_case_entity.rb b/app/serializers/test_case_entity.rb index b44aa62ad73..299160cd1bf 100644 --- a/app/serializers/test_case_entity.rb +++ b/app/serializers/test_case_entity.rb @@ -10,6 +10,7 @@ class TestCaseEntity < Grape::Entity expose :execution_time expose :system_output expose :stack_trace + expose :recent_failures expose :attachment_url, if: -> (*) { can_read_screenshots? } do |test_case| expose_url(test_case.attachment_url) end diff --git a/app/serializers/test_suite_comparer_entity.rb b/app/serializers/test_suite_comparer_entity.rb index a9f19564b60..aab805f9598 100644 --- a/app/serializers/test_suite_comparer_entity.rb +++ b/app/serializers/test_suite_comparer_entity.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true class TestSuiteComparerEntity < Grape::Entity - DEFAULT_MAX_TESTS = 100 - DEFAULT_MIN_TESTS = 10 - expose :name expose :total_status, as: :status @@ -14,39 +11,27 @@ class TestSuiteComparerEntity < Grape::Entity expose :error_count, as: :errored end - # rubocop: disable CodeReuse/ActiveRecord expose :new_failures, using: TestCaseEntity do |suite| - suite.new_failures.take(max_tests) + suite.limited_tests.new_failures end expose :existing_failures, using: TestCaseEntity do |suite| - suite.existing_failures.take( - max_tests(suite.new_failures)) + suite.limited_tests.existing_failures end expose :resolved_failures, using: TestCaseEntity do |suite| - suite.resolved_failures.take( - max_tests(suite.new_failures, suite.existing_failures)) + suite.limited_tests.resolved_failures end expose :new_errors, using: TestCaseEntity do |suite| - suite.new_errors.take(max_tests) + suite.limited_tests.new_errors end expose :existing_errors, using: TestCaseEntity do |suite| - suite.existing_errors.take( - max_tests(suite.new_errors)) + suite.limited_tests.existing_errors end expose :resolved_errors, using: TestCaseEntity do |suite| - suite.resolved_errors.take( - max_tests(suite.new_errors, suite.existing_errors)) - end - - private - - def max_tests(*used) - [DEFAULT_MAX_TESTS - used.map(&:count).sum, DEFAULT_MIN_TESTS].max + suite.limited_tests.resolved_errors end - # rubocop: enable CodeReuse/ActiveRecord end |