Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Remove visibility protection (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk authored Nov 4, 2022
1 parent 5e71768 commit 3d2d88f
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 639 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
package_protections (3.2.0)
package_protections (4.0.0)
activesupport
parse_packwerk
rubocop
Expand Down
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ This gem ships with the following checks
2) Other packages are not using the private API of your package (via `packwerk` `enforce_privacy`)
3) Your package has a typed public API (via the `rubocop` `PackageProtections/TypedPublicApi` cop)
4) Your package only creates a single namespace (via the `rubocop` `PackageProtections/NamespacedUnderPackageName` cop)
4) Your package is only visible to a select number of packages (via the `packwerk` `enforce_privacy` cop)

## Initial Configuration
Package protections first requires that your application is using [`packwerk`](https://github.com/Shopify/packwerk), [`rubocop`](https://github.com/rubocop/rubocop), and [`rubocop-sorbet`](https://github.com/Shopify/rubocop-sorbet). Follow the regular setup instructions for those tools before proceeding.
Expand Down Expand Up @@ -63,25 +62,6 @@ end

If you've worked through all of the TODOs for this cop and are able to set the value to `fail_on_any`, you can also set `automatic_pack_namespace` which will support your pack having one global namespace without extra subdirectories. That is, instead of `packs/foo/app/services/foo/bar.rb`, you can use `packs/foo/app/services/bar.rb` and still have it define `Foo::Bar`. [See the `stimpack` README.md](https://github.com/rubyatscale/stimpack#readme) for more information.

### `prevent_other_packages_from_using_this_package_without_explicit_visibility`
*This is only available if your package has `enforce_privacy` set to `true`!*
This protection exists to help packages have control over who their clients are. When turning on this protection, only clients who are listed in your `visible_to` metadata will be allowed to consume your package. Here is an example in `packs/apples/package.yml`:
```yml
enforce_privacy: true
enforce_dependencies: true
metadata:
protections:
prevent_other_packages_from_using_this_package_without_explicit_visibility: fail_on_new
# ... other protections are the same
visible_to:
- packs/other_pack
- packs/another_pack
```
In this package, only `packs/other_pack` and `packs/another_pack` can use `packs/apples`. With both the `fail_on_new` and `fail_on_any` setting, only those packs can state a dependency on `packs/apples` in their `package.yml`. If any other packs state a dependency on `packs/apples`, the build will fail, even with violations. With the `fail_on_new` setting, a pack can create a dependency or privacy violation on `packs/apples` even if it's not listed. With `fail_on_any`, no violations are allowed.
If `visible_to` is not set and the protection is turned on, then the package cannot be consumed by any package (a top-level package might be a good candidate for this).

Note that this protection's default behavior is `fail_never`, so it can remain unset in the `package.yml`.

## Violation Behaviors
#### `fail_on_any`
If this behavior is selected, the build will fail if there is *any* issue, new or old.
Expand Down
1 change: 0 additions & 1 deletion lib/package_protections/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'package_protections/private/incoming_privacy_protection'
require 'package_protections/private/outgoing_dependency_protection'
require 'package_protections/private/metadata_modifiers'
require 'package_protections/private/visibility_protection'
require 'package_protections/private/configuration'

module PackageProtections
Expand Down
1 change: 0 additions & 1 deletion lib/package_protections/private/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def default_protections
Private::IncomingPrivacyProtection.new,
RuboCop::Cop::PackageProtections::TypedPublicApi.new,
RuboCop::Cop::PackageProtections::NamespacedUnderPackageName.new,
Private::VisibilityProtection.new,
RuboCop::Cop::PackageProtections::OnlyClassMethods.new,
RuboCop::Cop::PackageProtections::RequireDocumentedPublicApis.new
]
Expand Down
186 changes: 0 additions & 186 deletions lib/package_protections/private/visibility_protection.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/package_protections/protected_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ def dependencies
original_package.dependencies
end

sig { returns(T::Set[String]) }
def visible_to
Set.new(metadata['visible_to'] || [])
end

sig { returns(T::Array[ParsePackwerk::Violation]) }
def violations
deprecated_references.violations
Expand Down
8 changes: 1 addition & 7 deletions lib/package_protections/rspec/application_fixture_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,17 @@ def write_package_yml(
dependencies: [],
enforce_dependencies: true,
enforce_privacy: true,
protections: {},
visible_to: []
protections: {}
)
defaults = {
'prevent_this_package_from_violating_its_stated_dependencies' => 'fail_on_new',
'prevent_other_packages_from_using_this_packages_internals' => 'fail_on_new',
'prevent_this_package_from_exposing_an_untyped_api' => 'fail_on_new',
'prevent_this_package_from_creating_other_namespaces' => 'fail_on_new',
'prevent_other_packages_from_using_this_package_without_explicit_visibility' => 'fail_never',
'prevent_this_package_from_exposing_instance_method_public_apis' => 'fail_never'
}
protections_with_defaults = defaults.merge(protections)
metadata = { 'protections' => protections_with_defaults }
if visible_to.any?
metadata.merge!('visible_to' => visible_to)
end

package = ParsePackwerk::Package.new(
name: pack_name,
dependencies: dependencies,
Expand Down
2 changes: 1 addition & 1 deletion package_protections.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'package_protections'
spec.version = '3.2.0'
spec.version = '4.0.0'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'Package protections for Rails apps'
Expand Down
2 changes: 1 addition & 1 deletion spec/package_protections/protected_package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
someprotection: true
YML

expect(PackageProtections.validate!).to include 'Invalid configuration for package `.`. The metadata keys ["someprotection"] are not a valid behavior under the `protection` metadata namespace. Valid keys are ["prevent_this_package_from_violating_its_stated_dependencies", "prevent_other_packages_from_using_this_packages_internals", "prevent_this_package_from_exposing_an_untyped_api", "prevent_this_package_from_creating_other_namespaces", "prevent_other_packages_from_using_this_package_without_explicit_visibility", "prevent_this_package_from_exposing_instance_method_public_apis", "prevent_this_package_from_exposing_undocumented_public_apis"]. See https://github.com/rubyatscale/package_protections#readme for more info'
expect(PackageProtections.validate!).to include 'Invalid configuration for package `.`. The metadata keys ["someprotection"] are not a valid behavior under the `protection` metadata namespace. Valid keys are ["prevent_this_package_from_violating_its_stated_dependencies", "prevent_other_packages_from_using_this_packages_internals", "prevent_this_package_from_exposing_an_untyped_api", "prevent_this_package_from_creating_other_namespaces", "prevent_this_package_from_exposing_instance_method_public_apis", "prevent_this_package_from_exposing_undocumented_public_apis"]. See https://github.com/rubyatscale/package_protections#readme for more info'
end
end

Expand Down
Loading

0 comments on commit 3d2d88f

Please sign in to comment.