Skip to content

Commit

Permalink
Merge pull request #16 from FrankMaverick/main
Browse files Browse the repository at this point in the history
Controlli automatici - pre-commit, test, GitHub Actions
  • Loading branch information
Clou-dia authored Apr 4, 2024
2 parents 931e052 + 00bd632 commit 8f14e91
Show file tree
Hide file tree
Showing 19 changed files with 839 additions and 100 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test

on:
push:
branches: [ "main", "master" ]
pull_request:
branches: [ "main", "master" ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
pip install pytest
- name: Run tests
run: pytest -s ./tests/
30 changes: 30 additions & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

name: Validate

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ "main", "master" ]
pull_request:
branches: [ "main", "master"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
pre-commit-hooks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
architecture: 'x64'
- name: Run a script
run: |-
pip install pre-commit
pip install rdflib
pre-commit autoupdate
pre-commit run --all-files --verbose
85 changes: 19 additions & 66 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,77 +1,29 @@
#
# Run pre-commit hooks. You can run them without installing
# the hook with
#
# $ pre-commit run --all-files
#
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
args: [--allow-multiple-documents]
- id: check-added-large-files
args:
- "--maxkb=4000"
- repo: https://github.com/myint/autoflake
rev: v1.4
hooks:
- id: autoflake
args:
- --in-place
- --remove-unused-variables
- --remove-all-unused-imports
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)
# Use black profile for isort to avoid conflicts
# see https://github.com/PyCQA/isort/issues/1518
args: ["--profile", "black"]
- id: isort
name: isort (cython)
types: [cython]
- id: isort
name: isort (pyi)
types: [pyi]
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
- repo: https://github.com/teamdigitale/dati-semantic-cookiecutter
rev: 931e0529c8839f6fa8c1ae315839ba7c3060c5f2
hooks:
- id: flake8
- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
hooks:
- id: bandit
name: bandit
args: ["-c", ".bandit.yaml"]
description: 'Bandit is a tool for finding common security issues in Python code'
entry: bandit
language: python
language_version: python3
types: [python]
- repo: https://github.com/Lucas-C/pre-commit-hooks-safety
rev: v1.3.0
hooks:
- id: python-safety-dependencies-check
- id: check-repo-structure
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
- id: check-filename-format
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
- id: check-filenames-match-uri
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
- id: check-filenames-match-directories
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
- id: check-supported-files
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
- id: check-versioning-pattern
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]

#
# Semantic checks.
#
- repo: https://github.com/teamdigitale/json-semantic-playground
rev: c10c5879438de0d321d6591ec88ace95e042e9d2
rev: 0b4ad4cc883a49878fdfd4539e694ae56b041e29
hooks:
- id: validate-csv
files: >-
^assets\/vocabularies/.*\.csv
^assets\/controlled-vocabularies/.*\.csv
- id: validate-oas-schema
files: >-
^assets\/schemas\/.*.oas3.yaml
Expand All @@ -81,5 +33,6 @@ repos:
- id: validate-turtle
files: >-
^assets\/controlled-vocabularies\/.*\.ttl
- id: validate-directory-versioning
files: '^assets\/ontologies\/.*\.ttl'
- id: validate-turtle
files: >-
^assets\/schemas\/.*\.ttl
74 changes: 74 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# Define running hooks.
#

