danger-packwerk integrates packwerk with danger to provide inline comments in PRs related to boundaries in a Rails application.
Step 1: Add this line to your Gemfile (to whatever group your CI uses, as it is not needed in production) and bundle install:
gem 'danger-packwerk', group: :testStep 2: Add these to your Dangerfile:
packwerk.check
package_todo_yml_changes.checkThat's it for basic usage!
There are currently two danger checks that ship with danger-packwerk:
- One that runs
bin/packwerk checkand leaves inline comments in source code on new violations - One that looks at changes to
package_todo.ymlfiles and leaves inline comments on added violations.
In upcoming iterations, we will include other danger checks, including:
- A danger check that detects changes to
package.ymlfiles and posts user-configurable messages on thepackage.ymlfiles that are modified. - A danger check that detects changes to
packwerk.ymlfiles and allows you to specify the action taken when that happens.
Without any configuration, packwerk.check should just work. By default, it will post a maximum of 15 messages in a PR and it will not fail the build.
packwerk.check can be configured to in the following ways:
To customize the message in the GitHub comment, pass in offenses_formatter to packwerk.check in your Dangerfile. Here's a simple example:
class MyFormatter
extend T::Sig
include DangerPackwerk::Check::OffensesFormatter
# Packwerk::ReferenceOffense: https://github.com/Shopify/packwerk/blob/main/lib/packwerk/reference_offense.rb
sig do
override.params(
offenses: T::Array[Packwerk::ReferenceOffense],
repo_link: String,
org_name: String
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
).returns(String)
end
def format_offenses(offenses, repo_link, org_name, repo_builder_url: nil)
# your logic here
end
end
packwerk.check(offenses_formatter: MyFormatter.new)If you'd like to keep the default messaging but add some context customized to your organization, you can pass that in as follows:
custom_help_message = "Need help? Check out our internal docs [here](www.example.com)"
packwerk.check(offenses_formatter: DangerPackwerk::Check::DefaultFormatter.new(custom_help_message: custom_help_message))Simply pass in fail_build: true into check, as such:
packwerk.check(fail_build: true)If you want to change the default error message, which is Packwerk violations were detected! Please resolve them to unblock the build., then you can also pass in failure_message.
If you do not change this, the default max is 15. More information about why we chose this number in the source code.
packwerk.check(max_comments: 3)Maybe you want to notify slack or do something else when there are packwerk failures.
packwerk.check(
# Offenses are a T::Array[Packwerk::ReferenceOffense] => https://github.com/Shopify/packwerk/blob/main/lib/packwerk/reference_offense.rb
on_failure: -> (offenses) do
# Notify slack or otherwise do something extra!
end
)By default, only dependency and privacy violation types are supported. If you wish to use other violation types you'll need to provide them when calling check:
packwerk.check(
violation_types: %w[dependency privacy layer]
)Any violations not included in this list will be ignored.
You will also most likely want to customize the offenses formatter and provide specific feedback for the new violation types.
Without any configuration, package_todo_yml_changes.check should just work. By default, it will post a maximum of 15 messages in a PR, using default messaging defined within this gem.
package_todo_yml_changes.check can be configured to in the following ways:
To customize the message in the GitHub comment, pass in offenses_formatter to package_todo_yml_changes.check in your Dangerfile. Here's a simple example:
class MyFormatter
extend T::Sig
include DangerPackwerk::Update::OffensesFormatter
# DangerPackwerk::BasicReferenceOffense
sig do
override.params(
offenses: T::Array[Packwerk::ReferenceOffense],
repo_link: String,
org_name: String
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
).returns(String)
end
def format_offenses(offenses, repo_link, org_name, repo_builder_url: nil)
# your logic here
end
end
package_todo_yml_changes.check(offenses_formatter: MyFormatter.new)If you'd like to keep the default messaging but add some context customized to your organization, you can pass that in as follows:
custom_help_message = "Need help? Check out our internal docs [here](www.example.com)"
package_todo_yml_changes.check(offenses_formatter: DangerPackwerk::Update::DefaultFormatter.new(custom_help_message: custom_help_message))If you do not change this, the default max is 15. More information about why we chose this number in the source code.
package_todo_yml_changes.check(max_comments: 3)Maybe you want to notify slack or do something else before we leave comments.
package_todo_yml_changes.check(
# violation_diff is a DangerPackwerk::ViolationDiff and changed_package_todo_ymls is a T::Array[String]
before_comment: -> (violation_diff, changed_package_todo_ymls) do
# Notify slack or otherwise do something extra!
end
)We welcome your contributions! Please create an issue or pull request and we'd be happy to take a look.


