Class: DiffNote

Inherits:
Note show all
Includes:
DiffPositionableNote, Gitlab::Utils::StrongMemoize, NoteOnDiff
Defined in:
app/models/diff_note.rb

Overview

A note on merge request or commit diffs

A note of this type can be resolvable.

Constant Summary collapse

NoteDiffFileCreationError =
Class.new(StandardError)
DIFF_LINE_NOT_FOUND_MESSAGE =
"Failed to find diff line for: %{file_path}, old_line: %{old_line}, new_line: %{new_line}"
DIFF_FILE_NOT_FOUND_MESSAGE =
"Failed to find diff file"

Constants inherited from Note

Note::ISSUE_TASK_SYSTEM_NOTE_PATTERN, Note::NON_DIFF_NOTE_TYPES, Note::TYPES_RESTRICTED_BY_GROUP_ABILITY, Note::TYPES_RESTRICTED_BY_PROJECT_ABILITY

Constants included from ThrottledTouch

ThrottledTouch::TOUCH_INTERVAL

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_TERM

Constants included from ResolvableNote

ResolvableNote::RESOLVABLE_TYPES

Constants included from CacheMarkdownField

CacheMarkdownField::INVALIDATED_BY

Constants included from Redactable

Redactable::UNSUBSCRIBE_PATTERN

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes inherited from Note

#command_names, #commands_changes, #redacted_note_html, #skip_keep_around_commits, #total_reference_count, #user_visible_reference_count

Attributes included from CacheMarkdownField

#skip_markdown_cache_validation

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DiffPositionableNote

#active?, #diff_refs_match_commit, #on_file?, #on_image?, #on_text?, #set_original_position, #should_update_position?, #update_position

Methods included from NoteOnDiff

#active?, #diff_attributes, #diff_note?

Methods inherited from Note

#active?, #attribute_names_for_serialization, #award_emoji?, #broadcast_noteable_notes_changed, #bump_updated_at, #can_be_award_emoji?, #can_be_discussion_note?, #can_create_todo?, #check_for_spam?, cherry_picked_merge_requests, #commit, #confidential?, #contains_emoji_only?, #contributor?, count_for_collection, #diff_note?, #discussion, #discussion_id, discussions, #editable?, #edited?, #emoji_awardable?, #exportable_record?, find_discussion, #for_alert_mangement_alert?, #for_commit?, #for_design?, #for_issuable?, #for_issue?, #for_merge_request?, #for_personal_snippet?, #for_project_noteable?, #for_project_snippet?, #for_snippet?, #for_vulnerability?, #for_work_item?, grouped_diff_discussions, #hook_attrs, #human_max_access, #in_reply_to?, #issuable_ability_name, #last_edited_at, #max_attachment_size, #mentioned_filtered_user_ids_for, #mentioned_users, #merge_requests, model_name, #note, #note_html, #noteable, #noteable_ability_name, #noteable_author?, #noteable_type=, #notify_after_create, #notify_after_destroy, #parent_user, #part_of_discussion?, positions, #post_processed_cache_key, #project_name, #references, #resource_parent, #retrieve_upload, search, #show_outdated_changes?, simple_sorts, #skip_notification?, #skip_project_check?, #start_of_discussion?, #system_note_visible_for?, #system_note_with_references?, #to_discussion, #touch, #touch_noteable, #trigger_note_subscription_create, #trigger_note_subscription_destroy, #trigger_note_subscription_update, #user_mention_class, #user_mention_identifier, #user_mentions, with_web_entity_associations

Methods included from Gitlab::Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Methods included from Spammable

#allow_possible_spam?, #check_for_spam, #check_for_spam?, #clear_spam_flags!, #invalidate_if_spam, #needs_recaptcha!, #recaptcha_error!, #render_recaptcha?, #spam, #spam!, #spam_description, #spam_title, #spammable_attribute_changed?, #spammable_entity_type, #spammable_text, #submittable_as_spam?, #submittable_as_spam_by?, #supports_recaptcha?, #unrecoverable_spam_error!

Methods included from ThrottledTouch

#touch

Methods included from Gitlab::SQL::Pattern

split_query_to_search_terms

Methods included from Editable

#edited?, #last_edited_by

Methods included from ResolvableNote

#potentially_resolvable?, #resolvable?, #resolve!, #resolve_without_save, #resolved?, #to_be_resolved?, #unresolve!, #unresolve_without_save

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from CacheMarkdownField

