Skip to content

Commit acaa9fe

Browse files
martinemdeivy
andauthored
Generate links in formatter using head ref (#54)
* Add repo_url_builder keyword arg * Bump minimum Ruby to 3.1 * Pass repo_url_builder to format_offenses * Use repo_url_builder to generate the link in the default formatter * Use head ref for url generation in offense formatter * Bump to 0.15.0 for change to OffenseFormatter interface The change is backwards compatible, but sorbet forces everyone to update their method signature anyway. * Test generating urls in the custom formatter to ensure it works --------- Co-authored-by: Ivy Evans <[email protected]>
1 parent f952ba3 commit acaa9fe

15 files changed

+88
-51
lines changed

.rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins:
33
- rubocop-performance
44

55
AllCops:
6-
TargetRubyVersion: 2.6
6+
TargetRubyVersion: 3.1
77
NewCops: enable
88
Exclude:
99
- bin/**/*

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ GIT
1818
PATH
1919
remote: .
2020
specs:
21-
danger-packwerk (0.14.4)
21+
danger-packwerk (0.15.0)
2222
code_ownership
2323
danger-plugin-api (~> 1.0)
2424
packs

README.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@ class MyFormatter
4343
extend T::Sig
4444
include DangerPackwerk::Check::OffensesFormatter
4545
# Packwerk::ReferenceOffense: https://github.com/Shopify/packwerk/blob/main/lib/packwerk/reference_offense.rb
46-
sig { override.params(offenses: T::Array[Packwerk::ReferenceOffense], repo_link: String, org_name: String).returns(String) }
47-
def format_offenses(offenses, repo_link, org_name)
46+
sig do
47+
override.params(
48+
offenses: T::Array[Packwerk::ReferenceOffense],
49+
repo_link: String,
50+
org_name: String
51+
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
52+
).returns(String)
53+
end
54+
def format_offenses(offenses, repo_link, org_name, repo_builder_url: nil)
4855
# your logic here
4956
end
5057
end
@@ -98,8 +105,15 @@ class MyFormatter
98105
extend T::Sig
99106
include DangerPackwerk::Update::OffensesFormatter
100107
# DangerPackwerk::BasicReferenceOffense
101-
sig { override.params(offenses: T::Array[DangerPackwerk::BasicReferenceOffense], repo_link: String, org_name: String).returns(String) }
102-
def format_offenses(offenses, repo_link, org_name)
108+
sig do
109+
override.params(
110+
offenses: T::Array[Packwerk::ReferenceOffense],
111+
repo_link: String,
112+
org_name: String
113+
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
114+
).returns(String)
115+
end
116+
def format_offenses(offenses, repo_link, org_name, repo_builder_url: nil)
103117
# your logic here
104118
end
105119
end

danger-packwerk.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
2727

2828
spec.files = Dir['README.md', 'lib/**/*']
2929
spec.require_paths = ['lib']
30-
spec.required_ruby_version = '>= 2.6'
30+
spec.required_ruby_version = '>= 3.1'
3131

3232
spec.add_dependency 'code_ownership'
3333
spec.add_dependency 'danger-plugin-api', '~> 1.0'

lib/danger-packwerk.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require 'sorbet-runtime'
66

77
module DangerPackwerk
8-
PACKAGE_TODO_PATTERN = T.let(/.*?package_todo\.yml\z/.freeze, Regexp)
8+
PACKAGE_TODO_PATTERN = T.let(/.*?package_todo\.yml\z/, Regexp)
99
DEPENDENCY_VIOLATION_TYPE = 'dependency'
1010
PRIVACY_VIOLATION_TYPE = 'privacy'
1111

lib/danger-packwerk/check/default_formatter.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ def initialize(custom_help_message: nil)
2121
override.params(
2222
offenses: T::Array[Packwerk::ReferenceOffense],
2323
repo_link: String,
24-
org_name: String
24+
org_name: String,
25+
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
2526
).returns(String)
2627
end
27-
def format_offenses(offenses, repo_link, org_name)
28+
def format_offenses(offenses, repo_link, org_name, repo_url_builder: nil)
2829
reference_offense = T.must(offenses.first)
2930
violation_types = offenses.map(&:violation_type)
3031
referencing_file = reference_offense.reference.relative_path
@@ -45,11 +46,18 @@ def format_offenses(offenses, repo_link, org_name)
4546
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'
4647
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}`."
4748

49+
constant_location_url =
50+
if repo_url_builder
51+
repo_url_builder.call(constant_location)
52+
else
53+
"#{repo_link}/blob/main/#{constant_location}"
54+
end
55+
4856
if violation_types.include?(::DangerPackwerk::DEPENDENCY_VIOLATION_TYPE) && violation_types.include?(::DangerPackwerk::PRIVACY_VIOLATION_TYPE)
4957
<<~MESSAGE
5058
**Packwerk Violation**
5159
- Type: Privacy :lock: + Dependency :knot:
52-
- Constant: [<ins>`#{constant_name}`</ins>](#{repo_link}/blob/main/#{constant_location})
60+
- Constant: [<ins>`#{constant_name}`</ins>](#{constant_location_url})
5361
- Owning pack: #{constant_source_package_name}
5462
#{constant_source_package_ownership_info.ownership_copy}
5563
@@ -69,7 +77,7 @@ def format_offenses(offenses, repo_link, org_name)
6977
<<~MESSAGE
7078
**Packwerk Violation**
7179
- Type: Dependency :knot:
72-
- Constant: [<ins>`#{constant_name}`</ins>](#{repo_link}/blob/main/#{constant_location})
80+
- Constant: [<ins>`#{constant_name}`</ins>](#{constant_location_url})
7381
- Owning pack: #{constant_source_package_name}
7482
#{constant_source_package_ownership_info.ownership_copy}
7583
@@ -88,7 +96,7 @@ def format_offenses(offenses, repo_link, org_name)
8896
<<~MESSAGE
8997
**Packwerk Violation**
9098
- Type: Privacy :lock:
91-
- Constant: [<ins>`#{constant_name}`</ins>](#{repo_link}/blob/main/#{constant_location})
99+
- Constant: [<ins>`#{constant_name}`</ins>](#{constant_location_url})
92100
- Owning pack: #{constant_source_package_name}
93101
#{constant_source_package_ownership_info.ownership_copy}
94102

lib/danger-packwerk/check/offenses_formatter.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ module OffensesFormatter
1414
abstract.params(
1515
offenses: T::Array[Packwerk::ReferenceOffense],
1616
repo_link: String,
17-
org_name: String
17+
org_name: String,
18+
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
1819
).returns(String)
1920
end
20-
def format_offenses(offenses, repo_link, org_name); end
21+
def format_offenses(offenses, repo_link, org_name, repo_url_builder: nil); end
2122
end
2223
end
2324
end

lib/danger-packwerk/danger_package_todo_yml_changes.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def check(
4242
)
4343
offenses_formatter ||= Update::DefaultFormatter.new
4444
repo_link = github.pr_json[:base][:repo][:html_url]
45+
repo_url_builder = ->(constant_path) { "#{repo_link}/blob/#{github.pr_json[:head][:ref]}/#{constant_path}" }
4546
org_name = github.pr_json[:base][:repo][:owner][:login]
4647

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

6465
markdown(
65-
offenses_formatter.format_offenses(violations, repo_link, org_name),
66+
offenses_formatter.format_offenses(violations, repo_link, org_name, repo_url_builder: repo_url_builder),
6667
line: location.line_number,
6768
file: git_filesystem.convert_to_filesystem(location.file)
6869
)

lib/danger-packwerk/danger_packwerk.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def check(
6464
)
6565
offenses_formatter ||= Check::DefaultFormatter.new
6666
repo_link = github.pr_json[:base][:repo][:html_url]
67+
repo_url_builder = ->(constant_path) { "#{repo_link}/blob/#{github.pr_json[:head][:ref]}/#{constant_path}" }
6768
org_name = github.pr_json[:base][:repo][:owner][:login]
6869

6970
# 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
@@ -140,7 +141,7 @@ def check(
140141
line_number = reference_offense.location&.line
141142
referencing_file = reference_offense.reference.relative_path
142143

143-
message = offenses_formatter.format_offenses(unique_packwerk_reference_offenses, repo_link, org_name)
144+
message = offenses_formatter.format_offenses(unique_packwerk_reference_offenses, repo_link, org_name, repo_url_builder: repo_url_builder)
144145
markdown(message, file: git_filesystem.convert_to_filesystem(referencing_file), line: line_number)
145146
end
146147

lib/danger-packwerk/update/default_formatter.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ def initialize(custom_help_message: nil)
1515
@custom_help_message = custom_help_message
1616
end
1717

18-
sig { override.params(offenses: T::Array[BasicReferenceOffense], repo_link: String, org_name: String).returns(String) }
19-
def format_offenses(offenses, repo_link, org_name)
18+
sig do
19+
override.params(
20+
offenses: T::Array[BasicReferenceOffense],
21+
repo_link: String,
22+
org_name: String,
23+
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
24+
).returns(String)
25+
end
26+
def format_offenses(offenses, repo_link, org_name, repo_url_builder: nil)
2027
violation = T.must(offenses.first)
2128
referencing_file_pack = ParsePackwerk.package_from_path(violation.file)
2229
# We remove leading double colons as they feel like an implementation detail of packwerk.

lib/danger-packwerk/update/offenses_formatter.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ module OffensesFormatter
1414
abstract.params(
1515
offenses: T::Array[BasicReferenceOffense],
1616
repo_link: String,
17-
org_name: String
17+
org_name: String,
18+
repo_url_builder: T.nilable(T.proc.params(constant_path: String).returns(String))
1819
).returns(String)
1920
end
20-
def format_offenses(offenses, repo_link, org_name); end
21+
def format_offenses(offenses, repo_link, org_name, repo_url_builder: nil); end
2122
end
2223
end
2324
end

lib/danger-packwerk/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# frozen_string_literal: true
33

44
module DangerPackwerk
5-
VERSION = '0.14.4'
5+
VERSION = '0.15.0'
66
end

spec/danger_packwerk/danger_package_todo_yml_changes_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ module DangerPackwerk
145145
Class.new do
146146
include Update::OffensesFormatter
147147

148-
def format_offenses(added_violations, repo_link, org_name)
148+
def format_offenses(added_violations, repo_link, org_name, repo_url_builder: nil)
149149
<<~MESSAGE
150150
There are #{added_violations.count} new violations,
151151
with class_names #{added_violations.map(&:class_name).uniq.sort},
@@ -1159,7 +1159,7 @@ def format_offenses(added_violations, repo_link, org_name)
11591159
Class.new do
11601160
include Update::OffensesFormatter
11611161

1162-
def format_offenses(added_violations, repo_link, org_name)
1162+
def format_offenses(added_violations, repo_link, org_name, repo_url_builder: nil)
11631163
<<~MESSAGE
11641164
There are #{added_violations.count} new violations,
11651165
with class_names #{added_violations.map(&:class_name).uniq.sort},

0 commit comments

Comments
 (0)