-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-activity.php
More file actions
238 lines (204 loc) · 7.13 KB
/
github-activity.php
File metadata and controls
238 lines (204 loc) · 7.13 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
<?php
$opts = [
'http' => [
'method'=>"GET",
// Use CRLF \r\n to separate multiple headers
'header' => "Accept: application/json\r\n" .
"X-Github-Api-Version: 2022-11-28\r\n" .
"User-Agent: Github-User-Activity\r\n"
]
];
$context = stream_context_create($opts);
if ($argc < 2) {
echo "GitHub User Activity Guide\n" . PHP_EOL;
echo "Usage: php github-activity.php <username>\n" . PHP_EOL;
exit(1);
}
$username = $argv[1];
$url = "https://api.github.com/users/{$username}/events";
// Open the file using HTTP headers above
// Use @ to suppress the default PHP warning
$jsonData = @file_get_contents($url, false, $context);
if(!$jsonData) {
if (!isset($http_response_header)) {
echo "Error: Network connection failure. Please check your internet connection." . PHP_EOL;
exit(1);
}
$statusLine = $http_response_header[0] ?? '';
preg_match('{HTTP\/\d\.\d\s+(\d{3})}', $statusLine, $matches);
$statusCode = $matches[1] ?? 'Unknown';
switch ($statusCode) {
case '404':
echo "Error: User '$username' not found on GitHub." . PHP_EOL;
break;
case '403':
echo "Error: API Rate limit exceeded. Try again later or use a token." . PHP_EOL;
break;
case '500':
case '502':
case '503':
echo "Error: GitHub servers are currently having trouble. Try again soon." . PHP_EOL;
break;
default:
echo "Error: Failed to fetch data. (HTTP Status: $statusCode)" . PHP_EOL;
break;
}
exit(1);
}
$events = json_decode($jsonData, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "Error: Failed to parse GitHub response." . PHP_EOL;
exit(1);
}
if(empty($events)) {
echo "No recent activity found for this user." . PHP_EOL;
exit;
}
foreach ($events as $event) {
$repoName = $event['repo']['name'];
$type = $event['type'];
$payload = $event['payload'];
switch ($type) {
case 'CommitCommentEvent':
handleCommitCommentEvent($event, $payload);
break;
case 'CreateEvent':
case 'DeleteEvent':
handleCreateDeleteEvent($event, $payload);
break;
case 'DiscussionEvent':
handleDiscussionEvent($event, $payload);
break;
case 'ForkEvent':
handleForkEvent($payload);
break;
case 'GollumEvent':
handleGollumEvent($event, $payload);
break;
case 'IssueCommentEvent':
handleIssueCommentEvent($event);
break;
case 'IssuesEvent':
handleIssueEvent($event, $payload);
break;
case 'MemberEvent':
handleMemberEvent($event, $payload);
break;
case 'PublicEvent':
handlePublicEvent($event);
break;
case 'PullRequestEvent':
handlePullRequestEvent($event, $payload);
break;
case 'PullRequestReviewEvent':
handlePullRequestReviewEvent($event, $payload);
break;
case 'PullRequestReviewCommentEvent':
handlePullRequestReviewCommentEvent($event, $payload);
break;
case 'PushEvent':
handlePushEvent($event, $payload);
break;
case 'ReleaseEvent':
handleReleaseEvent($event, $payload);
break;
case 'WatchEvent':
handleWatchEvent($event);
break;
default:
$cleanType = str_replace("Event", "", $type);
echo "- $cleanType activity in $repoName" . PHP_EOL;
break;
}
}
function handleCommitCommentEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
echo "- $action commit comment in $repoName" . PHP_EOL;
}
function handleCreateDeleteEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$type = ($event['type'] === 'CreateEvent') ? 'Created' : 'Deleted';
$ref_type = $payload['ref_type'];
$full_ref = $payload['full_ref'];
$refs = "refs/heads/";
$full_ref = str_replace($refs, "", $full_ref);
if (is_null($ref_type)) {
$ref_type = "";
}
if ($type == 'Created') {
$start = "Created a new";
} else {
$start = "Deleted a";
}
echo "- $start $ref_type named $full_ref in $repoName" . PHP_EOL;
}
function handleDiscussionEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
echo "- $action a discussion in $repoName" . PHP_EOL;
}
function handleForkEvent(array $payload) {
$forkedRepo = $payload['forkee']['full_name'] ?? $payload['forkee']['name'] ?? "a repository";
echo "- Forked $forkedRepo" . PHP_EOL;
}
function handleGollumEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$gollumCount = count($payload['pages']);
echo "- Updated $gollumCount pages in $repoName" . PHP_EOL;
}
function handleIssueCommentEvent(array $event) {
$repoName = $event['repo']['name'];
echo "- Created a comment of an issue in $repoName" . PHP_EOL;
}
function handleIssueEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
if ($action == "Opened") {
echo "- $action a new issue in $repoName" . PHP_EOL;
} else {
echo "- $action an issue in $repoName" . PHP_EOL;
}
}
function handleMemberEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
echo "- $action a new member in $repoName" . PHP_EOL;
}
function handlePublicEvent(array $event) {
$repoName = $event['repo']['name'];
echo "- Made $repoName public" . PHP_EOL;
}
function handlePullRequestEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
echo "- $action a pull request in $repoName" . PHP_EOL;
}
function handlePullRequestReviewEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
echo "- $action a pull request review in $repoName" . PHP_EOL;
}
function handlePullRequestReviewCommentEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
echo "- $action a pull request review comment in $repoName" . PHP_EOL;
}
function handlePushEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$commits = $payload['commits'] ?? [];
$pullCount = count($commits);
if ($pullCount > 0) {
$word = ($pullCount === 1) ? "commit" : "commits";
echo "- Pushed $pullCount $word to $repoName" . PHP_EOL;
}
}
function handleReleaseEvent(array $event, array $payload) {
$repoName = $event['repo']['name'];
$action = ucfirst($payload['action']);
echo "- $action $repoName" . PHP_EOL;
}
function handleWatchEvent(array $event) {
$repoName = $event['repo']['name'];
echo "- Starred $repoName" . PHP_EOL;
}