From 0e776393df34b6921a82911c2a7f182c17e86f6a Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Wed, 9 Apr 2025 00:44:33 +0200 Subject: [PATCH] Validate json files that were changed on PR --- .github/workflows/ci.yml | 10 ++++++++- src/validate_info_json.py | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/validate_info_json.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d698ea6..f16303d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,9 +31,17 @@ jobs: uses: actions/checkout@v4 with: path: custom + fetch-depth: 0 - name: Install Python dependencies for custom project generation - run: python -m pip install pyyaml + run: | + python -m pip install pyyaml + python -m pip install requests + + - name: Get changed info.json files and validate them + working-directory: custom + run: | + git diff origin/main --name-only | grep info\\.json | xargs python src/validate_info_json.py $1 - name: Test python scripts working-directory: custom diff --git a/src/validate_info_json.py b/src/validate_info_json.py new file mode 100644 index 0000000..baf956c --- /dev/null +++ b/src/validate_info_json.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +"""Validate the custom app json file passed in as an argument +Making sure we are using https for ZIM the file, +and that it is downloadable +""" + +from brand import Brand +import argparse +import info_parser +import json +import requests + + +def main(): + parser = argparse.ArgumentParser( + description="Validator for custom apps info.json file") + parser.add_argument( + "info_json", + help="The info.json file to be verified", + type=str + ) + args = parser.parse_args() + return _is_valid(args.info_json) + +def _is_valid(info_json): + with open(info_json, 'r') as info_file: + content = info_file.read() + data = json.loads(content) + zim_url = data[info_parser.JSON_KEY_ZIM_URL] + + # is https + assert zim_url.startswith( + "https://"), "ZIM URL is not using https: {}".format(zim_url) + + # try a head request on it + response = requests.head(zim_url) + + # is reachable + assert response.status_code in [200, 302], "ZIM URL is not reachable: {} response code: {}".format( + response.url, response) + + +if __name__ == "__main__": + main()