diff --git a/README.md b/README.md index b43cd18..9f1bf66 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,9 @@ Voor automatische berichten zijn aanvullende toegangsgegevens nodig. ## Bouwen en testen Bouw de installeerbare ZIP met `python3 scripts/build.py`. Voer de integratietest uit met `wp --skip-plugins=hg_socialfeed eval-file /pad/naar/tests/integration.php` op een testinstallatie met een bestaande WordPress-beheerder. De test simuleert API-antwoorden en houdt testdata in het testproces. + +## Gedeeld HG-beheer + +De installatie-ZIP bevat het HG-beheermenu en de updater. De build controleert de vastgelegde bestanden in `shared.lock.json`. [Gedeelde code](https://code.hosting.hippogrief.nl/hippogrief/wp-plugins) + +Open **HG Plugins → Updates** om openbare plug-inreleases te controleren. [Openbare releases](https://code.hosting.hippogrief.nl/hippogrief/wp-plugins/releases) diff --git a/scripts/build.py b/scripts/build.py index fd49586..3a8f744 100755 --- a/scripts/build.py +++ b/scripts/build.py @@ -7,7 +7,10 @@ import sys from zipfile import ZIP_DEFLATED, ZipFile +from sync_shared import verify_shared + root = Path(__file__).resolve().parents[1] +verify_shared(root) destination = Path(sys.argv[1]) if len(sys.argv) > 1 else root / "dist" version = re.search(r"^ \* Version: (\S+)$", (root / "hg-socialfeed.php").read_text(), re.MULTILINE) if not version: diff --git a/scripts/sync_shared.py b/scripts/sync_shared.py new file mode 100644 index 0000000..6e13320 --- /dev/null +++ b/scripts/sync_shared.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Sync or verify the shared HG files bundled by a standalone plugin.""" + +import argparse +import hashlib +import json +from pathlib import Path +import re +import subprocess + + +SOURCE_URL = 'https://code.hosting.hippogrief.nl/hippogrief/wp-plugins' +SHARED_FILES = ( + 'assets/hg-admin-menu.css', + 'includes/hg-admin-menu.php', + 'includes/hg-plugin-updates.php', + 'scripts/sync_shared.py', +) + + +def verify_shared(root: Path) -> None: + try: + lock = json.loads((root / 'shared.lock.json').read_text()) + if lock['source'] != SOURCE_URL or not re.fullmatch(r'[0-9a-f]{40}', lock['commit']): + raise ValueError('invalid source or commit') + if set(lock['sha256']) != set(SHARED_FILES): + raise ValueError('unexpected shared file list') + for name in SHARED_FILES: + checksum = hashlib.sha256((root / name).read_bytes()).hexdigest() + if checksum != lock['sha256'][name]: + raise ValueError(f'checksum differs for {name}') + except (OSError, ValueError, TypeError, KeyError) as error: + raise SystemExit(f'HG shared files failed verification: {error}') from error + + +def sync_shared(root: Path, source: Path, ref: str) -> None: + git = ['git', '-C', str(source)] + commit = subprocess.check_output( + git + ['rev-parse', '--verify', '--end-of-options', f'{ref}^{{commit}}'], + text=True, + ).strip() + shared_paths = {name: f'shared/{name}' for name in SHARED_FILES} + shared_paths['scripts/sync_shared.py'] = 'shared/sync_shared.py' + files = { + name: subprocess.check_output(git + ['show', f'{commit}:{remote_path}']) + for name, remote_path in shared_paths.items() + } + for name, contents in files.items(): + (root / name).write_bytes(contents) + lock = { + 'source': SOURCE_URL, + 'commit': commit, + 'sha256': {name: hashlib.sha256(contents).hexdigest() for name, contents in files.items()}, + } + (root / 'shared.lock.json').write_text(json.dumps(lock, indent=2) + '\n') + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--source', type=Path, help='Path to a checkout of wp-plugins') + parser.add_argument('--ref', help='Tag or commit to copy from the shared checkout') + options = parser.parse_args() + if bool(options.source) != bool(options.ref): + parser.error('--source and --ref must be supplied together') + plugin_root = Path(__file__).resolve().parents[1] + if options.source: + sync_shared(plugin_root, options.source, options.ref) + verify_shared(plugin_root) diff --git a/shared.lock.json b/shared.lock.json new file mode 100644 index 0000000..54a66a1 --- /dev/null +++ b/shared.lock.json @@ -0,0 +1,10 @@ +{ + "source": "https://code.hosting.hippogrief.nl/hippogrief/wp-plugins", + "commit": "b812c041a91981b1034d65f2e4ee1daff6cefaf6", + "sha256": { + "assets/hg-admin-menu.css": "a2af6a52dc9abef9127a9d26903b9809c1001720a89f7d85f4c22bc7d8b7f581", + "includes/hg-admin-menu.php": "0723f681aeb651f1737de686ba8657e8f7adc7c53073a47025fcd1565bbe1047", + "includes/hg-plugin-updates.php": "337705f1a5f1901080c5a5450a8f2e4f394a9fa8a4a7975ec5a2371fe2e55a52", + "scripts/sync_shared.py": "a803f6d9dbcd34e6023397d9a61cd028bd04867cd0309c5b709e4282be752871" + } +}