Initial commit

This commit is contained in:
Ismo Vuorinen
2022-10-17 09:42:32 +03:00
commit 78cca41be4
27 changed files with 9319 additions and 0 deletions

16
.editorconfig Normal file
View File

@@ -0,0 +1,16 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.yml]
indent_style = space
indent_size = 2

7
.gitattributes vendored Normal file
View File

@@ -0,0 +1,7 @@
* text=auto
/.github export-ignore
.styleci.yml export-ignore
.scrutinizer.yml export-ignore
BACKERS.md export-ignore
CONTRIBUTING.md export-ignore
CHANGELOG.md export-ignore

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
/vendor
/.idea
/.vscode
/.vagrant
.phpunit.result.cache

23
README.md Normal file
View File

@@ -0,0 +1,23 @@
<h1 style="text-align:center">
Branch usage checker
</h1>
<p style="text-align:center">
<a href="https://packagist.org/packages/ivuorinen/branch-usage-checker"><img src="https://img.shields.io/packagist/v/ivuorinen/branch-usage-checker.svg?label=stable" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/ivuorinen/branch-usage-checker"><img src="https://img.shields.io/packagist/l/ivuorinen/branch-usage-checker.svg" alt="License"></a>
</p>
<h2 style="text-align:center">
Check when your package branches have last been used.
</h2>
<p style="text-align:center">
Use this command line tool to cross-check project
public GitHub Branches and Packagist branch
download statistics to determine are branches
safe to delete.
</p>
## License
Branch usage checker is an open-source software licensed under the MIT license.

0
app/Commands/.gitkeep Normal file
View File

View File

