-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_github_integration.py
More file actions
executable file
·204 lines (158 loc) · 6.07 KB
/
setup_github_integration.py
File metadata and controls
executable file
·204 lines (158 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""
Setup script for GitHub repository scanning and CloudFlare integration
"""
import os
import sys
import json
import subprocess
from pathlib import Path
def print_header(text):
print(f"\n{'='*60}")
print(f" {text}")
print(f"{'='*60}\n")
def setup_github_scanner():
"""Setup GitHub scanner with token"""
print_header("GitHub Scanner Setup")
# Check for GitHub token
token = os.environ.get('GITHUB_TOKEN')
if not token:
print("❌ GITHUB_TOKEN environment variable not found!")
print("\nTo set up GitHub access:")
print("1. Go to https://github.com/settings/tokens")
print("2. Generate a new token with 'repo' scope")
print("3. Set the token:")
print(" export GITHUB_TOKEN=your_token_here")
return False
print("✅ GitHub token found")
# Test token
try:
import requests
response = requests.get(
'https://api.github.com/user',
headers={'Authorization': f'token {token}'}
)
if response.ok:
user = response.json()
print(f"✅ Authenticated as: {user['login']}")
else:
print("❌ Invalid GitHub token")
return False
except Exception as e:
print(f"❌ Error testing token: {e}")
return False
return True
def scan_github_repos():
"""Scan GitHub repositories"""
print_header("Scanning GitHub Repositories")
org = input("Enter GitHub organization name (or press Enter to skip): ").strip()
repo = input("Enter specific repository (owner/repo format, or press Enter to scan all): ").strip()
if not org and not repo:
print("❌ Must specify either organization or repository")
return False
# Build command
cmd = [
sys.executable, 'github_scanner.py',
'--token', os.environ.get('GITHUB_TOKEN'),
'--output', './github-repos'
]
if org:
cmd.extend(['--org', org])
if repo:
cmd.extend(['--repo', repo])
print(f"\n🔍 Running: {' '.join(cmd)}")
try:
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print(result.stdout)
return True
else:
print(f"❌ Scanner failed: {result.stderr}")
return False
except Exception as e:
print(f"❌ Error running scanner: {e}")
return False
def aggregate_data():
"""Aggregate all power.status.json files"""
print_header("Aggregating Data")
cmd = [sys.executable, 'data_aggregator.py']
print(f"📊 Running: {' '.join(cmd)}")
try:
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print(result.stdout)
# Check if data.json was created
if Path('./public/data.json').exists():
with open('./public/data.json', 'r') as f:
data = json.load(f)
print(f"\n✅ Data aggregated successfully:")
print(f" - Projects: {data['summary']['totalProjects']}")
print(f" - Nodes: {data['summary']['totalNodes']}")
print(f" - Edges: {data['summary']['totalEdges']}")
return True
else:
print(f"❌ Aggregator failed: {result.stderr}")
return False
except Exception as e:
print(f"❌ Error running aggregator: {e}")
return False
def setup_cloudflare():
"""Setup CloudFlare workers"""
print_header("CloudFlare Setup")
print("To deploy to CloudFlare:")
print("\n1. Install Wrangler CLI:")
print(" npm install -g wrangler")
print("\n2. Login to CloudFlare:")
print(" wrangler login")
print("\n3. Create KV namespaces:")
print(" wrangler kv:namespace create REPO_SCANS")
print(" wrangler kv:namespace create REPO_STATUS")
print(" wrangler kv:namespace create POWER_STATUS")
print("\n4. Create R2 bucket:")
print(" wrangler r2 bucket create power-status-files")
print("\n5. Update wrangler.toml with the IDs from step 3")
print("\n6. Set secrets:")
print(" wrangler secret put GITHUB_TOKEN")
print(" wrangler secret put GITHUB_WEBHOOK_SECRET")
print("\n7. Deploy:")
print(" wrangler pages deploy public")
print("\n8. Set up GitHub webhooks:")
print(" - Go to your repository settings")
print(" - Add webhook URL: https://thewatchmen.pages.dev/api/github-webhook")
print(" - Select events: push, pull_request, issues, release")
print(" - Set secret to match GITHUB_WEBHOOK_SECRET")
return True
def start_local_server():
"""Start local development server"""
print_header("Starting Local Server")
print("Starting visualization at http://localhost:3000")
print("Press Ctrl+C to stop\n")
try:
subprocess.run(['python', '-m', 'http.server', '3000'], cwd='public')
except KeyboardInterrupt:
print("\n\n✅ Server stopped")
except Exception as e:
print(f"❌ Error starting server: {e}")
def main():
"""Main setup flow"""
print_header("Pow3r.build GitHub Integration Setup")
steps = [
("1. Setup GitHub Scanner", setup_github_scanner),
("2. Scan GitHub Repositories", scan_github_repos),
("3. Aggregate Data", aggregate_data),
("4. CloudFlare Setup Guide", setup_cloudflare),
("5. Start Local Server", start_local_server)
]
for step_name, step_func in steps:
print(f"\n{step_name}...")
if not step_func():
print(f"\n❌ Setup failed at: {step_name}")
if input("\nContinue anyway? (y/N): ").lower() != 'y':
break
print("\n✅ Setup complete!")
print("\nNext steps:")
print("1. Deploy to CloudFlare (see instructions above)")
print("2. Configure GitHub webhooks")
print("3. Access visualization at https://thewatchmen.pages.dev")
if __name__ == '__main__':
main()