1111from cms .djangoapps .contentstore .git_export_utils import GitExportError , export_to_git
1212from django .conf import settings
1313from opaque_keys .edx .keys import LearningContextKey
14- from opaque_keys .edx .locator import LibraryLocator , LibraryLocatorV2
15- from openedx .core .djangoapps .content_libraries .api import get_library
1614from rest_framework import status
17- from xmodule .modulestore .django import modulestore
1815
1916from ol_openedx_git_auto_export .models import ContentGitRepository
2017from ol_openedx_git_auto_export .utils import (
18+ get_content_info ,
2119 github_repo_name_format ,
2220 is_auto_repo_creation_enabled ,
2321)
@@ -36,65 +34,57 @@ def async_export_to_git(content_key_string, user=None):
3634 # Parse as LearningContextKey to support all learning contexts
3735 try :
3836 content_key = LearningContextKey .from_string (content_key_string )
39- is_v1_library = isinstance (content_key , LibraryLocator )
40- is_v2_library = isinstance (content_key , LibraryLocatorV2 )
37+ content_info = get_content_info (content_key )
4138 except Exception :
4239 LOGGER .exception ("Failed to parse content key: %s" , content_key_string )
4340 return
4441
45- # Get the content module (course or library)
46- if is_v2_library :
47- # V2 libraries use content_libraries API
48- content_module = get_library (content_key )
49- content_type = "library"
50- elif is_v1_library :
51- # V1 libraries use modulestore
52- content_module = modulestore ().get_library (content_key )
53- content_type = "library"
54- else :
55- content_module = modulestore ().get_course (content_key )
56- content_type = "course"
57-
5842 try :
5943 content_repo = ContentGitRepository .objects .get (content_key = content_key )
6044
6145 if content_repo .is_export_enabled :
6246 LOGGER .info (
6347 "Starting async %s content export to git (%s id: %s)" ,
64- content_type ,
65- content_type ,
66- content_module .id if hasattr (content_module , "id" ) else content_key ,
48+ content_info ["content_type" ],
49+ content_info ["content_type" ],
50+ content_info ["content_module" ].id
51+ if hasattr (content_info ["content_module" ], "id" )
52+ else content_key ,
6753 )
6854 # Use unified export_to_git that handles both courses and libraries
6955 export_to_git (content_key , content_repo .git_url , user = user )
7056 else :
7157 LOGGER .info (
7258 "Git export is disabled for %s %s. Skipping export." ,
73- content_type ,
59+ content_info [ " content_type" ] ,
7460 content_key_string ,
7561 )
7662 except GitExportError :
7763 LOGGER .exception (
7864 "Failed async %s content export to git (%s id: %s)" ,
79- content_type ,
80- content_type ,
81- content_module .id if hasattr (content_module , "id" ) else content_key ,
65+ content_info ["content_type" ],
66+ content_info ["content_type" ],
67+ content_info ["content_module" ].id
68+ if hasattr (content_info ["content_module" ], "id" )
69+ else content_key ,
8270 )
8371 except ContentGitRepository .DoesNotExist :
8472 LOGGER .exception (
8573 "Git repository does not exist for %s %s. "
8674 "Creating repository and exporting content." ,
87- content_type ,
75+ content_info [ " content_type" ] ,
8876 content_key_string ,
8977 )
90- if is_auto_repo_creation_enabled (is_library = is_v1_library or is_v2_library ):
78+ if is_auto_repo_creation_enabled (is_library = content_info [ "is_library" ] ):
9179 async_create_github_repo .delay (str (content_key ), export_content = True )
9280 except Exception :
9381 LOGGER .exception (
9482 "Unknown error occurred during async %s content export to git (%s id: %s)" ,
95- content_type ,
96- content_type ,
97- content_module .id if hasattr (content_module , "id" ) else content_key ,
83+ content_info ["content_type" ],
84+ content_info ["content_type" ],
85+ content_info ["content_module" ].id
86+ if hasattr (content_info ["content_module" ], "id" )
87+ else content_key ,
9888 )
9989
10090
@@ -120,40 +110,28 @@ def async_create_github_repo(self, content_key_str, export_content=False): # no
120110 # Parse as LearningContextKey to support all learning contexts
121111 try :
122112 content_key = LearningContextKey .from_string (content_key_str )
123- is_v1_library = isinstance (content_key , LibraryLocator )
124- is_v2_library = isinstance (content_key , LibraryLocatorV2 )
113+ content_info = get_content_info (content_key )
125114 except Exception :
126115 LOGGER .exception ("Failed to parse content key: %s" , content_key_str )
127116 return False , f"Invalid content key: { content_key_str } "
128117
129- content_type = "library" if is_v1_library or is_v2_library else "course"
130118 content_id_slugified = github_repo_name_format (str (content_key ))
131-
132119 response_msg = ""
133120
134121 # Check if repository already exists
135122 if ContentGitRepository .objects .filter (content_key = content_key ).exists ():
136- response_msg = f"GitHub repository already exists for { content_type } { content_key } . Skipping creation." # noqa: E501
123+ response_msg = f"GitHub repository already exists for { content_info [ ' content_type' ] } { content_key } . Skipping creation." # noqa: E501
137124 LOGGER .info (response_msg )
138125 return False , response_msg
139126
140- # Get the content module (course or library)
141- if is_v2_library :
142- # V2 libraries use content_libraries API
143- content_module = get_library (content_key )
144- url_path = f"library/{ content_key_str } "
145- elif is_v1_library :
146- # V1 libraries use modulestore
147- content_module = modulestore ().get_library (content_key )
148- url_path = f"library/{ content_key_str } "
149- else :
150- content_module = modulestore ().get_course (content_key )
151- url_path = f"course/{ content_key_str } "
127+ # Determine URL path based on content type
128+ url_path = f"{ content_info ['content_type' ]} /{ content_key_str } "
152129
153- if is_v2_library :
154- display_name = content_module .title
130+ # Get display name (v2 libraries use 'title', others use 'display_name')
131+ if content_info ["is_v2_library" ]:
132+ display_name = content_info ["content_module" ].title
155133 else :
156- display_name = content_module .display_name
134+ display_name = content_info [ " content_module" ] .display_name
157135
158136 url = f"{ settings .GITHUB_ORG_API_URL } /repos"
159137 # https://docs.github.com/en/rest/authentication/authenticating-to-the-rest-api?apiVersion=2022-11-28
@@ -174,15 +152,15 @@ def async_create_github_repo(self, content_key_str, export_content=False): # no
174152 }
175153 response = requests .post (url , headers = headers , json = payload , timeout = 30 )
176154 if response .status_code != status .HTTP_201_CREATED :
177- response_msg = f"Failed to create GitHub repository for { content_type } { content_key } : { response .json ()} " # noqa: E501
155+ response_msg = f"Failed to create GitHub repository for { content_info [ ' content_type' ] } { content_key } : { response .json ()} " # noqa: E501
178156 LOGGER .error (response_msg )
179157
180158 # Retry the task if we haven't exceeded max retries
181159 max_retries = self .retry_kwargs .get ("max_retries" , 3 )
182160 if self .request .retries < max_retries :
183161 LOGGER .info (
184162 "Retrying GitHub repository creation for %s %s (attempt %d/%d)" ,
185- content_type ,
163+ content_info [ " content_type" ] ,
186164 content_key ,
187165 self .request .retries + 1 ,
188166 max_retries ,
@@ -202,14 +180,14 @@ def async_create_github_repo(self, content_key_str, export_content=False): # no
202180 )
203181 LOGGER .info (
204182 "GitHub repository created for %s %s: %s" ,
205- content_type ,
183+ content_info [ " content_type" ] ,
206184 content_key ,
207185 ssh_url ,
208186 )
209187 else :
210188 response_msg = f"""
211189 Failed to retrieve SSH URL from GitHub response
212- for { content_type } { content_key } .
190+ for { content_info [ " content_type" ] } { content_key } .
213191 Response data: { repo_data }
214192 """
215193 LOGGER .error (response_msg )
0 commit comments