-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
307 additions
and
31 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
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
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
51 changes: 51 additions & 0 deletions
51
backend/apps/github/migrations/0007_repositorycontributor.py
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,51 @@ | ||
# Generated by Django 5.1.1 on 2024-09-21 19:07 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("github", "0006_alter_issue_state_reason"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="RepositoryContributor", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
("node_id", models.CharField(unique=True, verbose_name="Node ID")), | ||
( | ||
"contributions_count", | ||
models.PositiveIntegerField(default=0, verbose_name="Contributions"), | ||
), | ||
( | ||
"repository", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="github.repository", | ||
verbose_name="Repository", | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="github.user", | ||
verbose_name="User", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name_plural": "Contributors", | ||
"db_table": "github_repository_contributors", | ||
}, | ||
), | ||
] |
16 changes: 16 additions & 0 deletions
16
backend/apps/github/migrations/0008_alter_repositorycontributor_options.py
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,16 @@ | ||
# Generated by Django 5.1.1 on 2024-09-21 19:09 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("github", "0007_repositorycontributor"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name="repositorycontributor", | ||
options={"verbose_name_plural": "Repository contributors"}, | ||
), | ||
] |
16 changes: 16 additions & 0 deletions
16
backend/apps/github/migrations/0009_remove_repositorycontributor_node_id.py
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,16 @@ | ||
# Generated by Django 5.1.1 on 2024-09-21 19:13 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("github", "0008_alter_repositorycontributor_options"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="repositorycontributor", | ||
name="node_id", | ||
), | ||
] |
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
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,74 @@ | ||
"""Github app label model.""" | ||
|
||
from django.db import models | ||
from django.template.defaultfilters import pluralize | ||
|
||
from apps.common.models import BulkSaveModel, TimestampedModel | ||
|
||
TOP_CONTRIBUTORS_LIMIT = 20 | ||
|
||
|
||
class RepositoryContributor(BulkSaveModel, TimestampedModel): | ||
"""Repository contributor model.""" | ||
|
||
class Meta: | ||
db_table = "github_repository_contributors" | ||
verbose_name_plural = "Repository contributors" | ||
|
||
contributions_count = models.PositiveIntegerField(verbose_name="Contributions", default=0) | ||
|
||
# FKs. | ||
repository = models.ForeignKey( | ||
"github.Repository", | ||
verbose_name="Repository", | ||
on_delete=models.CASCADE, | ||
) | ||
user = models.ForeignKey( | ||
"github.User", | ||
verbose_name="User", | ||
on_delete=models.CASCADE, | ||
) | ||
|
||
def __str__(self): | ||
"""Repository contributor human readable representation.""" | ||
return ( | ||
f"{self.user} has made {self.contributions_count} " | ||
f"contribution{pluralize(self.contributions_count)} to {self.repository}" | ||
) | ||
|
||
def from_github(self, gh_label): | ||
"""Update instance based on GitHub contributor data.""" | ||
field_mapping = { | ||
"contributions_count": "contributions", | ||
} | ||
|
||
# Direct fields. | ||
for model_field, gh_field in field_mapping.items(): | ||
value = getattr(gh_label, gh_field) | ||
if value is not None: | ||
setattr(self, model_field, value) | ||
|
||
@staticmethod | ||
def bulk_save(repository_contributors): | ||
"""Bulk save repository contributors.""" | ||
BulkSaveModel.bulk_save(RepositoryContributor, repository_contributors) | ||
|
||
@staticmethod | ||
def update_data(gh_contributor, repository, user, save=True): | ||
"""Update repository contributor data.""" | ||
try: | ||
repository_contributor = RepositoryContributor.objects.get( | ||
repository=repository, | ||
user=user, | ||
) | ||
except RepositoryContributor.DoesNotExist: | ||
repository_contributor = RepositoryContributor( | ||
repository=repository, | ||
user=user, | ||
) | ||
repository_contributor.from_github(gh_contributor) | ||
|
||
if save: | ||
repository_contributor.save() | ||
|
||
return repository_contributor |
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
Oops, something went wrong.