Skip to content

Commit

Permalink
Merge pull request #31 from Laserlicht/workflow
Browse files Browse the repository at this point in the history
Add automatically sizes / check JSON
  • Loading branch information
IvanSavenko authored Sep 17, 2023
2 parents 71c2e43 + 9edf83e commit 5c73b9f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/ignore_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore = [ "./github.json", "./vcmi-1.2.json", "./vcmi-1.3.json" ]
31 changes: 31 additions & 0 deletions .github/update_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
import glob
import os
import sys
import urllib.request

from ignore_json import ignore

for filename in glob.glob(os.path.join('.', '*.json')):
if filename not in ignore:
print(f"Opening: {filename}")
filecontent = open(filename, "r").read()
modlist = json.loads(filecontent)
for mod, data in modlist.items():
url = data["download"].replace(" ", "%20")
print(f"Download {mod}: {url}")
try:
response = urllib.request.urlopen(url)
except:
print(f"Error: download failed!")
sys.exit(os.EX_SOFTWARE)
filesize = round(len(response.read()) / 1024 / 1024, 3)
print(f"Size: {filesize}")
data["downloadSize"] = filesize

resultcontent = json.dumps(modlist, indent='\t', separators=(',', ' : '))

if filecontent != resultcontent:
open(filename, "w").write(resultcontent)

sys.exit(os.EX_OK)
23 changes: 23 additions & 0 deletions .github/validate_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json
import glob
import os
import sys
import urllib.request

from ignore_json import ignore

error = False

for filename in glob.glob(os.path.join('.', '*.json')):
if filename not in ignore:
print(f"Opening: {filename}")
filecontent = open(filename, "r").read()

try:
json.loads(filecontent)
except Exception as err:
print("Error: " + str(err))
sys.exit(os.EX_SOFTWARE)

print("Everything is ok!")
sys.exit(os.EX_OK)
37 changes: 37 additions & 0 deletions .github/validate_mod_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json
import glob
import os
import sys
import urllib.request

from ignore_json import ignore

error = False

for filename in glob.glob(os.path.join('.', '*.json')):
if filename not in ignore:
print(f"Opening: {filename}")
filecontent = open(filename, "r").read()
modlist = json.loads(filecontent)
for mod, data in modlist.items():
url = data["mod"].replace(" ", "%20")
print(f"Download {mod}: {url}")
try:
response = urllib.request.urlopen(url)
except:
error = True
print(f"Error: download failed!")
continue
filecontent = response.read()

try:
json.loads(filecontent)
except Exception as err:
error = True
print("Error: " + str(err))
continue
if error:
sys.exit(os.EX_SOFTWARE)
else:
print("Everything is ok!")
sys.exit(os.EX_OK)
52 changes: 52 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: VCMI Mods

on:
push:
branches:
- develop
pull_request:
paths:
- "**.json"
schedule:
- cron: '0 2 * * *'
workflow_dispatch:

jobs:
validate_json:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3
- name: Validate json
run: |
python3 .github/validate_json.py
validate_mod_json:
needs: validate_json
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3
- name: Validate mod json
run: |
python3 .github/validate_mod_json.py
update_size:
needs: validate_json
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3
- name: Update size
run: |
python3 .github/update_size.py
- name: Commit changes
uses: EndBug/add-and-commit@v9
with:
default_author: github_actions
message: Update sizes
add: '*.json'

0 comments on commit 5c73b9f

Please sign in to comment.