Skip to content

Commit

Permalink
Extend issue/project/release/repository/user index (OWASP#438)
Browse files Browse the repository at this point in the history
* Extend issue/project/release/repository/user index

* Address comments

---------

Co-authored-by: Kate Golovanova <[email protected]>
  • Loading branch information
arkid15r and kasya authored Jan 13, 2025
1 parent bcfdddc commit ffbd06a
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ def handle(self, *args, **kwargs):
"facets": [
{"attribute": "idx_key"},
{"attribute": "idx_name"},
{"attribute": "idx_repository_names"},
{"attribute": "idx_repositories.name"},
{"attribute": "idx_tags"},
],
"generate": [
["idx_name"],
["idx_tags"],
["idx_repository_names"],
["idx_repositories.name"],
],
},
"users": {
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/github/index/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RepositoryIndex(AlgoliaIndex, IndexBase):
"idx_license",
"idx_name",
"idx_open_issues_count",
"idx_project",
"idx_project_key",
"idx_pushed_at",
"idx_size",
"idx_stars_count",
Expand Down
5 changes: 5 additions & 0 deletions backend/apps/github/index/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ class UserIndex(AlgoliaIndex, IndexBase):
"idx_avatar_url",
"idx_bio",
"idx_company",
"idx_contributions",
"idx_created_at",
"idx_email",
"idx_followers_count",
"idx_following_count",
"idx_issues_count",
"idx_issues",
"idx_key",
"idx_location",
"idx_login",
"idx_name",
"idx_public_repositories_count",
"idx_releases_count",
"idx_releases",
"idx_title",
"idx_updated_at",
"idx_url",
Expand Down
24 changes: 24 additions & 0 deletions backend/apps/github/migrations/0015_alter_release_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.4 on 2025-01-13 01:36

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("github", "0014_repository_track_issues"),
]

operations = [
migrations.AlterField(
model_name="release",
name="author",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="created_releases",
to="github.user",
),
),
]
8 changes: 4 additions & 4 deletions backend/apps/github/models/mixins/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def idx_has_funding_yml(self):
@property
def idx_key(self):
"""Return key for indexing."""
return self.path.lower()
return self.nest_key

@property
def idx_languages(self):
Expand All @@ -66,9 +66,9 @@ def idx_open_issues_count(self):
return self.open_issues_count

@property
def idx_project(self):
"""Return project for indexing."""
return self.project.nest_key if self.project else None
def idx_project_key(self):
"""Return project key for indexing."""
return self.project.nest_key if self.project else ""

@property
def idx_pushed_at(self):
Expand Down
79 changes: 79 additions & 0 deletions backend/apps/github/models/mixins/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
"""GitHub user model mixins for index-related functionality."""

from apps.github.models.repository_contributor import RepositoryContributor

ISSUES_LIMIT = 6
RELEASES_LIMIT = 6
TOP_REPOSITORY_CONTRIBUTORS_LIMIT = 6


class UserIndexMixin:
"""User index mixin."""
Expand Down Expand Up @@ -69,6 +75,79 @@ def idx_title(self):
"""Return title for indexing."""
return self.title

@property
def idx_contributions(self):
"""Return contributions for indexing."""
return [
{
"contributions_count": rc.contributions_count,
"repository_contributors_count": rc.repository.contributors_count,
"repository_description": rc.repository.description,
"repository_forks_count": rc.repository.forks_count,
"repository_key": rc.repository.key.lower(),
"repository_name": rc.repository.name,
"repository_latest_release": str(rc.repository.latest_release.summary)
if rc.repository.latest_release
else "",
"repository_license": rc.repository.license,
"repository_owner_key": rc.repository.owner.login.lower(),
"repository_stars_count": rc.repository.stars_count,
}
for rc in RepositoryContributor.objects.filter(user=self)
.order_by("-contributions_count")
.select_related("repository")[:TOP_REPOSITORY_CONTRIBUTORS_LIMIT]
]

@property
def idx_issues(self):
"""Return issues for indexing."""
return [
{
"created_at": i.created_at.timestamp(),
"comments_count": i.comments_count,
"number": i.number,
"repository": {
"key": i.repository.key,
"owner_key": i.repository.owner.login,
},
"title": i.title,
}
for i in self.issues.select_related(
"repository",
"repository__owner",
).order_by("-created_at")[:ISSUES_LIMIT]
]

@property
def idx_issues_count(self):
"""Return issues count for indexing."""
return self.issues.count()

