-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_app.py
More file actions
61 lines (51 loc) · 2.02 KB
/
git_app.py
File metadata and controls
61 lines (51 loc) · 2.02 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
# Install dependencies with:
# pip install python-jwt requests python-dotenv
import os
import jwt # pip install python-jwt
import time
import requests # pip install requests
from dotenv import load_dotenv # pip install python-dotenv
# Load environment variables from .env
load_dotenv()
GITHUB_APP_ID = os.getenv("GITHUB_APP_ID")
GITHUB_PRIVATE_KEY_PATH = os.getenv("GITHUB_PRIVATE_KEY_PATH")
GITHUB_INSTALLATION_ID = os.getenv("GITHUB_INSTALLATION_ID")
GITHUB_API_URL = "https://api.github.com"
# Function to generate JWT for GitHub App authentication
def generate_jwt():
with open(GITHUB_PRIVATE_KEY_PATH, "r") as key_file:
private_key = key_file.read()
payload = {
"iat": int(time.time()), # Issued at time
"exp": int(time.time()) + 600, # Expiration time (max 10 min)clear
"iss": GITHUB_APP_ID, # GitHub App ID
}
return jwt.encode(payload, private_key, algorithm="RS256")
# Function to get an installation access token
def get_installation_token():
jwt_token = generate_jwt()
print(f"Generated JWT Token: {jwt_token}") # Debugging line
url = f"{GITHUB_API_URL}/app/installations/{GITHUB_INSTALLATION_ID}/access_tokens"
headers = {
"Authorization": f"Bearer {jwt_token}",
"Accept": "application/vnd.github+json",
}
response = requests.post(url, headers=headers)
response.raise_for_status() # Raise error if request fails
return response.json()["token"]
# Function to list issues from a repository
def list_issues(owner, repo):
token = get_installation_token()
url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/issues"
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github+json",
}
response = requests.get(url, headers=headers)
response.raise_for_status()
issues = response.json()
print(f"Issues in {owner}/{repo}:")
for issue in issues:
print(f"- {issue['title']} ({issue['html_url']})")
if __name__ == "__main__":
list_issues("your_github_username", "your_repository")