Class: Gitlab::GithubImport::Representation::DiffNotes::SuggestionFormatter

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/github_import/representation/diff_notes/suggestion_formatter.rb

Constant Summary collapse

GITHUB_SUGGESTION =

A github suggestion:

  • the “‘suggestion tag must be the first text of the line

    • it might have up to 3 spaces before the “‘suggestion tag

  • extra text on the “‘suggestion tag line will be ignored

/^\ {,3}(?<suggestion>```suggestion\b).*(?<eol>\R)/

Instance Method Summary collapse

Constructor Details

#initialize(note:, start_line: nil, end_line: nil) ⇒ SuggestionFormatter

Returns a new instance of SuggestionFormatter.



21
22
23
24
25
# File 'lib/gitlab/github_import/representation/diff_notes/suggestion_formatter.rb', line 21

def initialize(note:, start_line: nil, end_line: nil)
  @note = note
  @start_line = start_line
  @end_line = end_line
end

Instance Method Details

#contains_suggestion?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/gitlab/github_import/representation/diff_notes/suggestion_formatter.rb', line 42

def contains_suggestion?
  strong_memoize(:contain_suggestion) do
    note.to_s.match?(GITHUB_SUGGESTION)
  end
end

#formatted_noteObject

Returns a tuple with:

- a boolean indicating if the note has suggestions
- the note with the suggestion formatted for Gitlab


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/github_import/representation/diff_notes/suggestion_formatter.rb', line 30

def formatted_note
  @formatted_note ||=
    if contains_suggestion?
      note.gsub(
        GITHUB_SUGGESTION,
        "\\k<suggestion>:#{suggestion_range}\\k<eol>"
      )
    else
      note
    end
end