diff --git a/website/management/commands/update_projects.py b/website/management/commands/update_projects.py index 52503c564..f5561ce28 100644 --- a/website/management/commands/update_projects.py +++ b/website/management/commands/update_projects.py @@ -1,3 +1,4 @@ +import base64 import requests from django.conf import settings from django.core.management.base import BaseCommand @@ -51,6 +52,47 @@ def handle(self, *args, **kwargs): project.size = repo_data.get("size", 0) project.last_commit_date = parse_datetime(repo_data.get("pushed_at")) + # Fetch README + url = f"https://api.github.com/repos/{repo_name}/readme" + response = requests.get(url, headers=headers) + if response.status_code == 200: + readme_data = response.json() + readme_content_encoded = readme_data.get("content", "") + + # Decode the Base64 content + try: + readme_content = base64.b64decode(readme_content_encoded).decode("utf-8") + project.readme_content = readme_content + except (base64.binascii.Error, UnicodeDecodeError) as e: + self.stdout.write(self.style.WARNING(f"Failed to decode README for {repo_name}: {e}")) + project.readme_content = "" + else: + self.stdout.write( + self.style.WARNING( + f"Failed to fetch README for {repo_name}: {response.status_code}" + ) + ) + + # Check for Documentation URL (homepage) + project.documentation_url = repo_data.get("homepage") + + # Fetch Recent Commit Messages + url = f"https://api.github.com/repos/{repo_name}/commits" + response = requests.get(url, headers=headers) + if response.status_code == 200: + commits_data = response.json() + commit_messages = [commit["commit"]["message"] for commit in commits_data[:5]] + project.recent_commit_messages = "\n".join(commit_messages) + else: + self.stdout.write( + self.style.WARNING( + f"Failed to fetch recent commits for {repo_name}: {response.status_code}" + ) + ) + + # Set Issue Tracker URL + project.issue_tracker_url = f"https://github.com/{repo_name}/issues" + # Fetch counts of issues and pull requests using the Search API def get_issue_count(repo_name, query, headers): url = f"https://api.github.com/search/issues?q=repo:{repo_name}+{query}" diff --git a/website/migrations/0154_project_documentation_url_project_issue_tracker_url_and_more.py b/website/migrations/0154_project_documentation_url_project_issue_tracker_url_and_more.py new file mode 100644 index 000000000..0b91a7c31 --- /dev/null +++ b/website/migrations/0154_project_documentation_url_project_issue_tracker_url_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 5.1.3 on 2024-11-13 17:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("website", "0153_delete_contributorstats"), + ] + + operations = [ + migrations.AddField( + model_name="project", + name="documentation_url", + field=models.URLField(blank=True, null=True), + ), + migrations.AddField( + model_name="project", + name="issue_tracker_url", + field=models.URLField(blank=True, null=True), + ), + migrations.AddField( + model_name="project", + name="readme_content", + field=models.TextField(blank=True, null=True), + ), + migrations.AddField( + model_name="project", + name="recent_commit_messages", + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/website/models.py b/website/models.py index 3398c8e1c..8219d7130 100644 --- a/website/models.py +++ b/website/models.py @@ -754,6 +754,10 @@ class Project(models.Model): closed_issues = models.IntegerField(default=0) size = models.IntegerField(default=0) commit_count = models.IntegerField(default=0) + readme_content = models.TextField(null=True, blank=True) + documentation_url = models.URLField(null=True, blank=True) + recent_commit_messages = models.TextField(null=True, blank=True) + issue_tracker_url = models.URLField(null=True, blank=True) def __str__(self): return self.name