- id: check-repo-structure
name: Check Repository Structure
description: |-
Check whether the directory structure is correct
- assets/ontologies/*
- assets/controlled-vocabularies/*
- assets/schemas/*
entry: check_repo_structure
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
language: python
files: ^assets/.*
pass_filenames: false
types: [file]

- id: check-filename-format
name: Check Filename Format
description: |-
Check whether file and directory names follow the specified format (^[\\.a-z0-9 _-]{1,64}$)
entry: check_filename_format
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
language: python
files: ^assets/.*
pass_filenames: false
types: [file]

- id: check-filenames-match-uri
name: Check Filename match URI
description: |-
Checks whether the name of each TTL or oas3.yaml file matches the final part of its relative URI
entry: check_filename_match_uri
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
language: python
files: ^assets/.*
pass_filenames: false
types: [file]
additional_dependencies: [rdflib]

- id: check-filenames-match-directories
name: Check Filename match Directories
description: |-
Check if filenames match the containing directory names
entry: check_filenames_match_directories
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
language: python
files: ^assets/.*
pass_filenames: false
types: [file]

- id: check-supported-files
name: Check encoding and file suffix
entry: check_supported_files
description: |-
Checks the leaf directories of the specified root directories
to ensure that each leaf directory contains at least one .ttl file in UTF-8 format.
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
language: python
files: ^assets/.*
pass_filenames: false
types: [file]

- id: check-versioning-pattern
name: Check versioning pattern
entry: check_versioning_pattern
description: |-
Check if the versioning pattern is correct for leaf directories
args: ["assets/controlled-vocabularies/", "assets/ontologies/", "assets/schemas/"]
language: python
files: ^assets/.*
pass_filenames: false
types: [file]
28 changes: 24 additions & 4 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,32 @@ For readability:
and are either used for testing or for developing and validating what's in assets/.


## Development
## Automated Checks and Testing

This repository uses pre-commit to validate content.
An integrated testing environment to reproduce the CI pipeline
is available via docker-compose, which goes on thru a set of steps.
Below are described the procedures for automated checks and testing implemented, essential for ensuring the quality and integrity of the repository content.

### Automated Checks (Pre-commit)

This repository implements automated checks using [pre-commit](https://pre-commit.com/). The specifications of the checks are defined in the file [`.pre-commit-config.yaml`](.pre-commit-config.yaml).

These checks can be executed using GitHub Actions. The `validate.yaml` file in `.github/workflows` automatically enables pre-commit checks after each push or pull request (PR). Additionally, these checks can be manually activated at any time.

To enable pre-commit checks in another repository, simply copy the [`.pre-commit-config.yaml`](.pre-commit-config.yaml) file and the [`.github/workflows/validate.yaml`](.github/workflows/validate.yaml) file.

### URL Testing

In the `tests` directory, there is a script named `test_urls.py`, which verifies GitHub-related URLs present in the files of the `assets` subdirectories.

This test can also be automated using GitHub Actions. The `test.yaml` file in `.github/workflows` automatically activates tests after each push or pull request. Similarly, these tests can be manually initiated at any time.

To enable URL testing in another repository, simply copy the [`/tests/test_urls.py`](/tests/test_urls.py) file and the [`.github/workflows/test.yaml`](.github/workflows/test.yaml) file.

### Local Checks and Testing

Local checks and testing can be performed using Docker or simply Python. An integrated test environment to reproduce the CI pipeline is available through `docker-compose`, which executes a series of steps.

```bash
docker-compose -f docker-compose-test.yml up
```

Note: If you wish to transfer this environment to another repository, it's important to note that the Docker environment requires the Dockerfiles present in the tests directory (such as Dockerfile.precommit and Dockerfile.pytest).
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,33 @@ come:



## Sviluppo
## Controlli Automatici e Test

Questo repository utilizza [pre-commit](https://pre-commit.com/) per convalidare il contenuto dei file:
le verifiche sono indicate in [.pre-commit-config.yaml](.pre-commit-config.yaml).
Di seguito vengono descritte le procedure di controllo automatico e di test implementate, utili per garantire la qualità e l'integrità del contenuto del repository

Un ambiente di test integrato per riprodurre la pipeline CI
è disponibile tramite docker-compose, che esegue una serie di passaggi.
### Controlli Automatici (Pre-commit)

Questo repository implementa i controlli automatici utilizzando [pre-commit](https://pre-commit.com/). Le specifiche delle verifiche sono definite nel file [`.pre-commit-config.yaml`](.pre-commit-config.yaml).

È possibile eseguire tali verifiche mediante GitHub Actions. Il file `validate.yaml` in `.github/workflows` abilita automaticamente i controlli pre-commit dopo ogni push o pull request (PR). Inoltre, è possibile attivare manualmente tali controlli in qualsiasi momento.

Per abilitare i controlli pre-commit in un altro repository, è sufficiente copiare il file [`.pre-commit-config.yaml`](.pre-commit-config.yaml) e il file [`.github/workflows/validate.yaml`](.github/workflows/validate.yaml).

### Test URL

Nella directory `tests` è presente uno script denominato `test_urls.py`, che consente di verificare gli URL relativi a GitHub presenti nei file delle sottodirectory `assets`.

Anche questo test può essere automatizzato mediante GitHub Actions. Il file `test.yaml` in `.github/workflows` attiva automaticamente i test dopo ogni push o pull request. Allo stesso modo, è possibile avviare manualmente questi test in qualsiasi momento.

Per abilitare i test URL in un altro repository, è sufficiente copiare il file [`/tests/test_urls.py`](/tests/test_urls.py) e il file [`.github/workflows/test.yaml`](.github/workflows/test.yaml).

### Controlli e Test in Locale

È possibile eseguire i controlli e i test in locale utilizzando l'ambiente Docker o semplicemente Python. Un ambiente di test integrato per riprodurre la pipeline CI è disponibile tramite `docker-compose`, che esegue una serie di passaggi.

```bash
docker-compose -f docker-compose-test.yml up
```

Nota: Se si desidera trasferire questo ambiente su un altro repository, è importante considerare che l'ambiente Docker richiede i Dockerfile presenti nella directory `tests` (come `Dockerfile.precommit` e `Dockerfile.pytest`).

39 changes: 16 additions & 23 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
version: '3.9'
version: '3.10'

services:
checkout: &baseline
image: alpine/git
volumes:
- ./_deleteme:/code
working_dir: /code
entrypoint: ["git"]
command: >-
clone https://github.com/teamdigitale/dati-semantic-cookiecutter.git .
zero:
ci-precommit:
build:
context: ./
context: .
dockerfile: tests/Dockerfile.precommit
volumes:
- ./_deleteme:/code
working_dir: /code
entrypoint: ['pre-commit']
command: ['run', '-a']
depends_on:
- checkout
one:
<<: *baseline
image: busybox
command: ['sh', '-c', 'exit 0']
depends_on:
- zero
- .:/app
working_dir: /app
entrypoint: 'pre-commit run --all-files --verbose'

ci-pytest:
build:
context: .
dockerfile: tests/Dockerfile.pytest
volumes:
- .:/app
working_dir: /app
entrypoint: 'pytest -s ./tests/'
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest==8.1.1
pre-commit==3.6.0
requests==2.31.0
rdflib==7.0.0
Empty file added scripts/__init__.py
Empty file.
Loading

0 comments on commit 8f14e91

Please sign in to comment.