1818from openedx .core .djangoapps .content_libraries .api import extract_library_v2_zip_to_dir
1919from xmodule .contentstore .django import contentstore
2020from xmodule .modulestore .django import modulestore
21- from xmodule .modulestore .xml_exporter import export_course_to_xml , export_library_to_xml
21+ from xmodule .modulestore .xml_exporter import CourseLocator , export_course_to_xml , export_library_to_xml
2222
2323log = logging .getLogger (__name__ )
2424
@@ -68,12 +68,12 @@ def cmd_log(cmd, cwd):
6868 return output
6969
7070
71- def export_to_git (content_key , repo , user = '' , rdir = None ):
71+ def export_to_git (context_key , repo , user = '' , rdir = None ):
7272 """
7373 Export a course or library to git.
7474
7575 Args:
76- content_key: CourseKey or LibraryLocator for the content to export
76+ context_key: LearningContextKey for the content to export
7777 repo (str): Git repository URL
7878 user (str): Optional username for git commit identity
7979 rdir (str): Optional custom directory name for the repository
@@ -83,17 +83,12 @@ def export_to_git(content_key, repo, user='', rdir=None):
8383 """
8484 # pylint: disable=too-many-statements
8585
86- # Detect content type and select appropriate export function
87- content_type_label = "library"
88- is_library_v2 = isinstance (content_key , LibraryLocatorV2 )
89- if is_library_v2 :
90- # V2 libraries use backup API with zip extraction
91- content_export_func = extract_library_v2_zip_to_dir
92- elif isinstance (content_key , LibraryLocator ):
93- content_export_func = export_library_to_xml
94- else :
95- content_export_func = export_course_to_xml
96- content_type_label = "course"
86+ # Validate context_key type and determine export function and content type label
87+ if not isinstance (context_key , (LibraryLocatorV2 , LibraryLocator , CourseLocator )):
88+ raise TypeError (
89+ f"{ context_key !r} for git export must be LibraryLocatorV2, LibraryLocator, "
90+ f"or CourseLocator, not { type (context_key )} "
91+ )
9792
9893 if not GIT_REPO_EXPORT_DIR :
9994 raise GitExportError (GitExportError .NO_EXPORT_DIR )
@@ -157,12 +152,23 @@ def export_to_git(content_key, repo, user='', rdir=None):
157152 root_dir = os .path .dirname (rdirp )
158153 content_dir = os .path .basename (rdirp ).rsplit ('.git' , 1 )[0 ]
159154
155+ content_type_label = "course" if context_key .is_course else "library"
156+
157+ is_library_v2 = isinstance (context_key , LibraryLocatorV2 )
158+ if is_library_v2 :
159+ # V2 libraries use backup API with zip extraction
160+ content_export_func = extract_library_v2_zip_to_dir
161+ elif isinstance (context_key , LibraryLocator ):
162+ content_export_func = export_library_to_xml
163+ else :
164+ content_export_func = export_course_to_xml
165+
160166 try :
161167 if is_library_v2 :
162- content_export_func (content_key , root_dir , content_dir , user )
168+ content_export_func (context_key , root_dir , content_dir , user )
163169 else :
164170 # V1 libraries and courses: use XML export (no user parameter)
165- content_export_func (modulestore (), contentstore (), content_key ,
171+ content_export_func (modulestore (), contentstore (), context_key ,
166172 root_dir , content_dir )
167173 except (OSError , AttributeError ):
168174 log .exception ('Failed to export %s' , content_type_label )
@@ -212,6 +218,6 @@ def export_to_git(content_key, repo, user='', rdir=None):
212218 log .info (
213219 '%s %s exported to git repository %s successfully' ,
214220 content_type_label .capitalize (),
215- content_key ,
221+ context_key ,
216222 repo ,
217223 )
0 commit comments