Class: PackageProtections::Private::IncomingPrivacyProtection

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
PackageProtections::ProtectionInterface
Defined in:
lib/package_protections/private/incoming_privacy_protection.rb

Constant Summary collapse

IDENTIFIER =
'prevent_other_packages_from_using_this_packages_internals'

Instance Method Summary collapse

Methods included from PackageProtections::ProtectionInterface

#default_behavior, #get_offenses, #supports_violation_behavior?

Instance Method Details

#get_offenses_for_existing_violations(protected_packages) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/package_protections/private/incoming_privacy_protection.rb', line 78

def get_offenses_for_existing_violations(protected_packages)
  all_listed_violations = protected_packages.flat_map do |protected_package|
    protected_package.violations.select(&:privacy?).flat_map do |violation|
      PerFileViolation.from(violation, protected_package.original_package)
    end
  end

  all_listed_violations.flat_map do |per_file_violation|
    constant_source_package = Private.get_package_with_name(per_file_violation.constant_source_package)
    violation_behavior = constant_source_package.violation_behavior_for(identifier)

    case violation_behavior
    when ViolationBehavior::FailNever, ViolationBehavior::FailOnNew
      []
    when ViolationBehavior::FailOnAny
      Offense.new(
        file: per_file_violation.filepath,
        message: message_for_fail_on_any(per_file_violation),
        violation_type: identifier,
        package: constant_source_package.original_package
      )
    else
      T.absurd(violation_behavior)
    end
  end
end

#get_offenses_for_new_violations(new_violations) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/package_protections/private/incoming_privacy_protection.rb', line 48

def get_offenses_for_new_violations(new_violations)
  new_violations.select(&:privacy?).flat_map do |per_file_violation|
    protected_package = Private.get_package_with_name(per_file_violation.constant_source_package)
    violation_behavior = protected_package.violation_behavior_for(identifier)

    case violation_behavior
    when ViolationBehavior::FailNever
      next []
    when ViolationBehavior::FailOnNew
      message = message_for_fail_on_new(per_file_violation)
    when ViolationBehavior::FailOnAny
      message = message_for_fail_on_any(per_file_violation)
    else
      T.absurd(violation_behavior)
    end

    Offense.new(
      file: per_file_violation.filepath,
      message: message,
      violation_type: identifier,
      package: protected_package.original_package
    )
  end
end

#humanized_protection_descriptionObject



35
36
37
38
39
40
41
# File 'lib/package_protections/private/incoming_privacy_protection.rb', line 35

def humanized_protection_description
  <<~MESSAGE
    To resolve these violations, check the `public/` folder in each pack for public constants and APIs.
    If you need help or can't find what you need to meet your use case, reach out to the owning team.
    See https://go/packwerk_cheatsheet_privacy for more info.
  MESSAGE
end

#humanized_protection_nameObject



30
31
32
# File 'lib/package_protections/private/incoming_privacy_protection.rb', line 30

def humanized_protection_name
  'Privacy Violations'
end

#identifierObject



14
15
16
# File 'lib/package_protections/private/incoming_privacy_protection.rb', line 14

def identifier
  IDENTIFIER
end

#unmet_preconditions_for_behavior(behavior, package) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/package_protections/private/incoming_privacy_protection.rb', line 19

def unmet_preconditions_for_behavior(behavior, package)
  if behavior.enabled? && !package.enforces_privacy?
    "Package #{package.name} must have `enforce_privacy: true` to use this protection"
  elsif !behavior.enabled? && package.enforces_privacy?
    "Package #{package.name} must have `enforce_privacy: false` to turn this protection off"
  else
    nil
  end
end