Added logging to /var/log/???.log and Initial message to Rocket
This commit is contained in:
@@ -1,16 +1,52 @@
|
||||
import time
|
||||
import feedparser
|
||||
import requests
|
||||
import sqlite3
|
||||
import logging
|
||||
import os
|
||||
|
||||
# Bugzilla RSS feed URL
|
||||
BUGZILLA_RSS_URL = "https://bugs.koozali.org/buglist.cgi?chfield=%5BBug%20creation%5D&chfieldfrom=7d&ctype=atom&title=Bugs%20reported%20in%20the%20last%207%20days"
|
||||
# Updated Rocket.Chat webhook URL
|
||||
ROCKET_CHAT_URL = "https://chat.koozali.org/hooks/677e97a73ddf8049989dbc8c/r9uiYpTRAXo3mkFKxHnoTwGCdtKpYaDemCpHArgz89knkwLo"
|
||||
|
||||
# Set to keep track of sent bug IDs
|
||||
sent_bugs = set()
|
||||
# Set up logging
|
||||
log_file = "/var/log/BugzillaToRocket.log"
|
||||
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
# Function to send message to Rocket.Chat
|
||||
# Ensure the log directory exists
|
||||
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
||||
|
||||
# Database setup
|
||||
def setup_database():
|
||||
conn = sqlite3.connect('sent_bugs.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS sent_bugs (
|
||||
id TEXT PRIMARY KEY
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Function to check if a bug ID has been sent
|
||||
def has_bug_been_sent(bug_id):
|
||||
conn = sqlite3.connect('sent_bugs.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT * FROM sent_bugs WHERE id = ?', (bug_id,))
|
||||
exists = cursor.fetchone() is not None
|
||||
conn.close()
|
||||
return exists
|
||||
|
||||
# Function to mark a bug ID as sent
|
||||
def mark_bug_as_sent(bug_id):
|
||||
conn = sqlite3.connect('sent_bugs.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('INSERT OR IGNORE INTO sent_bugs (id) VALUES (?)', (bug_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Function to send a message to Rocket.Chat
|
||||
def send_to_rocket_chat(bug_title, bug_link, bug_id):
|
||||
payload = {
|
||||
"alias": "Bugzilla",
|
||||
@@ -25,9 +61,21 @@ def send_to_rocket_chat(bug_title, bug_link, bug_id):
|
||||
}
|
||||
response = requests.post(ROCKET_CHAT_URL, json=payload)
|
||||
if response.status_code == 200:
|
||||
print(f"Bug report sent successfully: {bug_title} (ID: {bug_id})")
|
||||
logging.info(f"Bug notification sent successfully: {bug_title} (ID: {bug_id})")
|
||||
else:
|
||||
print(f"Failed to send bug report: {response.status_code} - {response.text}")
|
||||
logging.error(f"Failed to send bug notification: {response.status_code} - {response.text}")
|
||||
|
||||
# Function to send a startup message to Rocket.Chat
|
||||
def send_startup_message():
|
||||
payload = {
|
||||
"alias": "Bugzilla",
|
||||
"text": "Bugzilla to Rocket.Chat integration started successfully.",
|
||||
}
|
||||
response = requests.post(ROCKET_CHAT_URL, json=payload)
|
||||
if response.status_code == 200:
|
||||
logging.info("Startup message sent successfully to Rocket.Chat.")
|
||||
else:
|
||||
logging.error(f"Failed to send startup message: {response.status_code} - {response.text}")
|
||||
|
||||
# Function to fetch and parse the Bugzilla RSS feed
|
||||
def fetch_bugzilla_feed():
|
||||
@@ -36,20 +84,23 @@ def fetch_bugzilla_feed():
|
||||
|
||||
# Main polling loop
|
||||
def main():
|
||||
setup_database() # Initialize the database
|
||||
send_startup_message() # Send the startup message
|
||||
|
||||
while True:
|
||||
entries = fetch_bugzilla_feed()
|
||||
|
||||
for entry in entries:
|
||||
bug_id = entry.id.split('=')[-1] # Extract the bug number from the URL
|
||||
if bug_id not in sent_bugs:
|
||||
if not has_bug_been_sent(bug_id): # Check if the bug has been sent
|
||||
title = entry.title.strip()
|
||||
link = entry.link
|
||||
# Send the bug title, link, and ID
|
||||
send_to_rocket_chat(title, link, bug_id)
|
||||
sent_bugs.add(bug_id) # Track the sent bug ID
|
||||
mark_bug_as_sent(bug_id) # Mark the bug ID as sent
|
||||
|
||||
# Wait for 1 minute before polling again
|
||||
time.sleep(60)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user