Skip to content

Commit a131dc7

Browse files
feat: management command fixed for re-runs
1 parent a15682e commit a131dc7

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/ol_openedx_git_auto_export/management/commands/migrate_giturl.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from django.core.management.base import BaseCommand
22
from ol_openedx_git_auto_export.models import CourseGitRepo
3+
from ol_openedx_git_auto_export.signals import listen_for_course_created
34
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
5+
from openedx_events.content_authoring.data import CourseData
6+
from xmodule.modulestore.django import modulestore
47

58

69
class Command(BaseCommand):
@@ -20,9 +23,31 @@ def handle(self, *args, **options): # noqa: ARG002
2023
if course_ids:
2124
courses = courses.filter(id__in=course_ids)
2225

26+
seen_giturls = set()
2327
for course in courses:
24-
if course.git_url:
28+
course_module = modulestore().get_course(course.id, depth=1)
29+
giturl = course_module.giturl
30+
if giturl and giturl not in seen_giturls:
31+
seen_giturls.add(giturl)
32+
self.stdout.write(
33+
self.style.SUCCESS(f"Course {course.id} has giturl: {giturl}")
34+
)
2535
CourseGitRepo.objects.get_or_create(
2636
course_id=course.id,
27-
defaults={"git_url": course.git_url},
37+
defaults={"git_url": giturl},
38+
)
39+
elif giturl and giturl in seen_giturls:
40+
self.stdout.write(
41+
self.style.WARNING(
42+
f"Course {course.id} has a duplicate giturl: {giturl}"
43+
)
2844
)
45+
ssh_url = listen_for_course_created(CourseData(course_key=course.id))
46+
if ssh_url:
47+
seen_giturls.add(ssh_url)
48+
else:
49+
self.stdout.write(
50+
self.style.WARNING(f"Course {course.id} does not have a giturl.")
51+
)
52+
53+
self.stdout.write(self.style.SUCCESS("Git URLs migrated successfully."))

src/ol_openedx_git_auto_export/signals.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ def listen_for_course_created(**kwargs):
4949
"GitHub repo creation is disabled. Skipping GitHub repo creation for course %s", # noqa: E501
5050
course_id,
5151
)
52-
return
52+
return None
5353

5454
# SignalHandler.course_published is called before COURSE_CREATED signal
5555
if CourseGitRepo.objects.filter(course_id=str(course_id)).exists():
5656
log.info(
5757
"GitHub repository already exists for course %s. Skipping creation.",
5858
course_id,
5959
)
60-
return
60+
return None
6161

6262
gh_access_token = settings.GITHUB_ACCESS_TOKEN
6363
if not settings.GITHUB_ORG_API_URL or not gh_access_token:
6464
log.error(
6565
"GITHUB_ORG_API_URL or GITHUB_ACCESS_TOKEN is not set in settings. Skipping GitHub repo creation." # noqa: E501
6666
)
67-
return
67+
return None
6868

6969
url = f"{settings.GITHUB_ORG_API_URL}/repos"
7070
headers = {
@@ -88,7 +88,7 @@ def listen_for_course_created(**kwargs):
8888
course_id,
8989
response.json(),
9090
)
91-
return
91+
return None
9292

9393
repo_data = response.json()
9494
ssh_url = repo_data.get("ssh_url")
@@ -107,4 +107,5 @@ def listen_for_course_created(**kwargs):
107107
"Failed to retrieve URL for GitHub repository for course %s",
108108
course_id,
109109
)
110-
return
110+
111+
return ssh_url

0 commit comments

Comments
 (0)