Browse Source

Run all linters via pre-commit

Executes all linters via pre-commit, which is much faster, guarantees
their version locking and allows upgrading them with a single command.

Before this change the only linter running via pre-commit was
ansible-lint.

Now we also run bashate, flake8 and yamllint via pre-commit.

For developer convenience we still keep the old tox environments
which allow running a single linter.

Added long_description_content_type to fix twine check failure

Change-Id: I037eae61921b2a84aa99838804f70e96ee8d8b13
changes/50/637150/5
Chandan Kumar 3 months ago
parent
commit
b541c5ef99

+ 2
- 0
.ansible-lint View File

@@ -0,0 +1,2 @@
1
+---
2
+parseable: true

+ 40
- 0
.pre-commit-config.yaml View File

@@ -0,0 +1,40 @@
1
+---
2
+repos:
3
+  - repo: https://github.com/pre-commit/pre-commit-hooks
4
+    rev: v2.1.0
5
+    hooks:
6
+      - id: end-of-file-fixer
7
+      - id: trailing-whitespace
8
+      - id: mixed-line-ending
9
+      - id: check-byte-order-marker
10
+      - id: check-executables-have-shebangs
11
+      - id: check-merge-conflict
12
+      - id: debug-statements
13
+      - id: flake8
14
+      - id: check-yaml
15
+        files: .*\.(yaml|yml)$
16
+  - repo: https://github.com/adrienverge/yamllint.git
17
+    rev: v1.15.0
18
+    hooks:
19
+      - id: yamllint
20
+        files: \.(yaml|yml)$
21
+        types: [file, yaml]
22
+        entry: yamllint --strict -f parsable
23
+  - repo: https://github.com/ansible/ansible-lint
24
+    rev: v4.1.0a0
25
+    hooks:
26
+      - id: ansible-lint
27
+        files: \.(yaml|yml)$
28
+        entry: ansible-lint --force-color -v
29
+  - repo: https://github.com/openstack-dev/bashate.git
30
+    rev: 0.6.0
31
+    hooks:
32
+      - id: bashate
33
+        entry: bashate --error . --verbose --ignore=E006,E040
34
+        # Run bashate check for all bash scripts
35
+        # Ignores the following rules:
36
+        # E006: Line longer than 79 columns (as many scripts use jinja
37
+        #       templating, this is very difficult)
38
+        # E040: Syntax error determined using `bash -n` (as many scripts
39
+        #       use jinja templating, this will often fail and the syntax
40
+        #       error will be discovered in execution anyway)

+ 7
- 0
.yamllint View File

@@ -0,0 +1,7 @@
1
+---
2
+extends: default
3
+
4
+rules:
5
+  line-length:
6
+    # matches hardcoded 160 value from ansible-lint
7
+    max: 160

+ 0
- 1
LICENSE View File

@@ -173,4 +173,3 @@
173 173
       defend, and hold each Contributor harmless for any liability
174 174
       incurred by, or claims asserted against, such Contributor by reason
175 175
       of your accepting any such warranty or additional liability.
176
-

+ 1
- 0
defaults/main.yml View File

@@ -1,2 +1,3 @@
1
+---
1 2
 update_repo: ''
2 3
 container_build_tool: 'docker'

+ 1
- 1
files/dev_install.sh View File

@@ -3,7 +3,7 @@ set -e
3 3
 # Cherry-pick a refspec
4 4
 # $1 : project name e.g. keystone
5 5
 # $2 : Gerrit refspec(s) to cherry pick
