Skip to content

Commit

Permalink
Allow use_packs to work without code_ownership configured (#77)
Browse files Browse the repository at this point in the history
* fix broken test

* add failing test

* upgrade sorbet

* Allow use_packs to work without code_ownership

* add failing test

* fix failing test

* fix tests

* bump version
  • Loading branch information
Alex Evanczuk authored Mar 30, 2023
1 parent c29bdc0 commit a024eb1
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 42 deletions.
40 changes: 21 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GIT
PATH
remote: .
specs:
use_packs (0.0.13)
use_packs (0.0.14)
code_ownership
colorize
packs
Expand Down Expand Up @@ -53,11 +53,11 @@ GEM
smart_properties
builder (3.2.4)
byebug (11.1.3)
code_ownership (1.29.2)
code_ownership (1.32.2)
code_teams (~> 1.0)
packs
sorbet-runtime
code_teams (1.0.0)
code_teams (1.0.1)
sorbet-runtime
coderay (1.1.3)
colorize (0.8.1)
Expand All @@ -78,10 +78,10 @@ GEM
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
packs (0.0.5)
packs (0.0.6)
sorbet-runtime
parallel (1.22.1)
parse_packwerk (0.18.0)
parse_packwerk (0.18.2)
sorbet-runtime
parser (3.1.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -133,7 +133,7 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-packs (0.0.33)
rubocop-packs (0.0.34)
activesupport
packs
parse_packwerk
Expand All @@ -142,24 +142,25 @@ GEM
sorbet-runtime
rubocop-rspec (2.13.2)
rubocop (~> 1.33)
rubocop-sorbet (0.6.11)
rubocop-sorbet (0.7.0)
rubocop (>= 0.90.0)
ruby-graphviz (1.2.5)
rexml
ruby-progressbar (1.11.0)
smart_properties (1.17.0)
sorbet (0.5.9962)
sorbet-static (= 0.5.9962)
sorbet (0.5.10736)
sorbet-static (= 0.5.10736)
sorbet-runtime (0.5.9976)
sorbet-static (0.5.9962-universal-darwin-14)
sorbet-static (0.5.9962-universal-darwin-15)
sorbet-static (0.5.9962-universal-darwin-16)
sorbet-static (0.5.9962-universal-darwin-17)
sorbet-static (0.5.9962-universal-darwin-18)
sorbet-static (0.5.9962-universal-darwin-19)
sorbet-static (0.5.9962-universal-darwin-20)
sorbet-static (0.5.9962-universal-darwin-21)
sorbet-static (0.5.9962-x86_64-linux)
sorbet-static (0.5.10736-universal-darwin-14)
sorbet-static (0.5.10736-universal-darwin-15)
sorbet-static (0.5.10736-universal-darwin-16)
sorbet-static (0.5.10736-universal-darwin-17)
sorbet-static (0.5.10736-universal-darwin-18)
sorbet-static (0.5.10736-universal-darwin-19)
sorbet-static (0.5.10736-universal-darwin-20)
sorbet-static (0.5.10736-universal-darwin-21)
sorbet-static (0.5.10736-universal-darwin-22)
sorbet-static (0.5.10736-x86_64-linux)
spoom (1.1.11)
sorbet (>= 0.5.9204)
sorbet-runtime (>= 0.5.9204)
Expand Down Expand Up @@ -190,8 +191,9 @@ GEM
unparser (0.6.5)
diff-lcs (~> 1.3)
parser (>= 3.1.0)
visualize_packwerk (0.0.6)
visualize_packwerk (0.1.2)
code_ownership
packs
parse_packwerk
rake
ruby-graphviz
Expand Down
13 changes: 6 additions & 7 deletions lib/use_packs/code_ownership_post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ def before_move_file!(file_move_operation)
relative_path_to_destination = file_move_operation.destination_pathname

code_owners_allow_list_file = Pathname.new('config/code_ownership.yml')
return if !code_owners_allow_list_file.exist?

if code_owners_allow_list_file.exist?
UsePacks.replace_in_file(
file: code_owners_allow_list_file.to_s,
find: relative_path_to_origin,
replace_with: relative_path_to_destination
)
end
UsePacks.replace_in_file(
file: code_owners_allow_list_file.to_s,
find: relative_path_to_origin,
replace_with: relative_path_to_destination
)

team = CodeOwnership.for_file(relative_path_to_origin.to_s)

Expand Down
14 changes: 11 additions & 3 deletions lib/use_packs/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,21 @@ def self.create_pack_if_not_exists!(pack_name:, enforce_privacy:, enforce_depend
if existing_package.nil?
should_enforce_dependenceies = enforce_dependencies.nil? ? UsePacks.config.enforce_dependencies : enforce_dependencies

# TODO: This should probably be `if defined?(CodeOwnership) && CodeOwnership.configured?`
# but we'll need to add an API to CodeOwnership to do this
if Pathname.new('config/code_ownership.yml').exist?
metadata = {
'owner' => team.nil? ? 'MyTeam' : team.name
}
else
metadata = {}
end

package = ParsePackwerk::Package.new(
enforce_dependencies: should_enforce_dependenceies,
enforce_privacy: enforce_privacy,
dependencies: [],
metadata: {
'owner' => team.nil? ? 'MyTeam' : team.name
},
metadata: metadata,
name: pack_name,
config: {}
)
Expand Down
3 changes: 2 additions & 1 deletion lib/use_packs/private/interactive_cli/team_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ module InteractiveCli
class TeamSelector
extend T::Sig

sig { params(prompt: TTY::Prompt, question_text: String).returns(CodeTeams::Team) }
sig { params(prompt: TTY::Prompt, question_text: String).returns(T.nilable(CodeTeams::Team)) }
def self.single_select(prompt, question_text: 'Please use space to select a team owner')
teams = CodeTeams.all.sort_by(&:name).to_h { |t| [t.name, t] }
return nil if teams.count == 0

team_selection = T.let(prompt.select(
question_text,
Expand Down
4 changes: 1 addition & 3 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/use_packs/private/interactive_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ module INPUTS
subject
end

it 'allows creating a pack even if no teams are setup' do
expect(UsePacks).to receive(:create_pack!).with(pack_name: 'packs/my_new_pack', team: nil)
prompt.input << "Create\r"
prompt.input << "my_new_pack\r"
prompt.input.rewind
subject
end

it 'shows teams listed alphabetically and you can pick one with arrow keys' do
write_file('config/teams/zebras.yml', 'name: Zebras')
write_file('config/teams/artists.yml', 'name: Artists')
Expand Down
55 changes: 47 additions & 8 deletions spec/use_packs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def expect_files_to_not_exist(files)
end
end

def write_codeownership_config
write_file('config/code_ownership.yml', <<~CONTENTS)
owned_globs:
- '{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx}'
unowned_globs:
- app/services/horse_like/donkey.rb
- spec/services/horse_like/donkey_spec.rb
CONTENTS
end

before do
UsePacks.bust_cache!
CodeTeams.bust_caches!
Expand All @@ -36,6 +46,8 @@ def expect_files_to_not_exist(files)
end

it 'creates a package.yml correctly' do
write_codeownership_config

RuboCop::Packs.configure do |config|
config.required_pack_level_cops = ['Department/SomeCop']
end
Expand Down Expand Up @@ -63,8 +75,33 @@ def expect_files_to_not_exist(files)
expect(package.yml.read).to eq expected
end

context 'code ownership is not yet configured' do
it 'creates a package.yml correctly' do
UsePacks.create_pack!(pack_name: 'packs/my_pack')
ParsePackwerk.bust_cache!
package = ParsePackwerk.find('packs/my_pack')
expect(package.name).to eq('packs/my_pack')
expect(package.enforce_privacy).to eq(true)
expect(package.enforce_dependencies).to eq(true)
expect(package.dependencies).to eq([])
expect(package.metadata).to eq({})
expect(package.directory.join(RuboCop::Packs::PACK_LEVEL_RUBOCOP_YML).read).to eq(<<~YML)
Department/SomeCop:
Enabled: true
YML

expected = <<~EXPECTED
enforce_dependencies: true
enforce_privacy: true
EXPECTED

expect(package.yml.read).to eq expected
end
end

context 'use packwerk is configured to not enforce dependencies by default' do
it 'creates a package.yml correctly' do
write_codeownership_config
UsePacks.configure { |config| config.enforce_dependencies = false }
UsePacks.create_pack!(pack_name: 'packs/my_pack')
expected_package = ParsePackwerk::Package.new(
Expand Down Expand Up @@ -104,6 +141,7 @@ def expect_files_to_not_exist(files)
end

it 'automatically adds the owner metadata key' do
write_codeownership_config
UsePacks.create_pack!(pack_name: 'packs/my_pack')
ParsePackwerk.bust_cache!
package = ParsePackwerk.find('packs/my_pack')
Expand All @@ -114,6 +152,7 @@ def expect_files_to_not_exist(files)

context 'team owner is provided' do
it 'automatically adds the owner metadata key' do
write_codeownership_config
write_file('config/teams/artists.yml', 'name: Artists')
UsePacks.create_pack!(pack_name: 'packs/my_pack', team: CodeTeams.find('Artists'))
ParsePackwerk.bust_cache!
Expand All @@ -138,6 +177,8 @@ def expect_files_to_not_exist(files)
let(:pack_name) { 'packs/fruits/apples' }

it 'creates a package.yml correctly' do
write_codeownership_config

RuboCop::Packs.configure do |config|
config.required_pack_level_cops = ['Department/SomeCop']
end
Expand Down Expand Up @@ -618,15 +659,11 @@ def expect_files_to_not_exist(files)
end

describe 'CodeOwnershipPostProcessor' do
it 'modifies an application-specific file, config/code_ownership.yml, correctly' do
write_file('config/code_ownership.yml', <<~CONTENTS)
owned_globs:
- '{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx}'
unowned_globs:
- app/services/horse_like/donkey.rb
- spec/services/horse_like/donkey_spec.rb
CONTENTS
before do
write_codeownership_config
end

it 'modifies an application-specific file, config/code_ownership.yml, correctly' do
write_file('app/services/horse_like/donkey.rb')
write_file('spec/services/horse_like/donkey_spec.rb')
write_package_yml('packs/animals')
Expand Down Expand Up @@ -672,6 +709,8 @@ def expect_files_to_not_exist(files)
end

it 'removes file annotations if the destination pack has file annotations' do
write_codeownership_config

write_package_yml('packs/owned_by_artists', owner: 'Artists')
write_file('app/services/foo.rb', '# @team Chefs')
write_file('config/teams/artists.yml', 'name: Artists')
Expand Down
2 changes: 1 addition & 1 deletion use_packs.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'use_packs'
spec.version = '0.0.13'
spec.version = '0.0.14'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']

Expand Down

0 comments on commit a024eb1

Please sign in to comment.