Last active
January 4, 2016 03:19
-
-
Save butonic/8561473 to your computer and use it in GitHub Desktop.
lists all your contributions via github api. including private repositories.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// parameters | |
$username = 'username'; | |
$password = 'secret'; | |
//how many days should be fetched? | |
$days = 7; | |
// functions | |
function addEventToRepo ($event, array &$repoEvents) { | |
if (!isset($repoEvents[$event->repo->name])) { | |
$repoEvents[$event->repo->name] = array(); | |
} | |
switch($event->type) { | |
case 'PushEvent': | |
//print_r($event); | |
if (!isset($repoEvents[$event->repo->name]['commits'])) { | |
$repoEvents[$event->repo->name]['commits'] = array(); | |
} | |
foreach ($event->payload->commits as $commit) { | |
$commit->ref = substr($event->payload->ref,strlen('refs/heads/')); | |
$repoEvents[$event->repo->name]['commits'][] = $commit; | |
} | |
break; | |
case 'PullRequestEvent': | |
//print_r($event->payload->pull_request); | |
if (!isset($repoEvents[$event->repo->name]['pull_requests'])) { | |
$repoEvents[$event->repo->name]['pull_requests'] = array(); | |
} | |
if (!isset($repoEvents[$event->repo->name]['pull_requests'][$event->payload->pull_request->number])) { | |
$repoEvents[$event->repo->name]['pull_requests'][$event->payload->pull_request->number] = array(); | |
} | |
$repoEvents[$event->repo->name]['pull_requests'][$event->payload->pull_request->number] = $event->payload->pull_request; | |
break; | |
case 'IssuesEvent': | |
case 'IssueCommentEvent': | |
if (!isset($repoEvents[$event->repo->name]['issues'])) { | |
$repoEvents[$event->repo->name]['issues'] = array(); | |
} | |
if (!isset($repoEvents[$event->repo->name]['issue'][$event->payload->issue->number])) { | |
$repoEvents[$event->repo->name]['issue'][$event->payload->issue->number] = array(); | |
} | |
$repoEvents[$event->repo->name]['issues'][$event->payload->issue->number] = $event->payload->issue; | |
break; | |
case 'CreateEvent': | |
case 'DeleteEvent': | |
//TODO log? | |
break; | |
default: | |
break; | |
//TODO log? | |
} | |
$repoEvents[$event->repo->name][] = $event; | |
} | |
function fetchEventsPage($page = 1, $username, $base64auth) { | |
$url = 'https://api.github.com/users/'.$username.'/events?page='.$page; | |
$context = array( | |
'http' => array( | |
'method' => 'GET', | |
'ignore_errors' => true, | |
'user_agent' => 'workreport script', | |
'header' => "Accept: application/json\r\n". | |
'Authorization: Basic '.$base64auth."\r\n" | |
) | |
); | |
$stream_context = stream_context_create($context); | |
$result = file_get_contents($url, false, $stream_context); | |
return json_decode($result); | |
} | |
// compute defaults | |
$auth = base64_encode($username.':'.$password); | |
$page = 1; | |
date_default_timezone_set('UTC'); | |
$nowDT = new DateTime(); | |
$repoEvents = array(); | |
// loop pages | |
do { | |
$events = fetchEventsPage($page, $username, $auth); | |
foreach ($events as $event) { | |
$lastDT = new DateTime($event->created_at); | |
$interval = date_diff($lastDT, $nowDT); | |
if ($interval->days < $days) { | |
addEventToRepo($event, $repoEvents); | |
} | |
} | |
$page++; | |
} while ($interval->days < $days); | |
foreach ($repoEvents as $repo) { | |
echo "\n".$repo[0]->repo->name."\n"; | |
if (isset($repo['commits'])) { | |
foreach ($repo['commits'] as $commit) { | |
echo ' '.(isset($commit->ref)?$commit->ref.' ':'').substr($commit->sha,0,8).' '.$commit->message."\n"; | |
} | |
} | |
if (isset($repo['pull_requests'])) { | |
foreach ($repo['pull_requests'] as $pull_request) { | |
echo ' '.$pull_request->base->ref.' PR#'.$pull_request->number.' '.$pull_request->title."\n"; | |
} | |
} | |
if (isset($repo['issues'])) { | |
foreach ($repo['issues'] as $issue) { | |
echo ' #'.$issue->number.' '.$issue->title."\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment