|
| 1 | +from urllib.parse import urlparse |
| 2 | + |
| 3 | +from website.management.base import LoggedBaseCommand |
| 4 | +from website.models import Organization |
| 5 | + |
| 6 | + |
| 7 | +class Command(LoggedBaseCommand): |
| 8 | + help = "Populate github_org field from source_code field for organizations" |
| 9 | + |
| 10 | + def add_arguments(self, parser): |
| 11 | + parser.add_argument( |
| 12 | + "--dry-run", |
| 13 | + action="store_true", |
| 14 | + help="Show what would be updated without making changes", |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + "--overwrite", |
| 18 | + action="store_true", |
| 19 | + help="Overwrite existing github_org values", |
| 20 | + ) |
| 21 | + |
| 22 | + def handle(self, *args, **options): |
| 23 | + dry_run = options["dry_run"] |
| 24 | + overwrite = options["overwrite"] |
| 25 | + |
| 26 | + self.stdout.write(self.style.SUCCESS("Starting GitHub organization population...")) |
| 27 | + |
| 28 | + # Get organizations with source_code but no github_org (or all if overwrite) |
| 29 | + if overwrite: |
| 30 | + orgs = Organization.objects.filter(source_code__isnull=False).exclude(source_code="") |
| 31 | + self.stdout.write(f"Processing {orgs.count()} organizations (overwrite mode)...") |
| 32 | + else: |
| 33 | + orgs = Organization.objects.filter(source_code__isnull=False, github_org__isnull=True).exclude( |
| 34 | + source_code="" |
| 35 | + ) | Organization.objects.filter(source_code__isnull=False, github_org="").exclude(source_code="") |
| 36 | + self.stdout.write(f"Processing {orgs.count()} organizations without github_org...") |
| 37 | + |
| 38 | + updated_count = 0 |
| 39 | + skipped_count = 0 |
| 40 | + error_count = 0 |
| 41 | + |
| 42 | + for org in orgs: |
| 43 | + try: |
| 44 | + github_org = self.extract_github_org(org.source_code) |
| 45 | + |
| 46 | + if github_org: |
| 47 | + if dry_run: |
| 48 | + self.stdout.write( |
| 49 | + f"[DRY RUN] Would set '{org.name}' github_org to: {github_org} (from {org.source_code})" |
| 50 | + ) |
| 51 | + updated_count += 1 |
| 52 | + else: |
| 53 | + org.github_org = github_org |
| 54 | + org.save(update_fields=["github_org"]) |
| 55 | + self.stdout.write( |
| 56 | + self.style.SUCCESS( |
| 57 | + f"✓ Updated '{org.name}' github_org to: {github_org} (from {org.source_code})" |
| 58 | + ) |
| 59 | + ) |
| 60 | + updated_count += 1 |
| 61 | + else: |
| 62 | + self.stdout.write( |
| 63 | + self.style.WARNING(f"⚠ Could not extract GitHub org from '{org.source_code}' for '{org.name}'") |
| 64 | + ) |
| 65 | + skipped_count += 1 |
| 66 | + |
| 67 | + except Exception as e: |
| 68 | + self.stdout.write(self.style.ERROR(f"✗ Error processing '{org.name}': {str(e)}")) |
| 69 | + error_count += 1 |
| 70 | + |
| 71 | + # Summary |
| 72 | + self.stdout.write("\n" + "=" * 60) |
| 73 | + self.stdout.write(self.style.SUCCESS("Summary:")) |
| 74 | + if dry_run: |
| 75 | + self.stdout.write(f" Organizations that would be updated: {updated_count}") |
| 76 | + else: |
| 77 | + self.stdout.write(f" Organizations updated: {updated_count}") |
| 78 | + self.stdout.write(f" Organizations skipped: {skipped_count}") |
| 79 | + self.stdout.write(f" Errors: {error_count}") |
| 80 | + self.stdout.write("=" * 60) |
| 81 | + |
| 82 | + if dry_run: |
| 83 | + self.stdout.write(self.style.WARNING("\nThis was a dry run. No changes were made.")) |
| 84 | + self.stdout.write("Run without --dry-run to apply changes.") |
| 85 | + |
| 86 | + def extract_github_org(self, url): |
| 87 | + """ |
| 88 | + Extract GitHub organization name from various GitHub URL formats. |
| 89 | +
|
| 90 | + Supports: |
| 91 | + - https://github.com/org |
| 92 | + - https://github.com/org/ |
| 93 | + - https://github.com/org/repo |
| 94 | + - https://github.com/org/repo/... |
| 95 | + - http://github.com/org |
| 96 | + - www.github.com/org |
| 97 | + - github.com/org |
| 98 | + """ |
| 99 | + if not url: |
| 100 | + return None |
| 101 | + |
| 102 | + try: |
| 103 | + # Handle URLs without scheme |
| 104 | + if not url.startswith(("http://", "https://")): |
| 105 | + url = "https://" + url |
| 106 | + |
| 107 | + parsed = urlparse(url) |
| 108 | + |
| 109 | + # Check if it's a GitHub URL |
| 110 | + if "github.com" not in parsed.netloc: |
| 111 | + return None |
| 112 | + |
| 113 | + # Extract path parts |
| 114 | + path_parts = [p for p in parsed.path.split("/") if p] |
| 115 | + |
| 116 | + # GitHub org is the first part of the path |
| 117 | + if path_parts: |
| 118 | + return path_parts[0] |
| 119 | + |
| 120 | + return None |
| 121 | + |
| 122 | + except Exception: |
| 123 | + return None |
0 commit comments