From 83d2151febbc043926fd08bbadebffa804807fed Mon Sep 17 00:00:00 2001 From: Yankyyyy Date: Fri, 22 Aug 2025 05:10:47 +0000 Subject: [PATCH 1/5] bug: Unhandled webhook event: None bug fix v9.0 --- erpnext_github_integration/webhooks.py | 46 ++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/erpnext_github_integration/webhooks.py b/erpnext_github_integration/webhooks.py index 4c25419..43b0697 100644 --- a/erpnext_github_integration/webhooks.py +++ b/erpnext_github_integration/webhooks.py @@ -2,6 +2,29 @@ from frappe import _ +def get_github_event_header(): + """Get GitHub event header in a more robust way""" + # Try different ways to get the event header + event = None + + # Method 1: Standard header access + if hasattr(frappe.request, 'headers'): + headers = frappe.request.headers + event = headers.get('X-GitHub-Event') or headers.get('x-github-event') + + # Method 2: Direct environ access + if not event and hasattr(frappe.local, 'request') and hasattr(frappe.local.request, 'environ'): + environ = frappe.local.request.environ + # HTTP headers are prefixed with HTTP_ and converted to uppercase with dashes as underscores + event = environ.get('HTTP_X_GITHUB_EVENT') + + # Method 3: Frappe's header helper + if not event: + event = frappe.get_request_header("X-GitHub-Event") or frappe.get_request_header("x-github-event") + + return event + + @frappe.whitelist(allow_guest=True) def github_webhook(): """Handle GitHub webhook events""" @@ -23,9 +46,22 @@ def github_webhook(): frappe.log_error('Invalid webhook signature', 'GitHub Webhook') frappe.throw(_('Invalid webhook signature'), exc=frappe.PermissionError) - # Get event name (case-insensitive headers) - event = frappe.get_request_header("X-GitHub-Event") or frappe.get_request_header("x-github-event") + # Get event name with robust header extraction + event = get_github_event_header() data = json.loads(payload.decode('utf-8')) + + # If event header is missing, try to infer from payload + if not event: + if 'commits' in data and 'ref' in data and 'before' in data and 'after' in data: + event = 'push' + elif 'issue' in data: + event = 'issues' + elif 'pull_request' in data: + event = 'pull_request' + elif 'member' in data: + event = 'member' + elif 'action' in data and 'repository' in data and data.get('repository'): + event = 'repository' # Get repository info repo_info = data.get('repository', {}) or {} @@ -34,6 +70,9 @@ def github_webhook(): if not repo_full_name: frappe.log_error('No repository information in webhook payload', 'GitHub Webhook') return 'ok' + + # Log the event for debugging + frappe.log_error(f'Processing webhook event: {event} for repo: {repo_full_name}', 'GitHub Webhook Debug') # Check if repo exists in system if not frappe.db.exists('Repository', {'full_name': repo_full_name}): @@ -61,10 +100,11 @@ def _process_github_webhook(event=None, data=None, repo_full_name=None): """Process GitHub webhook events in background""" try: if not event: - frappe.log_error(json.dumps(data, indent=2), "GitHub Webhook Missing Event") + frappe.log_error(f"Missing event parameter. Data: {json.dumps(data, indent=2)}", "GitHub Webhook Missing Event") return event = event.lower().strip() + frappe.log_error(f"Processing event: {event} for repo: {repo_full_name}", "GitHub Webhook Processing") if event == "issues": _handle_issues_event(data, repo_full_name) From 999eed92c6118afb93672ffc626b79d72fe4ad35 Mon Sep 17 00:00:00 2001 From: Yankyyyy Date: Fri, 22 Aug 2025 05:15:38 +0000 Subject: [PATCH 2/5] bug: Unhandled webhook event: None bug fix v10.0 --- erpnext_github_integration/webhooks.py | 29 ++++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/erpnext_github_integration/webhooks.py b/erpnext_github_integration/webhooks.py index 43b0697..522eeb3 100644 --- a/erpnext_github_integration/webhooks.py +++ b/erpnext_github_integration/webhooks.py @@ -79,15 +79,26 @@ def github_webhook(): frappe.log_error(f'Repository {repo_full_name} not found in system', 'GitHub Webhook') return 'ok' - # Queue background job - frappe.enqueue( - "erpnext_github_integration.webhooks._process_github_webhook", - queue='default', - event=event, - data=data, - repo_full_name=repo_full_name, - now=False # ensures it's async - ) + # Try to process in background, fallback to immediate processing + try: + # Get current module path dynamically + current_module = __name__ + function_path = f"{current_module}._process_github_webhook" + + frappe.enqueue( + function_path, + queue='default', + event=event, + data=data, + repo_full_name=repo_full_name, + timeout=300, # 5 minutes timeout + is_async=True, + now=False + ) + except Exception as enqueue_error: + # If background job fails, process immediately + frappe.log_error(f'Background job enqueue failed: {str(enqueue_error)}. Processing immediately.', 'GitHub Webhook Enqueue Error') + _process_github_webhook(event=event, data=data, repo_full_name=repo_full_name) return 'ok' From 5d200e4088bc043490ea85f25878b7b24132bf2a Mon Sep 17 00:00:00 2001 From: Yankyyyy Date: Fri, 22 Aug 2025 05:20:03 +0000 Subject: [PATCH 3/5] bug: Unhandled webhook event: None bug fix v11.0 --- erpnext_github_integration/webhooks.py | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/erpnext_github_integration/webhooks.py b/erpnext_github_integration/webhooks.py index 522eeb3..48e9bea 100644 --- a/erpnext_github_integration/webhooks.py +++ b/erpnext_github_integration/webhooks.py @@ -79,26 +79,9 @@ def github_webhook(): frappe.log_error(f'Repository {repo_full_name} not found in system', 'GitHub Webhook') return 'ok' - # Try to process in background, fallback to immediate processing - try: - # Get current module path dynamically - current_module = __name__ - function_path = f"{current_module}._process_github_webhook" - - frappe.enqueue( - function_path, - queue='default', - event=event, - data=data, - repo_full_name=repo_full_name, - timeout=300, # 5 minutes timeout - is_async=True, - now=False - ) - except Exception as enqueue_error: - # If background job fails, process immediately - frappe.log_error(f'Background job enqueue failed: {str(enqueue_error)}. Processing immediately.', 'GitHub Webhook Enqueue Error') - _process_github_webhook(event=event, data=data, repo_full_name=repo_full_name) + # Process webhook immediately to avoid background job issues + # You can change this back to background processing once it's working + _process_github_webhook(event=event, data=data, repo_full_name=repo_full_name) return 'ok' From c7c8dcb81f1e56172721638e9922e2ed1c6b82fb Mon Sep 17 00:00:00 2001 From: Yankyyyy Date: Fri, 22 Aug 2025 05:32:42 +0000 Subject: [PATCH 4/5] bug: Unhandled webhook event: None bug fix v12.0 --- erpnext_github_integration/github_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext_github_integration/github_api.py b/erpnext_github_integration/github_api.py index 5b031ba..ace88e3 100644 --- a/erpnext_github_integration/github_api.py +++ b/erpnext_github_integration/github_api.py @@ -26,8 +26,8 @@ def convert_github_datetime(dt_string): return None # Usage -if not has_role('GitHub Admin'): - frappe.throw("Permission required") +# if not has_role('GitHub Admin'): +# frappe.throw("Permission required") def _require_github_admin(): if not has_role('GitHub Admin'): From d0ac06fffb04ca775303aa3a3fa50236efcf13cc Mon Sep 17 00:00:00 2001 From: Yankyyyy Date: Fri, 22 Aug 2025 05:46:26 +0000 Subject: [PATCH 5/5] bug: Unhandled webhook event: None bug fix final --- erpnext_github_integration/github_api.py | 5 ++++- erpnext_github_integration/webhooks.py | 9 +++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/erpnext_github_integration/github_api.py b/erpnext_github_integration/github_api.py index ace88e3..f2d6a94 100644 --- a/erpnext_github_integration/github_api.py +++ b/erpnext_github_integration/github_api.py @@ -321,12 +321,15 @@ def sync_repo(repository): # Clear and update branches repo_doc.set('branches_table', []) for b in branches: + commit_date_str = b.get('timestamp') or b.get('commit', {}).get('committer', {}).get('date') + if commit_date_str: + commit_date = convert_github_datetime(commit_date_str) repo_doc.append('branches_table', { 'repo_full_name': repo_full, 'branch_name': b.get('name'), 'commit_sha': b.get('commit', {}).get('sha'), 'protected': b.get('protected', False), - 'last_updated': convert_github_datetime(b.get('commit', {}).get('committer', {}).get('date')) + 'last_updated': commit_date }) # Clear and update members diff --git a/erpnext_github_integration/webhooks.py b/erpnext_github_integration/webhooks.py index 48e9bea..76c8e6f 100644 --- a/erpnext_github_integration/webhooks.py +++ b/erpnext_github_integration/webhooks.py @@ -1,5 +1,6 @@ import frappe, hmac, hashlib, json from frappe import _ +from .github_api import convert_github_datetime def get_github_event_header(): @@ -171,8 +172,8 @@ def _handle_issues_event(data, repo_full_name): 'labels': ','.join([l.get('name', '') for l in issue.get('labels', [])]), 'url': issue.get('html_url', ''), 'github_id': str(issue.get('id', '')), - 'created_at': frappe.utils.get_datetime(issue.get('created_at')), - 'updated_at': frappe.utils.get_datetime(issue.get('updated_at')) + 'created_at': convert_github_datetime(issue.get('created_at')), + 'updated_at': convert_github_datetime(issue.get('updated_at')) }) # Add assignees @@ -254,8 +255,8 @@ def _handle_pull_request_event(data, repo_full_name): 'mergeable_state': pr.get('mergeable_state', ''), 'github_id': str(pr.get('id', '')), 'url': pr.get('html_url', ''), - 'created_at': frappe.utils.get_datetime(pr.get('created_at')), - 'updated_at': frappe.utils.get_datetime(pr.get('updated_at')) + 'created_at': convert_github_datetime(pr.get('created_at')), + 'updated_at': convert_github_datetime(pr.get('updated_at')) }) # Add reviewers