Skip to content

Commit

Permalink
Use theforeman-rubocop (#99)
Browse files Browse the repository at this point in the history
* Bump actions

* Use theforeman-rubocop gem

* Rubocopo auto-correct

* Rupocop auto-gen-config
  • Loading branch information
kamils-iRonin authored Apr 5, 2023
1 parent 856f46e commit 86659f8
Show file tree
Hide file tree
Showing 82 changed files with 432 additions and 405 deletions.
31 changes: 9 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,8 @@ env:
DATABASE_URL: postgresql://postgres:@localhost/test
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: true
jobs:
rubocop:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
- name: Setup
run: |
gem install bundler
bundle install --jobs=3 --retry=3
- name: Run rubocop
run: bundle exec rubocop
test:
runs-on: ubuntu-latest
needs: rubocop
timeout-minutes: 60
services:
postgres:
Expand All @@ -40,7 +24,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y build-essential libcurl4-openssl-dev libvirt-dev ruby-libvirt zlib1g-dev libpq-dev
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
repository: netbox-community/netbox-docker
ref: 2.2.0
Expand All @@ -59,23 +43,23 @@ jobs:
docker-compose pull
docker-compose up -d
sleep 180
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
repository: theforeman/foreman
ref: ${{ matrix.foreman-core-branch }}
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
path: foreman_netbox
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Setup Node
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Cache node modules
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Expand All @@ -92,7 +76,7 @@ jobs:
echo "gem 'foreman_netbox', path: './foreman_netbox'" > bundler.d/foreman_netbox.local.rb
bundle lock --update
- name: Cache gems
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
Expand All @@ -105,6 +89,9 @@ jobs:
bundle exec rake db:migrate
npm install
bundle exec rake webpack:compile
- name: Run rubocop
run: bundle exec rake foreman_netbox:rubocop
if: ${{ matrix.foreman-core-branch == 'develop' }}
- name: Run plugin tests
run: |
bundle exec rake test:foreman_netbox
Expand Down
57 changes: 13 additions & 44 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,24 @@
require: rubocop-rails
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.6
inherit_gem:
theforeman-rubocop:
- default.yml

Rails:
Enabled: true

# Don't enforce documentation
Style/Documentation:
Enabled: false

# Force before_filter until upgrade to Rails 4
Rails/ActionFilter:
EnforcedStyle: filter

Metrics/ClassLength:
Layout/LineLength:
Exclude:
- 'test/**/*'

Metrics/MethodLength:
Max: 20

Metrics/LineLength:
Max: 200

Metrics/AbcSize:
Max: 31

Metrics/PerceivedComplexity:
Max: 10

Metrics/BlockLength:
Exclude:
- 'lib/foreman_gaffer_tape/engine.rb'
- 'test/**/*'
- 'lib/foreman_netbox/engine.rb'

Metrics/CyclomaticComplexity:
Max: 10

Style/Next:
Enabled: false

# Support both ruby19 and hash_rockets
Style/HashSyntax:
Enabled: false

Style/FormatStringToken:
EnforcedStyle: template

Style/CaseLikeIf:
Enabled: false
Metrics/ClassLength:
Exclude:
- 'lib/foreman_gaffer_tape/engine.rb'
- 'test/**/*'

Gemspec/RequiredRubyVersion:
Enabled: false
Rails/DynamicFindBy:
Exclude:
- 'app/interactors/**/*'
67 changes: 67 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-03-02 19:59:08 UTC using RuboCop version 1.23.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'foreman_netbox.gemspec'

# Offense count: 10
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 31

# Offense count: 3
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 13
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 20

# Offense count: 2
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 10

# Offense count: 2
# Configuration parameters: MinSize.
Performance/CollectionLiteralInLoop:
Exclude:
- 'test/services/foreman_netbox/netbox_parameters_comparator_test.rb'

# Offense count: 4
# Configuration parameters: MaxUnannotatedPlaceholdersAllowed, IgnoredMethods.
# SupportedStyles: annotated, template, unannotated
Style/FormatStringToken:
EnforcedStyle: template

# Offense count: 3
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb'
- 'app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb'
- 'lib/tasks/foreman_netbox_tasks.rake'

# Offense count: 2
Style/MultilineBlockChain:
Exclude:
- 'app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb'
- 'app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb'

# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 149
2 changes: 1 addition & 1 deletion app/interactors/foreman_netbox/delete_host/organizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Organizer
include ::Interactor::Organizer

organize DeleteHost::DeleteVirtualMachine,
DeleteHost::DeleteDevice
DeleteHost::DeleteDevice
end
end
end
2 changes: 1 addition & 1 deletion app/interactors/foreman_netbox/sync_host/organizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Organizer
end

organize SyncHost::SyncVirtualMachine::Organizer,
SyncHost::SyncDevice::Organizer
SyncHost::SyncDevice::Organizer

def call
super
Expand Down
2 changes: 0 additions & 2 deletions app/interactors/foreman_netbox/sync_host/sync_device/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ class Find
include ::Interactor

def call
# rubocop:disable Rails/DynamicFindBy
context.device = find_by_serial || find_by_mac || find_by_name
# rubocop:enable Rails/DynamicFindBy
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
context.fail!(error: "#{self.class}: #{e}")
Expand Down
20 changes: 10 additions & 10 deletions app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class Organizer
end

organize SyncDevice::Validate,
SyncHost::SyncTags::Organizer,
SyncHost::SyncTenant::Organizer,
SyncDevice::SyncSite::Organizer,
SyncDevice::SyncDeviceRole::Organizer,
SyncDevice::SyncDeviceType::Organizer,
SyncDevice::Find,
SyncDevice::Create,
SyncDevice::SyncInterfaces::Organizer,
SyncDevice::Update,
SyncDevice::SaveNetboxUrl
SyncHost::SyncTags::Organizer,
SyncHost::SyncTenant::Organizer,
SyncDevice::SyncSite::Organizer,
SyncDevice::SyncDeviceRole::Organizer,
SyncDevice::SyncDeviceType::Organizer,
SyncDevice::Find,
SyncDevice::Create,
SyncDevice::SyncInterfaces::Organizer,
SyncDevice::Update,
SyncDevice::SaveNetboxUrl
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Organizer
end

organize SyncDeviceRole::Find,
SyncDeviceRole::Create
SyncDeviceRole::Create
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Organizer
end

organize SyncDeviceType::SyncManufacturer::Organizer,
SyncDeviceType::Find,
SyncDeviceType::Update,
SyncDeviceType::Create
SyncDeviceType::Find,
SyncDeviceType::Update,
SyncDeviceType::Create
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class Find
include ::Interactor

def call
# rubocop:disable Rails/DynamicFindBy
context.manufacturer = find_by_slug || find_by_name
# rubocop:enable Rails/DynamicFindBy
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError,
NetboxClientRuby::RemoteError => e
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
context.fail!(error: "#{self.class}: #{e}")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Organizer
end

organize SyncManufacturer::Find,
SyncManufacturer::Create
SyncManufacturer::Create
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call

def params
{
device_id: device.id
device_id: device.id,
}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Organizer
end

organize SyncInterfaces::Find,
SyncInterfaces::Delete,
SyncInterfaces::Create,
SyncInterfaces::SyncIpAddresses::Organizer,
SyncInterfaces::Update
SyncInterfaces::Delete,
SyncInterfaces::Create,
SyncInterfaces::SyncIpAddresses::Organizer,
SyncInterfaces::Update
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def call
ip_addresses_netbox_params = netbox_params.fetch(:ip_addresses, [])

context.interfaces.each do |netbox_interface|
host_interface_ips = ip_addresses_netbox_params.select { |i| i.dig(:interface, :name) == netbox_interface.name }
host_interface_ips = ip_addresses_netbox_params.select do |i|
i.dig(:interface, :name) == netbox_interface.name
end
.map { |i| i.fetch(:address) }

context.ip_addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call

def params
{
device_id: device.id
device_id: device.id,
}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Organizer
end

organize SyncIpAddresses::Find,
SyncIpAddresses::Delete,
SyncIpAddresses::Update,
SyncIpAddresses::Create
SyncIpAddresses::Delete,
SyncIpAddresses::Update,
SyncIpAddresses::Create
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def update(netbox_interface)

return unless new_params

netbox_interface.mac_address = new_params[:mac_address] if netbox_interface.mac_address != new_params[:mac_address]
if netbox_interface.mac_address != new_params[:mac_address]
netbox_interface.mac_address = new_params[:mac_address]
end
assign_tags_to(netbox_interface)

netbox_interface.save
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Organizer
end

organize SyncSite::Find,
SyncSite::Update,
SyncSite::Create
SyncSite::Update,
SyncSite::Create
end
end
end
Expand Down
Loading

0 comments on commit 86659f8

Please sign in to comment.