Skip to content

Commit

Permalink
add packagist integration
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Sep 30, 2024
1 parent ecbc34e commit 864a2fd
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 61 deletions.
6 changes: 6 additions & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"meta_url" => "Meta URL",
"meta" => "Meta",
"meta_redirect" => "Meta Redirect",
"github_starts" => "Github Stars",
"github_watchers" => "Github Watchers",
"github_forks" => "Github Forks",
"downloads_total" => "Downloads Total",
"downloads_monthly" => "Downloads Monthly",
"downloads_daily" => "Downloads Daily",
]
],
"images" => [
Expand Down
29 changes: 29 additions & 0 deletions src/Filament/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,35 @@ public static function form(Form $form): Form
->options(fn(Forms\Get $get)=> $get('author_type') ? $get('author_type')::pluck('name', 'id')->toArray() : [])
->searchable(),
]),
Forms\Components\Section::make(trans('filament-cms::messages.content.posts.sections.meta.title'))
->visible(fn($record)=> $record && !empty($record->meta_url))
->description(trans('filament-cms::messages.content.posts.sections.meta.description'))
->schema([
Forms\Components\TextInput::make('github_starts')
->disabled()
->numeric()
->label(trans('filament-cms::messages.content.posts.sections.meta.columns.github_starts')),
Forms\Components\TextInput::make('github_watchers')
->disabled()
->numeric()
->label(trans('filament-cms::messages.content.posts.sections.meta.columns.github_watchers')),
Forms\Components\TextInput::make('github_forks')
->disabled()
->numeric()
->label(trans('filament-cms::messages.content.posts.sections.meta.columns.github_forks')),
Forms\Components\TextInput::make('downloads_total')
->disabled()
->numeric()
->label(trans('filament-cms::messages.content.posts.sections.meta.columns.downloads_total')),
Forms\Components\TextInput::make('downloads_monthly')
->disabled()
->numeric()
->label(trans('filament-cms::messages.content.posts.sections.meta.columns.downloads_monthly')),
Forms\Components\TextInput::make('downloads_daily')
->disabled()
->numeric()
->label(trans('filament-cms::messages.content.posts.sections.meta.columns.downloads_daily')),
]),
])
->columnSpan([
'sm' => 1,
Expand Down
11 changes: 11 additions & 0 deletions src/Filament/Resources/PostResource/Pages/EditPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ protected function getHeaderActions(): array
Actions\LocaleSwitcher::make()
];
}

