-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconstructor.py
More file actions
93 lines (69 loc) · 2.41 KB
/
Copy pathconstructor.py
File metadata and controls
93 lines (69 loc) · 2.41 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
"""
Configuration examples - Constructor Parameters (Alternative).
Demonstrates configuration using constructor parameters.
Less flexible than environment variables but useful in some scenarios.
"""
from fastapi import FastAPI
from githubapp import GitHubApp
import os
def _env_bytes(name: str):
"""Helper to convert environment variable to bytes if needed."""
val = os.getenv(name)
return val.encode() if isinstance(val, str) else val
app = FastAPI()
# Constructor Parameters approach
github_app = GitHubApp(
app,
github_app_id=int(os.getenv("GITHUBAPP_ID", "0")) or None,
github_app_key=_env_bytes("GITHUBAPP_PRIVATE_KEY"),
github_app_secret=_env_bytes("GITHUBAPP_WEBHOOK_SECRET"),
github_app_route=os.getenv("GITHUBAPP_WEBHOOK_PATH", "/webhooks/github/"),
)
@app.get("/")
def home():
"""Configuration status."""
return {
"app": "Configuration Examples - Constructor Parameters",
"configuration_method": "constructor_parameters",
"github_app_id": os.getenv("GITHUBAPP_ID"),
"status": "ready",
}
@app.get("/health")
def health():
"""Health check endpoint."""
return {"status": "ok"}
@github_app.on("issues.opened")
def close_new_issue():
"""Automatically close newly opened issues."""
owner = github_app.payload["repository"]["owner"]["login"]
repo = github_app.payload["repository"]["name"]
issue_number = github_app.payload["issue"]["number"]
client = github_app.client()
client.issues.create_comment(
owner=owner,
repo=repo,
issue_number=issue_number,
body="You've got an issue? I've got a solution! Closing 😈",
)
client.issues.update(
owner=owner, repo=repo, issue_number=issue_number, state="closed"
)
@github_app.on("issues.reopened")
def close_reopened_issue():
"""Close reopened issues."""
owner = github_app.payload["repository"]["owner"]["login"]
repo = github_app.payload["repository"]["name"]
issue_number = github_app.payload["issue"]["number"]
client = github_app.client()
client.issues.create_comment(
owner=owner,
repo=repo,
issue_number=issue_number,
body="Nice try! Closing again. 😈",
)
client.issues.update(
owner=owner, repo=repo, issue_number=issue_number, state="closed"
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)