From 22ae898b2490d411104fe73a757f445c3c290fd2 Mon Sep 17 00:00:00 2001 From: Balasankar 'Balu' C Date: Mon, 8 Apr 2024 16:29:41 +0000 Subject: [PATCH] Add Rubocop cop to enforce default_version in software definitions Prevent empty build caches caused by a lack of default version. Closes https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/8476 Signed-off-by: Balasankar 'Balu' C --- .rubocop.yml | 15 +++++++ lib/rubocop/cop/specify_default_version.rb | 28 +++++++++++++ lib/rubocop/rubocop.rb | 1 + .../cop/specify_default_version_spec.rb | 41 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 lib/rubocop/cop/specify_default_version.rb create mode 100644 spec/lib/rubocop/cop/specify_default_version_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index b80b0ad7e..22fc66ec5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -78,6 +78,21 @@ Cop/AvoidUsingEnv: - lib/gitlab/util.rb - spec/lib/gitlab/util_spec.rb +Cop/SpecifyDefaultVersion: + Enabled: true + Include: + - config/software/*.rb + Exclude: + # These files either use `path` source and doesn't need a default_version + # or is just a wrapper around other definitions + - config/software/gitlab-config-template.rb + - config/software/gitlab-cookbooks.rb + - config/software/gitlab-ctl-ee.rb + - config/software/gitlab-ctl.rb + - config/software/gitlab-scripts.rb + - config/software/gitlab-selinux.rb + - config/software/openssl.rb + Style/MultilineIfModifier: Enabled: false diff --git a/lib/rubocop/cop/specify_default_version.rb b/lib/rubocop/cop/specify_default_version.rb new file mode 100644 index 000000000..4c2bf4aac --- /dev/null +++ b/lib/rubocop/cop/specify_default_version.rb @@ -0,0 +1,28 @@ +module Rubocop + module Cop + class SpecifyDefaultVersion < RuboCop::Cop::Base + NOTICE_REGEXP = '^ *default_version'.freeze + MSG = 'Specify default_version for the component.'.freeze + + def on_new_investigation + return if notice_found?(processed_source) + + add_global_offense(format(MSG)) + end + + private + + def notice_found?(processed_source) + notice_regexp = Regexp.new(NOTICE_REGEXP) + + notice_found = false + processed_source.lines.each do |line| + notice_found = notice_regexp.match?(line) + break if notice_found + end + + notice_found + end + end + end +end diff --git a/lib/rubocop/rubocop.rb b/lib/rubocop/rubocop.rb index a344d8094..7d7a20ee1 100644 --- a/lib/rubocop/rubocop.rb +++ b/lib/rubocop/rubocop.rb @@ -1 +1,2 @@ require_relative 'cop/avoid_using_env' +require_relative 'cop/specify_default_version' diff --git a/spec/lib/rubocop/cop/specify_default_version_spec.rb b/spec/lib/rubocop/cop/specify_default_version_spec.rb new file mode 100644 index 000000000..1dec5974f --- /dev/null +++ b/spec/lib/rubocop/cop/specify_default_version_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' +require 'rubocop/rspec/cop_helper' +require 'rubocop/rspec/expect_offense' + +require 'rubocop/cop/specify_default_version' + +RSpec.describe Rubocop::Cop::SpecifyDefaultVersion do + include CopHelper + include RuboCop::RSpec::ExpectOffense + + subject(:cop) { described_class.new } + + it 'flags violation for software definition files without default_version set' do + expect_offense(<<~RUBY) + name 'sample' + ^{} Specify default_version for the component. + + license 'MIT' + + build do + make + make install + end + RUBY + end + + it 'does not flag violation for software definition files with default_version set' do + expect_no_offenses(<<~RUBY) + name 'sample' + + license 'MIT' + + default_version '1.0.0' + + build do + make + make install + end + RUBY + end +end