-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
674 lines (553 loc) · 22.8 KB
/
basic.py
File metadata and controls
674 lines (553 loc) · 22.8 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import json
import sys
import os
import random
import logging
import time
from pathlib import Path
from typing import Dict, List, Optional, Union, Any
import requests
import schedule
import jwt # Added for GitHub App JWT generation
from dotenv import load_dotenv
# Optional imports for Azure components
try:
import prompty
import prompty.azure
from prompty.tracer import trace, Tracer, console_tracer, PromptyTracer
except ImportError:
logging.warning("Prompty package not found. Ensure it's installed with: pip install prompty[azure]")
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
logger = logging.getLogger("discussion-labeler")
# Load environment variables from .env file - ensure .env is in .gitignore
load_dotenv()
# Configure tracers if prompty is available
if 'prompty' in globals():
# Add console and json tracer at application startup
Tracer.add("console", console_tracer)
json_tracer = PromptyTracer()
Tracer.add("PromptyTracer", json_tracer.tracer)
# Global variables
TOKEN = os.getenv("TOKEN")
DEFAULT_REPO = os.getenv("DEFAULT_REPO", "golclinics/discussions")
REQUEST_TIMEOUT = int(os.getenv("REQUEST_TIMEOUT", "30")) # Default 30 second timeout
# App settings
APP_ID = os.getenv("APP_ID")
APP_PRIVATE_KEY = os.getenv("APP_PRIVATE_KEY") # Direct key content instead of file path
APP_PRIVATE_KEY_PATH = os.getenv("APP_PRIVATE_KEY_PATH") # Keep for backward compatibility
APP_INSTALLATION_ID = os.getenv("APP_INSTALLATION_ID") # Removed GITHUB_ prefix
# Azure OpenAI settings
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION")
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
AZURE_OPENAI_KEY = os.getenv("AZURE_OPENAI_KEY")
# Other settings
SECRET_KEY = os.getenv("SECRET_KEY")
# GitHub API rate limiting constants
MAX_RETRIES = 3
RETRY_BACKOFF = 2 # seconds, will be multiplied by attempt number
class TokenMissingError(Exception):
"""Exception raised when GitHub token is missing."""
pass
class GithubAppAuthError(Exception):
"""Exception raised when GitHub App authentication fails."""
pass
def validate_token() -> None:
"""Validate that the GitHub token is available."""
if not TOKEN:
raise TokenMissingError(
"GitHub token not found in environment variables. "
"Please set the TOKEN environment variable."
)
def validate_github_app_config() -> None:
"""Validate that the GitHub App configuration is available."""
missing = []
if not APP_ID:
missing.append("APP_ID")
if not APP_PRIVATE_KEY and not APP_PRIVATE_KEY_PATH:
missing.append("APP_PRIVATE_KEY or APP_PRIVATE_KEY_PATH")
if not APP_INSTALLATION_ID:
missing.append("APP_INSTALLATION_ID")
if missing:
raise GithubAppAuthError(
f"GitHub App configuration missing: {', '.join(missing)}. "
"Please set the required environment variables."
)
def generate_jwt() -> str:
"""Generate a JWT for GitHub App authentication.
Returns:
JWT token string
Raises:
GithubAppAuthError: If JWT generation fails
"""
validate_github_app_config()
try:
# JWT expiration time (10 minutes is recommended by GitHub)
now = int(time.time())
expiration = now + (10 * 60) # 10 minutes
# Prepare the JWT payload
payload = {
'iat': now, # Issued at time
'exp': expiration, # Expiration time
'iss': APP_ID # GitHub App ID
}
# Get the private key - either directly from env var or from file
private_key = None
# First try to use the direct key from environment variable
if APP_PRIVATE_KEY:
private_key = APP_PRIVATE_KEY
logger.info("Using private key directly from environment variable")
# Fallback to loading from file path
elif APP_PRIVATE_KEY_PATH:
try:
key_path = Path(APP_PRIVATE_KEY_PATH)
if key_path.exists():
with open(key_path, "r") as key_file:
private_key = key_file.read()
logger.info(f"Successfully loaded private key from file: {APP_PRIVATE_KEY_PATH}")
else:
logger.error(f"Private key file not found at: {APP_PRIVATE_KEY_PATH}")
raise FileNotFoundError(f"Private key file not found: {APP_PRIVATE_KEY_PATH}")
except Exception as e:
logger.error(f"Error reading private key file: {str(e)}")
raise
else:
raise GithubAppAuthError("No private key available. Set APP_PRIVATE_KEY or APP_PRIVATE_KEY_PATH")
# Generate the JWT
token = jwt.encode(
payload,
private_key,
algorithm='RS256'
)
# If token is bytes, decode to string (depends on PyJWT version)
if isinstance(token, bytes):
token = token.decode('utf-8')
return token
except Exception as e:
logger.error(f"Error generating JWT: {str(e)}")
raise GithubAppAuthError(f"Failed to generate JWT: {str(e)}")
def get_installation_token() -> str:
"""Get an installation access token for the GitHub App.
Returns:
Installation access token
Raises:
GithubAppAuthError: If token retrieval fails
"""
validate_github_app_config()
try:
# Generate JWT for authentication
jwt_token = generate_jwt()
# API endpoint for getting an installation token
url = f"https://api.github.com/app/installations/{APP_INSTALLATION_ID}/access_tokens"
headers = {
"Authorization": f"Bearer {jwt_token}",
"Accept": "application/vnd.github.v3+json"
}
response = handle_request_with_retry("post", url, headers)
data = response.json()
if "token" not in data:
logger.error(f"No token in response: {data}")
raise GithubAppAuthError("Failed to get installation token: No token in response")
return data["token"]
except Exception as e:
logger.error(f"Error getting installation token: {str(e)}")
raise GithubAppAuthError(f"Failed to get installation token: {str(e)}")
def get_auth_headers() -> Dict[str, str]:
"""Get authentication headers for GitHub API calls.
Returns:
Dictionary of headers including authorization
Raises:
GithubAppAuthError: If headers cannot be generated
"""
try:
token = get_installation_token()
return {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
except Exception as e:
logger.error(f"Error getting auth headers: {str(e)}")
raise GithubAppAuthError(f"Failed to get auth headers: {str(e)}")
def validate_repo_url(repo_url: str) -> tuple:
"""Validate and parse the repository URL.
Args:
repo_url: Repository URL in the format "owner/name"
Returns:
Tuple of (owner, name)
Raises:
ValueError: If repo_url format is invalid
"""
if not repo_url or "/" not in repo_url:
raise ValueError(f"Invalid repository URL format: {repo_url}. Expected format: owner/name")
parts = repo_url.split("/")
if len(parts) != 2 or not all(parts):
raise ValueError(f"Invalid repository URL format: {repo_url}. Expected format: owner/name")
return parts[0], parts[1]
def handle_request_with_retry(
method: str,
url: str,
headers: Dict[str, str],
json_data: Optional[Dict] = None,
max_retries: int = MAX_RETRIES
) -> requests.Response:
"""Make HTTP request with retry logic and exponential backoff for rate limits.
Args:
method: HTTP method (get, post, patch)
url: API URL
headers: HTTP headers
json_data: JSON payload
max_retries: Maximum number of retry attempts
Returns:
Response object
Raises:
requests.exceptions.RequestException: If request fails after retries
"""
attempt = 0
last_exception = None
while attempt < max_retries:
try:
if method.lower() == "get":
response = requests.get(url, headers=headers, timeout=REQUEST_TIMEOUT)
elif method.lower() == "post":
response = requests.post(url, headers=headers, json=json_data, timeout=REQUEST_TIMEOUT)
elif method.lower() == "patch":
response = requests.patch(url, headers=headers, json=json_data, timeout=REQUEST_TIMEOUT)
else:
raise ValueError(f"Unsupported HTTP method: {method}")
# Check for rate limiting
if response.status_code == 403 and "rate limit exceeded" in response.text.lower():
retry_after = int(response.headers.get("Retry-After", RETRY_BACKOFF * (attempt + 1)))
logger.warning(f"Rate limit exceeded. Retrying after {retry_after} seconds.")
time.sleep(retry_after)
attempt += 1
continue
# Check if successful
response.raise_for_status()
return response
except requests.exceptions.RequestException as e:
logger.warning(f"Request failed (attempt {attempt+1}/{max_retries}): {str(e)}")
last_exception = e
# Exponential backoff
sleep_time = RETRY_BACKOFF * (2 ** attempt)
time.sleep(sleep_time)
attempt += 1
# If we get here, all retries failed
if last_exception:
logger.error(f"All retry attempts failed: {str(last_exception)}")
raise last_exception
raise requests.exceptions.RequestException("All retry attempts failed.")
@trace
def fetch_github_discussions(repo_url: str) -> Optional[Dict]:
"""Fetch discussions from the specified GitHub repository.
Args:
repo_url: Repository URL in the format "owner/name"
Returns:
Dictionary containing discussion data or None if no discussions found
"""
try:
owner, name = validate_repo_url(repo_url)
# GitHub GraphQL API endpoint
api_url = "https://api.github.com/graphql"
# Get auth headers for GitHub App
headers = get_auth_headers()
# GraphQL query to fetch discussions
query = """
query RepoDiscussions($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
discussions(first: 10, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
id
number
title
body
category {
name
}
labels(first: 10) {
nodes {
name
}
}
}
}
}
}
"""
variables = {"owner": owner, "name": name}
payload = {"query": query, "variables": variables}
response = handle_request_with_retry("post", api_url, headers, payload)
data = response.json()
# Validate response structure
if not isinstance(data, dict) or "data" not in data:
logger.error(f"Invalid response format: {data}")
return None
# Navigate through the response with safe gets
repository = data.get("data", {}).get("repository", {})
discussions = repository.get("discussions", {}).get("nodes", [])
if not discussions:
logger.info("No discussions found in the repository.")
return None
return random.choice(discussions) # Pick a random discussion
except (ValueError, GithubAppAuthError, requests.exceptions.RequestException) as e:
logger.error(f"Error fetching discussions: {str(e)}")
return None
@trace
def run_with_rag(title: str, description: str) -> List[str]:
"""Run Prompty with RAG integration and return a Python list of tags.
Args:
title: Discussion title
description: Discussion description
Returns:
List of tags
"""
try:
# Load tags from JSON file with better error handling
tags_file_path = Path("tags.json")
if not tags_file_path.exists():
logger.error("tags.json file not found")
return []
with open(tags_file_path, "r") as f:
tags_data = json.load(f)
# Validate tags format
if not isinstance(tags_data, dict) or "tags" not in tags_data:
logger.error("Invalid tags.json format")
return []
azure_tags = tags_data.get("tags", [])
# Handle case with no tags
if not azure_tags:
logger.warning("No tags found in tags.json")
return []
# Convert tags to strings for joining if they're dictionaries
tag_strings = []
for tag in azure_tags:
if isinstance(tag, dict):
tag_string = f"{tag.get('name')}: {tag.get('description')}"
tag_strings.append(tag_string)
elif isinstance(tag, str):
tag_strings.append(tag)
# Combine search results with the original description
augmented_description = description + "\n\n" + "\n".join(tag_strings)
# Execute the Prompty file
prompty_file_path = Path("basic.prompty")
if not prompty_file_path.exists():
logger.error("basic.prompty file not found")
return []
raw = prompty.execute(
"basic.prompty",
inputs={
"title": title,
"tags": azure_tags,
"description": augmented_description
}
)
# Parse prompty's JSON output
try:
parsed = json.loads(raw)
# If prompty returns a bare list:
if isinstance(parsed, list):
return [str(item) for item in parsed] # Ensure all items are strings
# If it returns {"tags": [...]}:
if isinstance(parsed, dict):
tags = parsed.get("tags", [])
return [str(item) for item in tags] # Ensure all items are strings
except json.JSONDecodeError as e:
logger.error(f"Could not parse RAG output: {e}")
logger.debug(f"RAG raw output: {raw}")
except Exception as e:
logger.error(f"Error in run_with_rag: {str(e)}")
return []
@trace
def fetch_unlabeled_discussions(repo_url: str) -> List[Dict]:
"""Fetch open discussions that have no labels yet.
Args:
repo_url: Repository URL in the format "owner/name"
Returns:
List of dictionaries containing unlabeled discussion data
"""
try:
owner, name = validate_repo_url(repo_url)
# GitHub GraphQL API endpoint
api_url = "https://api.github.com/graphql"
# Get auth headers for GitHub App
headers = get_auth_headers()
# GraphQL query to fetch discussions without labels
query = """
query RepoDiscussionsWithoutLabels($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
discussions(first: 10, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
id
number
title
body
category {
name
}
labels(first: 10) {
nodes {
name
}
}
}
}
}
}
"""
variables = {"owner": owner, "name": name}
payload = {"query": query, "variables": variables}
response = handle_request_with_retry("post", api_url, headers, payload)
data = response.json()
# Validate response structure
if not isinstance(data, dict) or "data" not in data:
logger.error(f"Invalid response format: {data}")
return []
# Navigate through the response with safe gets
repository = data.get("data", {}).get("repository", {})
discussions = repository.get("discussions", {}).get("nodes", [])
if not discussions:
logger.info("No discussions found in the repository.")
return []
# Filter discussions with no labels
unlabeled_discussions = []
for discussion in discussions:
if not isinstance(discussion, dict):
continue
labels = discussion.get("labels", {}).get("nodes", [])
if not labels:
unlabeled_discussions.append(discussion)
return unlabeled_discussions
except (ValueError, GithubAppAuthError, requests.exceptions.RequestException) as e:
logger.error(f"Error fetching unlabeled discussions: {str(e)}")
return []
def get_discussion_node_id(owner: str, repo: str, discussion_number: int, headers: Dict[str, str]) -> str:
"""Fetch the node ID for a discussion given its number."""
query = '''
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
discussion(number: $number) {
id
}
}
}
'''
variables = {"owner": owner, "repo": repo, "number": discussion_number}
payload = {"query": query, "variables": variables}
response = requests.post("https://api.github.com/graphql", headers=headers, json=payload)
response.raise_for_status()
data = response.json()
return data["data"]["repository"]["discussion"]["id"]
def get_label_node_ids(owner: str, repo: str, label_names: list, headers: Dict[str, str]) -> list:
"""Fetch node IDs for label names in a repo."""
query = '''
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
labels(first: 50) {
nodes {
id
name
}
}
}
}
'''
variables = {"owner": owner, "repo": repo}
payload = {"query": query, "variables": variables}
response = requests.post("https://api.github.com/graphql", headers=headers, json=payload)
response.raise_for_status()
data = response.json()
all_labels = data["data"]["repository"]["labels"]["nodes"]
label_ids = [label["id"] for label in all_labels if label["name"] in label_names]
return label_ids
def assign_labels_to_discussion(repo_url: str, discussion_number: int, label_names: list) -> bool:
"""Assign labels to a discussion using GraphQL mutation."""
owner, repo = validate_repo_url(repo_url)
headers = get_auth_headers()
headers["Content-Type"] = "application/json"
try:
discussion_id = get_discussion_node_id(owner, repo, discussion_number, headers)
label_ids = get_label_node_ids(owner, repo, label_names, headers)
if not label_ids:
logger.error(f"No matching label IDs found for: {label_names}")
return False
mutation = '''
mutation($labelableId: ID!, $labelIds: [ID!]!) {
addLabelsToLabelable(input: {labelableId: $labelableId, labelIds: $labelIds}) {
clientMutationId
}
}
'''
variables = {"labelableId": discussion_id, "labelIds": label_ids}
payload = {"query": mutation, "variables": variables}
response = requests.post("https://api.github.com/graphql", headers=headers, json=payload)
response.raise_for_status()
logger.info(f"Successfully labeled discussion #{discussion_number} with {label_names}")
return True
except Exception as e:
logger.error(f"Failed to label discussion #{discussion_number}: {str(e)}")
return False
def label_discussion(repo_url: str, discussion_number: Union[str, int], labels: List[str]) -> bool:
"""Assign labels to a discussion using GraphQL mutation."""
if not labels:
logger.info(f"No labels for #{discussion_number}, skipping.")
return False
try:
return assign_labels_to_discussion(repo_url, int(discussion_number), labels)
except Exception as e:
logger.error(f"Failed to label discussion #{discussion_number}: {str(e)}")
return False
@trace
def process_discussions(repo: str = None) -> None:
"""Process unlabeled discussions and apply labels.
Args:
repo: Repository URL in the format "owner/name" (optional)
"""
if not repo:
repo = DEFAULT_REPO
try:
# Validate GitHub App configuration
validate_github_app_config()
# Fetch unlabeled discussions
discussions = fetch_unlabeled_discussions(repo)
logger.info(f"Found {len(discussions)} unlabeled discussions")
for discussion in discussions:
if not isinstance(discussion, dict):
continue
# Extract data with safe gets
discussion_number = discussion.get("number")
if not discussion_number:
continue
title = discussion.get("title", "")
body = discussion.get("body", "")
# Generate labels
labels = run_with_rag(title, body)
# Apply labels if generated
if labels:
label_discussion(repo, discussion_number, labels)
except GithubAppAuthError as e:
logger.error(str(e))
except Exception as e:
logger.error(f"Error processing discussions: {str(e)}")
def main() -> None:
"""Main entry point for the application."""
try:
# Initial run
logger.info("Performing initial run...")
process_discussions()
# Schedule periodic runs
run_interval = int(os.getenv("RUN_INTERVAL_MINUTES", "1"))
schedule.every(run_interval).minutes.do(process_discussions)
logger.info(f"Agent started: checking for new discussions every {run_interval} minutes.")
while True:
schedule.run_pending()
time.sleep(1)
except KeyboardInterrupt:
logger.info("Application stopped by user.")
except Exception as e:
logger.error(f"Unhandled exception: {str(e)}")
raise
if __name__ == "__main__":
main()