1
1
module Overcommit ::Hook ::PreCommit
2
- # Runs `foodcritic` against any modified Ruby files from chef directory structure.
2
+ # Runs `foodcritic` against any modified Ruby files from Chef directory structure.
3
3
#
4
4
# @see http://www.foodcritic.io/
5
5
#
6
- # Provides three configuration options :
6
+ # There are two "modes" you can run this hook in based on the repo :
7
7
#
8
- # * `cookbooks_directory`
9
- # If not set, or set to false, the current directory will be treated as a cookbook directory
10
- # (the one containing recipes, libraries, etc.)
11
- # If set to path (absolute or relative), hook will interpret it as a cookbooks directory
12
- # (so all each subdirectory will be treated as separate cookbook)
13
- # * `environments_directory`
14
- # If provided, the given path will be treated as environments directory
15
- # * `roles_directory`
16
- # If provided, the given path will be treated as roles directory
8
+ # SINGLE COOKBOOK REPO MODE
9
+ # -------------------------
10
+ # The default. Use this if your repository contains just a single cookbook,
11
+ # i.e. the top-level repo directory contains directories called `attributes`,
12
+ # `libraries`, `recipes`, etc.
17
13
#
18
- # By default, none of those options is set, which means, the repo directory will be treaded
19
- # as a cookbook directory (with recipes, libraries etc.)
14
+ # To get this to work well, you'll want to set your Overcommit configuration
15
+ # for this hook to something like:
20
16
#
21
- # Example:
17
+ # PreCommit:
18
+ # Foodcritic:
19
+ # enabled: true
20
+ # include:
21
+ # - 'attributes/**/*'
22
+ # - 'definitions/**/*'
23
+ # - 'files/**/*'
24
+ # - 'libraries/**/*'
25
+ # - 'providers/**/*'
26
+ # - 'recipes/**/*'
27
+ # - 'resources/**/*'
28
+ # - 'templates/**/*'
22
29
#
23
- # Foodcritic:
24
- # enabled: true
25
- # cookbooks_directory: './cookbooks'
26
- # environments_directory: './environments'
27
- # roles_directory: './roles'
30
+ # MONOLITHIC REPO MODE
31
+ # --------------------
32
+ # Use this if you store multiple cookbooks, environments, and roles (or any
33
+ # combination thereof) in a single repository.
34
+ #
35
+ # There are three configuration options relevant here:
36
+ #
37
+ # * `cookbooks_directory`
38
+ # When set, hook will treat the path as a directory containing cookbooks.
39
+ # Each subdirectory of this directory will be treated as a separate
40
+ # cookbook.
41
+ #
42
+ # * `environments_directory`
43
+ # When set, hook will treat the path as a directory containing environment
44
+ # files.
45
+ #
46
+ # * `roles_directory`
47
+ # When set, hook will treat the given path as a directory containing role
48
+ # files.
49
+ #
50
+ # In order to run in monolithic repo mode, YOU MUST SET `cookbooks_directory`.
51
+ # The other configuration options are optional, if you happen to store
52
+ # environments/roles in another repo.
53
+ #
54
+ # To get this to work well, you'll want to set your Overcommit configuration
55
+ # for this hook to something like:
56
+ #
57
+ # PreCommit:
58
+ # Foodcritic:
59
+ # enabled: true
60
+ # cookbooks_directory: 'cookbooks'
61
+ # environments_directory: 'environments'
62
+ # roles_directory: 'roles'
63
+ # include:
64
+ # - 'cookbooks/**/*'
65
+ # - 'environments/**/*'
66
+ # - 'roles/**/*'
67
+ #
68
+ # ADDITIONAL CONFIGURATION
69
+ # ------------------------
70
+ # You can disable rules using the `flags` hook option. For example:
71
+ #
72
+ # PreCommit:
73
+ # Foodcritic:
74
+ # enabled: true
75
+ # ...
76
+ # flags:
77
+ # - '--epic-fail=any'
78
+ # - '-t~FC011' # Missing README in markdown format
79
+ # - '-t~FC064' # Ensure issues_url is set in metadata
80
+ #
81
+ # Any other command line flag supported by the `foodcritic` executable can be
82
+ # specified here.
83
+ #
84
+ # If you want the hook run to fail (and not just warn), set the `on_warn`
85
+ # option for the hook to `fail`:
86
+ #
87
+ # PreCommit:
88
+ # Foodcritic:
89
+ # enabled: true
90
+ # on_warn: fail
91
+ # ...
92
+ #
93
+ # This will treat any warnings as failures and cause the hook to exit
94
+ # unsuccessfully.
28
95
class Foodcritic < Base
29
96
def run
30
- args = modified_environments_args + modified_roles_args + modified_cookbooks_args
31
-
32
- args += applicable_files . reject do |file |
33
- %w[ spec test ] . any? { |dir | file . include? ( "#{ File ::SEPARATOR } #{ dir } #{ File ::SEPARATOR } " ) }
34
- end - modified_environments - modified_roles if modified_cookbooks . empty?
35
-
97
+ args = modified_cookbooks_args + modified_environments_args + modified_roles_args
36
98
result = execute ( command , args : args )
37
99
38
100
if result . success?
39
101
:pass
40
102
else
41
- return [ :warn , result . stdout ]
103
+ return [ :warn , result . stderr + result . stdout ]
42
104
end
43
105
end
44
106
@@ -50,31 +112,25 @@ def directories_changed(dir_prefix)
50
112
map { |path | path . gsub ( %r{^#{ dir_prefix } /} , '' ) } .
51
113
group_by { |path | path . split ( '/' ) . first } .
52
114
keys .
53
- map { |cookbook | File . join ( dir_prefix , cookbook ) }
115
+ map { |path | File . join ( dir_prefix , path ) }
54
116
end
55
117
56
118
def modified_environments_args
57
- modified_environments . map { |env | %W[ -E #{ env } ] } . flatten
119
+ modified ( 'environments' ) . map { |env | %W[ -E #{ env } ] } . flatten
58
120
end
59
121
60
122
def modified_roles_args
61
- modified_roles . map { |role | %W[ -R #{ role } ] } . flatten
123
+ modified ( 'roles' ) . map { |role | %W[ -R #{ role } ] } . flatten
62
124
end
63
125
64
126
def modified_cookbooks_args
65
- modified_cookbooks . map { |cookbook | %W[ -B #{ cookbook } ] } . flatten
66
- end
67
-
68
- def modified_environments
69
- modified 'environments'
70
- end
71
-
72
- def modified_roles
73
- modified 'roles'
74
- end
75
-
76
- def modified_cookbooks
77
- modified 'cookbooks'
127
+ # Return the repo root if repository contains a single cookbook
128
+ if !config [ 'cookbooks_directory' ] || config [ 'cookbooks_directory' ] . empty?
129
+ [ '-B' , Overcommit ::Utils . repo_root ]
130
+ else
131
+ # Otherwise return all modified cookbooks in the cookbook directory
132
+ modified ( 'cookbooks' ) . map { |cookbook | [ '-B' , cookbook ] } . flatten
133
+ end
78
134
end
79
135
80
136
def modified ( type )
@@ -85,11 +141,7 @@ def modified(type)
85
141
86
142
def full_directory_path ( config_option )
87
143
return config [ config_option ] if config [ config_option ] . start_with? ( File ::SEPARATOR )
88
- File . absolute_path ( File . join ( repo_root , config [ config_option ] ) )
89
- end
90
-
91
- def repo_root
92
- Overcommit ::Utils . repo_root
144
+ File . absolute_path ( File . join ( Overcommit ::Utils . repo_root , config [ config_option ] ) )
93
145
end
94
146
end
95
147
end
0 commit comments