Skip to content

Commit

Permalink
Merge pull request #9 from thunderbird/features/8-gh-action-run-conve…
Browse files Browse the repository at this point in the history
…rter

Add GH action: convert yaml to json
  • Loading branch information
radishmouse authored Jul 22, 2024
2 parents 4734161 + 53570a7 commit 8ad5e2c
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 14 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/convert.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: convert-yaml-to-json

on:
push:
paths:
- "yaml/**.yaml"

jobs:
convert-yaml:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run convert-yaml.py
run: python convert-yaml.py yaml json

- name: Upload JSON artifacts for validation
uses: actions/upload-artifact@v4
with:
name: json-files
path: json/*.json

validate-json:
needs: convert-yaml
uses: ./.github/workflows/validate.yml
with:
should_download: true

commit-json:
runs-on: ubuntu-latest
needs: validate-json

steps:
- uses: actions/checkout@v4

- name: Check for validation success and file changes
run: |
if [ "${{ needs.validate-json.outputs.is_valid }}" != "true" ]; then
echo "JSON is not valid. Skipping commit."
echo "NO_COMMIT=true" >> $GITHUB_ENV
elif git diff --quiet; then
echo "No changes to commit"
echo "NO_COMMIT=true" >> $GITHUB_ENV
fi
- name: Commit changes
if: ${{ env.NO_COMMIT != 'true' }}
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
git add .
git commit -m "Automated commit by GitHub Action"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60 changes: 47 additions & 13 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
name: validate-json

on:
[push]
# push:
# branches: ["main"]
# pull_request:
# branches: ["main"]
workflow_call:
inputs:
should_download:
description: "Download JSON artifacts if called by another workflow?"
type: boolean
outputs:
is_valid:
description: "Is the JSON valid?"
value: ${{ jobs.validate.outputs.is_valid}}

pull_request:
branches: ["*"]

workflow_dispatch:

jobs:
build:
validate:
runs-on: ubuntu-latest
outputs:
is_valid: ${{ steps.save_state.outputs.is_valid}}

steps:
- uses: actions/checkout@v4

- name: Download JSON artifacts # if called by another workflow
if: ${{ inputs.should_download }}
uses: actions/download-artifact@v4
with:
name: json-files
path: json

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ">=1.22.4"
- name: Install dependencies

- name: Install Go dependencies
run: go install github.com/santhosh-tekuri/jsonschema/cmd/jv@latest

- name: Validate JSON files
run: |
if [ -d "json" ] && [ "$(ls -A json/*.json 2>/dev/null)" ]; then
jv schema.json json/*.json
else
echo "No JSON files to process."
fi
id: validate_json
run: python validate-json.py schema.json json

- name: Save state
id: save_state
if: steps.validate_json.conclusion == 'success'
run: echo "is_valid=true" >> $GITHUB_OUTPUT
8 changes: 7 additions & 1 deletion convert-yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ def write_schema_as_json(self, schema, json_file):

def convert(self):
"""Reads, validates YAML files from a directory and writes JSON files to separate directory."""
for yaml_file_path in self.get_yaml_file_paths():
yaml_file_paths = self.get_yaml_file_paths()
if not yaml_file_paths:
print("No YAML files to process.")
return

for yaml_file_path in yaml_file_paths:
schema = NotificationSchema.from_yaml(yaml_file_path)

json_file_name = self.generate_json_file_name(yaml_file_path)
Expand All @@ -53,6 +58,7 @@ def convert(self):
should_write = args.overwrite or not does_exist
if should_write:
with open(json_file_path, "w") as f:
print(f'Writing {json_file_path}')
self.write_schema_as_json(schema, f)

def main():
Expand Down
62 changes: 62 additions & 0 deletions json/example2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"id": "f103c749-9932-48bb-8f61-2e8ed2beacc3",
"start_at": "2012-03-29T10:05:45-06:00",
"end_at": "2012-03-29T10:05:45-06:00",
"title": "hello user - this needs translation",
"description": "lorem ipsum user - this needs translation",
"CTA": "lorem ipsum user - this needs translation",
"URL": "https://thunderbird.net",
"severity": 5,
"type": "donation",
"targeting": {
"percent_chance": 10,
"exclude": [
{
"locales": [],
"versions": [],
"channels": [],
"operating_systems": []
}
],
"include": [
{
"locales": [],
"versions": [],
"channels": [],
"operating_systems": []
}
]
}
},
{
"id": "f103c749-9932-48bb-8f61-2e8ed2beacc3",
"start_at": "2012-03-29T10:05:45-06:00",
"end_at": "2024-05-03T15:49:20.331Z",
"title": "hello user - this needs translation",
"description": "lorem ipsum user - this needs translation",
"CTA": "lorem ipsum user - this needs translation",
"URL": "https://thunderbird.net",
"severity": 5,
"type": "donation",
"targeting": {
"percent_chance": 10,
"exclude": [
{
"locales": [],
"versions": [],
"channels": [],
"operating_systems": []
}
],
"include": [
{
"locales": [],
"versions": [],
"channels": [],
"operating_systems": []
}
]
}
}
]
41 changes: 41 additions & 0 deletions test/data/yaml/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
- id: f103c749-9932-48bb-8f61-2e8ed2beacc3
start_at: "2012-03-29T10:05:45-06:00"
end_at: "2024-05-03T15:49:20.331Z"
title: hello user - this needs translation
description: lorem ipsum user - this needs translation
CTA: lorem ipsum user - this needs translation
URL: https://thunderbird.net
severity: 5
type: donation
targeting:
percent_chance: 100
exclude:
- locales: []
versions: []
channels: []
operating_systems: []
include:
- locales: []
versions: []
channels: []
- id: f103c749-9932-48bb-8f61-2e8ed2beacc3
start_at: "2024-05-03T15:49:20.331Z"
end_at: "2024-05-03T15:49:20.331Z"
title: hello user - this needs translation
description: lorem ipsum user - this needs translation
CTA: lorem ipsum user - this needs translation
URL: https://thunderbird.net
severity: 5
type: donation
targeting:
percent_chance: 10
exclude:
- locales: []
versions: []
channels: []
operating_systems: []
include:
- locales: []
versions: []
channels: []
49 changes: 49 additions & 0 deletions validate-json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import subprocess
import sys
import argparse

parser = argparse.ArgumentParser(description='Validate JSON schema and JSON files for Thunderbird notifications.')
parser.add_argument('schema_file', type=str, help='The schema file to validate against.')
parser.add_argument('json_dir', type=str, help='The directory containing JSON files.')
args = parser.parse_args()

class JSONValidator:
def __init__(self, schema_file, json_dir):
self.schema_file = schema_file
self.json_dir = json_dir

if not os.path.isfile(self.schema_file):
raise ValueError(f'Argument for schema file "{schema_file}" was not found.')

if not os.path.isdir(self.json_dir):
raise ValueError(f'Argument for json_dir "{self.json_dir}" is not a directory')

def validate(self):
json_files = [f for f in os.listdir(self.json_dir) if f.endswith('.json')]
if not json_files:
print("No JSON files to process.")
return 0 # Exit successfully if no JSON files are found

command = ['jv', self.schema_file] + [os.path.join(self.json_dir, f) for f in json_files]

try:
result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print('Validation command completed successfully')
return 0
except subprocess.CalledProcessError as e:
print(f'Error running validation command: {e.stdout}', file=sys.stdout)
return 1

def main():
if len(sys.argv) < 3 or len(sys.argv) > 4:
parser.print_help()
sys.exit(1)

validator = JSONValidator(args.schema_file, args.json_dir)
exit_code = validator.validate()
sys.exit(exit_code)

if __name__ == "__main__":
main()

43 changes: 43 additions & 0 deletions yaml/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
- id: f103c749-9932-48bb-8f61-2e8ed2beacc3
start_at: "2012-03-29T10:05:45-06:00"
end_at: "2024-05-03T15:49:20.331Z"
title: hello user - this needs translation
description: lorem ipsum user - this needs translation
CTA: lorem ipsum user - this needs translation
URL: https://thunderbird.net
severity: 5
type: donation
targeting:
percent_chance: 100
exclude:
- locales: []
versions: []
channels: []
operating_systems: []
include:
- locales: []
versions: []
channels: []
operating_systems: []
- id: f103c749-9932-48bb-8f61-2e8ed2beacc3
start_at: "2024-05-03T15:49:20.331Z"
end_at: "2024-05-03T15:49:20.331Z"
title: hello user - this needs translation
description: lorem ipsum user - this needs translation
CTA: lorem ipsum user - this needs translation
URL: https://thunderbird.net
severity: 5
type: donation
targeting:
percent_chance: 10
exclude:
- locales: []
versions: []
channels: []
operating_systems: []
include:
- locales: []
versions: []
channels: []
operating_systems: []

0 comments on commit 8ad5e2c

Please sign in to comment.