Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions app/Console/Commands/FroozeRepositories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Console\Commands;

use App\Models\FrozenRepository;
use App\Models\GithubApi;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class FroozeRepositories extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:frooze-repositories';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Load and frooze oldest and starred repositories';

/**
* Execute the console command.
*/
public function handle() {
$client = new GithubApi();
$repositories = $client->getOldestStarredRepositories();

FrozenRepository::truncate();

FrozenRepository::insert(
array_map(
function ($rep) {
return array(
'created_at' => Carbon::now(),
'name' => $rep->name,
'githubId' => $rep->id,
'fullName' => $rep->fullName,
'description' => $rep->description,
'url' => $rep->url,
'githubCreatedAt' => $rep->createdAt,
'githubUpdatedAt' => $rep->updatedAt,
'language' => $rep->language,
'topics' => json_encode($rep->topics),
'watchers' => $rep->watchers,
'forks' => $rep->forks,
'stars' => $rep->stars,
'ownerIsOrganization' => $rep->ownerIsOrganization,
'ownerLogin' => $rep->owner->login,
'ownerGithubId' => $rep->owner->id,
'ownerAvatarUrl' => $rep->owner->avatarUrl,
'year' => $rep->createdAt->format('Y'),
);
}, $repositories)
);
}
}
4 changes: 4 additions & 0 deletions app/Console/Commands/LoadProLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ private function getLanguages(int $page) {
* Execute the console command.
*/
public function handle() {
Log::info('action=load_prolang_command, status=started');

$this->getLanguages(1);

Log::info('action=load_prolang_command, status=finished');
}
}
5 changes: 5 additions & 0 deletions app/Console/Commands/ProLangAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\ProLang;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class ProLangAssets extends Command {
/**
Expand Down Expand Up @@ -42,6 +43,8 @@ private function getWikiImageSrc(string $authorLink) {
* Execute the console command.
*/
public function handle() {
Log::info('action=prolang_assets_command, status=started');

LangAuthor::whereNotNull('link')->each(
function ($author) {
$img = $this->getWikiImageSrc($author->link);
Expand All @@ -61,5 +64,7 @@ function ($lang) {
);
}
);

Log::info('action=prolang_assets_command, status=finished');
}
}
10 changes: 7 additions & 3 deletions app/Http/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Models\FrozenRepository;
use App\Models\GithubApi;
use App\Models\ProLang;
use App\Models\YearGroup;
Expand All @@ -21,13 +22,16 @@ public function __construct() {
* Homepage show oldest and starred repositories
*/
public function index(): \Inertia\Response {
$repositories = $this->client->getOldestStarredRepositories();
$repositories = FrozenRepository::orderBy('year')
->orderBy('githubCreatedAt', 'asc')
->get()
->groupBy('year')
->toArray();

$allLangs = ProLang::pluck('name')->toArray();

shuffle($allLangs);

Log::info('action=root_index');

return Inertia::render('HomePage', array(
'oldestRepos' => $repositories,
'allLangs' => $allLangs,
Expand Down
23 changes: 23 additions & 0 deletions app/Models/FrozenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class FrozenRepository extends Model {
protected $guarded = array('id');

protected $casts = array('topics' => 'array');

protected $appends = array('owner');

protected $hidden = array('ownerLogin', 'ownerGithubId', 'ownerAvatarUrl');

public function getOwnerAttribute() {
return array(
'login' => $this->ownerLogin,
'id' => $this->ownerGithubId,
'avatarUrl' => $this->ownerAvatarUrl,
);
}
}
16 changes: 2 additions & 14 deletions app/Models/GithubApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@

class GithubApi extends ApiClient {
/**
* @return array<int, GithubRepository[]>
* @return GithubRepository[]
*/
public function getOldestStarredRepositories(): array {
$repositories = GithubRepositoryApi::get()->getOldestStarredRepositories();
$years = array();

foreach ($repositories as $key => $item) {
$year = $item->createdAt->format('Y');
$needKey = array_key_exists($year, $years) === false;

if ($needKey) {
$years[$year] = array();
}

array_push($years[$year], $item);
}

return $years;
return $repositories;
}

public function getOldestRepository(string $lang) {
Expand Down
9 changes: 9 additions & 0 deletions app/Models/LangAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* @property int $id
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string $name
* @property string|null $pictureUrl
* @property string|null $country
* @property string|null $link
*/
class LangAuthor extends Model {
protected $guarded = array('id');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void {
Schema::create('frozen_repositories', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('githubId');
$table->string('name');
$table->string('fullName');
$table->longText('description')->nullable();
$table->string('url');
$table->dateTime('githubCreatedAt');
$table->dateTime('githubUpdatedAt');
$table->string('language')->nullable();
$table->json('topics');
$table->unsignedInteger('watchers');
$table->unsignedInteger('forks');
$table->unsignedInteger('stars');
$table->boolean('ownerIsOrganization');
$table->string('ownerLogin');
$table->string('ownerGithubId');
$table->string('ownerAvatarUrl');
$table->string('year');
});
}

/**
* Reverse the migrations.
*/
public function down(): void {
Schema::dropIfExists('frozen_repositories');
}
};
12 changes: 7 additions & 5 deletions routes/console.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

// use Illuminate\Foundation\Inspiring;
// use Illuminate\Support\Facades\Artisan;
use App\Console\Commands\FroozeRepositories;
use App\Console\Commands\LoadProLang;
use App\Console\Commands\ProLangAssets;
use Illuminate\Support\Facades\Schedule;

// Artisan::command('inspire', function () {
// $this->comment(Inspiring::quote());
// })->purpose('Display an inspiring quote');
Schedule::command(LoadProLang::class)->monthly();
Schedule::command(ProLangAssets::class)->monthly();
Schedule::command(FroozeRepositories::class)->monthly();
56 changes: 56 additions & 0 deletions tests/Unit/Commands/FroozeRepositoriesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use App\Console\Commands\FroozeRepositories;
use App\Models\FrozenRepository;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;

describe('FroozeRepositories', function () {
it('should save repositories', function (array $repos) {
Http::fake(array(
'https://api.github.com/search/repositories*' => Http::response(
array(
'items' => $repos,
),
200,
),
));

FrozenRepository::insert(array(
'created_at' => Carbon::now(),
'name' => 'remove-me',
'githubId' => '',
'fullName' => 'remove-me',
'description' => '',
'url' => '',
'githubCreatedAt' => Carbon::now(),
'githubUpdatedAt' => Carbon::now(),
'language' => '',
'topics' => '[]',
'watchers' => 1,
'forks' => 2,
'stars' => 69,
'ownerIsOrganization' => false,
'ownerLogin' => '',
'ownerGithubId' => '',
'ownerAvatarUrl' => '',
'year' => '',
));

(new FroozeRepositories())->handle();

expect(
FrozenRepository::where('name', 'remove-me')->first()
)->toBe(null);
expect(
FrozenRepository::all()->count()
)->toBe(count($repos));
expect(
FrozenRepository::where('name', 'beat.time')->first()
)->toMatchArray(array(
'name' => 'beat.time',
'year' => 2019,
'ownerIsOrganization' => false,
));
})->with('github-repos');
});