Skip to content

Commit

Permalink
Added github token header
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilDhillon21 committed Nov 13, 2024
1 parent f8ed8b0 commit 34add4f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
42 changes: 42 additions & 0 deletions website/management/commands/update_projects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import requests
from django.conf import settings
from django.core.management.base import BaseCommand
Expand Down Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]
4 changes: 4 additions & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34add4f

Please sign in to comment.