Class: Dependabot::PullRequestCreator::MessageBuilder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/pull_request_creator/message_builder.rb,
lib/dependabot/pull_request_creator/message_builder/issue_linker.rb,
lib/dependabot/pull_request_creator/message_builder/metadata_presenter.rb,
lib/dependabot/pull_request_creator/message_builder/link_and_mention_sanitizer.rb

Overview

MessageBuilder builds PR message for a dependency update

Defined Under Namespace

Classes: IssueLinker, LinkAndMentionSanitizer, MetadataPresenter

Constant Summary collapse

TRUNCATED_MSG =
"...\n\n_Description has been truncated_"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, dependencies:, files:, credentials:, pr_message_header: nil, pr_message_footer: nil, commit_message_options: {}, vulnerabilities_fixed: {}, github_redirection_service: DEFAULT_GITHUB_REDIRECTION_SERVICE, dependency_group: nil, pr_message_max_length: nil, pr_message_encoding: nil, ignore_conditions: [], notices: nil) ⇒ MessageBuilder

Returns a new instance of MessageBuilder.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 92

def initialize(source:, dependencies:, files:, credentials:,
               pr_message_header: nil, pr_message_footer: nil,
               commit_message_options: {}, vulnerabilities_fixed: {},
               github_redirection_service: DEFAULT_GITHUB_REDIRECTION_SERVICE,
               dependency_group: nil, pr_message_max_length: nil, pr_message_encoding: nil,
               ignore_conditions: [], notices: nil)
  @dependencies               = dependencies
  @files                      = files
  @source                     = source
  @credentials                = credentials
  @pr_message_header          = pr_message_header
  @pr_message_footer          = pr_message_footer
  @commit_message_options     = commit_message_options
  @vulnerabilities_fixed      = vulnerabilities_fixed
  @github_redirection_service = github_redirection_service
  @dependency_group           = dependency_group
  @pr_message_max_length      = pr_message_max_length
  @pr_message_encoding        = pr_message_encoding
  @ignore_conditions          = ignore_conditions
  @notices                    = notices
end

Instance Attribute Details

#commit_message_optionsObject (readonly)

Returns the value of attribute commit_message_options.



48
49
50
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 48

def commit_message_options
  @commit_message_options
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



39
40
41
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 39

def credentials
  @credentials
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



33
34
35
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 33

def dependencies
  @dependencies
end

#dependency_groupObject (readonly)

Returns the value of attribute dependency_group.



57
58
59
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 57

def dependency_group
  @dependency_group
end

#filesObject (readonly)

Returns the value of attribute files.



36
37
38
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 36

def files
  @files
end

#github_redirection_serviceObject (readonly)

Returns the value of attribute github_redirection_service.



54
55
56
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 54

def github_redirection_service
  @github_redirection_service
end

#ignore_conditionsObject (readonly)

Returns the value of attribute ignore_conditions.



66
67
68
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 66

def ignore_conditions
  @ignore_conditions
end

#noticesObject (readonly)

Returns the value of attribute notices.



69
70
71
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 69

def notices
  @notices
end

#pr_message_encodingObject

Returns the value of attribute pr_message_encoding.



63
64
65
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 63

def pr_message_encoding
  @pr_message_encoding
end

Returns the value of attribute pr_message_footer.



45
46
47
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 45

def pr_message_footer
  @pr_message_footer
end

#pr_message_headerObject (readonly)

Returns the value of attribute pr_message_header.



42
43
44
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 42

def pr_message_header
  @pr_message_header
end

#pr_message_max_lengthObject

Returns the value of attribute pr_message_max_length.



60
61
62
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 60

def pr_message_max_length
  @pr_message_max_length
end

#sourceObject (readonly)

Returns the value of attribute source.



30
31
32
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 30

def source
  @source
end

#vulnerabilities_fixedObject (readonly)

Returns the value of attribute vulnerabilities_fixed.



51
52
53
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 51

def vulnerabilities_fixed
  @vulnerabilities_fixed
end

Instance Method Details

#commit_messageObject



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 177

def commit_message
  message = commit_subject + "\n\n"
  message += commit_message_intro
  message += 
  message += "\n\n" + T.must(message_trailers) if message_trailers
  message
rescue StandardError => e
  suppress_error("commit message", e)
  message = commit_subject
  message += "\n\n" + T.must(message_trailers) if message_trailers
  message
end

#messageObject



191
192
193
194
195
196
197
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 191

def message
  Dependabot::PullRequestCreator::Message.new(
    pr_name: pr_name,
    pr_message: pr_message,
    commit_message: commit_message
  )
end

#pr_messageObject



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 128

def pr_message
  msg = "#{pr_notices}" \
        "#{suffixed_pr_message_header}" \
        "#{commit_message_intro}" \
        "#{}" \
        "#{ignore_conditions_table}" \
        "#{prefixed_pr_message_footer}"

  truncate_pr_message(msg)
rescue StandardError => e
  suppress_error("PR message", e)
  suffixed_pr_message_header + prefixed_pr_message_footer
end

#pr_nameObject



121
122
123
124
125
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 121

def pr_name
  name = dependency_group ? group_pr_name : solo_pr_name
  name[0] = T.must(name[0]).capitalize if pr_name_prefixer.capitalize_first_word?
  "#{pr_name_prefix}#{name}"
end

#pr_noticesObject



143
144
145
146
147
148
149
150
151
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 143

def pr_notices
  notices = @notices || []
  unique_messages = notices.filter_map do |notice|
    Dependabot::Notice.markdown_from_description(notice) if notice.show_in_pr
  end.uniq

  message = unique_messages.join("\n\n")
  message.empty? ? nil : message
end

#truncate_pr_message(msg) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/dependabot/pull_request_creator/message_builder.rb', line 156

def truncate_pr_message(msg)
  return msg if pr_message_max_length.nil?

  msg = msg.dup
  msg = msg.force_encoding(T.must(pr_message_encoding)) unless pr_message_encoding.nil?

  if msg.length > T.must(pr_message_max_length)
    tr_msg = if pr_message_encoding.nil?
               TRUNCATED_MSG
             else
               (+TRUNCATED_MSG).dup.force_encoding(T.must(pr_message_encoding))
             end
    trunc_length = T.must(pr_message_max_length) - tr_msg.length
    msg = (T.must(msg[0..trunc_length]) + tr_msg)
  end
  # if we used a custom encoding for calculating length, then we need to force back to UTF-8
  msg = msg.encode("utf-8", "binary", invalid: :replace, undef: :replace) unless pr_message_encoding.nil?
  msg
end