Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/python/cocalc-api/scripts/ci-create-project.py
5559 views
1
#!/usr/bin/env python3
2
"""
3
Create a test project for CI tests using the cocalc-api Hub.
4
5
This script creates a temporary project for running tests that require a project ID
6
(e.g., cocalc-api tests with account-scoped API keys).
7
8
Outputs the project ID to stdout so it can be captured by the CI workflow.
9
"""
10
11
import os
12
import sys
13
import httpx
14
15
16
def main():
17
"""Create a test project and output its ID."""
18
# Get configuration from environment
19
api_key = os.environ.get("COCALC_API_KEY")
20
host = os.environ.get("COCALC_HOST", "http://localhost:5000")
21
22
if not api_key:
23
print("Error: COCALC_API_KEY environment variable not set", file=sys.stderr)
24
sys.exit(1)
25
26
try:
27
# Create HTTP client with API key authentication
28
client = httpx.Client(
29
auth=(api_key, ""),
30
headers={"Content-Type": "application/json"},
31
timeout=120.0
32
)
33
34
# Call the hub API to create a project
35
resp = client.post(
36
f"{host}/api/conat/hub",
37
json={
38
"name": "projects.createProject",
39
"args": [{"title": "CI Test Project"}]
40
}
41
)
42
43
result = resp.json()
44
45
# Check for errors in the response
46
if "error" in result:
47
print(f"Error creating project: {result['error']}", file=sys.stderr)
48
sys.exit(1)
49
50
# The result should be the project ID
51
project_id = result
52
if not project_id or project_id == "None":
53
print(f"Error: Invalid project ID returned: {result}", file=sys.stderr)
54
sys.exit(1)
55
56
# Output the project ID to stdout
57
print(project_id)
58
59
except Exception as e:
60
print(f"Error: {e}", file=sys.stderr)
61
sys.exit(1)
62
63
64
if __name__ == "__main__":
65
main()
66
67