Path: blob/master/src/python/cocalc-api/scripts/ci-create-project.py
5559 views
#!/usr/bin/env python31"""2Create a test project for CI tests using the cocalc-api Hub.34This script creates a temporary project for running tests that require a project ID5(e.g., cocalc-api tests with account-scoped API keys).67Outputs the project ID to stdout so it can be captured by the CI workflow.8"""910import os11import sys12import httpx131415def main():16"""Create a test project and output its ID."""17# Get configuration from environment18api_key = os.environ.get("COCALC_API_KEY")19host = os.environ.get("COCALC_HOST", "http://localhost:5000")2021if not api_key:22print("Error: COCALC_API_KEY environment variable not set", file=sys.stderr)23sys.exit(1)2425try:26# Create HTTP client with API key authentication27client = httpx.Client(28auth=(api_key, ""),29headers={"Content-Type": "application/json"},30timeout=120.031)3233# Call the hub API to create a project34resp = client.post(35f"{host}/api/conat/hub",36json={37"name": "projects.createProject",38"args": [{"title": "CI Test Project"}]39}40)4142result = resp.json()4344# Check for errors in the response45if "error" in result:46print(f"Error creating project: {result['error']}", file=sys.stderr)47sys.exit(1)4849# The result should be the project ID50project_id = result51if not project_id or project_id == "None":52print(f"Error: Invalid project ID returned: {result}", file=sys.stderr)53sys.exit(1)5455# Output the project ID to stdout56print(project_id)5758except Exception as e:59print(f"Error: {e}", file=sys.stderr)60sys.exit(1)616263if __name__ == "__main__":64main()656667