@property
def idx_releases(self):
"""Return releases for indexing."""
return [
{
"is_pre_release": r.is_pre_release,
"name": r.name,
"published_at": r.published_at.timestamp(),
"repository": {
"key": r.repository.key,
"owner_key": r.repository.owner.login,
},
"tag_name": r.tag_name,
}
for r in self.releases.select_related(
"repository",
"repository__owner",
).order_by("-published_at")[:RELEASES_LIMIT]
]

@property
def idx_releases_count(self):
"""Return releases count for indexing."""
return self.releases.count()

@property
def idx_updated_at(self):
"""Return updated at timestamp for indexing."""
Expand Down
13 changes: 12 additions & 1 deletion backend/apps/github/models/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ class Meta:
published_at = models.DateTimeField(verbose_name="Published at")

# FKs.
author = models.ForeignKey("github.User", on_delete=models.SET_NULL, blank=True, null=True)
author = models.ForeignKey(
"github.User",
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name="created_releases",
)
repository = models.ForeignKey(
"github.Repository",
on_delete=models.SET_NULL,
Expand All @@ -39,6 +45,11 @@ def __str__(self):
"""User human readable representation."""
return f"{self.name} by {self.author}"

@property
def summary(self):
"""Return release summary."""
return f"{self.tag_name} on {self.published_at.strftime('%b %d, %Y')}"

@property
def is_indexable(self):
"""Releases to index."""
Expand Down
12 changes: 11 additions & 1 deletion backend/apps/github/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ def is_indexable(self):
@property
def latest_release(self):
"""Repository latest release."""
return self.releases.order_by("-created_at").first()
return self.published_releases.order_by("-published_at").first()

@property
def nest_key(self):
"""Return repository Nest key."""
return f"{self.owner.login}-{self.name}"

@property
def path(self):
Expand All @@ -126,6 +131,11 @@ def project(self):
"""Return project."""
return self.project_set.first()

@property
def published_releases(self):
"""Return published releases."""
return self.releases.filter(is_draft=False, published_at__isnull=False)

@property
def top_languages(self):
"""Return a list of top used languages."""
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/github/models/repository_contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from apps.common.models import BulkSaveModel, TimestampedModel

TOP_CONTRIBUTORS_LIMIT = 25
TOP_CONTRIBUTORS_LIMIT = 15


class RepositoryContributor(BulkSaveModel, TimestampedModel):
Expand Down
15 changes: 13 additions & 2 deletions backend/apps/github/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@ def __str__(self):
def is_indexable(self):
"""Users to index."""
return (
self.login != "ghost" and self.login not in Organization.get_logins()
) # See https://github.com/ghost for more info.
self.login != "ghost" # See https://github.com/ghost for more info.
and self.login not in Organization.get_logins()
)

@property
def issues(self):
"""Return user issues."""
return self.created_issues.all()

@property
def releases(self):
"""Return user releases."""
return self.created_releases.all()

def from_github(self, gh_user):
"""Update instance based on GitHub user data."""
Expand Down
12 changes: 8 additions & 4 deletions backend/apps/owasp/index/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ class ProjectIndex(AlgoliaIndex, IndexBase):
"idx_custom_tags",
"idx_description",
"idx_forks_count",
"idx_issues",
"idx_issues_count",
"idx_key",
"idx_languages",
"idx_leaders",
"idx_level_raw",
"idx_level",
"idx_name",
"idx_organizations",
"idx_repository_descriptions",
"idx_repository_names",
"idx_releases",
"idx_releases_count",
"idx_repositories",
"idx_repositories_count",
"idx_stars_count",
"idx_summary",
"idx_tags",
Expand All @@ -43,7 +47,7 @@ class ProjectIndex(AlgoliaIndex, IndexBase):
"filterOnly(idx_key)",
"idx_name",
"idx_tags",
"idx_repository_names",
"idx_repositories.name",
],
"indexLanguages": ["en"],
"customRanking": [
Expand All @@ -64,7 +68,7 @@ class ProjectIndex(AlgoliaIndex, IndexBase):
],
"searchableAttributes": [
"unordered(idx_name)",
"unordered(idx_repository_descriptions, idx_repository_names)",
"unordered(idx_repositories.description, idx_repositories.name)",
"unordered(idx_custom_tags, idx_languages, idx_tags, idx_topics)",
"unordered(idx_description)",
"unordered(idx_companies, idx_organizations)",
Expand Down
Loading

0 comments on commit ffbd06a

Please sign in to comment.