diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e920f5b..ef8d7d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,87 +1,34 @@ -name: CI -on: [push, pull_request] -env: - RAILS_ENV: test - DATABASE_URL: postgresql://postgres:@localhost/test - DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: true +name: Ruby test + +on: + pull_request: + push: + branches: + - master + +concurrency: + group: ${{ github.ref_name }}-${{ github.workflow }} + cancel-in-progress: true + jobs: + rubocop: + uses: theforeman/actions/.github/workflows/rubocop.yml@v0 + with: + command: bundle exec rubocop --parallel --format github + test: - runs-on: ubuntu-latest - services: - postgres: - image: postgres:12.1 - ports: ["5432:5432"] - options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 + name: Ruby + needs: rubocop strategy: fail-fast: false matrix: - foreman-core-branch: - - 3.1-stable - - 3.2-stable - - 3.3-stable - - 3.4-stable - - 3.5-stable - - 3.6-stable - - 3.7-stable - - 3.8-stable - - 3.9-stable - - develop - ruby-version: [2.7] - node-version: [14] - steps: - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install build-essential libcurl4-openssl-dev libvirt-dev ruby-libvirt zlib1g-dev libpq-dev - - uses: actions/checkout@v3 - with: - repository: theforeman/foreman - ref: ${{ matrix.foreman-core-branch }} - fetch-depth: 0 - - name: Apply patches for Foreman >= 3.2 and <= 3.7 - if: ${{ contains(fromJson('["3.2-stable", "3.3-stable", "3.4-stable", "3.5-stable", "3.6-stable", "3.7-stable"]'), matrix.foreman-core-branch) }} - run: | - git cherry-pick -n 1c3a4155f286352e0abbf0d0b298e47e81c2d6c5 # Pin minitest < 5.19 to resolve test failures (see https://github.com/theforeman/foreman/pull/9781) - - uses: actions/checkout@v3 - with: - path: foreman_git_templates - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby-version }} - - name: Setup Node - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - - name: Setup Bundler - run: | - gem install bundler -v 2.4.22 - bundle config path vendor/bundle - bundle config set without journald development console mysql2 sqlite - echo "gem 'foreman_git_templates', path: './foreman_git_templates'" > bundler.d/foreman_git_templates.local.rb - bundle lock --update - - name: Cache gems - uses: actions/cache@v3 - with: - path: vendor/bundle - key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }} - restore-keys: | - ${{ runner.os }}-gems- - - name: Setup Plugin - run: | - bundle install --jobs=3 --retry=3 - bundle exec rake db:create - bundle exec rake db:migrate - npm install - bundle exec rake webpack:compile - - name: Run rubocop - run: bundle exec rake foreman_git_templates:rubocop - if: ${{ matrix.foreman-core-branch == 'develop' }} - - name: Run plugin tests - run: | - bundle exec rake test:foreman_git_templates - bundle exec rake test TEST="test/unit/foreman/access_permissions_test.rb" - - name: Precompile plugin assets - run: bundle exec rake 'plugin:assets:precompile[foreman_git_templates]' - env: - RAILS_ENV: production + foreman: + - "develop" + - "3.10-stable" + - "3.9-stable" + - "3.8-stable" + - "3.7-stable" + uses: theforeman/actions/.github/workflows/foreman_plugin.yml@v0 + with: + plugin: foreman_git_templates + foreman_version: ${{ matrix.foreman }} diff --git a/.rubocop.yml b/.rubocop.yml index bba1010..e4a2608 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -12,4 +12,8 @@ Metrics/BlockLength: Metrics/ClassLength: Exclude: - - 'test/**/*' \ No newline at end of file + - 'test/**/*' + +Rails/ApplicationRecord: + Exclude: + - db/**/* diff --git a/Rakefile b/Rakefile index 6e71831..2973f90 100755 --- a/Rakefile +++ b/Rakefile @@ -40,7 +40,7 @@ task default: :test begin require 'rubocop/rake_task' RuboCop::RakeTask.new -rescue StandardError => _e +rescue StandardError puts 'Rubocop not loaded.' end diff --git a/app/lib/foreman_git_templates/renderer/source/repository.rb b/app/lib/foreman_git_templates/renderer/source/repository.rb index be46ee8..00c05b4 100644 --- a/app/lib/foreman_git_templates/renderer/source/repository.rb +++ b/app/lib/foreman_git_templates/renderer/source/repository.rb @@ -5,7 +5,7 @@ module Renderer module Source class Repository < Foreman::Renderer::Source::Base def initialize(template, repository_path) - @template = template + super(template) @repository_path = repository_path end diff --git a/app/models/concerns/foreman_git_templates/hostext/operating_system.rb b/app/models/concerns/foreman_git_templates/hostext/operating_system.rb index 9d6f831..c23206f 100644 --- a/app/models/concerns/foreman_git_templates/hostext/operating_system.rb +++ b/app/models/concerns/foreman_git_templates/hostext/operating_system.rb @@ -16,7 +16,6 @@ def provisioning_template(opts = {}) def available_template_kinds(provisioning = nil) return super unless repository_path - # rubocop:disable Layout/RescueEnsureAlignment @available_template_kinds ||= template_kinds(provisioning).map do |kind| repository_klass.new(name: kind.name).tap do |t| t.template = RepositoryReader.call(repository_path, t.path) @@ -24,7 +23,6 @@ def available_template_kinds(provisioning = nil) rescue RepositoryReader::FileUnreadableError # file is missing or empty next end.compact - # rubocop:enable Layout/RescueEnsureAlignment end end diff --git a/app/services/foreman_git_templates/repository_fetcher.rb b/app/services/foreman_git_templates/repository_fetcher.rb index df045e5..b446c37 100644 --- a/app/services/foreman_git_templates/repository_fetcher.rb +++ b/app/services/foreman_git_templates/repository_fetcher.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'down' +require 'rest-client' module ForemanGitTemplates class RepositoryFetcher @@ -9,10 +9,10 @@ def initialize(repository_url) end def call - Down.download(repository_url) - rescue Down::ResponseError => e + RestClient::Request.execute(method: :get, url: repository_url, raw_response: true).file + rescue RestClient::RequestFailed => e raise RepositoryFetcherError, "Cannot fetch repository from #{repository_url}. Response code: #{e.response.code}" - rescue Down::Error => e + rescue RestClient::Exception => e raise RepositoryFetcherError, "Cannot fetch repository from #{repository_url}, #{e.message}" end diff --git a/db/migrate/20230203130941_fix_git_templates_settings_category_to_dsl.rb b/db/migrate/20230203130941_fix_git_templates_settings_category_to_dsl.rb index 186ebfd..a018ebe 100644 --- a/db/migrate/20230203130941_fix_git_templates_settings_category_to_dsl.rb +++ b/db/migrate/20230203130941_fix_git_templates_settings_category_to_dsl.rb @@ -6,6 +6,8 @@ class MigrationSettings < ActiveRecord::Base end def up - MigrationSettings.where(category: 'Setting::GitTemplates').update_all(category: 'Setting') if column_exists?(:settings, :category) + return unless column_exists?(:settings, :category) + + MigrationSettings.where(category: 'Setting::GitTemplates').update_all(category: 'Setting') end end diff --git a/foreman_git_templates.gemspec b/foreman_git_templates.gemspec index d1114d5..f0848c1 100644 --- a/foreman_git_templates.gemspec +++ b/foreman_git_templates.gemspec @@ -14,7 +14,8 @@ Gem::Specification.new do |s| s.files = Dir['{app,config,db,lib}/**/*'] + ['LICENSE', 'Rakefile', 'README.md'] s.test_files = Dir['test/**/*'] - s.add_dependency 'down', '~> 4.5' + s.required_ruby_version = '>= 2.5', '< 4' + s.add_dependency 'rest-client', '~> 2' s.add_development_dependency 'rdoc' diff --git a/lib/tasks/foreman_git_templates_tasks.rake b/lib/tasks/foreman_git_templates_tasks.rake index d037891..2c7fa93 100644 --- a/lib/tasks/foreman_git_templates_tasks.rake +++ b/lib/tasks/foreman_git_templates_tasks.rake @@ -14,27 +14,4 @@ namespace :test do end end -namespace :foreman_git_templates do - task rubocop: :environment do - begin - require 'rubocop/rake_task' - RuboCop::RakeTask.new(:rubocop_foreman_git_templates) do |task| - task.patterns = ["#{ForemanGitTemplates::Engine.root}/app/**/*.rb", - "#{ForemanGitTemplates::Engine.root}/lib/**/*.rb", - "#{ForemanGitTemplates::Engine.root}/test/**/*.rb"] - end - rescue StandardError - puts 'Rubocop not loaded.' - end - - Rake::Task['rubocop_foreman_git_templates'].invoke - end -end - Rake::Task[:test].enhance ['test:foreman_git_templates'] - -load 'tasks/jenkins.rake' -if Rake::Task.task_defined?(:'jenkins:unit') - Rake::Task['jenkins:unit'].enhance ['test:foreman_git_templates', - 'foreman_git_templates:rubocop'] -end diff --git a/test/unit/foreman_git_templates/access_permissions_test.rb b/test/unit/foreman_git_templates/access_permissions_test.rb new file mode 100644 index 0000000..625b637 --- /dev/null +++ b/test/unit/foreman_git_templates/access_permissions_test.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'test_plugin_helper' +require 'unit/shared/access_permissions_test_base' + +# Permissions are added in AccessPermissions with lists of controllers and +# actions that they enable access to. For non-admin users, we need to test +# that there are permissions available that cover every controller action, else +# it can't be delegated and this will lead to parts of the application that +# aren't functional for non-admin users. +# +# In particular, it's important that actions for AJAX requests are added to +# an appropriate permission so views using those requests function. +class AccessPermissionsTest < ActiveSupport::TestCase + include AccessPermissionsTestBase + + check_routes(ForemanGitTemplates::Engine.routes, []) +end diff --git a/test/unit/repository_fetcher_test.rb b/test/unit/repository_fetcher_test.rb index df4e96b..d40c67f 100644 --- a/test/unit/repository_fetcher_test.rb +++ b/test/unit/repository_fetcher_test.rb @@ -29,7 +29,11 @@ class RepositoryFetcherTest < ActiveSupport::TestCase test 'should raise RepositoryFetcherError when url is incorrect' do url = 'incorrect_url' - msg = 'URL scheme needs to be http or https' + # "rest-client" normalizes a URL by adding a protocol if none is present + requested_url = "http://#{url}" + stub_request(:get, requested_url).to_return(status: 404) + + msg = 'Response code: 404' assert_raises_with_message(ForemanGitTemplates::RepositoryFetcher::RepositoryFetcherError, msg) do ForemanGitTemplates::RepositoryFetcher.call(url) end