-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'comments' of https://github.com/krrish-sehgal/Owasp-BLT …
…into comments
- Loading branch information
Showing
9 changed files
with
432 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import logging | ||
import time | ||
|
||
import requests | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
from website.models import Contributor, Repo | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Synchronize all contributors for a repository" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("--repo_id", type=int, help="Repository ID to sync") | ||
|
||
def handle(self, *args, **options): | ||
repo_id = options.get("repo_id") | ||
if not repo_id: | ||
return | ||
|
||
repo = Repo.objects.get(id=repo_id) | ||
owner_repo = repo.repo_url.rstrip("/").split("github.com/")[-1] | ||
|
||
headers = { | ||
"Authorization": f"token {settings.GITHUB_TOKEN}", | ||
"Accept": "application/vnd.github.v3+json", | ||
} | ||
|
||
# Get all contributors with pagination | ||
page = 1 | ||
all_contributors = [] | ||
|
||
while True: | ||
api_url = f"https://api.github.com/repos/{owner_repo}/contributors?anon=true&per_page=100&page={page}" | ||
response = requests.get(api_url, headers=headers) | ||
|
||
if response.status_code == 403: | ||
reset_time = int(response.headers.get("X-RateLimit-Reset", 0)) | ||
wait_time = reset_time - int(time.time()) | ||
if wait_time > 0: | ||
logger.info(f"Rate limit hit, waiting {wait_time} seconds") | ||
time.sleep(wait_time) | ||
continue | ||
|
||
if response.status_code != 200: | ||
break | ||
|
||
contributors_page = response.json() | ||
if not contributors_page: | ||
break | ||
|
||
all_contributors.extend(contributors_page) | ||
page += 1 | ||
|
||
# Be nice to GitHub API | ||
time.sleep(1) | ||
|
||
# Batch create/update contributors | ||
for contrib_data in all_contributors: | ||
github_id = contrib_data.get("id") | ||
if not github_id: | ||
# skip if 'id' is missing | ||
continue | ||
contributor, created = Contributor.objects.update_or_create( | ||
github_id=github_id, | ||
defaults={ | ||
"name": contrib_data.get("login", "unknown"), | ||
"github_url": contrib_data.get("html_url", ""), | ||
"avatar_url": contrib_data.get("avatar_url", ""), | ||
"contributions": contrib_data.get("contributions", 0), | ||
"contributor_type": contrib_data.get("type", "User"), | ||
}, | ||
) | ||
repo.contributor.add(contributor) | ||
|
||
repo.contributor_count = len(all_contributors) | ||
repo.save() | ||
|
||
logger.info(f"Synced {len(all_contributors)} contributors for {repo.name}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Generated by Django 5.1.3 on 2025-01-06 13:17 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("website", "0178_alter_ip_agent"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="ContributorStats", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("date", models.DateField()), | ||
("commits", models.PositiveIntegerField(default=0)), | ||
("issues_opened", models.PositiveIntegerField(default=0)), | ||
("issues_closed", models.PositiveIntegerField(default=0)), | ||
("pull_requests", models.PositiveIntegerField(default=0)), | ||
("comments", models.PositiveIntegerField(default=0)), | ||
( | ||
"granularity", | ||
models.CharField( | ||
choices=[("day", "Day"), ("month", "Month")], | ||
default="day", | ||
max_length=10, | ||
), | ||
), | ||
( | ||
"contributor", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="stats", | ||
to="website.contributor", | ||
), | ||
), | ||
( | ||
"repo", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="stats", | ||
to="website.repo", | ||
), | ||
), | ||
], | ||
options={ | ||
"unique_together": {("contributor", "repo", "date", "granularity")}, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.