Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor poetry logic to parse subdependency types #7826

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "toml-rb"
require "dependabot/python/file_parser"
require "dependabot/python/name_normaliser"

module Dependabot
module Python
class FileParser
class PoetrySubdependencyTypeParser
def initialize(lockfile:)
@lockfile = lockfile
end

def subdep_type(dep)
category =
TomlRB.parse(lockfile.content).fetch("package", []).
find { |dets| normalise(dets.fetch("name")) == dep.name }.
fetch("category")
deivid-rodriguez marked this conversation as resolved.
Show resolved Hide resolved

category == "dev" ? "dev-dependencies" : "dependencies"
end

private

attr_reader :lockfile

def normalise(name)
NameNormaliser.normalise(name)
end
end
end
end
end
21 changes: 11 additions & 10 deletions python/lib/dependabot/python/file_updater/poetry_file_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "dependabot/python/version"
require "dependabot/python/requirement"
require "dependabot/python/file_parser/python_requirement_parser"
require "dependabot/python/file_parser/subdependency_type_parser"
require "dependabot/python/file_updater"
require "dependabot/python/native_helpers"
require "dependabot/python/name_normaliser"
Expand Down Expand Up @@ -156,17 +157,10 @@ def lock_declaration_to_new_version!(poetry_object, dep)
end

def create_declaration_at_new_version!(poetry_object, dep)
poetry_object[subdep_type] ||= {}
poetry_object[subdep_type][dependency.name] = dep.version
end
subdep_type = subdependency_type_parser.subdep_type(dep)

def subdep_type
category =
TomlRB.parse(lockfile.content).fetch("package", []).
find { |dets| normalise(dets.fetch("name")) == dependency.name }.
fetch("category")

category == "dev" ? "dev-dependencies" : "dependencies"
poetry_object[subdep_type] ||= {}
poetry_object[subdep_type][dep.name] = dep.version
end

def sanitize(pyproject_content)
Expand Down Expand Up @@ -299,6 +293,13 @@ def python_requirement_parser
)
end

def subdependency_type_parser
@subdependency_type_parser ||=
FileParser::PoetrySubdependencyTypeParser.new(
lockfile: lockfile
)
end

def language_version_manager
@language_version_manager ||=
LanguageVersionManager.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "dependabot/shared_helpers"
require "dependabot/python/file_parser"
require "dependabot/python/file_parser/python_requirement_parser"
require "dependabot/python/file_parser/subdependency_type_parser"
require "dependabot/python/file_updater/pyproject_preparer"
require "dependabot/python/update_checker"
require "dependabot/python/version"
Expand Down Expand Up @@ -275,6 +276,8 @@ def set_target_dependency_req(pyproject_content, updated_requirement)

# If this is a sub-dependency, add the new requirement
unless dependency.requirements.find { |r| r[:file] == pyproject.name }
subdep_type = subdependency_type_parser.subdep_type(dependency)

poetry_object[subdep_type] ||= {}
poetry_object[subdep_type][dependency.name] = updated_requirement
end
Expand All @@ -294,22 +297,20 @@ def update_dependency_requirement(toml_node, requirement)
end
end

def subdep_type
category =
TomlRB.parse(lockfile.content).fetch("package", []).
find { |dets| normalise(dets.fetch("name")) == dependency.name }.
fetch("category")

category == "dev" ? "dev-dependencies" : "dependencies"
end

def python_requirement_parser
@python_requirement_parser ||=
FileParser::PythonRequirementParser.new(
dependency_files: dependency_files
)
end

def subdependency_type_parser
@subdependency_type_parser ||=
FileParser::PoetrySubdependencyTypeParser.new(
lockfile: lockfile
)
end

def language_version_manager
@language_version_manager ||=
LanguageVersionManager.new(
Expand Down