Skip to content

Commit

Permalink
refactor: use huey task for github mirroring
Browse files Browse the repository at this point in the history
  • Loading branch information
sgfost committed Sep 6, 2024
1 parent ccd2058 commit 0e9d82b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
14 changes: 11 additions & 3 deletions django/library/jinja2/library/codebases/releases/retrieve.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,17 @@
</a>
</div>
{% if codebase.git_mirror %}
<a href="{{ codebase.git_mirror.remote_url }}">
<span class='badge bg-info'>view on Github</span>
</a>
{% if codebase.git_mirror.remote_url %}
<a href="{{ codebase.git_mirror.remote_url }}">
<span class="badge bg-info">view on Github</span>
</a>
{% else %}
<div class="alert alert-info">
Mirroring in progress. This may take a few seconds.
<hr>
<a href=".">Refresh</a>
</div>
{% endif %}
{% elif has_change_perm %}
<div
id="github-mirror"
Expand Down
3 changes: 1 addition & 2 deletions django/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@
from core.fields import MarkdownField
from core.models import Platform, MemberProfile, ModeratedContent
from core.queryset import get_viewable_objects_for_user
from core.tasks import send_markdown_email
from core.utils import send_markdown_email
from core.view_helpers import get_search_queryset
from .fs import (
CodebaseGitRepositoryApi,
CodebaseReleaseFsApi,
StagingDirectories,
FileCategoryDirectories,
Expand Down
45 changes: 45 additions & 0 deletions django/library/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from huey.contrib.djhuey import db_task
from django.conf import settings

from .models import Codebase
from .github import GithubApi
from .fs import CodebaseGitRepositoryApi

import logging

logger = logging.getLogger(__name__)


@db_task(retries=3, retry_delay=30)
def mirror_codebase(codebase_id: int, is_user_repo=False, code=None, debug=False):
"""asynchronous task that mirrors a codebase to a remote Github repository"""
codebase = Codebase.objects.get(id=codebase_id)
mirror = codebase.git_mirror
if not mirror:
raise ValueError("Codebase does not have a git mirror")
if is_user_repo:
if not code:
raise ValueError("User repo requires a code")
else:
mirror.organization_login = settings.GITHUB_MODEL_LIBRARY_ORG_NAME

git_fs_api = CodebaseGitRepositoryApi(codebase)
local_repo = git_fs_api.get_or_build()

gh_api = GithubApi(
codebase=codebase,
local_repo=local_repo,
repo_name=mirror.repository_name,
is_user_repo=is_user_repo,
organization_login=mirror.organization_login,
user_access_token=(
GithubApi.get_user_access_token(code) if is_user_repo else None
),
debug=debug,
)
repo = gh_api.get_or_create_repo()
mirror.remote_url = repo.html_url
gh_api.push(local_repo)
gh_api.create_releases(local_repo)

mirror.update_remote_releases()
6 changes: 2 additions & 4 deletions django/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
PeerReviewFeedbackEditorSerializer,
PeerReviewEventLogSerializer,
)
from .github import GithubService
from .tasks import mirror_codebase

import logging
import pathlib
Expand Down Expand Up @@ -451,9 +451,7 @@ def github_mirror(self, request, *args, **kwargs):
CodebaseGitRepositoryApi.check_file_sizes(codebase)
repo_name = request.data.get("repo_name")
codebase.create_git_mirror(repo_name)
# TODO: do this with a celery task
service = GithubService(codebase, debug=True)
service.mirror_org_repo()
mirror_codebase(codebase.id, debug=True)
return Response(data={"job_id": "1234"}, status=status.HTTP_202_ACCEPTED)


Expand Down

0 comments on commit 0e9d82b

Please sign in to comment.