Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Merged in feature/hadolint (pull request #64)
Browse files Browse the repository at this point in the history
Support hadolint for Dockerfile lint

Approved-by: Xiaohong Zhao <mrluanma@gmail.com>
  • Loading branch information
Lusheng Lv committed Jan 12, 2018
2 parents 5608d7e + daa36ea commit 5335bb1
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ before_install:
- nvm use 8.1.1

install:
- curl -sSL -o /usr/bin/hadolint https://github.com/hadolint/hadolint/releases/download/v1.2.6/hadolint-Linux-x86_64 && chmod a+x /usr/bin/hadolint
- npm install -g eslint csslint sass-lint jsonlint stylelint eslint-plugin-react eslint-plugin-react-native babel-eslint
- pip install -U tox

Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ RUN echo 'deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial main' > /etc
&& git config --global user.email "badwolf@localhost" \
&& git config --global user.name "badwolf" \
&& curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash \
&& apt-get install git-lfs
&& apt-get install git-lfs \
&& curl -sSL -o /usr/bin/hadolint https://github.com/hadolint/hadolint/releases/download/v1.2.6/hadolint-Linux-x86_64 \
&& chmod a+x /usr/bin/hadolint

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - \
&& apt-get install -y nodejs \
Expand Down
53 changes: 53 additions & 0 deletions badwolf/lint/linters/hadolint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
import os

from badwolf.utils import run_command
from badwolf.lint import Problem
from badwolf.lint.linters import Linter
from badwolf.lint.utils import in_path


class HadoLinter(Linter):
name = 'hadolint'
default_pattern = '*Dockerfile*'

def is_usable(self):
return in_path('hadolint')

def lint_files(self, files):
for file in files:
command = ['hadolint', file]
_, output = run_command(
command,
split=True,
include_errors=True,
cwd=self.working_dir,
env={
'HOME': os.getenv('HOME', '/'),
'XDG_CONFIG_HOME': os.getenv('XDG_CONFIG_HOME', '/')
}
)
if not output:
continue

for line in output:
parsed = self._parse_line(line)
if not parsed:
continue

filename, line, message = parsed
yield Problem(filename, line, message, self.name)

def _parse_line(self, line):
parts = line.split(' ', 1)
if len(parts) != 2:
return

location, message = parts
if ':' in location:
filename, line = location.split(':', 1)
line = int(line)
else:
filename = location
line = 1
return filename, line, message
8 changes: 5 additions & 3 deletions badwolf/lint/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from badwolf.lint.linters.sasslint import SassLinter
from badwolf.lint.linters.stylelint import StyleLinter
from badwolf.lint.linters.mypy import MypyLinter
from badwolf.lint.linters.hadolint import HadoLinter


logger = logging.getLogger(__name__)
Expand All @@ -40,6 +41,7 @@ class LintProcessor(object):
'sasslint': SassLinter,
'stylelint': StyleLinter,
'mypy': MypyLinter,
'hadolint': HadoLinter,
}

def __init__(self, context, spec, working_dir=None):
Expand Down Expand Up @@ -162,15 +164,15 @@ def _report(self):
lint_comments = set()
problem_count = 0
for problem in self.problems:
if problem_count >= 50:
# Avoid sending too many comments
break
content = ':broken_heart: **{}**: {}'.format(problem.linter, problem.message)
comment_tuple = (problem.filename, problem.line, content)
lint_comments.add(comment_tuple)
if comment_tuple in existing_comments_ids:
continue

if problem_count >= 50:
# Avoid sending too many comments
break
comment_kwargs = {
'filename': problem.filename,
'anchor': revision_after,
Expand Down
1 change: 1 addition & 0 deletions docs/lint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ shellcheck bash/zsh https://github.com/koalaman/shellcheck
yamllint YAML https://github.com/adrienverge/yamllint
jsonlint JSON https://github.com/zaach/jsonlint
rstlint RestructuredText https://github.com/twolfson/restructuredtext-lint
hadolint Dockerfile https://github.com/hadolint/hadolint
=================== =================== =======================================================

指定 Python 代码检查工具使用的 Python 版本
Expand Down
4 changes: 3 additions & 1 deletion src.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ RUN echo 'deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial main' > /etc
&& git config --global user.email "badwolf@localhost" \
&& git config --global user.name "badwolf" \
&& curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash \
&& apt-get install git-lfs
&& apt-get install git-lfs \
&& curl -sSL -o /usr/bin/hadolint https://github.com/hadolint/hadolint/releases/download/v1.2.6/hadolint-Linux-x86_64 \
&& chmod a+x /usr/bin/hadolint

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - \
&& apt-get install -y nodejs \
Expand Down
4 changes: 3 additions & 1 deletion test.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ RUN echo 'deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial main' > /etc
git \
libssl-dev \
openssh-client \
&& curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash \
&& curl -sSL https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash \
&& apt-get install git-lfs \
&& curl -sSL -o /usr/bin/hadolint https://github.com/hadolint/hadolint/releases/download/v1.2.6/hadolint-Linux-x86_64 \
&& chmod a+x /usr/bin/hadolint \
&& pip3 install -U pip wheel tox

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - \
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/hadolint/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM ubuntu:16.04

RUN apt-get update && apt-get install file
33 changes: 33 additions & 0 deletions tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,36 @@ def test_mypy_lint_a_py(app, pr_context):
problem = lint.problems[0]
assert problem.line == 5
assert problem.filename == 'a.py'


def test_hadolint_lint_a_dockerfile(app, pr_context):
diff = """diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..cd19857
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,3 @@
+FROM ubuntu:16.04
+
+RUN apt-get update && apt-get install file
"""

spec = Specification()
spec.linters.append(ObjectDict(name='hadolint', pattern=None))
lint = LintProcessor(pr_context, spec, os.path.join(FIXTURES_PATH, 'hadolint'))
patch = PatchSet(diff.split('\n'))
with mock.patch.object(lint, 'load_changes') as load_changes,\
mock.patch.object(lint, 'update_build_status') as build_status,\
mock.patch.object(lint, '_report') as report:
load_changes.return_value = patch
build_status.return_value = None
report.return_value = (1, 2)
lint.problems.set_changes(patch)
lint.process()

assert load_changes.called

assert len(lint.problems) == 4
problem = lint.problems[0]
assert problem.line == 3
assert problem.filename == 'Dockerfile'

0 comments on commit 5335bb1

Please sign in to comment.