protected function mutateFormDataBeforeFill(array $data): array
{
$data['github_starts'] = $this->getRecord()->meta('github_starts');
$data['github_watchers'] = $this->getRecord()->meta('github_watchers');
$data['github_forks'] = $this->getRecord()->meta('github_forks');
$data['downloads_total'] = $this->getRecord()->meta('downloads_total');
$data['downloads_monthly'] = $this->getRecord()->meta('downloads_monthly');
$data['downloads_daily'] = $this->getRecord()->meta('downloads_daily');
return $data;
}
}
139 changes: 78 additions & 61 deletions src/Jobs/GitHubMetaGetterJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,75 +36,92 @@ public function __construct(
*/
public function handle(): void
{
$user = $this->userType::find($this->userId);
$repo = Str::of($this->url)->remove('https://github.com/', '')->remove('https://www.github.com/','')->toString();
$github = Http::get('https://api.github.com/repos/' . $repo)->json();
if(isset($github['id'])){
$gitReadme = Http::get('https://raw.githubusercontent.com/'.$repo.'/'.$github['default_branch'].'/README.md')->body();
if($gitReadme){
$checkIfPostExists = Post::query()->withTrashed()->where('slug', str($github['full_name'])->explode('/')->last())->first();
if($checkIfPostExists){
if($checkIfPostExists->deleted_at){
$checkIfPostExists->restore();
try{
$user = $this->userType::find($this->userId);
$repo = Str::of($this->url)->remove('https://github.com/', '')->remove('https://www.github.com/','')->toString();
$github = Http::get('https://api.github.com/repos/' . $repo)->json();
if(isset($github['id'])){
$gitReadme = Http::get('https://raw.githubusercontent.com/'.$repo.'/'.$github['default_branch'].'/README.md')->body();
$packgiest = Http::get('https://packagist.org/packages/'.$github['full_name'].'.json')->json();
if($gitReadme){
$checkIfPostExists = Post::query()->withTrashed()->where('slug', str($github['full_name'])->explode('/')->last())->first();
if($checkIfPostExists){
if($checkIfPostExists->deleted_at){
$checkIfPostExists->restore();
}
$checkIfPostExists->clearMediaCollection('feature_image');
$post = $checkIfPostExists;
}
else {
$post = new Post();
}
$checkIfPostExists->clearMediaCollection('feature_image');
$post = $checkIfPostExists;
}
else {
$post = new Post();
}

$post->title = [
"ar" => $github['name'],
"en" => $github['name']
];
$post->body = [
"ar" => $gitReadme,
"en" => $gitReadme
];
$post->short_description = [
"ar" => $github['description'],
"en" => $github['description']
];
$post->slug = str($github['full_name'])->explode('/')->last();
$post->meta = $github;
$post->meta_url = $this->url;
$post->type = 'open-source';
$post->is_published = true;
$post->published_at = now();
$post->author_type = $this->userType;
$post->author_id = $this->userId;
$post->save();
$post->title = [
"ar" => $github['name'],
"en" => $github['name']
];
$post->body = [
"ar" => $gitReadme,
"en" => $gitReadme
];
$post->short_description = [
"ar" => $github['description'],
"en" => $github['description']
];
$post->slug = str($github['full_name'])->explode('/')->last();
$post->meta = $github;
$post->meta_url = $this->url;
$post->type = 'open-source';
$post->is_published = true;
$post->published_at = now();
$post->author_type = $this->userType;
$post->author_id = $this->userId;
if(!isset($packgiest['status'])){
$post->keywords = [
'ar' => implode( ',', collect($packgiest['package']['versions'])->first()['keywords']),
'en' => implode( ',', collect($packgiest['package']['versions'])->first()['keywords'])
];
}
$post->save();

$post->meta('github_starts', $github['stargazers_count']);
$post->meta('github_watchers', $github['watchers_count']);
$post->meta('github_language', $github['language']);
$post->meta('github_forks', $github['forks_count']);
$post->meta('github_open_issues', $github['open_issues_count']);
$post->meta('github_default_branch', $github['default_branch']);
$post->meta('github_docs', $github['homepage']);
if(!isset($packgiest['status'])){
$post->meta('downloads_total', $packgiest['package']['downloads']['total']);
$post->meta('downloads_monthly', $packgiest['package']['downloads']['monthly']);
$post->meta('downloads_daily', $packgiest['package']['downloads']['daily']);
}

$post->addMediaFromUrl($github['owner']['avatar_url'])->toMediaCollection('feature_image');
$post->meta('github_starts', $github['stargazers_count']);
$post->meta('github_watchers', $github['watchers_count']);
$post->meta('github_language', $github['language']);
$post->meta('github_forks', $github['forks_count']);
$post->meta('github_open_issues', $github['open_issues_count']);
$post->meta('github_default_branch', $github['default_branch']);
$post->meta('github_docs', $github['homepage']);

$post->addMediaFromUrl($github['owner']['avatar_url'])->toMediaCollection('feature_image');

Notification::make()
->title(trans('filament-cms::messages.content.posts.import.github.notifications.title'))
->body(trans('filament-cms::messages.content.posts.import.github.notifications.description', ['name'=> $github['full_name']]))
->success()
->actions([
Action::make('view')
->label(trans('filament-cms::messages.content.posts.import.github.notifications.view'))
->url($this->panel. '/posts/'.$post->id.'/edit')
->icon('heroicon-o-eye')
])
->sendToDatabase($user);
}
}
else {
Notification::make()
->title(trans('filament-cms::messages.content.posts.import.github.notifications.title'))
->body(trans('filament-cms::messages.content.posts.import.github.notifications.description', ['name'=> $github['full_name']]))
->success()
->actions([
Action::make('view')
->label(trans('filament-cms::messages.content.posts.import.github.notifications.view'))
->url($this->panel. '/posts/'.$post->id.'/edit')
->icon('heroicon-o-eye')
])
->title(trans('filament-cms::messages.content.posts.import.github.notifications.failed_title'))
->body(trans('filament-cms::messages.content.posts.import.github.notifications.failed_description', ['name'=> $repo]))
->danger()
->sendToDatabase($user);
}
}
else {
Notification::make()
->title(trans('filament-cms::messages.content.posts.import.github.notifications.failed_title'))
->body(trans('filament-cms::messages.content.posts.import.github.notifications.failed_description', ['name'=> $repo]))
->danger()
->sendToDatabase($user);
}catch (\Exception $e) {
dd($e);
}

}
Expand Down
13 changes: 13 additions & 0 deletions src/Jobs/GitHubMetaRefreshJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function handle(): void
$github = Http::get('https://api.github.com/repos/' . $repo)->json();
if(isset($github['id'])){
$gitReadme = Http::get('https://raw.githubusercontent.com/'.$repo.'/'.$github['default_branch'].'/README.md')->body();
$packgiest = Http::get('https://packagist.org/packages/'.$github['full_name'].'.json')->json();
if($gitReadme){
$post->body = [
"ar" => $gitReadme,
Expand All @@ -38,8 +39,20 @@ public function handle(): void
"en" => $github['description']
];
$post->meta = $github;
if(!isset($packgiest['status'])){
$post->keywords = [
'ar' => implode( ',', collect($packgiest['package']['versions'])->first()['keywords']),
'en' => implode( ',', collect($packgiest['package']['versions'])->first()['keywords'])
];
}
$post->save();

if(!isset($packgiest['status'])){
$post->meta('downloads_total', $packgiest['package']['downloads']['total']);
$post->meta('downloads_monthly', $packgiest['package']['downloads']['monthly']);
$post->meta('downloads_daily', $packgiest['package']['downloads']['daily']);
}

$post->meta('github_starts', $github['stargazers_count']);
$post->meta('github_watchers', $github['watchers_count']);
$post->meta('github_language', $github['language']);
Expand Down

0 comments on commit 864a2fd

Please sign in to comment.