Skip to content

Instantly share code, notes, and snippets.

@dcoppari
Created January 28, 2020 01:40
Show Gist options
  • Save dcoppari/4936dad6d282807aea77a383b17ba9da to your computer and use it in GitHub Desktop.
Save dcoppari/4936dad6d282807aea77a383b17ba9da to your computer and use it in GitHub Desktop.
Get BITBUCKET STATISTICS
<?php
# composer install gentle/bitbucket-api
require dirname(__FILE__) . "/vendor/autoload.php";
$bb_user = 'BITBUCKET_USER';
$bb_pass = 'BITBUCKET_PASS';
$account_name = 'BITBUCKET_ACCOUNT';
header( 'Content-type: text/html; charset=utf-8' );
showlog("<pre>");
$user = new Bitbucket\API\User();
$user->getClient()->addListener( new Bitbucket\API\Http\Listener\BasicAuthListener($bb_user, $bb_pass) );
$response = $user->get();
$repositories = new Bitbucket\API\Repositories();
$repositories->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
$result = new \Bitbucket\API\Http\Response\Pager($repositories->getClient(), $repositories->all($account_name));
$repos = $result->fetchAll();
$repos = json_decode( $repos->getContent(), true );
foreach($repos['values'] as $repo)
{
$repo_slug = $repo['slug'];
if( $repo_slug == 'histrix') continue;
if( strtotime( $repo['updated_on'] ) < strtotime('-60 days') ) continue;
showlog("---------------------- $repo_slug ------------------------------");
$commits = new Bitbucket\API\Repositories\Commits();
$commits->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
$result = new \Bitbucket\API\Http\Response\Pager($commits->getClient(), $commits->all($account_name, $repo_slug, [ 'branch' => 'master' ]) );
$commits = $result->getCurrent();
$commits = json_decode($commits->getContent(), true );
$pullmore = true;
while($pullmore)
{
if($commits === null) break;
foreach( $commits['values'] as $commit)
{
if( strtotime($commit['date']) < strtotime('-60 days') ) {
$pullmore = false;
break;
}
$text = trim($commit['message'],"\n");
if( strpos($text,'Merge branch') !== false ) continue;
$period = date("Y-m", strtotime($commit['date']) );
$fecha = date("Y-m-d", strtotime($commit['date']) );
$user = trim($commit['author']['user']['username']);
if($user == '') $user = trim($commit['author']['raw']);
showlog("$fecha $user $period $text");
$totales['user'][$user][$period][$repo_slug] += 1;
$totales['period'][$user][$period] += 1;
$totales['total'][$user] += 1;
}
if($pullmore !== true) break;
$commits = json_decode($result->fetchNext(), true );
}
}
print_r($totales);
function showlog($text)
{
echo "$text\n";
flush();
ob_flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment