Skip to content

Commit

Permalink
Add interactive command-line interface and enhance subs mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
DjangoPeng committed Jul 1, 2024
1 parent ce733aa commit 8da7236
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 8 deletions.
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"github_token": "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",
"notification_settings": {
"email": "[email protected]",
"slack_webhook_url": "your_slack_webhook_url"
},
"subscriptions_file": "subscriptions.json",
"update_interval": 86400
}
2 changes: 1 addition & 1 deletion src/github_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def fetch_updates(self, subscriptions):
}
updates = {}
for repo in subscriptions:
response = requests.get(f'https://api.github.com/repos/{repo}/events', headers=headers)
response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest', headers=headers)
if response.status_code == 200:
updates[repo] = response.json()
return updates
83 changes: 82 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
import argparse
from config import Config
from scheduler import Scheduler
from github_client import GitHubClient
from notifier import Notifier
from report_generator import ReportGenerator
from subscription_manager import SubscriptionManager
import threading
import shlex

def run_scheduler(scheduler):
scheduler.start()

def add_subscription(args, subscription_manager):
subscription_manager.add_subscription(args.repo)
print(f"Added subscription: {args.repo}")

def remove_subscription(args, subscription_manager):
subscription_manager.remove_subscription(args.repo)
print(f"Removed subscription: {args.repo}")

def list_subscriptions(subscription_manager):
subscriptions = subscription_manager.get_subscriptions()
print("Current subscriptions:")
for sub in subscriptions:
print(f"- {sub}")

def fetch_updates(github_client, subscription_manager, report_generator):
subscriptions = subscription_manager.get_subscriptions()
updates = github_client.fetch_updates(subscriptions)
report = report_generator.generate(updates)
print("Updates fetched:")
print(report)

def print_help():
help_text = """
GitHub Sentinel Command Line Interface
Available commands:
add <repo> Add a subscription (e.g., owner/repo)
remove <repo> Remove a subscription (e.g., owner/repo)
list List all subscriptions
fetch Fetch updates immediately
help Show this help message
exit Exit the tool
quit Exit the tool
"""
print(help_text)

def main():
config = Config()
Expand All @@ -20,7 +62,46 @@ def main():
interval=config.update_interval
)

scheduler.start()
scheduler_thread = threading.Thread(target=run_scheduler, args=(scheduler,))
scheduler_thread.daemon = True
scheduler_thread.start()

parser = argparse.ArgumentParser(description='GitHub Sentinel Command Line Interface')
subparsers = parser.add_subparsers(title='Commands', dest='command')

parser_add = subparsers.add_parser('add', help='Add a subscription')
parser_add.add_argument('repo', type=str, help='The repository to subscribe to (e.g., owner/repo)')
parser_add.set_defaults(func=lambda args: add_subscription(args, subscription_manager))

parser_remove = subparsers.add_parser('remove', help='Remove a subscription')
parser_remove.add_argument('repo', type=str, help='The repository to unsubscribe from (e.g., owner/repo)')
parser_remove.set_defaults(func=lambda args: remove_subscription(args, subscription_manager))

parser_list = subparsers.add_parser('list', help='List all subscriptions')
parser_list.set_defaults(func=lambda args: list_subscriptions(subscription_manager))

parser_fetch = subparsers.add_parser('fetch', help='Fetch updates immediately')
parser_fetch.set_defaults(func=lambda args: fetch_updates(github_client, subscription_manager, report_generator))

parser_help = subparsers.add_parser('help', help='Show this help message')
parser_help.set_defaults(func=lambda args: print_help())

# Print help on startup
print_help()

while True:
try:
user_input = input("GitHub Sentinel> ")
if user_input in ["exit", "quit"]:
print("Exiting GitHub Sentinel...")
break
args = parser.parse_args(shlex.split(user_input))
if args.command is not None:
args.func(args)
else:
parser.print_help()
except Exception as e:
print(f"Error: {e}")

if __name__ == "__main__":
main()
12 changes: 7 additions & 5 deletions src/report_generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class ReportGenerator:
def generate(self, updates):
# Implement report generation logic
report = ""
for repo, events in updates.items():
report = "Latest Release Information:\n\n"
for repo, release in updates.items():
report += f"Repository: {repo}\n"
for event in events:
report += f"- {event['type']} at {event['created_at']}\n"
report += f"Latest Version: {release['tag_name']}\n"
report += f"Release Name: {release['name']}\n"
report += f"Published at: {release['published_at']}\n"
report += f"Release Notes:\n{release['body']}\n"
report += "-" * 40 + "\n"
return report
20 changes: 19 additions & 1 deletion src/subscription_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@
class SubscriptionManager:
def __init__(self, subscriptions_file):
self.subscriptions_file = subscriptions_file
self.subscriptions = self.load_subscriptions()

def get_subscriptions(self):
def load_subscriptions(self):
with open(self.subscriptions_file, 'r') as f:
return json.load(f)

def save_subscriptions(self):
with open(self.subscriptions_file, 'w') as f:
json.dump(self.subscriptions, f, indent=4)

def get_subscriptions(self):
return self.subscriptions

def add_subscription(self, repo):
if repo not in self.subscriptions:
self.subscriptions.append(repo)
self.save_subscriptions()

def remove_subscription(self, repo):
if repo in self.subscriptions:
self.subscriptions.remove(repo)
self.save_subscriptions()
3 changes: 3 additions & 0 deletions subscriptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"langchain-ai/langchain"
]

0 comments on commit 8da7236

Please sign in to comment.