Class: Dependabot::Notice

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/notices.rb

Defined Under Namespace

Modules: NoticeMode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode:, type:, package_manager_name:, title: "", description: "", show_in_pr: false, show_alert: false) ⇒ Notice

Returns a new instance of Notice.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dependabot/notices.rb', line 42

def initialize(
  mode:, type:, package_manager_name:,
  title: "", description: "",
  show_in_pr: false, show_alert: false
)
  @mode = mode
  @type = type
  @package_manager_name = package_manager_name
  @title = title
  @description = description
  @show_in_pr = show_in_pr
  @show_alert = show_alert
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



18
19
20
# File 'lib/dependabot/notices.rb', line 18

def description
  @description
end

#modeObject (readonly)

Returns the value of attribute mode.



18
19
20
# File 'lib/dependabot/notices.rb', line 18

def mode
  @mode
end

#package_manager_nameObject (readonly)

Returns the value of attribute package_manager_name.



18
19
20
# File 'lib/dependabot/notices.rb', line 18

def package_manager_name
  @package_manager_name
end

#show_alertObject (readonly)

Returns the value of attribute show_alert.



21
22
23
# File 'lib/dependabot/notices.rb', line 21

def show_alert
  @show_alert
end

#show_in_prObject (readonly)

Returns the value of attribute show_in_pr.



21
22
23
# File 'lib/dependabot/notices.rb', line 21

def show_in_pr
  @show_in_pr
end

#titleObject (readonly)

Returns the value of attribute title.



18
19
20
# File 'lib/dependabot/notices.rb', line 18

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/dependabot/notices.rb', line 18

def type
  @type
end

Class Method Details

.generate_pm_deprecation_notice(package_manager) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dependabot/notices.rb', line 105

def self.generate_pm_deprecation_notice(package_manager)
  return nil unless package_manager.deprecated?

  mode = NoticeMode::WARN
  supported_versions_description = generate_supported_versions_description(
    package_manager.supported_versions,
    package_manager.support_later_versions?
  )
  notice_type = "#{package_manager.name}_deprecated_warn"
  title = "Package manager deprecation notice"
  description = "Dependabot will stop supporting `#{package_manager.name} v#{package_manager.version}`!"

  ## Add the supported versions to the description
  description += "\n\n#{supported_versions_description}\n" unless supported_versions_description.empty?

  Notice.new(
    mode: mode,
    type: notice_type,
    package_manager_name: package_manager.name,
    title: title,
    description: description,
    show_in_pr: true,
    show_alert: true
  )
end

.generate_supported_versions_description(supported_versions, support_later_versions) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dependabot/notices.rb', line 81

def self.generate_supported_versions_description(supported_versions, support_later_versions)
  return "Please upgrade your package manager version" unless supported_versions&.any?

  versions_string = supported_versions.map { |version| "`v#{version}`" }

  versions_string[-1] = "or #{versions_string[-1]}" if versions_string.count > 1 && !support_later_versions

  versions_string = versions_string.join(", ")

  later_description = support_later_versions ? ", or later" : ""

  return "Please upgrade to version #{versions_string}#{later_description}." if supported_versions.count == 1

  "Please upgrade to one of the following versions: #{versions_string}#{later_description}."
end

.markdown_from_description(notice) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dependabot/notices.rb', line 132

def self.markdown_from_description(notice)
  description = notice.description

  return if description.empty?

  markdown = "> [!#{markdown_mode(notice.mode)}]\n"
  # Log each non-empty line of the deprecation notice description
  description.each_line do |line|
    line = line.strip
    markdown += "> #{line}\n"
  end
  markdown += ">\n\n"
  markdown
end

.markdown_mode(mode) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dependabot/notices.rb', line 148

def self.markdown_mode(mode)
  case mode
  when NoticeMode::INFO
    "INFO"
  when NoticeMode::WARN
    "WARNING"
  when NoticeMode::ERROR
    "IMPORTANT"
  else
    "INFO"
  end
end

Instance Method Details

#to_hashObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dependabot/notices.rb', line 59

def to_hash
  {
    mode: @mode,
    type: @type,
    package_manager_name: @package_manager_name,
    title: @title,
    description: @description,
    show_in_pr: @show_in_pr,
    show_alert: @show_alert
  }
end