Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
arkid15r committed Mar 3, 2025
1 parent 86eed91 commit 719df12
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 77 deletions.
2 changes: 1 addition & 1 deletion backend/apps/github/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ class UserAdmin(admin.ModelAdmin):
search_fields = ("login", "name")


admin.site.register(PullRequest, PullRequestAdmin)
admin.site.register(Issue, IssueAdmin)
admin.site.register(Label, LabelAdmin)
admin.site.register(Organization, OrganizationAdmin)
admin.site.register(PullRequest, PullRequestAdmin)
admin.site.register(Release, ReleaseAdmin)
admin.site.register(Repository, RepositoryAdmin)
admin.site.register(RepositoryContributor, RepositoryContributorAdmin)
Expand Down
124 changes: 53 additions & 71 deletions backend/apps/github/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""GitHub app common module."""

import logging
from datetime import timedelta as td

from django.utils import timezone
from github.GithubException import UnknownObjectException

from apps.github.models.issue import Issue
Expand Down Expand Up @@ -46,97 +48,77 @@ def sync_repository(gh_repository, organization=None, user=None):
user=user,
)

# GitHub repository issues.
if (
not repository.is_archived
and repository.track_issues
and repository.project
and repository.project.track_issues
):
if not repository.is_archived:
# GitHub repository issues.
project_track_issues = repository.project.track_issues if repository.project else True
if repository.track_issues and project_track_issues:
kwargs = {
"direction": "asc",
"sort": "created",
"state": "all",
}
if latest_updated_issue := repository.latest_updated_issue:
# Get only what has been updated after the latest sync.
kwargs.update({"since": latest_updated_issue.updated_at})

for gh_issue in gh_repository.get_issues(**kwargs):
if gh_issue.pull_request: # Skip pull requests.
continue

author = User.update_data(gh_issue.user)
issue = Issue.update_data(gh_issue, author=author, repository=repository)

# Assignees.
issue.assignees.clear()
for gh_issue_assignee in gh_issue.assignees:
issue.assignees.add(User.update_data(gh_issue_assignee))

# Labels.
issue.labels.clear()
for gh_issue_label in gh_issue.labels:
try:
issue.labels.add(Label.update_data(gh_issue_label))
except UnknownObjectException:
logger.info("Couldn't get GitHub issue label %s", issue.url)
else:
logger.info("Skipping issues sync for %s", repository.name)

# GitHub repository pull requests.
kwargs = {
"direction": "asc",
"sort": "created",
"direction": "desc",
"sort": "updated",
"state": "all",
}
if latest_updated_issue := repository.latest_updated_issue:
# Get only what has been updated after the latest sync.
kwargs.update({"since": latest_updated_issue.updated_at})

for gh_issue in gh_repository.get_issues(**kwargs):
if gh_issue.pull_request: # Skip pull requests.
continue

pull_request_cut_off_at = timezone.now() - td(days=30)
latest_updated_pull_request = repository.latest_updated_pull_request
for gh_pull_request in gh_repository.get_pulls(**kwargs):
author = User.update_data(gh_issue.user)
issue = Issue.update_data(gh_issue, author=author, repository=repository)

# Assignees.
issue.assignees.clear()
for gh_issue_assignee in gh_issue.assignees:
issue.assignees.add(User.update_data(gh_issue_assignee))

# Labels.
issue.labels.clear()
for gh_issue_label in gh_issue.labels:
try:
issue.labels.add(Label.update_data(gh_issue_label))
except UnknownObjectException:
logger.info("Couldn't get GitHub issue label %s", issue.url)
else:
logger.info("Skipping issues sync for %s", repository.name)

if not repository.is_archived and repository.project:
# Fetch both open and closed PRs from GitHub
kwargs = {
"direction": "desc",
"sort": "created",
"state": "open",
}

latest_pull_request = repository.latest_pull_request
if latest_pull_request:
gh_first_pr = PullRequest.objects.order_by("created_at").first().created_at
kwargs["state"] = "all"

gh_pull_requests = gh_repository.get_pulls(**kwargs)

for gh_pull_request in gh_pull_requests:
if latest_pull_request and gh_pull_request.state == "closed":
# Skipping closed PR before first sync
if gh_first_pr > gh_pull_request.created_at:
break
# Check if this PR already exists in the database and is open
existing_open_pr = PullRequest.objects.filter(
repository=repository, state="open", number=gh_pull_request.number
).first()

if not existing_open_pr:
continue # Skip closed PRs from previous syncs

# Extract author details
author = (
User.update_data(gh_pull_request.user)
if gh_pull_request.user and gh_pull_request.user.type != "Bot"
else None
)

# Update PR data
pull_request = PullRequest.update_data(
gh_pull_request, author=author, repository=repository
)

# Clear and update assignees
# Assignees.
pull_request.assignees.clear()
for gh_pull_request_assignee in gh_pull_request.assignees:
pull_request.assignees.add(User.update_data(gh_pull_request_assignee))

# Clear and update labels
# Labels.
pull_request.labels.clear()
for gh_pull_request_label in gh_pull_request.labels:
try:
pull_request.labels.add(Label.update_data(gh_pull_request_label))
except UnknownObjectException:
logger.info("Couldn't get GitHub pull request label %s", pull_request.url)

pull_request_cut_off = pull_request.updated_at <= pull_request_cut_off_at
pull_request_seen = (
latest_updated_pull_request
and pull_request.updated_at <= latest_updated_pull_request.updated_at
)
if pull_request_seen or pull_request_cut_off:
break

# GitHub repository releases.
releases = []
if not is_owasp_site_repository:
Expand Down
5 changes: 5 additions & 0 deletions backend/apps/github/models/generic_issue_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def __str__(self):
"""Issue human readable representation."""
return f"{self.title} by {self.author}"

@property
def is_open(self):
"""Return whether issue is open."""
return self.state == self.State.OPEN

@property
def project(self):
"""Return project."""
Expand Down
9 changes: 5 additions & 4 deletions backend/apps/github/models/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ def generate_summary(self, open_ai=None, max_tokens=500):

def save(self, *args, **kwargs):
"""Save issue."""
if not self.hint:
self.generate_hint()
if self.is_open:
if not self.hint:
self.generate_hint()

if not self.summary:
self.generate_summary()
if not self.summary:
self.generate_summary()

super().save(*args, **kwargs)

Expand Down
5 changes: 5 additions & 0 deletions backend/apps/github/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def latest_updated_issue(self):
"""Repository latest updated issue."""
return self.issues.order_by("-updated_at").first()

@property
def latest_updated_pull_request(self):
"""Repository latest updated pull request."""
return self.pull_requests.order_by("-updated_at").first()

@property
def nest_key(self):
"""Return repository Nest key."""
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/owasp/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def from_github(self, repository):

def save(self, *args, **kwargs):
"""Save project."""
if not self.summary and (prompt := Prompt.get_owasp_project_summary()):
if self.is_active and not self.summary and (prompt := Prompt.get_owasp_project_summary()):
self.generate_summary(prompt=prompt)

super().save(*args, **kwargs)
Expand Down

0 comments on commit 719df12

Please sign in to comment.