Skip to content

Commit

Permalink
Implement Logging for Bundler v1 Deprecation Warnings (#10466)
Browse files Browse the repository at this point in the history
* replaced warning `message` as `description` to reduce confusion logged deprecation warning.
* moved the warning to the top of the job
* remove the markdown field, and created generation markdown method.
* moved deprecation notice generation in the dependency_snapshot
  • Loading branch information
kbukum1 authored Aug 26, 2024
1 parent 007f69e commit be07237
Show file tree
Hide file tree
Showing 18 changed files with 367 additions and 249 deletions.
140 changes: 91 additions & 49 deletions common/lib/dependabot/notices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,51 @@

module Dependabot
class Notice
module NoticeMode
INFO = "INFO"
WARN = "WARN"
ERROR = "ERROR"
end

extend T::Sig

sig { returns(String) }
attr_reader :mode, :type, :package_manager_name, :message, :markdown
attr_reader :mode, :type, :package_manager_name, :title, :description

sig { returns(T::Boolean) }
attr_reader :show_in_pr, :show_alert

# Initializes a new Notice object.
# @param mode [String] The mode of the notice (e.g., "WARN", "ERROR").
# @param type [String] The type of the notice (e.g., "bundler_deprecated_warn").
# @param package_manager_name [String] The name of the package manager (e.g., "bundler").
# @param message [String] The main message of the notice.
# @param markdown [String] The markdown formatted message.
# @param title [String] The title of the notice.
# @param description [String] The main description of the notice.
# @param show_in_pr [Boolean] Whether the notice should be shown in a pull request.
# @param show_alert [Boolean] Whether the notice should be shown in alerts.
sig do
params(
mode: String,
type: String,
package_manager_name: String,
message: String,
markdown: String
title: String,
description: String,
show_in_pr: T::Boolean,
show_alert: T::Boolean
).void
end
def initialize(mode:, type:, package_manager_name:, message: "", markdown: "")
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
@message = message
@markdown = markdown
@title = title
@description = description
@show_in_pr = show_in_pr
@show_alert = show_alert
end

# Converts the Notice object to a hash.
Expand All @@ -42,35 +61,37 @@ def to_hash
mode: @mode,
type: @type,
package_manager_name: @package_manager_name,
message: @message,
markdown: @markdown
title: @title,
description: @description,
show_in_pr: @show_in_pr,
show_alert: @show_alert
}
end

# Generates a message for supported versions.
# Generates a description for supported versions.
# @param supported_versions [Array<Dependabot::Version>, nil] The supported versions of the package manager.
# @param support_later_versions [Boolean] Whether later versions are supported.
# @return [String, nil] The generated message or nil if no supported versions are provided.
# @return [String, nil] The generated description or nil if no supported versions are provided.
sig do
params(
supported_versions: T.nilable(T::Array[Dependabot::Version]),
support_later_versions: T::Boolean
).returns(String)
end
def self.generate_supported_versions_message(supported_versions, support_later_versions)
return "" unless supported_versions&.any?
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_message = support_later_versions ? ", or later" : ""
later_description = support_later_versions ? ", or later" : ""

return "Please upgrade to version #{versions_string}#{later_message}." if supported_versions.count == 1
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_message}."
"Please upgrade to one of the following versions: #{versions_string}#{later_description}."
end

# Generates a support notice for the given package manager.
Expand Down Expand Up @@ -100,30 +121,26 @@ def self.generate_support_notice(package_manager)
def self.generate_pm_deprecation_notice(package_manager)
return nil unless package_manager.deprecated?

mode = "WARN"
supported_versions_message = generate_supported_versions_message(
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_#{mode.downcase}"
message = "Dependabot will stop supporting `#{package_manager.name} v#{package_manager.version}`!"
## Create a warning markdown message
markdown = "> [!WARNING]\n"
## Add the deprecation warning to the message
markdown += "> #{message}\n>\n"

## Add the supported versions to the message
unless supported_versions_message.empty?
message += "\n#{supported_versions_message}\n"
markdown += "> #{supported_versions_message}\n>\n"
end
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,
message: message,
markdown: markdown
title: title,
description: description,
show_in_pr: true,
show_alert: true
)
end

Expand All @@ -138,31 +155,56 @@ def self.generate_pm_deprecation_notice(package_manager)
def self.generate_pm_unsupported_notice(package_manager)
return nil unless package_manager.unsupported?

mode = "ERROR"
supported_versions_message = generate_supported_versions_message(
mode = NoticeMode::ERROR
supported_versions_description = generate_supported_versions_description(
package_manager.supported_versions,
package_manager.support_later_versions?
)
notice_type = "#{package_manager.name}_unsupported_#{mode.downcase}"
message = "Dependabot no longer supports `#{package_manager.name} v#{package_manager.version}`!"
## Create an error markdown message
markdown = "> [!IMPORTANT]\n"
## Add the error message to the message
markdown += "> #{message}\n>\n"

## Add the supported versions to the message
unless supported_versions_message.empty?
message += "\n#{supported_versions_message}\n"
markdown += "> #{supported_versions_message}\n>\n"
end
notice_type = "#{package_manager.name}_unsupported_error"
title = "Package manager unsupported notice"
description = "Dependabot no longer supports `#{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,
message: message,
markdown: markdown
title: title,
description: description,
show_in_pr: true,
show_alert: true
)
end

sig { params(notice: Notice).returns(T.nilable(String)) }
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
end