@@ -0,0 +1,155 @@
<?php
namespace App\Commands;
use App\Dto\PackagistApiPackagePayload;
use Illuminate\Support\Facades\Http;
use LaravelZero\Framework\Commands\Command;
class CheckCommand extends Command {
protected $signature = 'check
{vendor : Package vendor (required)}
{package : Package name (required)}
{months=9 : How many months should we return for review (optional)}
';
protected $description = 'Check package branch usage';
private string $vendor = '';
private string $package = '';
private string $filter = '';
private int $total_branches = 0;
public function handle() : int {
$this->vendor = (string) $this->argument( 'vendor' );
$this->package = (string) $this->argument( 'package' );
$months = (int) $this->argument( 'months' );
$this->info( 'Checking: ' . sprintf( '%s/%s', $this->vendor, $this->package ) );
$this->info( 'Months: ' . $months );
$payload = Http::get( sprintf(
'https://packagist.org/packages/%s/%s.json',
$this->vendor,
$this->package
) );
$this->filter = now()->subMonths( $months )->day( 1 )->toDateString();
try {
$pkg = new PackagistApiPackagePayload( $payload->json() );
$this->info( 'Found the package. Type: ' . $pkg->type );
$versions = collect( $pkg->versions ?? [] )
->keys()
// Filter actual versions out.
->filter( fn( $version ) => \str_starts_with( $version, 'dev-' ) )
->sort();
$this->total_branches = $versions->count();
$this->info( sprintf(
'Package has %d branches. Starting to download statistics.',
$this->total_branches
) );
$statistics = collect( $versions )
->mapWithKeys( fn( $branch ) => $this->get_statistics( $branch ) )
->toArray();
$this->info( 'Downloaded statistics...' );
$this->output_table( $statistics );
$this->output_suggestions( $statistics );
}
catch ( \Exception $e ) {
$this->error( $e->getMessage(), $e );
}
return 0;
}
private function get_statistics( $branch ) : array {
$payload = Http::get( sprintf(
'https://packagist.org/packages/%s/%s/stats/%s.json?average=monthly&from=%s',
$this->vendor,
$this->package,
$branch,
$this->filter
) );
$data = collect( $payload->json() );
$labels = collect( $data->get( 'labels', [] ) )->toArray();
$values = collect( $data->get( 'values', [] ) )->flatten()->toArray();
$labels[] = 'Total';
$values[] = array_sum( $values );
return [ $branch => \array_combine( $labels, $values ) ];
}
private function output_table( array $statistics ) : void {
if ( empty( $statistics ) ) {
$this->info( 'No statistics found... Stopping.' );
exit( 0 );
}
$tableHeaders = [ '' => 'Branch' ];
$tableBranches = [];
foreach ( $statistics as $branch => $stats ) {
foreach ( $stats as $m => $v ) {
$tableHeaders[ $m ] = (string) $m;
$tableBranches[ $branch ][ $branch ] = $branch;
$tableBranches[ $branch ][ $m ] = (string) $v;
}
}
$this->line('');
$this->table( $tableHeaders, $tableBranches );
}
private function output_suggestions( array $statistics = [] ) : void {
$deletable = [];
if ( empty( $statistics ) ) {
$this->info( 'No statistics to give suggestions for. Quitting...' );
exit( 0 );
}
foreach ( $statistics as $k => $values ) {
if ( ! empty( $values['Total'] ) ) {
continue;
}
$deletable[ $k ] = $values['Total'];
}
if ( empty( $deletable ) ) {
$this->info( 'No suggestions available. Good job!' );
exit( 0 );
}
$keys = array_keys( $deletable );
$branches = collect( $keys )->mapWithKeys( function ( $branch ) {
return [
$branch => [
$branch,
sprintf(
'https://packagist.org/packages/%s/%s#%s',
$this->vendor,
$this->package,
$branch
),
],
];
} );
$this->line('');
$this->info( sprintf(
'Found %d branches (out of %d total) with no downloads since %s',
$branches->count(),
$this->total_branches,
$this->filter
) );
$this->table( [ 'Branch', 'URL' ], $branches );
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use function Termwind\{render};
class InspireCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'inspire {name=Artisan}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Display an inspiring quote';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
render(<<<'HTML'
<div class="py-1 ml-2">
<div class="px-1 bg-blue-300 text-black">Laravel Zero</div>
<em class="ml-1">
Simplicity is the ultimate sophistication.
</em>
</div>
HTML);
}
/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule)
{
// $schedule->command(static::class)->everyMinute();
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Dto;
use Spatie\DataTransferObject\DataTransferObject;
class GitHubApiBranch extends DataTransferObject {
public string $name;
public bool $protected;
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Dto;
use Spatie\DataTransferObject\Attributes\MapFrom;
class PackagistApiPackagePayload extends \Spatie\DataTransferObject\DataTransferObject {
#[MapFrom('package.name')]
public string $name = '';
#[MapFrom('package.description')]
public string $description = '';
#[MapFrom('package.time')]
public string $time = '';
#[MapFrom('package.versions')]
public array $versions = [];
#[MapFrom('package.type')]
public string $type = '';
#[MapFrom('package.repository')]
public string $repository = '';
#[MapFrom('package.language')]
public string $language = '';
}

View File

@@ -0,0 +1,14 @@
<?php
namespace App\Dto;
use Spatie\DataTransferObject\Attributes\MapFrom;
class PackagistApiStatsPayload extends \Spatie\DataTransferObject\DataTransferObject {
public array $labels;
#[MapFrom('values.[0]')]
public string $version;
#[MapFrom('values.[0][]')]
public array $values;
public string $average = 'monthly';
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Fetchers;
use Illuminate\Support\Facades\Http;
class GitHubRestApi {
public static function getBranches( string $vendor, string $package ) : array {
$pages = self::downloader( $vendor, $package );
$pages = \collect($pages)
->flatten(1)
->toArray();
return $pages;
}
public static function downloader( $vendor, $package ) : array {
$responses = [];
$continue = true;
$page = 1;
$gh_api = sprintf(
'https://api.github.com/repos/%s/%s/branches?per_page=100',
$vendor,
$package
);
while ( $continue ) {
$response = Http::get( $gh_api . '&page=' . $page );
if ( empty( $response ) ) {
$continue = false;
}
$responses[ $page ] = $response;
$page ++;
}
return $responses;
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

50
bootstrap/app.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new LaravelZero\Framework\Application(
dirname(__DIR__)
);
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
LaravelZero\Framework\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
Illuminate\Foundation\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;

19
box.json Normal file
View File

@@ -0,0 +1,19 @@
{
"chmod": "0755",
"directories": [
"app",
"bootstrap",
"config",
"vendor"
],
"files": [
"composer.json"
],
"exclude-composer-files": false,
"exclude-dev-files": false,
"compression": "GZ",
"compactors": [
"KevinGH\\Box\\Compactor\\Php",
"KevinGH\\Box\\Compactor\\Json"
]
}

53
branch-usage-checker Executable file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
$autoloader = require file_exists(__DIR__.'/vendor/autoload.php') ? __DIR__.'/vendor/autoload.php' : __DIR__.'/../../autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);

BIN
builds/branch-usage-checker Executable file

Binary file not shown.

76
composer.json Normal file
View File

@@ -0,0 +1,76 @@
{
"name": "ivuorinen/branch-usage-checker",
"description": "GitHub Project branch usage checker.",
"license": "MIT",
"type": "project",
"keywords": [
"branch",
"usage",
"github",
"console",
"cli"
],
"authors": [
{
"name": "Ismo Vuorinen",
"email": "ismo@ivuorinen.net"
}
],
"homepage": "https://github.com/ivuorinen/branch-usage-checker",
"support": {
"issues": "https://github.com/ivuorinen/branch-usage-checker/issues",
"source": "https://github.com/ivuorinen/branch-usage-checker"
},
"require": {
"php": ">=8.0.2",
"guzzlehttp/guzzle": "^7.4",
"illuminate/http": "^9.0",
"laravel-zero/phar-updater": "^1.2",
"nunomaduro/termwind": "^1.3",
"spatie/data-transfer-object": "^3.7"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.23",
"laravel-zero/framework": "^9.0",
"mockery/mockery": "^1.4.4",
"pestphp/pest": "^1.21.1",
"roave/security-advisories": "dev-latest"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"bin": [
"builds/branch-usage-checker"
],
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true,
"ergebnis/composer-normalize": true,
"dealerdirect/phpcodesniffer-composer-installer": true
},
"optimize-autoloader": true,
"platform": {
"php": "8.0.2"
},
"preferred-install": "dist",
"sort-packages": true
},
"scripts": {
"post-autoload-dump": [
"composer normalize"
],
"build": "php branch-usage-checker app:build branch-usage-checker",
"x": "@php branch-usage-checker"
}
}

8493
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

60
config/app.php Normal file
View File

@@ -0,0 +1,60 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Application Name
|--------------------------------------------------------------------------
|
| This value is the name of your application. This value is used when the
| framework needs to place the application's name in a notification or
| any other location as required by the application or its packages.
|
*/
'name' => 'Branch usage checker',
/*
|--------------------------------------------------------------------------
| Application Version
|--------------------------------------------------------------------------
|
| This value determines the "version" your application is currently running
| in. You may want to follow the "Semantic Versioning" - Given a version
| number MAJOR.MINOR.PATCH when an update happens: https://semver.org.
|
*/
'version' => app('git.version'),
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services the application utilizes. This can be overridden using
| the global command line "--env" option when calling commands.
|
*/
'env' => 'development',
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
'providers' => [
App\Providers\AppServiceProvider::class,
],
];

77
config/commands.php Normal file
View File

@@ -0,0 +1,77 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Command
|--------------------------------------------------------------------------
|
| Laravel Zero will always run the command specified below when no command name is
| provided. Consider update the default command for single command applications.
| You cannot pass arguments to the default command because they are ignored.
|
*/
'default' => NunoMaduro\LaravelConsoleSummary\SummaryCommand::class,
/*
|--------------------------------------------------------------------------
| Commands Paths
|--------------------------------------------------------------------------
|
| This value determines the "paths" that should be loaded by the console's
| kernel. Foreach "path" present on the array provided below the kernel
| will extract all "Illuminate\Console\Command" based class commands.
|
*/
'paths' => [ app_path( 'Commands' ) ],
/*
|--------------------------------------------------------------------------
| Added Commands
|--------------------------------------------------------------------------
|
| You may want to include a single command class without having to load an
| entire folder. Here you can specify which commands should be added to
| your list of commands. The console's kernel will try to load them.
|
*/
'add' => [
// ..
],
/*
|--------------------------------------------------------------------------
| Hidden Commands
|--------------------------------------------------------------------------
|
| Your application commands will always be visible on the application list
| of commands. But you can still make them "hidden" specifying an array
| of commands below. All "hidden" commands can still be run/executed.
|
*/
'hidden' => [
NunoMaduro\LaravelConsoleSummary\SummaryCommand::class,
Symfony\Component\Console\Command\DumpCompletionCommand::class,
Symfony\Component\Console\Command\HelpCommand::class,
LaravelZero\Framework\Commands\StubPublishCommand::class,
],
/*
|--------------------------------------------------------------------------
| Removed Commands
|--------------------------------------------------------------------------
|
| Do you have a service provider that loads a list of commands that
| you don't need? No problem. Laravel Zero allows you to specify
| below a list of commands that you don't to see in your app.
|
*/
'remove' => [
\App\Commands\InspireCommand::class,
Illuminate\Console\Scheduling\ScheduleRunCommand::class,
Illuminate\Console\Scheduling\ScheduleListCommand::class,
Illuminate\Console\Scheduling\ScheduleFinishCommand::class,
],
];

24
phpunit.xml.dist Normal file
View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
</phpunit>

View File

@@ -0,0 +1,22 @@
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}

View File

@@ -0,0 +1,7 @@
<?php
test('inspire command', function () {
$this->artisan('inspire')
// ->expectsOutput('')
->assertExitCode(0);
});

View File

@@ -0,0 +1,5 @@
<?php
it('inspire artisans', function () {
$this->artisan('inspire')->assertExitCode(0);
});

45
tests/Pest.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/
uses(Tests\TestCase::class)->in('Feature');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
function something()
{
// ..
}

10
tests/TestCase.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace Tests;
use LaravelZero\Framework\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}

View File

@@ -0,0 +1,5 @@
<?php
test('example', function () {
expect(true)->toBeTrue();
});