Skip to content

Commit

Permalink
update: validate & build workflows.
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherbrumm committed Nov 28, 2023
1 parent c91c840 commit c759615
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 61 deletions.
71 changes: 71 additions & 0 deletions .github/build_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import logging
import os

import yaml

from validate import validate_against_schema

# Set up basic logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')


def read_and_merge_configs(base_dir="."):
"""
Reads all config.yml files in the directory structure and merges them.
"""
merged_config = {}

for root, _, files in os.walk(base_dir):
if "config.yml" in files:
file_path = os.path.join(root, "config.yml")

try:
with open(file_path, 'r') as stream:
config = yaml.safe_load(stream)

# Store and remove global properties
global_properties = config.pop("properties", {})

# Merge global properties into network-specific properties
for _, network_config in config.get("networks", {}).items():
local_properties = network_config.get("properties", {})
network_config["properties"] = {**global_properties, **local_properties}

# validate configuration
validate_against_schema(config)

# get source id
source_id = config["source-id"]
# check if source id already exists
if source_id in merged_config:
raise Exception(f"Duplicate source_id: {source_id}")

logging.info(f"Successfully validated source: {source_id}")

merged_config[source_id] = config

except Exception as e:
logging.error(f"Error validating file {file_path}: {e}")

return merged_config


def write_yaml_file(data, file_path=".github/registry.yml"):
"""
Writes data to a YAML file.
"""
try:
with open(file_path, "w") as output_file:
yaml.dump(data, output_file, default_flow_style=False, indent=2)
logging.info(f"Successfully wrote to {file_path}")
except Exception as e:
logging.error(f"Error writing to file {file_path}: {e}")


def main():
merged_config = read_and_merge_configs()
write_yaml_file(merged_config)


if __name__ == "__main__":
main()
File renamed without changes.
6 changes: 5 additions & 1 deletion validate.py → .github/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ def validate_against_schema(data):
Validator = validator_for(schema)
validator = Validator(schema, registry=registry)

validator.validate(data)
try:
validator.validate(data)
except Exception as e:
logging.error(f"Validation error: {e}")
raise
3 changes: 1 addition & 2 deletions main.py → .github/validate_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def read_and_merge_configs(base_dir="."):
merged_config[source_id] = config

except Exception as e:
raise
logging.error(f"Error validating file {file_path}: {e}")

return merged_config

Expand All @@ -64,7 +64,6 @@ def write_yaml_file(data, file_path="registry.yml"):

def main():
merged_config = read_and_merge_configs()
write_yaml_file(merged_config)


if __name__ == "__main__":
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/build_registry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Merge Source configs and write Source-Registry

on:
pull_request:
branches:
- main
types:
- closed
paths:
- '**.yml'

jobs:
merge_yaml:
runs-on: ubuntu-latest

steps:
- name: Check out the code
uses: actions/checkout@v2

- name: Install required Python packages
run: pip install -r .github/requirements.txt
working-directory: ${{ github.workspace }}

- name: Merge Source configs and write Source-Registry
run: python .github/build_registry.py
working-directory: ${{ github.workspace }}

- name: Commit and push changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Actions"
git add .github/registry.yml
git commit -m "Update registry.yml"
git push
working-directory: ${{ github.workspace }}
55 changes: 0 additions & 55 deletions .github/workflows/release_registry.yml

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/validate_registry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Merge Source configs and write Source-Registry

on:
push:
paths:
- '**.yml'

jobs:
merge_yaml:
runs-on: ubuntu-latest

steps:
- name: Check out the code
uses: actions/checkout@v2

- name: Install required Python packages
run: pip install -r .github/requirements.txt
working-directory: ${{ github.workspace }}

- name: Merge Source configs and write Source-Registry
run: python .github/validate_registry.py
working-directory: ${{ github.workspace }}
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
/.idea
# ignoring built registry to avoid a push
registry.yml
/.idea

0 comments on commit c759615

Please sign in to comment.