#attribute_invalidated?, #cached_html_for, #cached_html_up_to_date?, #can_cache_field?, #invalidated_markdown_cache?, #latest_cached_markdown_version, #local_version, #mentionable_attributes_changed?, #mentioned_filtered_user_ids_for, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #store_mentions!, #updated_cached_html_for

Methods included from FasterCacheKeys

#cache_key

Methods included from Awardable

#awarded_emoji?, #downvotes, #emoji_awardable?, #grouped_awards, #upvotes, #user_authored?, #user_can_award?

Methods included from Mentionable

#all_references, #create_cross_references!, #create_new_cross_references!, #directly_addressed_users, #extractors, #gfm_reference, #local_reference, #matches_cross_reference_regex?, #mentioned_users, #referenced_group_users, #referenced_groups, #referenced_mentionables, #referenced_project_users, #referenced_projects, #referenced_users, #user_mention_class, #user_mention_identifier

Methods included from Participable

#participant?, #participants, #visible_participants

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.noteable_typesObject



11
12
13
# File 'app/models/diff_note.rb', line 11

def self.noteable_types
  %w[MergeRequest Commit DesignManagement::Design]
end

Instance Method Details

#banzai_render_context(field) ⇒ Object



110
111
112
# File 'app/models/diff_note.rb', line 110

def banzai_render_context(field)
  super.merge(suggestions_filter_enabled: true)
end

#create_diff_fileObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/diff_note.rb', line 38

def create_diff_file
  return unless should_create_diff_file?

  diff_file = fetch_diff_file
  raise NoteDiffFileCreationError, DIFF_FILE_NOT_FOUND_MESSAGE unless diff_file

  diff_line = diff_file.line_for_position(self.original_position)
  unless diff_line
    raise NoteDiffFileCreationError, DIFF_LINE_NOT_FOUND_MESSAGE % {
        file_path: diff_file.file_path,
        old_line: original_position.old_line,
        new_line: original_position.new_line
    }
  end

  creation_params = diff_file.diff.to_hash
    .except(:too_large)
    .merge(diff: diff_file.diff_hunk(diff_line))

  create_note_diff_file(creation_params)
end

#created_at_diff?(diff_refs) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'app/models/diff_note.rb', line 90

def created_at_diff?(diff_refs)
  return false unless supported?
  return true if for_commit?

  self.original_position.diff_refs == diff_refs
end

#diff_fileObject

Returns the diff file from ‘original_position`



70
71
72
73
74
75
76
77
78
# File 'app/models/diff_note.rb', line 70

def diff_file
  strong_memoize(:diff_file) do
    next if for_design?

    enqueue_diff_file_creation_job if should_create_diff_file?

    fetch_diff_file
  end
end

#diff_lineObject



80
81
82
# File 'app/models/diff_note.rb', line 80

def diff_line
  @diff_line ||= diff_file&.line_for_position(self.original_position)
end

#discussion_classObject



34
35
36
# File 'app/models/diff_note.rb', line 34

def discussion_class(*)
  DiffDiscussion
end

#latest_diff_fileObject

Returns the diff file from ‘position`



61
62
63
64
65
66
67
# File 'app/models/diff_note.rb', line 61

def latest_diff_file
  strong_memoize(:latest_diff_file) do
    next if for_design?

    position.diff_file(repository)
  end
end

#multiline?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/diff_note.rb', line 114

def multiline?
  position&.multiline?
end

#original_line_codeObject



84
85
86
87
88
# File 'app/models/diff_note.rb', line 84

def original_line_code
  return unless on_text?

  self.diff_file.line_code(self.diff_line)
end

#shasObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/diff_note.rb', line 118

def shas
  [
    self.original_position.base_sha,
    self.original_position.start_sha,
    self.original_position.head_sha
  ].tap do |a|
    if self.position != self.original_position
      a << self.position.base_sha
      a << self.position.start_sha
      a << self.position.head_sha
    end
  end
end

#supports_suggestion?Boolean

Checks if the current ‘position` line in the diff exists and is suggestible (not a deletion).

Avoid using in iterations as it requests Gitaly.

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'app/models/diff_note.rb', line 101

def supports_suggestion?
  return false unless noteable&.supports_suggestion? && on_text?
  # We don't want to trigger side-effects of `diff_file` call.
  return false unless file = latest_diff_file
  return false unless line = file.line_for_position(self.position)

  line&.suggestible?
end