Skip to content

Commit 22ae898

Browse files
balasankarcRobert Marshall
authored andcommitted
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 <[email protected]>
1 parent 72c2e00 commit 22ae898

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

.rubocop.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ Cop/AvoidUsingEnv:
7878
- lib/gitlab/util.rb
7979
- spec/lib/gitlab/util_spec.rb
8080

81+
Cop/SpecifyDefaultVersion:
82+
Enabled: true
83+
Include:
84+
- config/software/*.rb
85+
Exclude:
86+
# These files either use `path` source and doesn't need a default_version
87+
# or is just a wrapper around other definitions
88+
- config/software/gitlab-config-template.rb
89+
- config/software/gitlab-cookbooks.rb
90+
- config/software/gitlab-ctl-ee.rb
91+
- config/software/gitlab-ctl.rb
92+
- config/software/gitlab-scripts.rb
93+
- config/software/gitlab-selinux.rb
94+
- config/software/openssl.rb
95+
8196
Style/MultilineIfModifier:
8297
Enabled: false
8398

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Rubocop
2+
module Cop
3+
class SpecifyDefaultVersion < RuboCop::Cop::Base
4+
NOTICE_REGEXP = '^ *default_version'.freeze
5+
MSG = 'Specify default_version for the component.'.freeze
6+
7+
def on_new_investigation
8+
return if notice_found?(processed_source)
9+
10+
add_global_offense(format(MSG))
11+
end
12+
13+
private
14+
15+
def notice_found?(processed_source)
16+
notice_regexp = Regexp.new(NOTICE_REGEXP)
17+
18+
notice_found = false
19+
processed_source.lines.each do |line|
20+
notice_found = notice_regexp.match?(line)
21+
break if notice_found
22+
end
23+
24+
notice_found
25+
end
26+
end
27+
end
28+
end

lib/rubocop/rubocop.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
require_relative 'cop/avoid_using_env'
2+
require_relative 'cop/specify_default_version'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'spec_helper'
2+
require 'rubocop/rspec/cop_helper'
3+
require 'rubocop/rspec/expect_offense'
4+
5+
require 'rubocop/cop/specify_default_version'
6+
7+
RSpec.describe Rubocop::Cop::SpecifyDefaultVersion do
8+
include CopHelper
9+
include RuboCop::RSpec::ExpectOffense
10+
11+
subject(:cop) { described_class.new }
12+
13+
it 'flags violation for software definition files without default_version set' do
14+
expect_offense(<<~RUBY)
15+
name 'sample'
16+
^{} Specify default_version for the component.
17+
18+
license 'MIT'
19+
20+
build do
21+
make
22+
make install
23+
end
24+
RUBY
25+
end
26+
27+
it 'does not flag violation for software definition files with default_version set' do
28+
expect_no_offenses(<<~RUBY)
29+
name 'sample'
30+
31+
license 'MIT'
32+
33+
default_version '1.0.0'
34+
35+
build do
36+
make
37+
make install
38+
end
39+
RUBY
40+
end
41+
end

0 commit comments

Comments
 (0)