|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Add nf-core maintainers to the AWSMegatests workspace as participants with MAINTAIN role. |
| 4 | +
|
| 5 | +This script serves as a workaround for missing Terraform provider resources. |
| 6 | +
|
| 7 | +TODO: Replace this script with proper Terraform resources once implemented: |
| 8 | +- seqera_workspace_participant |
| 9 | +- seqera_workspace_participant_role |
| 10 | +
|
| 11 | +See GitHub issue: https://github.com/seqeralabs/terraform-provider-seqera/issues/[TO_BE_CREATED] |
| 12 | +
|
| 13 | +The API endpoints exist in the provider SDK but are not exposed as Terraform resources. |
| 14 | +""" |
| 15 | + |
| 16 | +import json |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import time |
| 20 | +import requests |
| 21 | +from typing import Dict, List, Any |
| 22 | + |
| 23 | + |
| 24 | +class SeqeraWorkspaceManager: |
| 25 | + """Manager for Seqera Platform workspace participant operations.""" |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + token: str, |
| 30 | + org_id: int = 252464779077610, |
| 31 | + workspace_id: int = 59994744926013, |
| 32 | + ): |
| 33 | + """Initialize the workspace manager.""" |
| 34 | + self.token = token |
| 35 | + self.org_id = org_id # nf-core |
| 36 | + self.workspace_id = workspace_id # AWSMegatests |
| 37 | + self.api_url = "https://api.cloud.seqera.io" |
| 38 | + self.headers = { |
| 39 | + "Authorization": f"Bearer {token}", |
| 40 | + "Content-Type": "application/json", |
| 41 | + } |
| 42 | + |
| 43 | + def get_current_participants(self) -> List[Dict[str, Any]]: |
| 44 | + """Get current workspace participants.""" |
| 45 | + url = f"{self.api_url}/orgs/{self.org_id}/workspaces/{self.workspace_id}/participants" |
| 46 | + |
| 47 | + try: |
| 48 | + response = requests.get(url, headers=self.headers, timeout=30) |
| 49 | + |
| 50 | + if response.status_code == 200: |
| 51 | + data = response.json() |
| 52 | + return data.get("participants", []) |
| 53 | + else: |
| 54 | + print(f"✗ Failed to get participants. Status: {response.status_code}") |
| 55 | + return [] |
| 56 | + |
| 57 | + except requests.exceptions.RequestException as e: |
| 58 | + print(f"✗ Network error getting participants: {e}") |
| 59 | + return [] |
| 60 | + |
| 61 | + def add_participant(self, email: str, role: str = "MAINTAIN") -> bool: |
| 62 | + """Add a single participant to the workspace.""" |
| 63 | + url = f"{self.api_url}/orgs/{self.org_id}/workspaces/{self.workspace_id}/participants/add" |
| 64 | + |
| 65 | + # Fixed payload format based on terraform-provider-seqera SDK analysis |
| 66 | + payload = { |
| 67 | + "userNameOrEmail": email # API expects this field name, not "email" |
| 68 | + # Note: Role is set separately via update endpoint after participant is added |
| 69 | + } |
| 70 | + |
| 71 | + try: |
| 72 | + response = requests.put(url, headers=self.headers, json=payload, timeout=30) |
| 73 | + |
| 74 | + if response.status_code in [200, 201, 204]: |
| 75 | + print(f" ✓ Added {email} with role {role}") |
| 76 | + return True |
| 77 | + elif response.status_code == 409: |
| 78 | + print(f" ~ {email} already exists (checking role...)") |
| 79 | + return self._check_and_update_role(email, role) |
| 80 | + else: |
| 81 | + error_msg = f"Status {response.status_code}" |
| 82 | + try: |
| 83 | + error_data = response.json() |
| 84 | + error_msg += f": {error_data.get('message', 'Unknown error')}" |
| 85 | + except Exception: |
| 86 | + error_msg += f": {response.text}" |
| 87 | + |
| 88 | + print(f" ✗ Failed to add {email} - {error_msg}") |
| 89 | + return False |
| 90 | + |
| 91 | + except requests.exceptions.RequestException as e: |
| 92 | + print(f" ✗ Network error adding {email}: {e}") |
| 93 | + return False |
| 94 | + |
| 95 | + def _check_and_update_role(self, email: str, desired_role: str) -> bool: |
| 96 | + """Check if existing participant has the correct role.""" |
| 97 | + participants = self.get_current_participants() |
| 98 | + |
| 99 | + for participant in participants: |
| 100 | + if participant.get("email", "").lower() == email.lower(): |
| 101 | + current_role = participant.get("wspRole", "").lower() |
| 102 | + if current_role == desired_role.lower(): |
| 103 | + print(f" ✓ Already has correct role: {current_role}") |
| 104 | + return True |
| 105 | + else: |
| 106 | + print( |
| 107 | + f" ! Has role {current_role}, desired {desired_role.lower()}" |
| 108 | + ) |
| 109 | + # Note: Role update would require additional API call if supported |
| 110 | + return True # Consider this successful for now |
| 111 | + |
| 112 | + print(f" ? Could not find {email} in participants list") |
| 113 | + return False |
| 114 | + |
| 115 | + def add_maintainers_batch( |
| 116 | + self, maintainers_data: List[Dict[str, str]], delay: float = 1.0 |
| 117 | + ) -> Dict[str, bool]: |
| 118 | + """Add multiple maintainers with a delay between requests.""" |
| 119 | + results = {} |
| 120 | + |
| 121 | + print(f"Adding {len(maintainers_data)} maintainers to workspace...") |
| 122 | + print(f"Organization: nf-core (ID: {self.org_id})") |
| 123 | + print(f"Workspace: AWSMegatests (ID: {self.workspace_id})") |
| 124 | + print() |
| 125 | + |
| 126 | + for i, maintainer in enumerate(maintainers_data, 1): |
| 127 | + email = maintainer["name"] # 'name' field contains the email |
| 128 | + role = maintainer["role"] |
| 129 | + github_username = maintainer.get("github_username", "unknown") |
| 130 | + |
| 131 | + print(f"{i:2d}/{len(maintainers_data)}: {email} ({github_username})") |
| 132 | + |
| 133 | + success = self.add_participant(email, role) |
| 134 | + results[email] = success |
| 135 | + |
| 136 | + # Add delay between requests to be nice to the API |
| 137 | + if i < len(maintainers_data) and delay > 0: |
| 138 | + time.sleep(delay) |
| 139 | + |
| 140 | + return results |
| 141 | + |
| 142 | + |
| 143 | +def load_maintainers_data() -> List[Dict[str, str]]: |
| 144 | + """Load maintainers data from JSON file.""" |
| 145 | + data_file = "scripts/maintainers_data.json" |
| 146 | + |
| 147 | + try: |
| 148 | + with open(data_file, "r") as f: |
| 149 | + data = json.load(f) |
| 150 | + |
| 151 | + return data.get("seqera_participants", []) |
| 152 | + |
| 153 | + except FileNotFoundError: |
| 154 | + print(f"✗ Maintainers data file not found: {data_file}") |
| 155 | + print( |
| 156 | + "Run 'python scripts/fetch_maintainer_emails.py' first to generate the data" |
| 157 | + ) |
| 158 | + return [] |
| 159 | + except json.JSONDecodeError as e: |
| 160 | + print(f"✗ Error parsing maintainers data file: {e}") |
| 161 | + return [] |
| 162 | + |
| 163 | + |
| 164 | +def main(): |
| 165 | + """Main function.""" |
| 166 | + |
| 167 | + # Check for non-interactive mode |
| 168 | + non_interactive = "--yes" in sys.argv or "--non-interactive" in sys.argv |
| 169 | + |
| 170 | + print("=== nf-core Maintainers → Seqera Platform Workspace ===") |
| 171 | + print() |
| 172 | + |
| 173 | + # Get token from environment |
| 174 | + token = os.getenv("TOWER_ACCESS_TOKEN") |
| 175 | + if not token: |
| 176 | + print("✗ Error: TOWER_ACCESS_TOKEN environment variable not set") |
| 177 | + sys.exit(1) |
| 178 | + |
| 179 | + # Load maintainers data |
| 180 | + maintainers_data = load_maintainers_data() |
| 181 | + if not maintainers_data: |
| 182 | + print("✗ No maintainers data available") |
| 183 | + sys.exit(1) |
| 184 | + |
| 185 | + print(f"Loaded {len(maintainers_data)} maintainers with public emails") |
| 186 | + print() |
| 187 | + |
| 188 | + # Initialize workspace manager |
| 189 | + manager = SeqeraWorkspaceManager(token) |
| 190 | + |
| 191 | + # Get current participants for comparison |
| 192 | + print("Current workspace participants:") |
| 193 | + current_participants = manager.get_current_participants() |
| 194 | + if current_participants: |
| 195 | + print(f" Found {len(current_participants)} existing participants") |
| 196 | + |
| 197 | + # Show maintainers already in workspace |
| 198 | + current_emails = {p.get("email", "").lower() for p in current_participants} |
| 199 | + already_added = [ |
| 200 | + m for m in maintainers_data if m["name"].lower() in current_emails |
| 201 | + ] |
| 202 | + |
| 203 | + if already_added: |
| 204 | + print(f" {len(already_added)} maintainers already in workspace:") |
| 205 | + for m in already_added: |
| 206 | + print(f" - {m['name']} ({m.get('github_username', 'unknown')})") |
| 207 | + |
| 208 | + to_add = [ |
| 209 | + m for m in maintainers_data if m["name"].lower() not in current_emails |
| 210 | + ] |
| 211 | + print(f" {len(to_add)} maintainers to be added") |
| 212 | + else: |
| 213 | + to_add = maintainers_data |
| 214 | + print( |
| 215 | + f" Could not get current participants, will attempt to add all {len(to_add)}" |
| 216 | + ) |
| 217 | + |
| 218 | + print() |
| 219 | + |
| 220 | + # Confirm before proceeding (unless non-interactive) |
| 221 | + if not non_interactive: |
| 222 | + try: |
| 223 | + response = input( |
| 224 | + f"Add {len(to_add) if 'to_add' in locals() else len(maintainers_data)} maintainers to workspace? (y/N): " |
| 225 | + ) |
| 226 | + if response.lower() not in ["y", "yes"]: |
| 227 | + print("Aborted by user") |
| 228 | + sys.exit(0) |
| 229 | + except (EOFError, KeyboardInterrupt): |
| 230 | + print("\nAborted by user") |
| 231 | + sys.exit(0) |
| 232 | + else: |
| 233 | + print( |
| 234 | + f"Non-interactive mode: proceeding to add {len(to_add) if 'to_add' in locals() else len(maintainers_data)} maintainers..." |
| 235 | + ) |
| 236 | + |
| 237 | + print() |
| 238 | + |
| 239 | + # Add maintainers |
| 240 | + data_to_add = to_add if "to_add" in locals() else maintainers_data |
| 241 | + results = manager.add_maintainers_batch(data_to_add, delay=1.0) |
| 242 | + |
| 243 | + # Summary |
| 244 | + print() |
| 245 | + print("=== Summary ===") |
| 246 | + successful = sum(1 for success in results.values() if success) |
| 247 | + failed = len(results) - successful |
| 248 | + |
| 249 | + print(f"Successfully added: {successful}") |
| 250 | + print(f"Failed: {failed}") |
| 251 | + |
| 252 | + if failed > 0: |
| 253 | + print("\nFailed additions:") |
| 254 | + for email, success in results.items(): |
| 255 | + if not success: |
| 256 | + print(f" - {email}") |
| 257 | + |
| 258 | + print( |
| 259 | + f"\nTotal workspace participants after operation: {len(current_participants) + successful}" |
| 260 | + ) |
| 261 | + |
| 262 | + |
| 263 | +if __name__ == "__main__": |
| 264 | + main() |
0 commit comments