Skip to content

Commit

Permalink
Merge pull request #16 from kchason/python-action-logic
Browse files Browse the repository at this point in the history
Python Action Logic
  • Loading branch information
kchason authored Aug 25, 2023
2 parents 9eb0bd0 + 9148e54 commit 2afea31
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 55 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ jobs:
- name: Get Repo
uses: actions/checkout@v3

# Install pre-commit and run the checks
- name: Pre-commit Checks
run: |
pip -q install pre-commit
pre-commit run --all-files
# Lint the Dockerfile for syntax correctness and conformance with
# standards.
- name: Docker Lint
Expand All @@ -18,7 +24,7 @@ jobs:
target: Dockerfile
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

test:
runs-on: ubuntu-latest
steps:
Expand All @@ -41,6 +47,6 @@ jobs:

- name: Run Action - Directory No Filter
uses: ./
with:
with:
case-path: "./tests/data/"
case-version: "case-1.2.0"
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# IDE Settings
.idea/
.vs/
.vscode/

# Environments
venv/
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ FROM python:3.10-slim-bullseye
WORKDIR /opt/workspace

# Install dependencies
RUN python -m pip install case-utils==0.11.0
RUN python -m pip install case-utils==0.12.0

# Delete source files now that package has been installed
WORKDIR /opt/workspace

# Copy in the entrypoint file
COPY entrypoint.sh /opt/workspace/entrypoint.sh
COPY entrypoint.py /opt/workspace/entrypoint.py

# Define the base path for the validation path
ENV CASE_PATH "/opt/json/"
ENV CASE_VERSION "case-1.2.0"
ENV FILTER_EXTENSION ""
ENV CASE_EXTENSION_FILTER ""
ENV CASE_VALIDATE_ABORT "false"

# Define the command to run the entrypoint.sh script that will detect the type
# Define the command to run the entrypoint.py script that will detect the type
# of the path that was provided, apply the filter extension (if appropriate) and
# run the `case_validate` command against the CASE file(s) to be validated.
CMD ["bash", "/opt/workspace/entrypoint.sh"]
# run the `case_validate` function against the CASE file(s) to be validated.
CMD ["python3", "/opt/workspace/entrypoint.py"]
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Include the validation action in your GitHub action workflow and specify the fil
```yaml
# Run the CASE validation job to confirm the output is valid
- name: CASE Export Validation
uses: kchason/case-validation-action@v2.6.1
uses: kchason/case-validation-action@v2.7.0
with:
case-path: ./output/
case-version: "case-1.2.0"
Expand All @@ -39,7 +39,7 @@ The environment variables are defined below in the "Inputs" table, and the below
docker run --rm \
-e CASE_PATH="/opt/case/" \
-e CASE_VERSION="case-1.2.0" \
-e FILTER_EXTENSION="jsonld" \
-e CASE_EXTENSION_FILTER="jsonld" \
-v "/path/to/local:/opt/case" \
kchason/case-validator:latest
```
Expand All @@ -52,12 +52,12 @@ Example usage and documentation are available in a demo project on GitLab at [ht

## Inputs

| Action Variable | Environment Variable | Description | Options | Default |
| ---------------- | -------------------- | ------------------------------------------------------------ | ---------------------------------- | ------------ |
| case-path | CASE_PATH | The path to the file or directory to be validated. | Any | /opt/json |
| case-version | CASE_VERSION | The version of the ontology against which the graph should be validatated. | "none", "case-0.5.0", "case-0.6.0" , "case-0.7.0", "case-0.7.1", "case-1.0.0", "case-1.1.0", "case-1.2.0" | "case-1.2.0" |
| extension-filter | FILTER_EXTENSION | The extension of only the files against which the validator should be run. Eg. `"json"`, `"jsonld"`, `"case"`. Defaults to `""` to run against all files defined in `case-path`. | Any | "" |
| abort | CASE_VALIDATE_ABORT | Whether to abort the validator on the first failure | "true", "false" | "false" |
| Action Variable | Environment Variable | Description | Options | Default |
| ---------------- |-----------------------| ------------------------------------------------------------ | ---------------------------------- | ------------ |
| case-path | CASE_PATH | The path to the file or directory to be validated. | Any | /opt/json |
| case-version | CASE_VERSION | The version of the ontology against which the graph should be validatated. | "none", "case-0.5.0", "case-0.6.0" , "case-0.7.0", "case-0.7.1", "case-1.0.0", "case-1.1.0", "case-1.2.0" | "case-1.2.0" |
| extension-filter | CASE_EXTENSION_FILTER | The extension of only the files against which the validator should be run. Eg. `"json"`, `"jsonld"`, `"case"`. Defaults to `""` to run against all files defined in `case-path`. | Any | "" |
| abort | CASE_VALIDATE_ABORT | Whether to abort the validator on the first failure | "true", "false" | "false" |

## Example Output

Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ inputs:
default: 'false'
runs:
using: 'docker'
image: 'docker://kchason/case-validator:1.2.0'
image: 'docker://kchason/case-validator:1.3.0'
env:
CASE_PATH: ${{ inputs.case-path }}
CASE_VERSION: ${{ inputs.case-version }}
EXTENSION_FILTER: ${{ inputs.extension-filter }}
CASE_EXTENSION_FILTER: ${{ inputs.extension-filter }}
CASE_VALIDATE_ABORT: ${{ inputs.abort }}
61 changes: 61 additions & 0 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import sys

from case_utils.case_validate import validate
from case_utils.case_validate.validate_types import ValidationResult
from case_utils.ontology.version_info import CURRENT_CASE_VERSION

# Get the environment variables as the settings for the validation
case_version: str = os.environ.get("CASE_VERSION", CURRENT_CASE_VERSION)
abort_on_failure: bool = (
os.environ.get("CASE_VALIDATE_ABORT", "false").lower() == "true"
) # noqa: E501
case_path: str = os.environ.get("CASE_PATH", "/opt/json/")
extension_filter: str = os.environ.get("CASE_EXTENSION_FILTER", "")

# Determine if the provided path is a directory. If so, then there is filtering
# and other handling to address. If it is a file, then it is assumed it should
# be validated.
if os.path.isdir(case_path):
# Get the list of files that end with the provided extension
files: list = [
os.path.join(case_path, f)
for f in os.listdir(case_path)
if f.endswith(extension_filter)
]

print(f"Validating {len(files)} files at: {case_path}")
# Loop through each file and validate it
has_failure: bool = False
for f in files:
result: ValidationResult = validate(
f, case_version=case_version, abort_on_first=abort_on_failure
)

print(f"Validating file at: {f}")
print(result.text)

if not result.conforms:
has_failure = True
if abort_on_failure:
sys.exit(1)

# If there was a failure, then exit with a non-zero exit code
sys.exit(1) if has_failure else sys.exit(0)

elif os.path.isfile(case_path):
# If the path is a file, then it is assumed it should be validated
# and ignore the filter
result: ValidationResult = validate(
case_path, case_version=case_version, abort_on_first=abort_on_failure
)

print(f"Validating file at: {case_path}")
print(result.text)
sys.exit(0) if result.conforms else sys.exit(1)

else:
print(
f"${case_path} is not a valid path and the validation cannot continue"
) # noqa: E501
sys.exit(1)
37 changes: 0 additions & 37 deletions entrypoint.sh

This file was deleted.

0 comments on commit 2afea31

Please sign in to comment.