sig { params(mode: String).returns(String) }
def self.markdown_mode(mode)
case mode
when NoticeMode::INFO
"INFO"
when NoticeMode::WARN
"WARNING"
when NoticeMode::ERROR
"IMPORTANT"
else
"INFO"
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def pr_message
def pr_notices
notices = @notices || []
unique_messages = notices.filter_map do |notice|
markdown = notice.markdown if notice
markdown unless markdown.empty?
Dependabot::Notice.markdown_from_description(notice) if notice.show_in_pr
end.uniq

message = unique_messages.join("\n\n")
Expand Down
62 changes: 33 additions & 29 deletions common/spec/dependabot/notices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
end

RSpec.describe Dependabot::Notice do
describe ".generate_supported_versions_message" do
subject(:generate_supported_versions_message) do
described_class.generate_supported_versions_message(supported_versions, support_later_versions)
describe ".generate_supported_versions_description" do
subject(:generate_supported_versions_description) do
described_class.generate_supported_versions_description(supported_versions, support_later_versions)
end

context "when supported_versions has one version" do
let(:supported_versions) { [Dependabot::Version.new("2")] }
let(:support_later_versions) { false }

it "returns the correct message" do
expect(generate_supported_versions_message)
it "returns the correct description" do
expect(generate_supported_versions_description)
.to eq("Please upgrade to version `v2`.")
end
end
Expand All @@ -45,8 +45,8 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
let(:supported_versions) { [Dependabot::Version.new("2")] }
let(:support_later_versions) { true }

it "returns the correct message" do
expect(generate_supported_versions_message)
it "returns the correct description" do
expect(generate_supported_versions_description)
.to eq("Please upgrade to version `v2`, or later.")
end
end
Expand All @@ -58,8 +58,8 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
end
let(:support_later_versions) { false }

it "returns the correct message" do
expect(generate_supported_versions_message)
it "returns the correct description" do
expect(generate_supported_versions_description)
.to eq("Please upgrade to one of the following versions: `v2`, `v3`, or `v4`.")
end
end
Expand All @@ -71,8 +71,8 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
end
let(:support_later_versions) { true }

it "returns the correct message" do
expect(generate_supported_versions_message)
it "returns the correct description" do
expect(generate_supported_versions_description)
.to eq("Please upgrade to one of the following versions: `v2`, `v3`, `v4`, or later.")
end
end
Expand All @@ -82,7 +82,7 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
let(:support_later_versions) { false }

it "returns empty string" do
expect(generate_supported_versions_message).to eq("")
expect(generate_supported_versions_description).to eq("Please upgrade your package manager version")
end
end

Expand All @@ -91,7 +91,7 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
let(:support_later_versions) { false }

it "returns nil" do
expect(generate_supported_versions_message).to eq("")
expect(generate_supported_versions_description).to eq("Please upgrade your package manager version")
end
end
end
Expand Down Expand Up @@ -124,10 +124,11 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
mode: "WARN",
type: "bundler_deprecated_warn",
package_manager_name: "bundler",
message: "Dependabot will stop supporting `bundler v1`!\n" \
"Please upgrade to one of the following versions: `v2`, or `v3`.\n",
markdown: "> [!WARNING]\n> Dependabot will stop supporting `bundler v1`!\n>\n" \
"> Please upgrade to one of the following versions: `v2`, or `v3`.\n>\n"
title: "Package manager deprecation notice",
description: "Dependabot will stop supporting `bundler v1`!" \
"\n\nPlease upgrade to one of the following versions: `v2`, or `v3`.\n",
show_in_pr: true,
show_alert: true
})
end
end
Expand All @@ -142,10 +143,11 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
mode: "ERROR",
type: "bundler_unsupported_error",
package_manager_name: "bundler",
message: "Dependabot no longer supports `bundler v1`!\n" \
"Please upgrade to one of the following versions: `v2`, or `v3`.\n",
markdown: "> [!IMPORTANT]\n> Dependabot no longer supports `bundler v1`!\n>\n" \
"> Please upgrade to one of the following versions: `v2`, or `v3`.\n>\n"
title: "Package manager unsupported notice",
description: "Dependabot no longer supports `bundler v1`!" \
"\n\nPlease upgrade to one of the following versions: `v2`, or `v3`.\n",
show_in_pr: true,
show_alert: true
})
end
end
Expand Down Expand Up @@ -194,10 +196,11 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
mode: "WARN",
type: "bundler_deprecated_warn",
package_manager_name: "bundler",
message: "Dependabot will stop supporting `bundler v1`!\n" \
"Please upgrade to one of the following versions: `v2`, or `v3`.\n",
markdown: "> [!WARNING]\n> Dependabot will stop supporting `bundler v1`!\n>\n" \
"> Please upgrade to one of the following versions: `v2`, or `v3`.\n>\n"
title: "Package manager deprecation notice",
description: "Dependabot will stop supporting `bundler v1`!" \
"\n\nPlease upgrade to one of the following versions: `v2`, or `v3`.\n",
show_in_pr: true,
show_alert: true
})
end
end
Expand All @@ -222,10 +225,11 @@ def initialize(name:, version:, deprecated_versions: [], unsupported_versions: [
mode: "ERROR",
type: "bundler_unsupported_error",
package_manager_name: "bundler",
message: "Dependabot no longer supports `bundler v1`!\n" \
"Please upgrade to one of the following versions: `v2`, or `v3`.\n",
markdown: "> [!IMPORTANT]\n> Dependabot no longer supports `bundler v1`!\n>\n" \
"> Please upgrade to one of the following versions: `v2`, or `v3`.\n>\n"
title: "Package manager unsupported notice",
description: "Dependabot no longer supports `bundler v1`!" \
"\n\nPlease upgrade to one of the following versions: `v2`, or `v3`.\n",
show_in_pr: true,
show_alert: true
})
end
end
Expand Down
Loading

0 comments on commit be07237

Please sign in to comment.