Skip to content

Commit 4afe59d

Browse files
ajgonsds
authored andcommitted
Add pre commit hook for hadolint
1 parent 55efc38 commit 4afe59d

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Overcommit Changelog
22

3+
## master
4+
5+
* Add [`hadolint`](https://github.com/lukasmartinelli/hadolint) pre-commit hook
6+
37
## 0.39.1
48

59
### Bug Fixes

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
490490
* [ForbiddenBranches](lib/overcommit/hook/pre_commit/forbidden_branches.rb)
491491
* [GoLint](lib/overcommit/hook/pre_commit/go_lint.rb)
492492
* [GoVet](lib/overcommit/hook/pre_commit/go_vet.rb)
493+
* [Hadolint](lib/overcommit/hook/pre_commit/hadolint.rb)
493494
* [HamlLint](lib/overcommit/hook/pre_commit/haml_lint.rb)
494495
* [HardTabs](lib/overcommit/hook/pre_commit/hard_tabs.rb)
495496
* [Hlint](lib/overcommit/hook/pre_commit/hlint.rb)

config/default.yml

+7
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ PreCommit:
283283
install_command: 'go get golang.org/x/tools/cmd/vet'
284284
include: '**/*.go'
285285

286+
Hadolint:
287+
enabled: false
288+
description: 'Analyze with hadolint'
289+
required_executable: 'hadolint'
290+
include:
291+
- '**/Dockerfile*'
292+
286293
HamlLint:
287294
enabled: false
288295
description: 'Analyze with haml-lint'
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Overcommit::Hook::PreCommit
2+
# Runs `hadolint` against any modified Dockefile files.
3+
#
4+
# @see http://hadolint.lukasmartinelli.ch/
5+
class Hadolint < Base
6+
def run
7+
output = ''
8+
success = true
9+
10+
# hadolint doesn't accept multiple arguments
11+
applicable_files.each do |dockerfile|
12+
result = execute(command, args: Array(dockerfile))
13+
output += result.stdout
14+
success &&= result.success?
15+
end
16+
17+
return :pass if success
18+
19+
extract_messages(
20+
output.split("\n"),
21+
/^(?<file>[^:]+):(?<line>\d+)/,
22+
)
23+
end
24+
end
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::Hadolint do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:applicable_files) { %w[Dockerfile Dockerfile.web] }
7+
subject { described_class.new(config, context) }
8+
9+
before do
10+
subject.stub(:applicable_files).and_return(applicable_files)
11+
end
12+
13+
around do |example|
14+
repo do
15+
example.run
16+
end
17+
end
18+
19+
before do
20+
subject.stub(:execute).with(%w[hadolint], args: Array(applicable_files.first)).
21+
and_return(result_dockerfile)
22+
subject.stub(:execute).with(%w[hadolint], args: Array(applicable_files.last)).
23+
and_return(result_dockerfile_web)
24+
end
25+
26+
context 'and has 2 suggestions' do
27+
let(:result_dockerfile) do
28+
double(
29+
success?: false,
30+
stdout: <<-EOF
31+
Dockerfile:5 DL3015 Avoid additional packages by specifying `--no-install-recommends`
32+
EOF
33+
)
34+
end
35+
let(:result_dockerfile_web) do
36+
double(
37+
success?: false,
38+
stdout: <<-EOF
39+
Dockerfile.web:13 DL3020 Use COPY instead of ADD for files and folders
40+
EOF
41+
)
42+
end
43+
44+
it { should fail_hook }
45+
end
46+
47+
context 'and has single suggestion for double quote' do
48+
let(:result_dockerfile) do
49+
double(
50+
success?: false,
51+
stdout: <<-EOF
52+
Dockerfile:11 SC2086 Double quote to prevent globbing and word splitting.
53+
EOF
54+
)
55+
end
56+
let(:result_dockerfile_web) do
57+
double(success?: true, stdout: '')
58+
end
59+
60+
it { should fail_hook }
61+
end
62+
63+
context 'and does not have any suggestion' do
64+
let(:result_dockerfile) do
65+
double(success?: true, stdout: '')
66+
end
67+
let(:result_dockerfile_web) do
68+
double(success?: true, stdout: '')
69+
end
70+
71+
it { should pass }
72+
end
73+
end

0 commit comments

Comments
 (0)