diff --git a/bugzilla rss to rocket json.py b/bugzilla rss to rocket json.py index c28e67c..82d1256 100644 --- a/bugzilla rss to rocket json.py +++ b/bugzilla rss to rocket json.py @@ -6,40 +6,34 @@ import sqlite3 import logging import os import xml.etree.ElementTree as ET +import socket # Ensure you import socket for gethostbyname -# Common chat URL for both feeds +# Common chat URL for the feed COMMON_CHAT_URL = "https://chat.koozali.org/hooks/677e97a73ddf8049989dbc8c/r9uiYpTRAXo3mkFKxHnoTwGCdtKpYaDemCpHArgz89knkwLo" # Constants: Mapping Bugzilla RSS feeds to specific Rocket.Chat URLs with filtering criteria FEED_TO_CHAT_MAP = { "Bugzilla Feed 1": { - "rss_feed": "https://bugs.koozali.org/buglist.cgi?chfield=%5BBug%20creation%5D&chfieldfrom=7d&ctype=atom&title=Bugs%20reported%20in%20the%20last%207%20days", - "chat_url": COMMON_CHAT_URL, # Use common chat URL + "rss_feed": None, # To be set later with the resolved IP address + "chat_url": COMMON_CHAT_URL, "filter_field": "status", "filter_value": "open", "bypass_filter": True, - }, - "Bugzilla Feed 2": { - "rss_feed": "https://another.bugzilla.instance/buglist.cgi?chfield=%5BBug%20creation%5D&chfieldfrom=7d&ctype=atom&title=Another%20set%20of%20Bugs", - "chat_url": COMMON_CHAT_URL, # Use common chat URL - "filter_field": "priority", - "filter_value": "high", - "bypass_filter": True, } } def get_ip_address(domain, retries=10, delay=1): """ Resolves the IP address of a domain, retrying up to `retries` times if it fails. - + Args: domain (str): The domain name to resolve. retries (int): Number of retry attempts (default is 10). delay (int): Delay between retries in seconds (default is 1 second). - + Returns: str: The IP address if resolved successfully. - + Raises: RuntimeError: If unable to resolve the domain after all attempts. """ @@ -54,7 +48,6 @@ def get_ip_address(domain, retries=10, delay=1): raise RuntimeError(f"Unable to resolve domain '{domain}' after {retries} attempts.") - # Set up logging to the current directory log_file = "/var/log/BugzillaToRocket.log" logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @@ -152,8 +145,8 @@ def send_to_rocket_chat(bug_title, bug_link, bug_id, status, reported_by, last_c # Function to send a startup message to Rocket.Chat def send_startup_message(chat_url): - global sent_chat_urls - if chat_url not in sent_chat_urls: + global sent_chat_urls + if chat_url not in sent_chat_urls: payload = { "alias": "Bugzilla", "text": "Bugzilla to Rocket.Chat integration started successfully.", @@ -163,7 +156,7 @@ def send_startup_message(chat_url): if response.status_code == 200: logging.info(f"Startup message sent successfully to {chat_url}.") - sent_chat_urls.add(chat_url) + sent_chat_urls.add(chat_url) else: logging.error(f"Failed to send startup message to {chat_url}: {response.status_code} - {response.text}") @@ -196,9 +189,10 @@ def parse_summary(summary): return status, reported_by, last_changed, product, component # Function to fetch and parse Bugzilla RSS feeds -def fetch_bugzilla_feed(feed_url): +def fetch_bugzilla_feed(feed_url, original_domain): + headers = {'Host': original_domain} # Set Host header logging.info(f"Fetching feed: {feed_url}") - feed = feedparser.parse(feed_url) + feed = feedparser.parse(feed_url, request_headers=headers) entries = [] for entry in feed.entries: @@ -222,7 +216,7 @@ def parse_arguments(): parser.add_argument( '--sleep', type=int, - default=1, + default=1, help='Number of minutes to sleep between polls (default: 1 minute)' ) parser.add_argument( @@ -234,29 +228,35 @@ def parse_arguments(): # Main polling loop def main(): - args = parse_arguments() - sleep_duration = args.sleep * 60 - + args = parse_arguments() + sleep_duration = args.sleep * 60 + setup_database() - # Send startup messages for each feed's chat URL - for feed_info in FEED_TO_CHAT_MAP.values(): - send_startup_message(feed_info['chat_url']) + # Preload IP address for Bugzilla instance + bugzilla_ip = get_ip_address("bugs.koozali.org") + + # Update the RSS feed URL with the resolved IP address + FEED_TO_CHAT_MAP["Bugzilla Feed 1"]["rss_feed"] = f"http://{bugzilla_ip}/buglist.cgi?chfield=%5BBug%20creation%5D&chfieldfrom=7d&ctype=atom&title=Bugs%20reported%20in%20the%20last%207%20days" + + # Send startup message for the feed's chat URL + send_startup_message(FEED_TO_CHAT_MAP["Bugzilla Feed 1"]["chat_url"]) while True: for feed_name, chat_info in FEED_TO_CHAT_MAP.items(): - entries = fetch_bugzilla_feed(chat_info['rss_feed']) + # Use the resolved IP address in fetch_bugzilla_feed + entries = fetch_bugzilla_feed(chat_info['rss_feed'], "bugs.koozali.org") - filter_field = chat_info.get('filter_field', '') - filter_value = chat_info.get('filter_value', '') - bypass_filter = chat_info.get('bypass_filter', False) + filter_field = chat_info.get('filter_field', '') + filter_value = chat_info.get('filter_value', '') + bypass_filter = chat_info.get('bypass_filter', False) for entry in entries: bug_id = entry.id.split('=')[-1] status = entry.status.lower() if hasattr(entry, 'status') else "unknown" if bypass_filter or (getattr(entry, filter_field, "").lower() == filter_value): - if not has_bug_been_sent(bug_id, status): + if not has_bug_been_sent(bug_id, status): title = entry.title.strip() link = entry.link reported_by = entry.reported_by