Skip to content

Commit

Permalink
Merge branch 'main' of github.com:devfile/api
Browse files Browse the repository at this point in the history
  • Loading branch information
yangcao77 committed Sep 12, 2023
2 parents a4782d9 + f331a0b commit 4b53bf4
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .devfile/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ ENV GOROOT /usr/lib/go
RUN apk add --no-cache --update curl bash jq go git openssh \
&& pip3 install "cython<3.0.0" \
&& pip3 install yq \
&& pip3 install jsonschema-cli
&& pip3 install pyyaml \
&& pip3 install jsonschema

RUN mkdir -p /home/user/go && chmod -R a+w /home/user
ENV HOME /home/user
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Setup Go environment
uses: actions/setup-go@v2.1.3
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
# The Go version to download (if necessary) and use. Supports semver spec and ranges.
go-version: 1.18
Expand All @@ -42,7 +42,7 @@ jobs:
run: go test -coverprofile cover.out -v ./...

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2.1.0
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4

- name: Check typescript model generation
run: bash ./build/typescript-model/generate.sh
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2.3.1
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
persist-credentials: false
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: 1.17
- name: Run tests
run: go test ./... -coverprofile cover.out
- name: Codecov
uses: codecov/codecov-action@v2.1.0
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
4 changes: 2 additions & 2 deletions .github/workflows/publish-devfile-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ jobs:
python-version: '3.9'

- name: Checkout devfile/devfile-web
uses: actions/checkout@v2
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
repository: devfile/devfile-web
persist-credentials: false
path: devfile-web-repo

- name: Checkout devfile api
uses: actions/checkout@v2
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
path: api-repo

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-devfile-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ jobs:
python-version: '3.9'

- name: Checkout devfile/devfile-web
uses: actions/checkout@v2
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
repository: devfile/devfile-web
persist-credentials: false
path: devfile-web-repo

- name: Checkout devfile/api
uses: actions/checkout@v2
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
path: api-repo

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-typescript-models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout devfile/api
uses: actions/checkout@v2
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
with:
path: api

Expand Down
5 changes: 3 additions & 2 deletions validate-samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ then
exit 1
fi

if ! command -v jsonschema-cli &> /dev/null
if ! command -v jsonschema &> /dev/null
then
echo
echo "#### ERROR ####"
Expand All @@ -58,7 +58,8 @@ do
echo "Validating $schema files against ${schemaPath}"
for devfile in $devfiles
do
if ! jsonschema-cli validate "${BASE_DIR}/${schemaPath}" "${BASE_DIR}/${devfile}" >> validate-output.txt
python3 validate_yaml.py "${BASE_DIR}/${schemaPath}" "${BASE_DIR}/${devfile}" >> validate-output.txt
if [ "$(cat validate-output.txt)" != "" ]
then
echo " - $devfile => INVALID"
else
Expand Down
67 changes: 67 additions & 0 deletions validate_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python
import json
import yaml
import sys

from typing import Any
from jsonschema import validate, ValidationError


class ParseSchemaError(Exception):
pass


class YamlValidationError(Exception):
pass


class OpenFileError(Exception):
pass


class NotEnoughArgsError(Exception):
pass


class YamlValidator:
"""
Yaml validator validates a given yaml file against
a chosen template.
"""
def __init__(self, schema_path: str) -> None:
self.schema = self._parse_json_file(schema_path)

def _open_file(self, path: str) -> Any:
try:
return open(path)
except OSError as exc:
raise OpenFileError(f"::error:: failed to open file {path}: {exc}")

def _parse_json_file(self, json_path: str) -> dict[str, Any]:
return json.load(self._open_file(json_path))

def _get_yaml_file(self, yaml_path: str):
return yaml.load(self._open_file(yaml_path), Loader=yaml.SafeLoader)

def validate(self, path: str) -> bool:
try:
_ = validate(instance=self._get_yaml_file(path), schema=self.schema)
except ValidationError as exc:
raise YamlValidationError(f"error:: validation failed: {str(exc.message)}")
return True


def parse_arg(index: int) -> str:
try:
return sys.argv[index]
except IndexError:
raise NotEnoughArgsError(
"Missing Args: Example usage -> validate-yaml.py <schema_path> <yaml_path>"
)


if __name__ == "__main__":
schema_path = parse_arg(1)
yaml_path = parse_arg(2)
validator = YamlValidator(schema_path=schema_path)
validator.validate(yaml_path)

0 comments on commit 4b53bf4

Please sign in to comment.