Skip to content
Closed
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
2 changes: 2 additions & 0 deletions lib/danger-packwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ module DangerPackwerk
require 'danger-packwerk/check/default_formatter'
require 'danger-packwerk/update/offenses_formatter'
require 'danger-packwerk/update/default_formatter'
require 'danger-packwerk/pks_offense'
require 'danger-packwerk/pks_wrapper'
end
38 changes: 21 additions & 17 deletions lib/danger-packwerk/danger_packwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require 'parse_packwerk'
require 'sorbet-runtime'
require 'danger-packwerk/packwerk_wrapper'
require 'danger-packwerk/pks_wrapper'
require 'danger-packwerk/pks_offense'
require 'danger-packwerk/private/git'

module DangerPackwerk
Expand All @@ -18,7 +20,8 @@ class DangerPackwerk < Danger::Plugin
# especially given all violations should fail the build anyways.
# We set a max (rather than unlimited) to avoid GitHub rate limiting and general spam if a PR does some sort of mass rename.
DEFAULT_MAX_COMMENTS = 15
OnFailure = T.type_alias { T.proc.params(offenses: T::Array[Packwerk::ReferenceOffense]).void }
# Support both legacy Packwerk::ReferenceOffense and new PksOffense types
OnFailure = T.type_alias { T.proc.params(offenses: T::Array[T.any(Packwerk::ReferenceOffense, PksOffense)]).void }
DEFAULT_ON_FAILURE = T.let(->(offenses) {}, OnFailure)
DEFAULT_FAIL = false
DEFAULT_FAILURE_MESSAGE = 'Packwerk violations were detected! Please resolve them to unblock the build.'
Expand Down Expand Up @@ -101,54 +104,55 @@ def check(

current_comment_count = 0

packwerk_reference_offenses = PackwerkWrapper.get_offenses_for_files(targeted_files.to_a).compact
pks_offenses = PksWrapper.get_offenses_for_files(targeted_files.to_a)

renamed_files = git_filesystem.renamed_files.map { |before_after_file| before_after_file[:after] }

packwerk_reference_offenses_to_care_about = packwerk_reference_offenses.reject do |packwerk_reference_offense|
constant_name = packwerk_reference_offense.reference.constant.name
offenses_to_care_about = pks_offenses.reject do |offense|
constant_name = offense.reference.constant.name
filepath_that_defines_this_constant = Private.constant_resolver.resolve(constant_name)&.location
# Ignore references that have been renamed
renamed_files.include?(filepath_that_defines_this_constant) ||
# Ignore violations that are not in the allow-list of violation types to leave comments for
!violation_types.include?(packwerk_reference_offense.violation_type)
!violation_types.include?(offense.violation_type)
end

# We group by the constant name, line number, and reference path. Any offenses with these same values should only differ on what type of violation
# they are (privacy or dependency). We put privacy and dependency violation messages in the same comment since they would occur on the same line.
packwerk_reference_offenses_to_care_about.group_by do |packwerk_reference_offense|
offenses_to_care_about.group_by do |offense|
case grouping_strategy
when CommentGroupingStrategy::PerConstantPerLocation
[
packwerk_reference_offense.reference.constant.name,
packwerk_reference_offense.location&.line,
packwerk_reference_offense.reference.relative_path
offense.reference.constant.name,
offense.location.line,
offense.reference.relative_path
]
when CommentGroupingStrategy::PerConstantPerPack
[
packwerk_reference_offense.reference.constant.name,
ParsePackwerk.package_from_path(packwerk_reference_offense.reference.relative_path)
offense.reference.constant.name,
ParsePackwerk.package_from_path(offense.reference.relative_path)
]
else
T.absurd(grouping_strategy)
end
end.each_value do |unique_packwerk_reference_offenses|
end.each_value do |unique_offenses|
break if current_comment_count >= max_comments

current_comment_count += 1

reference_offense = T.must(unique_packwerk_reference_offenses.first)
line_number = reference_offense.location&.line
referencing_file = reference_offense.reference.relative_path
offense = T.must(unique_offenses.first)
line_number = offense.location.line
referencing_file = offense.reference.relative_path

message = offenses_formatter.format_offenses(unique_packwerk_reference_offenses, repo_link, org_name, repo_url_builder: repo_url_builder)
# PksOffense adapters provide Packwerk::ReferenceOffense-compatible interface
message = offenses_formatter.format_offenses(T.unsafe(unique_offenses), repo_link, org_name, repo_url_builder: repo_url_builder)
markdown(message, file: git_filesystem.convert_to_filesystem(referencing_file), line: line_number)
end

if current_comment_count > 0
fail(failure_message) if fail_build

on_failure.call(packwerk_reference_offenses)
on_failure.call(pks_offenses)
end
end
end
Expand Down
146 changes: 146 additions & 0 deletions lib/danger-packwerk/pks_offense.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# typed: strict

require 'json'

module DangerPackwerk
#
# PksOffense represents a violation from pks JSON output.
# It is designed to have a compatible interface with BasicReferenceOffense
# so it can be used with the Update::OffensesFormatter.
#
# It also provides adapter objects (reference, location) for compatibility
# with Packwerk::ReferenceOffense interface used by Check::OffensesFormatter.
#
class PksOffense < T::Struct
extend T::Sig

const :violation_type, String
const :file, String
const :line, Integer
const :column, Integer
const :constant_name, String
const :referencing_pack_name, String
const :defining_pack_name, String
const :strict, T::Boolean
const :message, String

#
# Adapter classes for Packwerk::ReferenceOffense compatibility
# These allow PksOffense to be used with Check::OffensesFormatter
#
class ConstantAdapter < T::Struct
const :name, String
const :location, String
const :package, T.untyped # Returns an object with .name method
end

class PackageAdapter < T::Struct
const :name, String
end

class ReferenceAdapter < T::Struct
const :relative_path, String
const :constant, ConstantAdapter
end

class LocationAdapter < T::Struct
const :line, Integer
end

# Adapter for Packwerk::ReferenceOffense.reference
sig { returns(ReferenceAdapter) }
def reference
package_adapter = PackageAdapter.new(name: defining_pack_name)
constant_adapter = ConstantAdapter.new(
name: constant_name,
location: file, # Best approximation - pks doesn't provide constant definition location
package: package_adapter
)
ReferenceAdapter.new(
relative_path: file,
constant: constant_adapter
)
end

# Adapter for Packwerk::ReferenceOffense.location
sig { returns(LocationAdapter) }
def location
LocationAdapter.new(line: line)
end

# Alias methods for compatibility with BasicReferenceOffense interface
sig { returns(String) }
def class_name
constant_name
end

sig { returns(String) }
def type
violation_type
end

sig { returns(String) }
def to_package_name
defining_pack_name
end

sig { returns(String) }
def from_package_name
referencing_pack_name
end

sig { returns(T::Boolean) }
def privacy?
violation_type == PRIVACY_VIOLATION_TYPE
end

sig { returns(T::Boolean) }
def dependency?
violation_type == DEPENDENCY_VIOLATION_TYPE
end

sig { params(other: PksOffense).returns(T::Boolean) }
def ==(other)
other.constant_name == constant_name &&
other.file == file &&
other.defining_pack_name == defining_pack_name &&
other.violation_type == violation_type
end

sig { params(other: PksOffense).returns(T::Boolean) }
def eql?(other)
self == other
end

sig { returns(Integer) }
def hash
[constant_name, file, defining_pack_name, violation_type].hash
end

class << self
extend T::Sig

sig { params(json_string: String).returns(T::Array[PksOffense]) }
def from_json(json_string)
data = JSON.parse(json_string)
offenses = data['offenses'] || []
offenses.map { |offense| from_hash(offense) }
end

sig { params(hash: T::Hash[String, T.untyped]).returns(PksOffense) }
def from_hash(hash)
PksOffense.new(
violation_type: hash.fetch('violation_type'),
file: hash.fetch('file'),
line: hash.fetch('line'),
column: hash.fetch('column'),
constant_name: hash.fetch('constant_name'),
referencing_pack_name: hash.fetch('referencing_pack_name'),
defining_pack_name: hash.fetch('defining_pack_name'),
strict: hash.fetch('strict', false),
message: hash.fetch('message', '')
)
end
end
end
end
39 changes: 39 additions & 0 deletions lib/danger-packwerk/pks_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# typed: strict

require 'open3'
require 'json'

module DangerPackwerk
class PksWrapper
extend T::Sig

class PksBinaryNotFoundError < StandardError; end

sig { params(files: T::Array[String]).returns(T::Array[PksOffense]) }
def self.get_offenses_for_files(files)
return [] if files.empty?

stdout, stderr, _status = run_pks_check(files)

if stderr.include?('command not found') || stderr.include?('No such file or directory')
raise PksBinaryNotFoundError, 'pks binary not found. Please install pks to use this feature.'
end

PksOffense.from_json(stdout)
end

sig { params(files: T::Array[String]).returns([String, String, Process::Status]) }
def self.run_pks_check(files)
require 'shellwords'
escaped_files = files.map { |f| Shellwords.escape(f) }.join(' ')
command = "pks check --output-format json #{escaped_files}"
Open3.capture3(command)
end

sig { returns(T::Boolean) }
def self.pks_available?
_, _, status = Open3.capture3('which', 'pks')
!!status.success?
end
end
end
Loading
Loading