135 lines
4.9 KiB
Python
135 lines
4.9 KiB
Python
"""Database module for tracking previously processed issues and pull requests.
|
|
|
|
This module provides functionality to track which issues have already been processed
|
|
by the system to avoid duplicate processing. It uses a simple SQLite database to
|
|
store information about seen issues and their associated pull requests for efficient lookup.
|
|
"""
|
|
|
|
import sqlite3
|
|
from hashlib import sha256
|
|
from typing import Optional, Tuple
|
|
|
|
DEFAULT_DB_PATH = 'output/seen_issues.db'
|
|
|
|
|
|
class SeenIssuesDB:
|
|
"""Database handler for tracking processed issues and pull requests.
|
|
|
|
This class manages a SQLite database that stores information about issues that have
|
|
already been processed and their associated pull requests. It provides methods to mark
|
|
issues as seen, check if an issue has been seen before, and retrieve pull request
|
|
information for an issue.
|
|
|
|
Attributes:
|
|
conn: SQLite database connection
|
|
"""
|
|
|
|
def __init__(self, db_path=DEFAULT_DB_PATH):
|
|
"""Initialize the database connection.
|
|
|
|
Args:
|
|
db_path: Path to the SQLite database file. Defaults to 'output/seen_issues.db'.
|
|
"""
|
|
self.conn = sqlite3.connect(db_path)
|
|
self._create_table()
|
|
|
|
def _create_table(self):
|
|
"""Create the seen_issues table if it doesn't exist.
|
|
|
|
Creates a table with columns for storing issue hashes and associated pull request information.
|
|
"""
|
|
with self.conn:
|
|
self.conn.execute("""
|
|
CREATE TABLE IF NOT EXISTS seen_issues (
|
|
issue_hash TEXT PRIMARY KEY,
|
|
issue_number TEXT,
|
|
pr_number TEXT,
|
|
pr_url TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
""")
|
|
|
|
def mark_as_seen(self, issue_text: str, issue_number: str = None, pr_number: str = None, pr_url: str = None):
|
|
"""Mark an issue as seen in the database.
|
|
|
|
Computes a hash of the issue text and stores it in the database along with pull request information.
|
|
If the issue has already been marked as seen, this operation has no effect.
|
|
|
|
Args:
|
|
issue_text: The text content of the issue to mark as seen.
|
|
issue_number: The issue number.
|
|
pr_number: The pull request number associated with this issue.
|
|
pr_url: The URL of the pull request associated with this issue.
|
|
"""
|
|
issue_hash = self._compute_hash(issue_text)
|
|
with self.conn:
|
|
self.conn.execute(
|
|
'INSERT OR IGNORE INTO seen_issues (issue_hash, issue_number, pr_number, pr_url) VALUES (?, ?, ?, ?)',
|
|
(issue_hash, issue_number, pr_number, pr_url),
|
|
)
|
|
|
|
def has_seen(self, issue_text: str) -> bool:
|
|
"""Check if an issue has been seen before.
|
|
|
|
Computes a hash of the issue text and checks if it exists in the database.
|
|
|
|
Args:
|
|
issue_text: The text content of the issue to check.
|
|
|
|
Returns:
|
|
True if the issue has been seen before, False otherwise.
|
|
"""
|
|
issue_hash = self._compute_hash(issue_text)
|
|
cursor = self.conn.execute(
|
|
'SELECT 1 FROM seen_issues WHERE issue_hash = ?',
|
|
(issue_hash,),
|
|
)
|
|
return cursor.fetchone() is not None
|
|
|
|
def get_pr_info(self, issue_text: str) -> Optional[Tuple[str, str]]:
|
|
"""Get pull request information for an issue.
|
|
|
|
Args:
|
|
issue_text: The text content of the issue to check.
|
|
|
|
Returns:
|
|
A tuple containing (pr_number, pr_url) if found, None otherwise.
|
|
"""
|
|
issue_hash = self._compute_hash(issue_text)
|
|
cursor = self.conn.execute(
|
|
'SELECT pr_number, pr_url FROM seen_issues WHERE issue_hash = ?',
|
|
(issue_hash,),
|
|
)
|
|
result = cursor.fetchone()
|
|
return result if result else None
|
|
|
|
def update_pr_info(self, issue_text: str, pr_number: str, pr_url: str) -> bool:
|
|
"""Update pull request information for an existing issue.
|
|
|
|
Args:
|
|
issue_text: The text content of the issue to update.
|
|
pr_number: The pull request number.
|
|
pr_url: The URL of the pull request.
|
|
|
|
Returns:
|
|
True if the update was successful, False if the issue wasn't found.
|
|
"""
|
|
issue_hash = self._compute_hash(issue_text)
|
|
with self.conn:
|
|
cursor = self.conn.execute(
|
|
'UPDATE seen_issues SET pr_number = ?, pr_url = ? WHERE issue_hash = ?',
|
|
(pr_number, pr_url, issue_hash),
|
|
)
|
|
return cursor.rowcount > 0
|
|
|
|
def _compute_hash(self, text: str) -> str:
|
|
"""Compute a SHA-256 hash of the given text.
|
|
|
|
Args:
|
|
text: The text to hash.
|
|
|
|
Returns:
|
|
A hexadecimal string representation of the hash.
|
|
"""
|
|
return sha256(text.encode('utf-8')).hexdigest()
|