-
Notifications
You must be signed in to change notification settings - Fork 3
/
monitor_push_users.py
83 lines (73 loc) · 3 KB
/
monitor_push_users.py
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
import requests
import sqlite3
import json
import time
# Define the SQLite DB name
DB_NAME = '/app/db/github_users.db'
# Read configuration file
with open('config.json', 'r') as f:
config = json.load(f)
ORGANIZATIONS = config['organizations']
API_KEY = config['api_key']
# Create the SQLite DB and the table if they don't exist
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS GithubUsers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
organization TEXT NOT NULL
)
""")
conn.commit()
def monitor_github():
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'token {API_KEY}'
}
for org in ORGANIZATIONS:
url = f'https://api.github.com/orgs/{org}/events'
response = requests.get(url, headers=headers)
if response.status_code == 200:
events = response.json()
for event in events:
if event['type'] == 'PushEvent':
username = event['actor']['login']
try:
cursor.execute("""
INSERT INTO GithubUsers (username, organization)
VALUES (?, ?)
""", (username, org))
conn.commit()
print(f'Successfully inserted {username} from {org}')
except sqlite3.IntegrityError:
print(f'User {username} from {org} already exists in the database')
monitor_user_commits(username, headers)
else:
print(f'Failed to retrieve events for {org}. Status code: {response.status_code}')
# GitHub API has a rate limit, adjust accordingly
time.sleep(1)
def monitor_user_commits(username, headers):
url = f'https://api.github.com/users/{username}/events'
response = requests.get(url, headers=headers)
if response.status_code == 200:
events = response.json()
for event in events:
if event['type'] == 'PushEvent':
for commit in event['payload']['commits']:
repo_name = event['repo']['name']
sha = commit['sha']
commit_url = f'https://api.github.com/repos/{repo_name}/commits/{sha}'
commit_response = requests.get(commit_url, headers=headers)
if commit_response.status_code == 200:
commit_content = commit_response.json()
print(f'Commit by {username}: {commit_content}') # print the entire commit content
else:
print(f'Failed to retrieve commit content for {username}. Status code: {commit_response.status_code}')
else:
print(f'Failed to retrieve events for {username}. Status code: {response.status_code}')
if __name__ == "__main__":
while True:
monitor_github()
# Delay between checks, adjust as needed
time.sleep(60)