Skip to content

Instantly share code, notes, and snippets.

@codyphobe
Forked from mpociot/FetchGitHubTotals.php
Created May 29, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyphobe/f0030f56167c5250eeb9ddb13cf3b6de to your computer and use it in GitHub Desktop.
Save codyphobe/f0030f56167c5250eeb9ddb13cf3b6de to your computer and use it in GitHub Desktop.
GitHub component for the excellent Spatie dashboard package from https://github.com/spatie/dashboard.spatie.be
<?php
namespace App\Components\GitHub;
use App\Events\GitHub\FileContentFetched;
use App\Events\GitHub\TotalsFetched;
use GitHub;
use Illuminate\Console\Command;
class FetchGitHubTotals extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'dashboard:github-totals';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fetch GitHub forks, issues, PRs.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$username = config('services.github.username');
$repos = collect(Github::user()->repositories($username));
$forks = $repos->sum('forks_count');
$issues = $repos->sum('open_issues');
$pullRequests = $repos->sum(function($repo) use ($username) {
return count(Github::pull_request()->all($username, $repo['name']));
});
$contributors = $repos->sum(function($repo) use ($username) {
return count(Github::repo()->contributors($username, $repo['name']));
});
event(new TotalsFetched([
'issues' => $issues,
'forks' => $forks,
'pullRequests' => $pullRequests,
'contributors' => $contributors
]));
}
}
<template>
<grid :position="grid" modifiers="padded overflow blue">
<section class="packagist-statistics">
<h1>GitHub</h1>
<ul>
<li class="packagist-statistic">
<span class="packagist-statistics__period">Issues</span>
<span class="packagist-statistics__count">{{ formatNumber(issues) }}</span>
</li>
<li class="packagist-statistic">
<h2 class="packagist-statistics__period">Pull Requests</h2>
<span class="packagist-statistics__count">{{ formatNumber(pullRequests) }}</span>
</li>
<li class="packagist-statistic">
<h2 class="packagist-statistics__period">Forks</h2>
<span class="packagist-statistics__count">{{ formatNumber(forks) }}</span>
</li>
<li class="packagist-statistic">
<h2 class="packagist-statistics__period">Contributors</h2>
<span class="packagist-statistics__count">{{ formatNumber(contributors) }}</span>
</li>
</ul>
</section>
</grid>
</template>
<script>
import { formatNumber } from '../helpers';
import echo from '../mixins/echo';
import Grid from './Grid';
import saveState from 'vue-save-state';
export default {
components: {
Grid,
},
mixins: [echo, saveState],
props: ['grid'],
data() {
return {
issues: 0,
contributors: 0,
pullRequests: 0,
forks: 0
};
},
methods: {
formatNumber,
getEventHandlers() {
return {
'GitHub.TotalsFetched': response => {
this.issues = response.issues;
this.contributors = response.contributors;
this.pullRequests = response.pullRequests;
this.forks = response.forks;
},
};
},
getSaveStateConfig() {
return {
cacheKey: `github-statistics`,
};
},
},
};
</script>
<?php
namespace App\Events\GitHub;
use App\Events\DashboardEvent;
class TotalsFetched extends DashboardEvent
{
public $issues;
public $pullRequests;
public $forks;
public $contributors;
public function __construct(array $totals)
{
foreach ($totals as $sumName => $total) {
$this->$sumName = $total;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment