Skip to content

Commit

Permalink
Merge pull request #177 from Astrotomic/dev
Browse files Browse the repository at this point in the history
add output to github commands
  • Loading branch information
Gummibeer authored Aug 20, 2024
2 parents 9c3784a + 30936f7 commit 5a88e54
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 49 deletions.
51 changes: 51 additions & 0 deletions app/Console/Commands/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Console\Commands;

use Closure;
use Illuminate\Console\Command as IlluminateCommand;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\LazyCollection;

abstract class Command extends IlluminateCommand
{
/**
* @param int|iterable|Builder $totalSteps
*/
public function withProgressBar($totalSteps, Closure $callback): void
{
if ($totalSteps instanceof Builder) {
$query = $totalSteps;

$this->output->progressStart($query->count());
$query->eachById(function (Model $value) use ($callback): void {
$callback($value);
$this->output->progressAdvance();
});
$this->output->progressFinish();

return;
}

if (is_iterable($totalSteps)) {
$this->output->progressStart(count($totalSteps));
LazyCollection::make($totalSteps)->each(function (mixed $value) use ($callback): void {
$callback($value);
$this->output->progressAdvance();
});
$this->output->progressFinish();

return;
}

$this->output->progressStart($totalSteps);

LazyCollection::range(1, $totalSteps)->each(function (int $value) use ($callback): void {
$callback($value);
$this->output->progressAdvance();
});

$this->output->progressFinish();
}
}
12 changes: 6 additions & 6 deletions app/Console/Commands/GithubOrganizationDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Jobs\UpdateOrganizationDetails;
use App\Models\Organization;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

class GithubOrganizationDetails extends Command
Expand All @@ -15,14 +14,15 @@ class GithubOrganizationDetails extends Command

public function handle(): void
{
Organization::query()
$query = Organization::query()
->whereHas('members', fn (Builder $query) => $query->whereIsRegistered())
->when(
$this->argument('name'),
fn (Builder $query, string $name) => $query->where('name', $name)
)
->each(static function (Organization $organization): void {
UpdateOrganizationDetails::dispatch($organization);
});
);

$this->withProgressBar($query, static function (Organization $organization): void {
UpdateOrganizationDetails::dispatch($organization);
});
}
}
12 changes: 6 additions & 6 deletions app/Console/Commands/GithubOrganizationRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Jobs\LoadOrganizationRepositories;
use App\Models\Organization;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

class GithubOrganizationRepositories extends Command
Expand All @@ -15,14 +14,15 @@ class GithubOrganizationRepositories extends Command

public function handle(): void
{
Organization::query()
$query = Organization::query()
->whereHas('members', fn (Builder $query) => $query->whereIsRegistered())
->when(
$this->argument('name'),
fn (Builder $query, string $name) => $query->where('name', $name)
)
->each(static function (Organization $organization): void {
LoadOrganizationRepositories::dispatch($organization);
});
);

$this->withProgressBar($query, static function (Organization $organization): void {
LoadOrganizationRepositories::dispatch($organization);
});
}
}
27 changes: 14 additions & 13 deletions app/Console/Commands/GithubRepositoryDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Models\Organization;
use App\Models\Repository;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

class GithubRepositoryDetails extends Command
Expand All @@ -17,21 +16,23 @@ class GithubRepositoryDetails extends Command

public function handle(): void
{
Repository::query()
$query = Repository::query()
->when(
$this->argument('name'),
fn (Builder $query, string $name) => $query->where('name', $name)
)
->with('owner')
->get()
->reject(function (Repository $repository): bool {
return $repository->owner instanceof User && $repository->owner->github_access_token === null;
})
->reject(function (Repository $repository): bool {
return $repository->owner instanceof Organization && $repository->owner->members()->whereIsRegistered()->doesntExist();
})
->each(static function (Repository $repository): void {
UpdateRepositoryDetails::dispatch($repository);
});
->with('owner');

$this->withProgressBar($query, static function (Repository $repository): void {
if ($repository->owner instanceof User && $repository->owner->github_access_token === null) {
return;
}

if ($repository->owner instanceof Organization && $repository->owner->members()->whereIsRegistered()->doesntExist()) {
return;
}

UpdateRepositoryDetails::dispatch($repository);
});
}
}
12 changes: 6 additions & 6 deletions app/Console/Commands/GithubUserContributions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Jobs\SyncUserContributions;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

class GithubUserContributions extends Command
Expand All @@ -15,14 +14,15 @@ class GithubUserContributions extends Command

public function handle(): void
{
User::query()
$query = User::query()
->whereIsRegistered()
->when(
$this->argument('name'),
fn (Builder $query, string $name) => $query->where('name', $name)
)
->each(static function (User $user): void {
SyncUserContributions::dispatch($user);
});
);

$this->withProgressBar($query, static function (User $user): void {
SyncUserContributions::dispatch($user);
});
}
}
12 changes: 6 additions & 6 deletions app/Console/Commands/GithubUserDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Jobs\UpdateUserDetails;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

class GithubUserDetails extends Command
Expand All @@ -15,14 +14,15 @@ class GithubUserDetails extends Command

public function handle(): void
{
User::query()
$query = User::query()
->whereIsRegistered()
->when(
$this->argument('name'),
fn (Builder $query, string $name) => $query->where('name', $name)
)
->each(static function (User $user): void {
UpdateUserDetails::dispatch($user);
});
);

$this->withProgressBar($query, static function (User $user): void {
UpdateUserDetails::dispatch($user);
});
}
}
12 changes: 6 additions & 6 deletions app/Console/Commands/GithubUserOrganizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Jobs\SyncUserOrganizations;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

class GithubUserOrganizations extends Command
Expand All @@ -15,14 +14,15 @@ class GithubUserOrganizations extends Command

public function handle(): void
{
User::query()
$query = User::query()
->whereIsRegistered()
->when(
$this->argument('name'),
fn (Builder $query, string $name) => $query->where('name', $name)
)
->each(static function (User $user): void {
SyncUserOrganizations::dispatch($user);
});
);

$this->withProgressBar($query, static function (User $user): void {
SyncUserOrganizations::dispatch($user);
});
}
}
12 changes: 6 additions & 6 deletions app/Console/Commands/GithubUserRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Jobs\LoadUserRepositories;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

class GithubUserRepositories extends Command
Expand All @@ -15,14 +14,15 @@ class GithubUserRepositories extends Command

public function handle(): void
{
User::query()
$query = User::query()
->whereIsRegistered()
->when(
$this->argument('name'),
fn (Builder $query, string $name) => $query->where('name', $name)
)
->each(static function (User $use): void {
LoadUserRepositories::dispatch($use);
});
);

$this->withProgressBar($query, static function (User $use): void {
LoadUserRepositories::dispatch($use);
});
}
}

0 comments on commit 5a88e54

Please sign in to comment.