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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class MyFormatter
extend T::Sig
include DangerPackwerk::Check::OffensesFormatter
# Packwerk::ReferenceOffense: https://github.com/Shopify/packwerk/blob/main/lib/packwerk/reference_offense.rb
sig { override.params(offenses: T::Array[Packwerk::ReferenceOffense], repo_link: String, org_name: String).returns(String) }
def format_offenses(offenses, repo_link, org_name)
sig { override.params(offenses: T::Array[Packwerk::ReferenceOffense], plugin: Danger::Plugin, org_name: String).returns(String) }
def format_offenses(offenses, plugin, org_name)
# your logic here
end
end
Expand Down Expand Up @@ -98,8 +98,8 @@ class MyFormatter
extend T::Sig
include DangerPackwerk::Update::OffensesFormatter
# DangerPackwerk::BasicReferenceOffense
sig { override.params(offenses: T::Array[DangerPackwerk::BasicReferenceOffense], repo_link: String, org_name: String).returns(String) }
def format_offenses(offenses, repo_link, org_name)
sig { override.params(offenses: T::Array[DangerPackwerk::BasicReferenceOffense], plugin: Danger::Plugin, org_name: String).returns(String) }
def format_offenses(offenses, plugin, org_name)
# your logic here
end
end
Expand Down
12 changes: 7 additions & 5 deletions lib/danger-packwerk/check/default_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def initialize(custom_help_message: nil)
sig do
override.params(
offenses: T::Array[Packwerk::ReferenceOffense],
repo_link: String,
plugin: Danger::Plugin,
org_name: String
).returns(String)
end
def format_offenses(offenses, repo_link, org_name)
def format_offenses(offenses, plugin, org_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this PR! I think this is a great idea because as you pointed out, not every project is going to have a branch called main.

To make things more backwards compatible here and minimize changes, we could continue to pass in a repo_link but extend it to include the branch (#{github.pr_json[:base][:repo][:html_url]}/blob/#{github.head_commit} instead of github.pr_json[:base][:repo][:html_url]). This would require less refactoring so old uses of the previous type signature wouldn't result in exceptions.

Copy link
Contributor Author

@Catsuko Catsuko Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make things more backwards compatible here and minimize changes, we could continue to pass in a repo_link but extend it to include the branch (#{github.pr_json[:base][:repo][:html_url]}/blob/#{github.head_commit} instead of github.pr_json[:base][:repo][:html_url]). This would require less refactoring so old uses of the previous type signature wouldn't result in exceptions.

I like the idea of making it more backwards compatible but i think including the branch would be a breaking change for any formatter that doesn't expect repo_link to include the branch. For example, take the existing default implementation:

[<ins>`#{constant_name}`</ins>](#{repo_link}/blob/main/#{constant_location})

Do you think that would be a problem?

reference_offense = T.must(offenses.first)
violation_types = offenses.map(&:violation_type)
referencing_file = reference_offense.reference.relative_path
Expand All @@ -43,13 +43,15 @@ def format_offenses(offenses, repo_link, org_name)
referenced_code_in_right_pack = "- Does #{constant_name} live in the right pack?\n - If not, try `bin/packs move packs/destination_pack #{constant_location}`"
dependency_violation_message = "- Do we actually want to depend on #{constant_source_package_name}?\n - If so, try `bin/packs add_dependency #{referencing_file_pack} #{constant_source_package_name}`\n - If not, what can we change about the design so we do not have to depend on #{constant_source_package_name}?"
team_to_work_with = constant_source_package_ownership_info.owning_team ? constant_source_package_ownership_info.markdown_link_to_github_members_no_tag : 'the pack owner'

privacy_violation_message = "- Does API in #{constant_source_package.name}/public support this use case?\n - If not, can we work with #{team_to_work_with} to create and use a public API?\n - If `#{constant_name}` should already be public, try `bin/packs make_public #{constant_location}`."
constant_link = "`#{constant_name}` (#{plugin.github.html_link(constant_location)})"

if violation_types.include?(::DangerPackwerk::DEPENDENCY_VIOLATION_TYPE) && violation_types.include?(::DangerPackwerk::PRIVACY_VIOLATION_TYPE)
<<~MESSAGE
**Packwerk Violation**
- Type: Privacy :lock: + Dependency :knot:
- Constant: [<ins>`#{constant_name}`</ins>](#{repo_link}/blob/main/#{constant_location})
- Constant: #{constant_link}
- Owning pack: #{constant_source_package_name}
#{constant_source_package_ownership_info.ownership_copy}

Expand All @@ -69,7 +71,7 @@ def format_offenses(offenses, repo_link, org_name)
<<~MESSAGE
**Packwerk Violation**
- Type: Dependency :knot:
- Constant: [<ins>`#{constant_name}`</ins>](#{repo_link}/blob/main/#{constant_location})
- Constant: #{constant_link}
- Owning pack: #{constant_source_package_name}
#{constant_source_package_ownership_info.ownership_copy}

Expand All @@ -88,7 +90,7 @@ def format_offenses(offenses, repo_link, org_name)
<<~MESSAGE
**Packwerk Violation**
- Type: Privacy :lock:
- Constant: [<ins>`#{constant_name}`</ins>](#{repo_link}/blob/main/#{constant_location})
- Constant: #{constant_link}
- Owning pack: #{constant_source_package_name}
#{constant_source_package_ownership_info.ownership_copy}

Expand Down
4 changes: 2 additions & 2 deletions lib/danger-packwerk/check/offenses_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module OffensesFormatter
sig do
abstract.params(
offenses: T::Array[Packwerk::ReferenceOffense],
repo_link: String,
plugin: Danger::Plugin,
org_name: String
).returns(String)
end
def format_offenses(offenses, repo_link, org_name); end
def format_offenses(offenses, plugin, org_name); end
end
end
end
3 changes: 1 addition & 2 deletions lib/danger-packwerk/danger_package_todo_yml_changes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def check(
root_path: nil
)
offenses_formatter ||= Update::DefaultFormatter.new
repo_link = github.pr_json[:base][:repo][:html_url]
org_name = github.pr_json[:base][:repo][:owner][:login]

git_filesystem = Private::GitFilesystem.new(git: git, root: root_path || '')
Expand All @@ -62,7 +61,7 @@ def check(
location = T.must(violations.first).file_location

markdown(
offenses_formatter.format_offenses(violations, repo_link, org_name),
offenses_formatter.format_offenses(violations, self, org_name),
line: location.line_number,
file: git_filesystem.convert_to_filesystem(location.file)
)
Expand Down
3 changes: 1 addition & 2 deletions lib/danger-packwerk/danger_packwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def check(
root_path: nil
)
offenses_formatter ||= Check::DefaultFormatter.new
repo_link = github.pr_json[:base][:repo][:html_url]
org_name = github.pr_json[:base][:repo][:owner][:login]

# This is important because by default, Danger will leave a concantenated list of all its messages if it can't find a commentable place in the
Expand Down Expand Up @@ -136,7 +135,7 @@ def check(
line_number = reference_offense.location&.line
referencing_file = reference_offense.reference.relative_path

message = offenses_formatter.format_offenses(unique_packwerk_reference_offenses, repo_link, org_name)
message = offenses_formatter.format_offenses(unique_packwerk_reference_offenses, self, org_name)
markdown(message, file: git_filesystem.convert_to_filesystem(referencing_file), line: line_number)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/danger-packwerk/update/default_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def initialize(custom_help_message: nil)
@custom_help_message = custom_help_message
end

sig { override.params(offenses: T::Array[BasicReferenceOffense], repo_link: String, org_name: String).returns(String) }
def format_offenses(offenses, repo_link, org_name)
sig { override.params(offenses: T::Array[BasicReferenceOffense], plugin: Danger::Plugin, org_name: String).returns(String) }
def format_offenses(offenses, plugin, org_name)
violation = T.must(offenses.first)
referencing_file_pack = ParsePackwerk.package_from_path(violation.file)
# We remove leading double colons as they feel like an implementation detail of packwerk.
Expand Down
4 changes: 2 additions & 2 deletions lib/danger-packwerk/update/offenses_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module OffensesFormatter
sig do
abstract.params(
offenses: T::Array[BasicReferenceOffense],
repo_link: String,
plugin: Danger::Plugin,
org_name: String
).returns(String)
end
def format_offenses(offenses, repo_link, org_name); end
def format_offenses(offenses, plugin, org_name); end
end
end
end
4 changes: 2 additions & 2 deletions spec/danger_packwerk/danger_package_todo_yml_changes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ module DangerPackwerk
Class.new do
include Update::OffensesFormatter

def format_offenses(added_violations, repo_link, org_name)
def format_offenses(added_violations, plugin, org_name)
<<~MESSAGE
There are #{added_violations.count} new violations,
with class_names #{added_violations.map(&:class_name).uniq.sort},
Expand Down Expand Up @@ -1160,7 +1160,7 @@ def format_offenses(added_violations, repo_link, org_name)
Class.new do
include Update::OffensesFormatter

def format_offenses(added_violations, repo_link, org_name)
def format_offenses(added_violations, plugin, org_name)
<<~MESSAGE
There are #{added_violations.count} new violations,
with class_names #{added_violations.map(&:class_name).uniq.sort},
Expand Down
12 changes: 6 additions & 6 deletions spec/danger_packwerk/danger_packwerk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module DangerPackwerk
Class.new do
include Check::OffensesFormatter

def format_offenses(offenses, repo_link, org_name)
def format_offenses(offenses, plugin, org_name)
offenses.map(&:message).join("\n\n")
end
end
Expand Down Expand Up @@ -863,7 +863,7 @@ def format_offenses(offenses, repo_link, org_name)
expected = <<~EXPECTED
**Packwerk Violation**
- Type: Privacy :lock:
- Constant: [<ins>`PrivateConstant`</ins>](https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb)
- Constant: `PrivateConstant` (<a href=https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb>packs/gusto_slack/app/services/private_constant.rb</a>)
- Owning pack: packs/gusto_slack
- Owned by [<ins>@MyOrg/product-infrastructure</ins>](https://github.com/orgs/MyOrg/teams/product-infrastructure/members) (Slack: [<ins>#prod-infra</ins>](https://slack.com/app_redirect?channel=prod-infra))

Expand Down Expand Up @@ -904,7 +904,7 @@ def format_offenses(offenses, repo_link, org_name)
expected = <<~EXPECTED
**Packwerk Violation**
- Type: Privacy :lock:
- Constant: [<ins>`PrivateConstant`</ins>](https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb)
- Constant: `PrivateConstant` (<a href=https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb>packs/gusto_slack/app/services/private_constant.rb</a>)
- Owning pack: packs/gusto_slack
- This pack is unowned.

Expand Down Expand Up @@ -947,7 +947,7 @@ def format_offenses(offenses, repo_link, org_name)
expected = <<~EXPECTED
**Packwerk Violation**
- Type: Dependency :knot:
- Constant: [<ins>`PrivateConstant`</ins>](https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb)
- Constant: `PrivateConstant` (<a href=https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb>packs/gusto_slack/app/services/private_constant.rb</a>)
- Owning pack: packs/gusto_slack
- Owned by [<ins>@MyOrg/product-infrastructure</ins>](https://github.com/orgs/MyOrg/teams/product-infrastructure/members) (Slack: [<ins>#prod-infra</ins>](https://slack.com/app_redirect?channel=prod-infra))

Expand Down Expand Up @@ -988,7 +988,7 @@ def format_offenses(offenses, repo_link, org_name)
expected = <<~EXPECTED
**Packwerk Violation**
- Type: Privacy :lock: + Dependency :knot:
- Constant: [<ins>`PrivateConstant`</ins>](https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb)
- Constant: `PrivateConstant` (<a href=https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb>packs/gusto_slack/app/services/private_constant.rb</a>)
- Owning pack: packs/gusto_slack
- Owned by [<ins>@MyOrg/product-infrastructure</ins>](https://github.com/orgs/MyOrg/teams/product-infrastructure/members) (Slack: [<ins>#prod-infra</ins>](https://slack.com/app_redirect?channel=prod-infra))

Expand Down Expand Up @@ -1061,7 +1061,7 @@ def format_offenses(offenses, repo_link, org_name)
expected = <<~EXPECTED
**Packwerk Violation**
- Type: Privacy :lock:
- Constant: [<ins>`PrivateConstant`</ins>](https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb)
- Constant: `PrivateConstant` (<a href=https://github.com/MyOrg/my_repo/blob/main/packs/gusto_slack/app/services/private_constant.rb>packs/gusto_slack/app/services/private_constant.rb</a>)
- Owning pack: packs/gusto_slack
- Owned by [<ins>@MyOrg/product-infrastructure</ins>](https://github.com/orgs/MyOrg/teams/product-infrastructure/members) (Slack: [<ins>#prod-infra</ins>](https://slack.com/app_redirect?channel=prod-infra))

Expand Down
4 changes: 4 additions & 0 deletions spec/support/danger_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
allow(plugin).to receive(:git).and_return(mock_git)

allow(plugin.github).to receive(:pr_json).and_return(pr_json)
allow(plugin.github).to receive(:html_link) do |location|
href = "#{pr_json[:base][:repo][:html_url]}/blob/main/#{location}"
"<a href=#{href}>#{location}</a>"
end
end
end
Loading