diff --git a/.github/workflows/auto_apply_version_metadata.yml b/.github/workflows/auto_apply_version_metadata.yml new file mode 100644 index 0000000..268c38a --- /dev/null +++ b/.github/workflows/auto_apply_version_metadata.yml @@ -0,0 +1,25 @@ +name: Apply Version Metadata + +on: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.10"] + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Auto Apply Version Metadata + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Apply version metadata + run: | + python test/auto_apply_version_metadata.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d04e2fb..d96fffa 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,6 @@ name: Tests -on: +on: push: pull_request: diff --git a/plugins/minigames.json b/plugins/minigames.json index 6ed54d6..19608e3 100644 --- a/plugins/minigames.json +++ b/plugins/minigames.json @@ -14,11 +14,11 @@ } ], "versions": { - "1.1.0": { - "api_version": 7, - "commit_sha": "40b70fe", - "released_on": "08-08-2022", - "md5sum": "f4f0bb91f5d10cf8f591ecf7d2848182" + "1.1.0": { + "api_version": 7, + "commit_sha": "40b70fe", + "released_on": "08-08-2022", + "md5sum": "f4f0bb91f5d10cf8f591ecf7d2848182" } } } diff --git a/plugins/utilities.json b/plugins/utilities.json index 928a86d..41eda5e 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -19,23 +19,23 @@ } ], "versions": { - "1.2.1": { - "api_version": 7, - "commit_sha": "109e61c5a", - "released_on": "29-08-2022", - "md5sum": "970360789f4605132eb5091e30a64642" + "1.2.1": { + "api_version": 7, + "commit_sha": "109e61c5a", + "released_on": "29-08-2022", + "md5sum": "970360789f4605132eb5091e30a64642" }, - "1.2.0": { - "api_version": 7, - "commit_sha": "963a17379", - "released_on": "12-08-2022", - "md5sum": "41084bfec41119ca9df8e6d899cd3cc0" + "1.2.0": { + "api_version": 7, + "commit_sha": "963a17379", + "released_on": "12-08-2022", + "md5sum": "41084bfec41119ca9df8e6d899cd3cc0" }, - "1.0.0": { - "api_version": 6, - "commit_sha": "da5032a7", - "released_on": "28-05-2021", - "md5sum": "527acfec13a2d0a6115a52df7abca920" + "1.0.0": { + "api_version": 6, + "commit_sha": "da5032a7", + "released_on": "28-05-2021", + "md5sum": "527acfec13a2d0a6115a52df7abca920" } } }, @@ -50,11 +50,11 @@ } ], "versions": { - "1.0.0": { - "api_version": 7, - "commit_sha": "63d674cf", - "released_on": "06-08-2022", - "md5sum": "233dfaa7f0e9394d21454f4ffa7d0205" + "1.0.0": { + "api_version": 7, + "commit_sha": "63d674cf", + "released_on": "06-08-2022", + "md5sum": "233dfaa7f0e9394d21454f4ffa7d0205" } } }, @@ -69,13 +69,13 @@ } ], "versions": { - "1.0.0": { - "api_version": 7, - "commit_sha": "6beb8ddf", - "released_on": "30-08-2022", - "md5sum": "aac4edfcaeca1dc2910f97e739d67482" + "1.0.0": { + "api_version": 7, + "commit_sha": "6beb8ddf", + "released_on": "30-08-2022", + "md5sum": "aac4edfcaeca1dc2910f97e739d67482" } } } } -} +} \ No newline at end of file diff --git a/test/auto_apply_version_metadata.py b/test/auto_apply_version_metadata.py new file mode 100644 index 0000000..467c689 --- /dev/null +++ b/test/auto_apply_version_metadata.py @@ -0,0 +1,124 @@ +import hashlib +import json +import os +import sys +import re +import datetime + + +class NullVersionedPlugin: + def __init__(self, plugin_name, version_name, plugin_path, from_json={}): + self.plugin_name = plugin_name + self.version_name = version_name + self.plugin_path = plugin_path + self.json = from_json + self.api_version_regexp = re.compile(b"(?<=ba_meta require api )(.*)") + self._content = None + + def set_json(self, json): + self.json = json + return self + + def set_api_version(self): + if self._content is None: + with open(self.plugin_path, "rb") as fin: + self._content = fin.read() + + versions = self.json["plugins"][self.plugin_name]["versions"] + if versions[self.version_name] is None: + versions[self.version_name] = {} + + api_version = self.api_version_regexp.search(self._content).group() + versions[self.version_name]["api_version"] = int(api_version) + return self + + def set_commit_sha(self, commit_sha): + versions = self.json["plugins"][self.plugin_name]["versions"] + if versions[self.version_name] is None: + versions[self.version_name] = {} + + versions[self.version_name]["commit_sha"] = commit_sha[:8] + return self + + def set_released_on(self, date): + versions = self.json["plugins"][self.plugin_name]["versions"] + if versions[self.version_name] is None: + versions[self.version_name] = {} + + versions[self.version_name]["released_on"] = date + return self + + def set_md5sum(self): + if self._content is None: + with open(self.plugin_path, "rb") as fin: + self._content = fin.read() + + versions = self.json["plugins"][self.plugin_name]["versions"] + if versions[self.version_name] is None: + versions[self.version_name] = {} + + md5sum = hashlib.md5(self._content).hexdigest() + versions[self.version_name]["md5sum"] = md5sum + return self + + +class CategoryVersionMetadata: + def __init__(self, category_metadata_base): + self.category_metadata_base = category_metadata_base + self.category_metadata_file = f"{self.category_metadata_base}.json" + with open(self.category_metadata_file, "r") as fin: + self.category_metadata = json.load(fin) + + def get_plugins_having_null_version_values(self): + for plugin_name, plugin_metadata in self.category_metadata["plugins"].items(): + latest_version_name, latest_version_metadata = tuple(plugin_metadata["versions"].items())[0] + if latest_version_metadata is None: + plugin_path = f"{os.path.join(self.category_metadata_base, f'{plugin_name}.py')}" + yield NullVersionedPlugin( + plugin_name, + latest_version_name, + plugin_path, + ) + + def apply_version_metadata_to_null_version_values(self, commit_sha): + null_versioned_plugins = self.get_plugins_having_null_version_values() + today = datetime.date.today().strftime("%d-%m-%Y") + category_json = self.category_metadata + for plugin in null_versioned_plugins: + category_json = ( + plugin.set_json(category_json) + .set_api_version() + .set_commit_sha(commit_sha) + .set_released_on(today) + .set_md5sum() + .json + ) + return category_json + + def save(self, category_json): + with open(self.category_metadata_file, "w") as fout: + json.dump( + category_json, + fout, + indent=2, + ensure_ascii=False, + ) + + +def auto_apply_version_metadata(last_commit_sha): + utilities = CategoryVersionMetadata(os.path.join("plugins", "utilities")) + category_json = utilities.apply_version_metadata_to_null_version_values(last_commit_sha) + utilities.save(category_json) + + minigames = CategoryVersionMetadata(os.path.join("plugins", "minigames")) + category_json = minigames.apply_version_metadata_to_null_version_values(last_commit_sha) + minigames.save(category_json) + + +if __name__ == "__main__": + try: + last_commit_sha = sys.argv[1] + except KeyError: + raise ValueError("Last commit SHA not provided.") + else: + auto_apply_version_metadata(last_commit_sha)