-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-status-stats.php
73 lines (62 loc) · 1.74 KB
/
github-status-stats.php
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
<?php
/**
* @file
* Statistics for your commits based on automated checks like Travis.
*/
use Github\Client;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;
require_once __DIR__ . '/vendor/autoload.php';
$username = isset($argv[1]) ? $argv[1] : NULL;
if (empty($username)) {
exit(1);
}
$client = new Client();
// Activate caching.
$cache_target = __DIR__ . '/.github-cache/';
if (!is_dir($cache_target)) {
mkdir($cache_target);
}
$filesystemAdapter = new Local($cache_target);
$filesystem = new Filesystem($filesystemAdapter);
$pool = new FilesystemCachePool($filesystem);
$client->addCache($pool);
// Optionally authenticate.
$token_path = __DIR__ . '/.github-token';
if (is_file($token_path)) {
$token = file_get_contents($token_path);
if (!empty($token)) {
$client->authenticate($token, NULL, $client::AUTH_HTTP_TOKEN);
}
}
$events = $client->api('user')->publicEvents($username);
$stats = [];
foreach ($events as $event) {
if ($event['type'] != 'PushEvent') {
continue;
}
$timestamp = strtotime($event['created_at']);
$hour_of_day = date('G', $timestamp);
if (!isset($stats[$hour_of_day])) {
$stats[$hour_of_day] = [];
}
list($owner, $repo) = explode('/', $event['repo']['name']);
$commits = $event['payload']['commits'];
foreach ($commits as $commit) {
$statuses = $client->api('repo')
->statuses()
->show($owner, $repo, $commit['sha']);
foreach ($statuses as $status) {
if ($status['state'] == 'pending') {
continue;
}
$counter = &$stats[$hour_of_day][$status['state']];
if (!isset($counter)) {
$counter = 0;
}
$counter++;
}
}
}
print json_encode($stats, JSON_PRETTY_PRINT);