6
-function cherrypick(){
6
+function cherrypick {
7 7
     local PROJ_NAME=$1
8 8
     local REFSPECS="$2"
9 9
 

+ 4
- 3
meta/main.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 galaxy_info:
2 3
   author: Steve Baker
3 4
   description: Modify container images built for TripleO
@@ -6,9 +7,9 @@ galaxy_info:
6 7
   min_ansible_version: 2.4
7 8
 
8 9
   platforms:
9
-  - name: EL
10
-    versions:
11
-      - 7
10
+    - name: EL
11
+      versions:
12
+        - 7
12 13
 
13 14
   galaxy_tags:
14 15
     - docker

+ 5
- 0
setup.cfg View File

@@ -34,3 +34,8 @@ universal = 1
34 34
 skip_authors = True
35 35
 skip_changelog = True
36 36
 
37
+[flake8]
38
+# E123, E125 skipped as they are invalid PEP-8.
39
+show-source = True
40
+ignore = E123,E125
41
+builtins = _

+ 1
- 0
setup.py View File

@@ -15,5 +15,6 @@
15 15
 import setuptools
16 16
 
17 17
 setuptools.setup(
18
+    long_description_content_type='text/markdown',
18 19
     setup_requires=['pbr'],
19 20
     pbr=True)

+ 1
- 0
tasks/dev_install.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - import_tasks: precheck.yml
2 3
   tags:
3 4
     - always

+ 3
- 3
tasks/get_original_user.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - when: container_build_tool == 'docker'
2 3
   block:
3 4
     - name: Inspect image with Docker
@@ -19,8 +20,7 @@
19 20
       become: true
20 21
     - name: Set config with Buildah
21 22
       set_fact:
22
-       buildah_config: "{{ source_image_facts.stdout_lines | join('') | from_json }}"
23
+        buildah_config: "{{ source_image_facts.stdout_lines | join('') | from_json }}"
23 24
     - name: Set original_user with Buildah
24 25
       set_fact:
25
-       original_user: "{{ buildah_config['Docker']['config']['User'] }}"
26
-
26
+        original_user: "{{ buildah_config['Docker']['config']['User'] }}"

+ 1
- 0
tasks/main.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - import_tasks: precheck.yml
2 3
   tags:
3 4
     - always

+ 2
- 1
tasks/modify_image.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - import_tasks: precheck.yml
2 3
   tags:
3 4
     - always
@@ -36,7 +37,7 @@
36 37
      {{ build_commands[container_build_tool] }} \
37 38
          --tag {{ target_image | default(source_image) }}{{ modified_append_tag }} \
38 39
          --file {{ dockerfile.path }} --network host ./
39
-  #FIXME: buildah should not required root commands to build an image
40
+  # FIXME: buildah should not required root commands to build an image
40 41
   become: "{{ true if build_commands[container_build_tool] == 'buildah' else false }}"
41 42
   args:
42 43
     chdir: "{{ modify_dir_path }}"

+ 3
- 1
tasks/precheck.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - name: Ensure that source_image is defined
2 3
   assert:
3 4
     that:
@@ -5,7 +6,8 @@
5 6
       - source_image | length > 0
6 7
 
7 8
 - name: Ensure that container_build_tool is correctly set
8
-  fail: msg="{{ container_build_tool }} is not a valid value for container_build_tool. Pick docker or buildah."
9
+  fail: msg="{{ container_build_tool }} is not a valid value for
10
+             container_build_tool. Pick docker or buildah."
9 11
   when: container_build_tool not in ['docker', 'buildah']
10 12
 
11 13
 - name: Set default modified_append_tag

+ 2
- 1
tasks/rpm_install.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - import_tasks: precheck.yml
2 3
   tags:
3 4
     - always
@@ -18,7 +19,7 @@
18 19
   find:
19 20
     paths: "{{ rpms_path }}"
20 21
     patterns: "^.*?\\.rpm$"
21
-    use_regex: yes
22
+    use_regex: true
22 23
   when: rpms_path is defined
23 24
   register: context_rpms
24 25
 

+ 1
- 0
tasks/yum_update.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - import_tasks: precheck.yml
2 3
   tags:
3 4
     - always

+ 3
- 1
tasks/yum_update_buildah.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - import_tasks: precheck.yml
2 3
   tags:
3 4
     - always
@@ -57,7 +58,8 @@
57 58
     path: "{{ yum_update.path }}"
58 59
     state: absent
59 60
 
60
-- name: Commit changes to image {{ target_image | default(source_image) }}{{ modified_append_tag }}
61
+- name: Commit changes to image
62
+        ({{ target_image | default(source_image) }}{{ modified_append_tag }})
61 63
   command: >
62 64
     buildah commit
63 65
       {{ from_image }}

+ 1
- 0
tasks/yum_update_docker.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - import_tasks: precheck.yml
2 3
   tags:
3 4
     - always

+ 1
- 1
test-requirements.txt View File

@@ -1 +1 @@
1
-hacking>=1.1.0,<1.2.0 # Apache-2.0
1
+pre-commit # MIT

+ 13
- 19
tox.ini View File

@@ -20,39 +20,33 @@ deps = bindep
20 20
 commands = bindep test
21 21
 
22 22
 [testenv:pep8]
23
-basepython = python3
23
+envdir = {toxworkdir}/linters
24 24
 commands =
25
-    # Run hacking/flake8 check for all python files
26
-    bash -c "git ls-files | grep -v releasenotes |  xargs grep --binary-files=without-match \
27
-        --files-with-match '^.!.*python$' \
28
-        --exclude-dir .tox \
29
-        --exclude-dir .git \
30
-        --exclude-dir .eggs \
31
-        --exclude-dir *.egg-info \
32
-        --exclude-dir dist \
33
-        --exclude-dir *lib/python* \
34
-        --exclude-dir doc \
35
-        | xargs flake8 --verbose"
25
+    python -m pre_commit run flake8 -a
36 26
 
37 27
 [testenv:ansible-lint]
38
-basepython=python3
28
+setenv =
29
+    ANSIBLE_LIBRARY=./library
30
+envdir = {toxworkdir}/linters
39 31
 commands =
40
-  bash ci-scripts/ansible-lint.sh
32
+    python -m pre_commit run ansible-lint -a
41 33
 
42 34
 [testenv:linters]
43 35
 basepython = python3
44
-deps =
45
-    -r{toxinidir}/test-requirements.txt
46
-    -r{toxinidir}/ansible-requirements.txt
47 36
 commands =
48
-    {[testenv:pep8]commands}
49
-    {[testenv:ansible-lint]commands}
37
+    # check only modified files:
38
+    python -m pre_commit run -a
50 39
 
51 40
 [testenv:releasenotes]
52 41
 basepython = python3
53 42
 whitelist_externals = bash
54 43
 commands = bash -c ci-scripts/releasenotes_tox.sh
55 44
 
45
+[testenv:bashate]
46
+envdir = {toxworkdir}/linters
47
+commands =
48
+    python -m pre_commit run bashate -a
49
+
56 50
 [testenv:venv]
57 51
 basepython = python3
58 52
 commands = {posargs}

+ 1
- 0
vars/main.yml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 # we support 'docker' or 'buildah'
2 3
 build_commands:
3 4
   docker: docker build

+ 1
- 0
zuul.d/layout.yaml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - project:
2 3
     templates:
3 4
       - tripleo-multinode-container-minimal

Loading…
Cancel
Save