danger-missing_codeowners 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bcc8b0409118eaa0ddbab4a903a7573e734444cc7362c29e1eb1725cc617e70b
4
+ data.tar.gz: cf214013da36794cc4ab47da98914e3630eeed04e20fc42988188a0a12196206
5
+ SHA512:
6
+ metadata.gz: 217d2d8cc83f6e9b8d4eb64e30ee9cb8140da863665b8677ceafa25a85736b6b1dfce4d5d8aa3052335c578d997197e488dae1468e3279138c34374b546f16cb
7
+ data.tar.gz: 2fb655ba4b7c3966b6758b091e502f5489bc77d3a0e5106fd317bb4a5198d0a6761ddd6a100b31b6a4432b8179474c2426d2675f39b2bb0da20de090ba5be805
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg
3
+ .idea/
4
+ .yardoc
data/.rubocop.yml ADDED
@@ -0,0 +1,151 @@
1
+ # Defaults can be found here: https://github.com/bbatsov/rubocop/blob/master/config/default.yml
2
+
3
+ # If you don't like these settings, just delete this file :)
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.6
7
+
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+ Enabled: true
11
+
12
+ # kind_of? is a good way to check a type
13
+ Style/ClassCheck:
14
+ EnforcedStyle: kind_of?
15
+
16
+ # specs sometimes have useless assignments, which is fine
17
+ Lint/UselessAssignment:
18
+ Exclude:
19
+ - '**/spec/**/*'
20
+
21
+ # We could potentially enable the 2 below:
22
+ Layout/FirstHashElementIndentation:
23
+ Enabled: false
24
+
25
+ Layout/HashAlignment:
26
+ Enabled: false
27
+
28
+ # HoundCI doesn't like this rule
29
+ Layout/DotPosition:
30
+ Enabled: false
31
+
32
+ # We allow !! as it's an easy way to convert ot boolean
33
+ Style/DoubleNegation:
34
+ Enabled: false
35
+
36
+ # Cop supports --auto-correct.
37
+ Lint/UnusedBlockArgument:
38
+ Enabled: false
39
+
40
+ # We want to allow class Fastlane::Class
41
+ Style/ClassAndModuleChildren:
42
+ Enabled: false
43
+
44
+ Metrics/AbcSize:
45
+ Max: 60
46
+
47
+ # The %w might be confusing for new users
48
+ Style/WordArray:
49
+ MinSize: 19
50
+
51
+ # raise and fail are both okay
52
+ Style/SignalException:
53
+ Enabled: false
54
+
55
+ # Better too much 'return' than one missing
56
+ Style/RedundantReturn:
57
+ Enabled: false
58
+
59
+ # Having if in the same line might not always be good
60
+ Style/IfUnlessModifier:
61
+ Enabled: false
62
+
63
+ # and and or is okay
64
+ Style/AndOr:
65
+ Enabled: false
66
+
67
+ # Configuration parameters: CountComments.
68
+ Metrics/ClassLength:
69
+ Max: 350
70
+
71
+ Metrics/CyclomaticComplexity:
72
+ Max: 17
73
+
74
+ # Configuration parameters: AllowURI, URISchemes.
75
+ Metrics/LineLength:
76
+ Max: 370
77
+
78
+ # Configuration parameters: CountKeywordArgs.
79
+ Metrics/ParameterLists:
80
+ Max: 10
81
+
82
+ Metrics/PerceivedComplexity:
83
+ Max: 18
84
+
85
+ # Sometimes it's easier to read without guards
86
+ Style/GuardClause:
87
+ Enabled: false
88
+
89
+ # something = if something_else
90
+ # that's confusing
91
+ Style/ConditionalAssignment:
92
+ Enabled: false
93
+
94
+ # Better to have too much self than missing a self
95
+ Style/RedundantSelf:
96
+ Enabled: false
97
+
98
+ Metrics/MethodLength:
99
+ Max: 60
100
+
101
+ # We're not there yet
102
+ Style/Documentation:
103
+ Enabled: false
104
+
105
+ Metrics/ModuleLength:
106
+ Enabled: false
107
+
108
+ # Adds complexity
109
+ Style/IfInsideElse:
110
+ Enabled: false
111
+
112
+ # danger specific
113
+
114
+ Style/BlockComments:
115
+ Enabled: false
116
+
117
+ Layout/MultilineMethodCallIndentation:
118
+ EnforcedStyle: indented
119
+
120
+ # FIXME: 25
121
+ Metrics/BlockLength:
122
+ Max: 345
123
+ Exclude:
124
+ - "**/*_spec.rb"
125
+
126
+ Style/MixinGrouping:
127
+ Enabled: false
128
+
129
+ Style/FileName:
130
+ Enabled: false
131
+
132
+ Layout/HeredocIndentation:
133
+ Enabled: false
134
+
135
+ Style/SpecialGlobalVars:
136
+ Enabled: false
137
+
138
+ PercentLiteralDelimiters:
139
+ PreferredDelimiters:
140
+ "%": ()
141
+ "%i": ()
142
+ "%q": ()
143
+ "%Q": ()
144
+ "%r": "{}"
145
+ "%s": ()
146
+ "%w": ()
147
+ "%W": ()
148
+ "%x": ()
149
+
150
+ Security/YAMLLoad:
151
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ cache:
3
+ directories:
4
+ - bundle
5
+
6
+ rvm:
7
+ - 2.0
8
+ - 2.1.3
9
+ - 2.3.1
10
+
11
+ script:
12
+ - bundle exec rake spec
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in danger-missing_codeowners.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,166 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ danger-missing_codeowners (1.0.0)
5
+ danger-plugin-api (~> 1.0)
6
+ pathspec (~> 1.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ ast (2.4.2)
14
+ claide (1.0.3)
15
+ claide-plugins (0.9.2)
16
+ cork
17
+ nap
18
+ open4 (~> 1.3)
19
+ coderay (1.1.3)
20
+ colored2 (3.1.2)
21
+ cork (0.3.0)
22
+ colored2 (~> 3.1)
23
+ danger (8.3.1)
24
+ claide (~> 1.0)
25
+ claide-plugins (>= 0.9.2)
26
+ colored2 (~> 3.1)
27
+ cork (~> 0.1)
28
+ faraday (>= 0.9.0, < 2.0)
29
+ faraday-http-cache (~> 2.0)
30
+ git (~> 1.7)
31
+ kramdown (~> 2.3)
32
+ kramdown-parser-gfm (~> 1.0)
33
+ no_proxy_fix
34
+ octokit (~> 4.7)
35
+ terminal-table (>= 1, < 4)
36
+ danger-plugin-api (1.0.0)
37
+ danger (> 2.0)
38
+ diff-lcs (1.4.4)
39
+ faraday (1.6.0)
40
+ faraday-em_http (~> 1.0)
41
+ faraday-em_synchrony (~> 1.0)
42
+ faraday-excon (~> 1.1)
43
+ faraday-httpclient (~> 1.0.1)
44
+ faraday-net_http (~> 1.0)
45
+ faraday-net_http_persistent (~> 1.1)
46
+ faraday-patron (~> 1.0)
47
+ faraday-rack (~> 1.0)
48
+ multipart-post (>= 1.2, < 3)
49
+ ruby2_keywords (>= 0.0.4)
50
+ faraday-em_http (1.0.0)
51
+ faraday-em_synchrony (1.0.0)
52
+ faraday-excon (1.1.0)
53
+ faraday-http-cache (2.2.0)
54
+ faraday (>= 0.8)
55
+ faraday-httpclient (1.0.1)
56
+ faraday-net_http (1.0.1)
57
+ faraday-net_http_persistent (1.2.0)
58
+ faraday-patron (1.0.0)
59
+ faraday-rack (1.0.0)
60
+ ffi (1.15.3)
61
+ formatador (0.3.0)
62
+ git (1.9.1)
63
+ rchardet (~> 1.8)
64
+ guard (2.18.0)
65
+ formatador (>= 0.2.4)
66
+ listen (>= 2.7, < 4.0)
67
+ lumberjack (>= 1.0.12, < 2.0)
68
+ nenv (~> 0.1)
69
+ notiffany (~> 0.0)
70
+ pry (>= 0.13.0)
71
+ shellany (~> 0.0)
72
+ thor (>= 0.18.1)
73
+ guard-compat (1.2.1)
74
+ guard-rspec (4.7.3)
75
+ guard (~> 2.1)
76
+ guard-compat (~> 1.1)
77
+ rspec (>= 2.99.0, < 4.0)
78
+ kramdown (2.3.1)
79
+ rexml
80
+ kramdown-parser-gfm (1.1.0)
81
+ kramdown (~> 2.0)
82
+ listen (3.0.7)
83
+ rb-fsevent (>= 0.9.3)
84
+ rb-inotify (>= 0.9.7)
85
+ lumberjack (1.2.8)
86
+ method_source (1.0.0)
87
+ multipart-post (2.1.1)
88
+ nap (1.1.0)
89
+ nenv (0.3.0)
90
+ no_proxy_fix (0.1.2)
91
+ notiffany (0.1.3)
92
+ nenv (~> 0.1)
93
+ shellany (~> 0.0)
94
+ octokit (4.21.0)
95
+ faraday (>= 0.9)
96
+ sawyer (~> 0.8.0, >= 0.5.3)
97
+ open4 (1.3.4)
98
+ parallel (1.20.1)
99
+ parser (3.0.2.0)
100
+ ast (~> 2.4.1)
101
+ pathspec (1.0.0)
102
+ pry (0.14.1)
103
+ coderay (~> 1.1)
104
+ method_source (~> 1.0)
105
+ public_suffix (4.0.6)
106
+ rainbow (3.0.0)
107
+ rake (10.5.0)
108
+ rb-fsevent (0.11.0)
109
+ rb-inotify (0.10.1)
110
+ ffi (~> 1.0)
111
+ rchardet (1.8.0)
112
+ regexp_parser (2.1.1)
113
+ rexml (3.2.5)
114
+ rspec (3.10.0)
115
+ rspec-core (~> 3.10.0)
116
+ rspec-expectations (~> 3.10.0)
117
+ rspec-mocks (~> 3.10.0)
118
+ rspec-core (3.10.1)
119
+ rspec-support (~> 3.10.0)
120
+ rspec-expectations (3.10.1)
121
+ diff-lcs (>= 1.2.0, < 2.0)
122
+ rspec-support (~> 3.10.0)
123
+ rspec-mocks (3.10.2)
124
+ diff-lcs (>= 1.2.0, < 2.0)
125
+ rspec-support (~> 3.10.0)
126
+ rspec-support (3.10.2)
127
+ rubocop (1.18.4)
128
+ parallel (~> 1.10)
129
+ parser (>= 3.0.0.0)
130
+ rainbow (>= 2.2.2, < 4.0)
131
+ regexp_parser (>= 1.8, < 3.0)
132
+ rexml
133
+ rubocop-ast (>= 1.8.0, < 2.0)
134
+ ruby-progressbar (~> 1.7)
135
+ unicode-display_width (>= 1.4.0, < 3.0)
136
+ rubocop-ast (1.8.0)
137
+ parser (>= 3.0.1.1)
138
+ ruby-progressbar (1.11.0)
139
+ ruby2_keywords (0.0.5)
140
+ sawyer (0.8.2)
141
+ addressable (>= 2.3.5)
142
+ faraday (> 0.8, < 2.0)
143
+ shellany (0.0.1)
144
+ terminal-table (3.0.1)
145
+ unicode-display_width (>= 1.1.1, < 3)
146
+ thor (1.1.0)
147
+ unicode-display_width (2.0.0)
148
+ yard (0.9.26)
149
+
150
+ PLATFORMS
151
+ x86_64-darwin-20
152
+
153
+ DEPENDENCIES
154
+ bundler (~> 2.0)
155
+ danger-missing_codeowners!
156
+ guard (~> 2.14)
157
+ guard-rspec (~> 4.7)
158
+ listen (= 3.0.7)
159
+ pry
160
+ rake (~> 10.0)
161
+ rspec (~> 3.4)
162
+ rubocop
163
+ yard
164
+
165
+ BUNDLED WITH
166
+ 2.2.18
data/Guardfile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A guardfile for making Danger Plugins
4
+ # For more info see https://github.com/guard/guard#readme
5
+
6
+ # To run, use `bundle exec guard`.
7
+
8
+ guard :rspec, cmd: "bundle exec rspec" do
9
+ require "guard/rspec/dsl"
10
+ dsl = Guard::RSpec::Dsl.new(self)
11
+
12
+ # RSpec files
13
+ rspec = dsl.rspec
14
+ watch(rspec.spec_helper) { rspec.spec_dir }
15
+ watch(rspec.spec_support) { rspec.spec_dir }
16
+ watch(rspec.spec_files)
17
+
18
+ # Ruby files
19
+ ruby = dsl.ruby
20
+ dsl.watch_spec_files_for(ruby.lib_files)
21
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2021 andre-alves-ifood <andre.alves@ifood.com.br>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # danger-missing_codeowners
2
+
3
+ A [Danger Ruby](https://github.com/danger/danger) plugin for inspecting [CODEOWNERS](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-code-owners) and finding which files have no owners.
4
+
5
+ Works with GitHub and GitLab.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your Gemfile:
10
+
11
+ ```rb
12
+ gem 'danger-missing_codeowners'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Just call the `verify` methd in your Dangerfile:
18
+
19
+ ```rb
20
+ missing_codeowners.verify
21
+ ```
22
+
23
+ By default danger-missing_codeowners will only verify files that were added or modified in the pull request diff.
24
+
25
+ To verify all files, use the `verify_all_files` option:
26
+
27
+ ```rb
28
+ missing_codeowners.verify_all_files = true
29
+ missing_codeowners.verify
30
+ ```
31
+
32
+ You can also adjust the severity of the execution. Possible valures are `error` (default) and `warning`:
33
+
34
+ ```rb
35
+ missing_codeowners.severity = 'warning'
36
+ missing_codeowners.verify
37
+ ```
38
+
39
+ ## Development
40
+
41
+ 1. Clone this repo
42
+ 2. Run `bundle install` to setup dependencies.
43
+ 3. Run `bundle exec rake spec` to run the tests.
44
+ 4. Use `bundle exec guard` to automatically have tests run as you make changes.
45
+
46
+ ## License
47
+
48
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:specs)
8
+
9
+ task default: :specs
10
+
11
+ task :spec do
12
+ Rake::Task["specs"].invoke
13
+ Rake::Task["rubocop"].invoke
14
+ Rake::Task["spec_docs"].invoke
15
+ end
16
+
17
+ desc "Run RuboCop on the lib/specs directory"
18
+ RuboCop::RakeTask.new(:rubocop) do |task|
19
+ task.patterns = ["lib/**/*.rb", "spec/**/*.rb"]
20
+ end
21
+
22
+ desc "Ensure that the plugin passes `danger plugins lint`"
23
+ task :spec_docs do
24
+ sh "bundle exec danger plugins lint"
25
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "missing_codeowners/gem_version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "danger-missing_codeowners"
9
+ spec.version = MissingCodeowners::VERSION
10
+ spec.authors = ["andre-alves"]
11
+ spec.email = ["andre.ver93@gmail.com"]
12
+ spec.description = "A Danger plugin for checking if files have owners. Works with GitHub and GitLab."
13
+ spec.summary = "A Danger plugin for checking if files have owners."
14
+ spec.homepage = "https://github.com/andre-alves/danger-missing_codeowners"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "danger-plugin-api", "~> 1.0"
23
+ spec.add_runtime_dependency "pathspec", "~> 1.0.0"
24
+
25
+ # General ruby development
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+
29
+ # Testing support
30
+ spec.add_development_dependency "rspec", "~> 3.4"
31
+
32
+ # Linting code and docs
33
+ spec.add_development_dependency "rubocop"
34
+ spec.add_development_dependency "yard"
35
+
36
+ # Makes testing easy via `bundle exec guard`
37
+ spec.add_development_dependency "guard", "~> 2.14"
38
+ spec.add_development_dependency "guard-rspec", "~> 4.7"
39
+
40
+ # If you want to work on older builds of ruby
41
+ spec.add_development_dependency "listen", "3.0.7"
42
+
43
+ # This gives you the chance to run a REPL inside your tests
44
+ # via:
45
+ #
46
+ # require 'pry'
47
+ # binding.pry
48
+ #
49
+ # This will stop test execution and let you inspect the results
50
+ spec.add_development_dependency "pry"
51
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "missing_codeowners/gem_version"
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "missing_codeowners/plugin"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MissingCodeowners
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathspec"
4
+
5
+ module Danger
6
+ # Parses the CODEOWNERS file and verifies if files have at least one owner.
7
+ # Works with GitHub and GitLab.
8
+ # Results are passed out as a table in markdown.
9
+ #
10
+ # @example Verifying files missing codeowners.
11
+ #
12
+ # missing_codeowners.verify
13
+ #
14
+ # @see andre-alves/danger-missing_codeowners
15
+ # @tags codeowners
16
+ #
17
+ class DangerMissingCodeowners < Plugin
18
+ # The list of files that are missing owners.
19
+ #
20
+ # @return [Array<String>]
21
+ attr_accessor :files_missing_codeowners
22
+
23
+ # Whether all files or only ones in PR diff to be reported. Default is false.
24
+ #
25
+ # @return [Bool]
26
+ attr_accessor :verify_all_files
27
+
28
+ # The maximum number of files missing owners Danger should report. Default is 100.
29
+ #
30
+ # @return [Int]
31
+ attr_accessor :max_number_of_files_to_report
32
+
33
+ # Defines the severity level of the execution. Possible values are: 'error' or 'warning'. Default is 'error'.
34
+ #
35
+ # @return [String]
36
+ attr_accessor :severity
37
+
38
+ # Provides additional logging diagnostic information. Default is false.
39
+ #
40
+ # @return [Bool]
41
+ attr_accessor :verbose
42
+
43
+ # Verifies git added and modified files for missing owners.
44
+ # Generates a `markdown` list of warnings for the prose in a corpus of
45
+ # .markdown and .md files.
46
+ #
47
+ # @return [void]
48
+ #
49
+ def verify
50
+ @verify_all_files ||= false
51
+ @max_number_of_files_to_report ||= 100
52
+ @severity ||= "error"
53
+ @verbose ||= false
54
+
55
+ files = files_to_verify
56
+ codeowners_path = find_codeowners_file
57
+ codeowners_lines = read_codeowners_file(codeowners_path)
58
+ codeowners_spec = parse_codeowners_spec(codeowners_lines)
59
+ @files_missing_codeowners = files.reject { |file| codeowners_spec.match file }
60
+
61
+ return if @files_missing_codeowners.empty?
62
+
63
+ log "Files missing CODEOWNERS:"
64
+ log @files_missing_codeowners.join("\n")
65
+
66
+ markdown format_missing_owners_message(@files_missing_codeowners, @max_number_of_files_to_report)
67
+ danger_message = "Add CODEOWNERS rules to match all files."
68
+
69
+ @severity == "error" ? (fail danger_message) : (warn danger_message)
70
+ end
71
+
72
+ private
73
+
74
+ def files_to_verify
75
+ @verify_all_files == true ? git_all_files : git_modified_files
76
+ end
77
+
78
+ def git_modified_files
79
+ git.added_files + git.modified_files
80
+ end
81
+
82
+ def git_all_files
83
+ # The git object provided by Danger doesn't have ls_files
84
+ `git ls-files`.split($/)
85
+ end
86
+
87
+ def find_codeowners_file
88
+ directories = ["", ".gitlab", ".github", "docs"]
89
+ paths = directories.map { |dir| File.join(dir, "CODEOWNERS") }
90
+ Dir.glob(paths).first || paths.first
91
+ end
92
+
93
+ def read_codeowners_file(path)
94
+ log "Reading the CODEOWNERS file from path: #{path}"
95
+ File.readlines(path).map(&:chomp)
96
+ end
97
+
98
+ def parse_codeowners_spec(lines)
99
+ patterns = []
100
+ lines.each do |line|
101
+ components = line.split(/\s+(@\S+|\S+@\S+)/).reject { |c| c.strip.empty? }
102
+ if line.match(/^\s*((?:#.*)|(?:\[.*)|(?:\^.*))?$/)
103
+ next # Comment, group or empty line
104
+ elsif components.length < 2
105
+ raise "[ERROR] Invalid CODEOWNERS line: '#{line}'"
106
+ else
107
+ pattern = components[0]
108
+ patterns << pattern
109
+ log "Adding pattern: '#{pattern}'"
110
+ end
111
+ end
112
+ PathSpec.from_lines(patterns)
113
+ end
114
+
115
+ def format_missing_owners_message(files, max_count)
116
+ message = "### Files missing CODEOWNERS\n\n".dup
117
+ message << "| File |\n"
118
+ message << "| ---- |\n"
119
+ files.take(max_count).each do |file|
120
+ message << "| #{file} |\n"
121
+ end
122
+
123
+ other_files_length = files.length - max_count
124
+ if other_files_length.positive?
125
+ message << "...and #{other_files_length} other files.\n"
126
+ end
127
+
128
+ message
129
+ end
130
+
131
+ def log(text)
132
+ puts(text) if @verbose
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,46 @@
1
+ # This is an example of a CODEOWNERS file
2
+ # lines starting with a `#` will be ignored.
3
+
4
+ # app/ @commented-rule
5
+
6
+ # We can specify a default match using wildcards:
7
+ *.yml @default-codeowner
8
+
9
+ # We can also specify "multiple tab or space" separated codeowners:
10
+ *.yml @multiple @code @owners
11
+
12
+ # Files with a `#` can still be accessed by escaping the pound sign
13
+ \#file_with_pound.rb @owner-file-with-pound
14
+
15
+ # You can use both usernames or email addresses to match
16
+ LICENSE janedoe@gitlab.com @username
17
+
18
+ # Group names can be used to match groups and nested groups to specify
19
+ # them as owners for a file
20
+ README @group/with-nested/subgroup
21
+
22
+ # Ending a path in a `/` will specify the Code Owners for every file
23
+ # nested in that directory, on any level
24
+ /configs/ @all-configs
25
+
26
+ # Ending a path in `/*` will specify Code Owners for every file in
27
+ # that directory, but not nested deeper. This will match
28
+ # `docs/index.md` but not `docs/projects/index.md`
29
+ /docs/* @root-docs
30
+
31
+ # This will make a `lib` directory nested anywhere in the repository match
32
+ lib/ @lib-owner
33
+
34
+ # Nested Wildcards is also supported
35
+ widgets/**/Sources/ @widgets-owner
36
+
37
+ # If the path contains spaces, escape them like this:
38
+ path\ with\ spaces/ @space-owner
39
+
40
+ # Code Owners section:
41
+
42
+ [Database]
43
+ model/db/ @gl-database
44
+
45
+ ^[Optional Group]
46
+ *.go @golang
@@ -0,0 +1,5 @@
1
+ # Example of Invalid CODEOWNERS
2
+
3
+ *.yml @default-codeowner
4
+ *.go @golang
5
+ invalid.txt group
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("spec_helper", __dir__)
4
+
5
+ module Danger
6
+ describe Danger::DangerMissingCodeowners do
7
+ it "should be a plugin" do
8
+ expect(Danger::DangerMissingCodeowners.new(nil)).to be_a Danger::Plugin
9
+ end
10
+
11
+ describe "with Dangerfile" do
12
+ before do
13
+ @dangerfile = testing_dangerfile
14
+ @my_plugin = @dangerfile.missing_codeowners
15
+ @my_plugin.verbose = true
16
+ end
17
+
18
+ context "and valid CODEOWNERS file" do
19
+ before do
20
+ allow(@my_plugin).to receive(:find_codeowners_file)
21
+ .and_return("#{File.dirname(__FILE__)}/fixtures/CODEOWNERS")
22
+ end
23
+
24
+ it "fails when there are modified files without CODEOWNERS rules and severity is error" do
25
+ allow(@my_plugin).to receive(:git_modified_files)
26
+ .and_return([
27
+ "app/source.swift",
28
+ ".swiftlint.yml",
29
+ "configs/header.xml",
30
+ "configs/nested/header.xml",
31
+ "project/configs/header.xml",
32
+ "docs/index.md",
33
+ "docs/projects/index.md",
34
+ "widgets/a/Sources/file.java",
35
+ "widgets/b/Sources/file.java",
36
+ "widgets/Sources/file.java",
37
+ "widgets/a/Tests/file.java",
38
+ "widgets/Tests/file.java",
39
+ "path with spaces/ok.php",
40
+ "path with spaces2/missing.php",
41
+ "sources/#file_with_pound.rb",
42
+ "sources/#file_with_pound2.rb",
43
+ "module/LICENSE",
44
+ "module/README",
45
+ "nested/lib/source.js",
46
+ "lib/source.js",
47
+ "model/db/",
48
+ "sources/something.go"
49
+ ])
50
+
51
+ @my_plugin.verify
52
+
53
+ markdown = @dangerfile.status_report[:markdowns].first.to_s
54
+ expect(markdown).to include("app/source.swift")
55
+ expect(markdown).to include("project/configs/header.xml")
56
+ expect(markdown).to include("path with spaces2/missing.php")
57
+ expect(markdown).to include("docs/projects/index.md")
58
+ expect(markdown).to include("widgets/a/Tests/file.java")
59
+ expect(markdown).to include("widgets/Tests/file.java")
60
+ expect(markdown).to include("sources/#file_with_pound2.rb")
61
+ expect(@my_plugin.files_missing_codeowners.length).to eq(7)
62
+ expect(@dangerfile.status_report[:errors]).to eq(["Add CODEOWNERS rules to match all files."])
63
+ end
64
+
65
+ it "succeeds when all modified files have CODEOWNERS rules" do
66
+ allow(@my_plugin).to receive(:git_modified_files).and_return(["any_file.yml", "any_file.go"])
67
+
68
+ @my_plugin.verify
69
+
70
+ expect(@my_plugin.files_missing_codeowners.length).to eq(0)
71
+ end
72
+
73
+ it "fails when there are files without CODEOWNERS rules and severity is error" do
74
+ @my_plugin.verify_all_files = true
75
+ allow(@my_plugin).to receive(:git_all_files).and_return(["app/source.swift", ".swiftlint.yml"])
76
+
77
+ @my_plugin.verify
78
+
79
+ markdown = @dangerfile.status_report[:markdowns].first.to_s
80
+ expect(markdown).to include("app/source.swift")
81
+ expect(@my_plugin.files_missing_codeowners.length).to eq(1)
82
+ expect(@dangerfile.status_report[:errors]).to eq(["Add CODEOWNERS rules to match all files."])
83
+ end
84
+
85
+ it "warns when there are files without CODEOWNERS rules and severity is warning" do
86
+ @my_plugin.verify_all_files = true
87
+ @my_plugin.severity = "warning"
88
+ allow(@my_plugin).to receive(:git_all_files).and_return(["app/source.swift", ".swiftlint.yml"])
89
+
90
+ @my_plugin.verify
91
+
92
+ markdown = @dangerfile.status_report[:markdowns].first.to_s
93
+ expect(markdown).to include("app/source.swift")
94
+ expect(@my_plugin.files_missing_codeowners.length).to eq(1)
95
+ expect(@dangerfile.status_report[:warnings]).to eq(["Add CODEOWNERS rules to match all files."])
96
+ end
97
+
98
+ it "succeeds when all files have CODEOWNERS rules" do
99
+ @my_plugin.verify_all_files = true
100
+ allow(@my_plugin).to receive(:git_all_files).and_return(["any_file.yml", "any_file.go"])
101
+
102
+ @my_plugin.verify
103
+
104
+ expect(@my_plugin.files_missing_codeowners.length).to eq(0)
105
+ end
106
+
107
+ it "does not truncates the markdown when there are not too many files missing owners" do
108
+ @my_plugin.max_number_of_files_to_report = 3
109
+ allow(@my_plugin).to receive(:git_modified_files).and_return(["a.xml", "b.xml", "c.xml"])
110
+
111
+ @my_plugin.verify
112
+
113
+ expect(@dangerfile.status_report[:markdowns].first.to_s).to_not include("other files")
114
+ expect(@my_plugin.files_missing_codeowners.length).to eq(3)
115
+ end
116
+
117
+ it "truncates the markdown when there are too many files missing owners" do
118
+ @my_plugin.max_number_of_files_to_report = 1
119
+ allow(@my_plugin).to receive(:git_modified_files).and_return(["a.xml", "b.xml", "c.xml"])
120
+
121
+ @my_plugin.verify
122
+
123
+ expect(@dangerfile.status_report[:markdowns].first.to_s).to include("2 other files")
124
+ expect(@my_plugin.files_missing_codeowners.length).to eq(3)
125
+ end
126
+ end
127
+
128
+ context "and invalid CODEOWNERS file" do
129
+ before do
130
+ allow(@my_plugin).to receive(:find_codeowners_file)
131
+ .and_return("#{File.dirname(__FILE__)}/fixtures/INVALID_CODEOWNERS")
132
+ end
133
+
134
+ it "raises exception with invalid line" do
135
+ allow(@my_plugin).to receive(:git_modified_files).and_return(["any_file.yml", "any_file.go"])
136
+
137
+ expect { @my_plugin.verify }.to raise_error(RuntimeError)
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ ROOT = Pathname.new(File.expand_path("..", __dir__))
5
+ $:.unshift("#{ROOT}lib".to_s)
6
+ $:.unshift("#{ROOT}spec".to_s)
7
+
8
+ require "bundler/setup"
9
+ require "pry"
10
+
11
+ require "rspec"
12
+ require "danger"
13
+
14
+ if `git remote -v` == ""
15
+ puts "You cannot run tests without setting a local git remote on this repo"
16
+ puts "It's a weird side-effect of Danger's internals."
17
+ exit(0)
18
+ end
19
+
20
+ # Use coloured output, it's the best.
21
+ RSpec.configure do |config|
22
+ config.filter_gems_from_backtrace "bundler"
23
+ config.color = true
24
+ config.tty = true
25
+ end
26
+
27
+ require "danger_plugin"
28
+
29
+ # These functions are a subset of https://github.com/danger/danger/blob/master/spec/spec_helper.rb
30
+ # If you are expanding these files, see if it's already been done ^.
31
+
32
+ # A silent version of the user interface,
33
+ # it comes with an extra function `.string` which will
34
+ # strip all ANSI colours from the string.
35
+
36
+ # rubocop:disable Lint/NestedMethodDefinition
37
+ def testing_ui
38
+ @output = StringIO.new
39
+ def @output.winsize
40
+ [20, 9999]
41
+ end
42
+
43
+ cork = Cork::Board.new(out: @output)
44
+ def cork.string
45
+ out.string.gsub(/\e\[([;\d]+)?m/, "")
46
+ end
47
+ cork
48
+ end
49
+ # rubocop:enable Lint/NestedMethodDefinition
50
+
51
+ # Example environment (ENV) that would come from
52
+ # running a PR on TravisCI
53
+ def testing_env
54
+ {
55
+ "HAS_JOSH_K_SEAL_OF_APPROVAL" => "true",
56
+ "TRAVIS_PULL_REQUEST" => "800",
57
+ "TRAVIS_REPO_SLUG" => "artsy/eigen",
58
+ "TRAVIS_COMMIT_RANGE" => "759adcbd0d8f...13c4dc8bb61d",
59
+ "DANGER_GITHUB_API_TOKEN" => "123sbdq54erfsd3422gdfio"
60
+ }
61
+ end
62
+
63
+ # A stubbed out Dangerfile for use in tests
64
+ def testing_dangerfile
65
+ env = Danger::EnvironmentManager.new(testing_env)
66
+ Danger::Dangerfile.new(env, testing_ui)
67
+ end
metadata ADDED
@@ -0,0 +1,220 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: danger-missing_codeowners
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - andre-alves
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: danger-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pathspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.14'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.14'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '4.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '4.7'
139
+ - !ruby/object:Gem::Dependency
140
+ name: listen
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '='
144
+ - !ruby/object:Gem::Version
145
+ version: 3.0.7
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 3.0.7
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: A Danger plugin for checking if files have owners. Works with GitHub
168
+ and GitLab.
169
+ email:
170
+ - andre.ver93@gmail.com
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - Gemfile
179
+ - Gemfile.lock
180
+ - Guardfile
181
+ - LICENSE.txt
182
+ - README.md
183
+ - Rakefile
184
+ - danger-missing_codeowners.gemspec
185
+ - lib/danger_missing_codeowners.rb
186
+ - lib/danger_plugin.rb
187
+ - lib/missing_codeowners/gem_version.rb
188
+ - lib/missing_codeowners/plugin.rb
189
+ - spec/fixtures/CODEOWNERS
190
+ - spec/fixtures/INVALID_CODEOWNERS
191
+ - spec/missing_codeowners_spec.rb
192
+ - spec/spec_helper.rb
193
+ homepage: https://github.com/andre-alves/danger-missing_codeowners
194
+ licenses:
195
+ - MIT
196
+ metadata: {}
197
+ post_install_message:
198
+ rdoc_options: []
199
+ require_paths:
200
+ - lib
201
+ required_ruby_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ required_rubygems_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ requirements: []
212
+ rubygems_version: 3.0.3
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: A Danger plugin for checking if files have owners.
216
+ test_files:
217
+ - spec/fixtures/CODEOWNERS
218
+ - spec/fixtures/INVALID_CODEOWNERS
219
+ - spec/missing_codeowners_spec.rb
220
+ - spec/spec_helper.rb