forked from primetheus/fastapi-githubapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (60 loc) · 1.92 KB
/
Copy pathapp.py
File metadata and controls
82 lines (60 loc) · 1.92 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
"""
Basic GitHub App webhook example.
Demonstrates simple webhook event handling for issues.opened and issues.reopened.
"""
from fastapi import FastAPI
from githubapp import GitHubApp
app = FastAPI()
# Initialize config for environment variable loading
app.config = {}
GitHubApp.load_env(app)
# Create GitHubApp instance
github_app = GitHubApp(app)
@app.get("/")
def home():
"""Status endpoint."""
return {
"app": "Basic GitHub App",
"github_app_id": app.config.get("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()
# Add comment and close issue
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=5000, reload=True)