-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from thunderbird/features/8-gh-action-run-conve…
…rter Add GH action: convert yaml to json
- Loading branch information
Showing
7 changed files
with
315 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |