diff --git a/github_webhook_updater.py b/github_webhook_updater.py index 6c66d2d..99ea99e 100644 --- a/github_webhook_updater.py +++ b/github_webhook_updater.py @@ -48,7 +48,7 @@ def verify_signature(payload: bytes, signature: str) -> bool: return hmac.compare_digest(expected, signature) -def pull_and_restart(changed_files: list[str]) -> tuple[bool, str]: +def pull_and_restart(changed_files: list[str], before: str = '', after: str = '') -> tuple[bool, str]: """Pull latest changes from main and restart pm2 processes.""" try: # Change to repo directory @@ -79,6 +79,17 @@ def pull_and_restart(changed_files: list[str]) -> tuple[bool, str]: logger.info(f"Pull successful: {pull_result.stdout.strip()}") + # If the payload commits list was empty (e.g. GitHub PR merge), derive + # changed files from the before/after SHAs instead. + if not changed_files and before and after and before != '0' * 40: + diff_result = subprocess.run( + ['git', 'diff', '--name-only', before, after], + capture_output=True, text=True, timeout=15 + ) + if diff_result.returncode == 0: + changed_files = [f for f in diff_result.stdout.splitlines() if f] + logger.info(f"Derived changed files from git diff: {changed_files}") + # Determine which processes to restart based on changed files processes_to_restart = [] @@ -242,6 +253,8 @@ def webhook(): # Extract changed files from commits commits = payload.get('commits', []) pusher = payload.get('pusher', {}).get('name', 'unknown') + before = payload.get('before', '') + after = payload.get('after', '') logger.info(f"Push from {pusher} with {len(commits)} commit(s)") changed_files = [] @@ -253,7 +266,7 @@ def webhook(): logger.info(f"Changed files: {changed_files}") # Pull and restart - success, message = pull_and_restart(changed_files) + success, message = pull_and_restart(changed_files, before, after) if success: logger.info(f"Webhook update successful: {message}")