|
1 | 1 | import getpass |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import tempfile |
2 | 5 | from pathlib import Path |
3 | 6 | from typing import Optional, List, Any |
4 | 7 |
|
@@ -170,7 +173,14 @@ def _select_azure_template(self, space: str, **kwargs) -> Optional[dict]: |
170 | 173 | """Select template from Azure Repos""" |
171 | 174 | try: |
172 | 175 | from ....utils.crypto import get_credentials_with_password, save_credentials |
173 | | - from ....core.integrations.azure_devops.get_azure_devops import get_pattern_from_azure |
| 176 | + from ....core.integrations.azure_devops.get_azure_devops import ( |
| 177 | + create_connection, |
| 178 | + get_repos_patterns, |
| 179 | + allowed_pattern_prefixes, |
| 180 | + allowed_pattern_suffixes |
| 181 | + ) |
| 182 | + import inquirer |
| 183 | + from colorama import Fore |
174 | 184 |
|
175 | 185 | pat = None |
176 | 186 | org_name = None |
@@ -225,16 +235,66 @@ def _select_azure_template(self, space: str, **kwargs) -> Optional[dict]: |
225 | 235 |
|
226 | 236 | org_url = f"https://dev.azure.com/{org_name}/" |
227 | 237 |
|
228 | | - # Try to list templates |
| 238 | + # Get connection and list projects |
229 | 239 | self.ui.print_info("🔍 Fetching available templates...") |
230 | | - template_info = get_pattern_from_azure( |
231 | | - pat=pat, |
232 | | - org_url=org_url, |
233 | | - directory="temp", # Dummy directory for listing |
234 | | - action="list", |
| 240 | + conn = create_connection(personal_access_token=pat, organization_url=org_url) |
| 241 | + core_client = conn.clients.get_core_client() |
| 242 | + get_projects_response = core_client.get_projects() |
| 243 | + |
| 244 | + projects = [project.name for project in get_projects_response] |
| 245 | + |
| 246 | + if not projects: |
| 247 | + self.ui.print_error("No projects found in Azure DevOps organization") |
| 248 | + return None |
| 249 | + |
| 250 | + # Ask user to select project |
| 251 | + questions = [ |
| 252 | + inquirer.List( |
| 253 | + "project", |
| 254 | + message=f"{Fore.GREEN} What is the templates project?", |
| 255 | + choices=projects, |
| 256 | + ), |
| 257 | + ] |
| 258 | + tmp_project = inquirer.prompt(questions) |
| 259 | + project_name = tmp_project["project"] |
| 260 | + self.ui.print_success(f"✅ {project_name} was selected.") |
| 261 | + |
| 262 | + # Get repositories from selected project |
| 263 | + git_client = conn.clients.get_git_client() |
| 264 | + repositories = get_repos_patterns( |
| 265 | + project_name=project_name, |
| 266 | + git_client=git_client, |
| 267 | + allowed_pattern_names=allowed_pattern_prefixes, |
| 268 | + allowed_pattern_names_end=allowed_pattern_suffixes, |
235 | 269 | ) |
236 | 270 |
|
237 | | - return template_info |
| 271 | + if not repositories: |
| 272 | + self.ui.print_error("No template repositories found") |
| 273 | + return None |
| 274 | + |
| 275 | + # Ask user to select repository |
| 276 | + repository_names = [r["Name"] for r in repositories] |
| 277 | + questions = [ |
| 278 | + inquirer.List( |
| 279 | + "repository", |
| 280 | + message=f"{Fore.GREEN} Select a pattern to reuse: {Fore.RESET} 🔍 ", |
| 281 | + choices=repository_names, |
| 282 | + ), |
| 283 | + ] |
| 284 | + tmp_repo = inquirer.prompt(questions) |
| 285 | + repository_name = tmp_repo["repository"] |
| 286 | + repository = [r for r in repositories if r["Name"] == repository_name][0] |
| 287 | + |
| 288 | + self.ui.print_success(f"✅ Selected template: {repository_name}") |
| 289 | + |
| 290 | + # Return repository metadata for later cloning |
| 291 | + return { |
| 292 | + "repo_name": repository["Name"], |
| 293 | + "repo_url": repository["RemoteUrl"], |
| 294 | + "pat": pat, |
| 295 | + "org_url": org_url, |
| 296 | + "project_name": project_name, |
| 297 | + } |
238 | 298 |
|
239 | 299 | except Exception as e: |
240 | 300 | self.ui.print_error(f"Failed to select Azure template: {e}") |
@@ -299,16 +359,57 @@ def _select_github_template(self, space: str, **kwargs) -> Optional[dict]: |
299 | 359 | self.ui.print_error("GitHub username is required") |
300 | 360 | return None |
301 | 361 |
|
302 | | - # Try to list templates |
| 362 | + # Get list of template repositories |
303 | 363 | self.ui.print_info("🔍 Fetching available templates...") |
304 | | - template_info = get_pattern_from_github( |
305 | | - token=token, # Can be None for public repos |
| 364 | + |
| 365 | + from ....core.integrations.github.get_github import ( |
| 366 | + get_repos_patterns, |
| 367 | + allowed_pattern_prefixes, |
| 368 | + allowed_pattern_suffixes |
| 369 | + ) |
| 370 | + import inquirer |
| 371 | + from colorama import Fore |
| 372 | + import requests |
| 373 | + |
| 374 | + # Create session for GitHub API |
| 375 | + session = requests.Session() |
| 376 | + if token: |
| 377 | + session.headers.update({"Authorization": f"token {token}"}) |
| 378 | + |
| 379 | + # Get repositories matching patterns |
| 380 | + repositories = get_repos_patterns( |
| 381 | + session=session, |
306 | 382 | username=username, |
307 | | - directory="temp", # Dummy directory for listing |
308 | | - action="list", |
| 383 | + allowed_pattern_prefixes=allowed_pattern_prefixes, |
| 384 | + allowed_pattern_suffixes=allowed_pattern_suffixes, |
309 | 385 | ) |
310 | 386 |
|
311 | | - return template_info |
| 387 | + if not repositories: |
| 388 | + self.ui.print_error("No template repositories found") |
| 389 | + return None |
| 390 | + |
| 391 | + # Ask user to select repository |
| 392 | + repository_names = [r["Name"] for r in repositories] |
| 393 | + questions = [ |
| 394 | + inquirer.List( |
| 395 | + "repository", |
| 396 | + message=f"{Fore.GREEN} Select a pattern to reuse: {Fore.RESET} 🔍 ", |
| 397 | + choices=repository_names, |
| 398 | + ), |
| 399 | + ] |
| 400 | + tmp_repo = inquirer.prompt(questions) |
| 401 | + repository_name = tmp_repo["repository"] |
| 402 | + repository = [r for r in repositories if r["Name"] == repository_name][0] |
| 403 | + |
| 404 | + self.ui.print_success(f"✅ Selected template: {repository_name}") |
| 405 | + |
| 406 | + # Return repository metadata for later cloning |
| 407 | + return { |
| 408 | + "repo_name": repository["Name"], |
| 409 | + "repo_url": repository["RemoteUrl"], |
| 410 | + "token": token, |
| 411 | + "username": username, |
| 412 | + } |
312 | 413 |
|
313 | 414 | except Exception as e: |
314 | 415 | self.ui.print_error(f"Failed to select GitHub template: {e}") |
|
0 commit comments