Skip to content
Draft
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
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_REDIRECT=
GITHUB_REDIRECT=

DISCORD_APP_ID=
DISCORD_PUBLIC_KEY=
DISCORD_BOT_TOKEN=
DISCORD_GUILD_ID=
DISCORD_CODE_REVIEW_CHANNELS_ID=
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
run: |
npm i -g @liara/cli@3
liara deploy --app="codereviewpals" --api-token="$LIARA_TOKEN" --detach
liara shell --app="codereviewpals" --api-token="$LIARA_TOKEN" --command="php artisan migrate --force && php artisan horizon:terminate --wait && php artisan discord:terminate"
liara shell --app="codereviewpals" --api-token="$LIARA_TOKEN" --command="bash deploy.sh"
38 changes: 38 additions & 0 deletions app/Actions/Channel/GetOrCreateAChannelByPullRequestAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Actions\Channel;

use App\Actions\Discord\Channels\CreateChannel;
use ReflectionException;
use App\Enums\Discord\ChannelType;
use Saloon\Exceptions\PendingRequestException;
use Saloon\Exceptions\InvalidResponseClassException;
use App\Models\Channel;
use App\Models\PullRequest;

class GetOrCreateAChannelByPullRequestAction
{
/**
* @throws InvalidResponseClassException
* @throws ReflectionException
* @throws PendingRequestException
*/
public function execute(PullRequest $pullRequest): Channel
{
$mainLang = $pullRequest->repository->language;

$channel = Channel::name($mainLang)->first();
if ($channel instanceof Channel) {
return $channel;
}
$channelData = app(CreateChannel::class)
->execute(
name: $mainLang,
channelType: ChannelType::FORUM,
parentId: config('services.discord.channels.code_reviews')
)
->dtoOrFail();

return Channel::create($channelData->toArray());
}
}
17 changes: 7 additions & 10 deletions app/Actions/Discord/AddPullRequestCommandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\Discord;

use App\Jobs\CreatePullRequestByUrlJob;
use App\Models\User;
use App\Services\Github\PullRequestService;
use Discord\Builders\MessageBuilder;
Expand All @@ -19,16 +20,12 @@ public function __invoke(Message $message, Discord $discord, array $matches)
{
$url = $matches[1] ?? '';
$user = $this->getUser($message->author);
try {
$this->service->createFromUrl($url, $user);
$message->reply(
"Thank you for your interest ❤️ \n Your Pull Request submitted successfully."
);
} catch (\Throwable $th) {
$message->reply(
'error occurred on creating PR. please check your URL and if everything is ok contact with admins 😉'
);
}

CreatePullRequestByUrlJob::dispatch($url, $user);

$message->reply(
"Thank you for your interest ❤️ \n Your Pull Request submitted successfully."
);
}

public function getUser(DiscordUser $discordUser): User
Expand Down
35 changes: 35 additions & 0 deletions app/Actions/Discord/Channels/CreateChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Actions\Discord\Channels;

use ReflectionException;
use Saloon\Contracts\Response;
use App\Enums\Discord\ChannelType;
use App\Actions\Discord\DiscordAction;
use Saloon\Exceptions\PendingRequestException;
use Saloon\Exceptions\InvalidResponseClassException;
use App\Http\Integrations\Discord\Channel\Requests\CreateGuildChannel;

class CreateChannel
{
use DiscordAction;

/**
* @throws InvalidResponseClassException
* @throws ReflectionException
* @throws PendingRequestException
*/
public function execute(
string $name,
ChannelType $channelType = ChannelType::TEXT,
?string $parentId = null

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your opinion on union types?

I think its more readable and clear using it, like null|string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for null its ok to use "?"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string|int|null ok
bcuz ?string|int not readable and may not work

): Response {
$request = new CreateGuildChannel(
name: $name,
channelType: $channelType,
parentId: $parentId
);

return $this->connector->send($request);
}
}
39 changes: 39 additions & 0 deletions app/Actions/Discord/CreateThreadFromPullRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Actions\Discord;

use App\DTO\Discord\MessageObject;
use App\Http\Integrations\Discord\Channel\Requests\CreateForumPost;
use App\Models\Channel;
use App\Models\PullRequest;
use Saloon\Contracts\Response;

class CreateThreadFromPullRequest
{
use DiscordAction;

/**
* @throws InvalidResponseClassException
* @throws ReflectionException
* @throws PendingRequestException
*/
public function execute(PullRequest $pullRequest, Channel $channel): Response
{
$repository = $pullRequest->repository;
$tags = $repository->topics;
$tags[] = $repository->language;
$message = $this->makeMessage($pullRequest);
$request = new CreateForumPost(
name: $pullRequest->title,
message: $message,
channelId: $channel->channel_id
);

return $this->connector->send($request);
}

public function makeMessage(PullRequest $pullRequest): MessageObject
{
return new MessageObject('HELLO');
}
}
15 changes: 15 additions & 0 deletions app/Actions/Discord/DiscordAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Actions\Discord;

use App\Http\Integrations\Discord\DiscordAPIConnector;

trait DiscordAction
{
protected DiscordAPIConnector $connector;

public function __construct()
{
$this->connector = new DiscordAPIConnector;
}
}
15 changes: 8 additions & 7 deletions app/Actions/Github/Repository/CreateWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace App\Actions\Github\Repository;

use App\Models\Webhook;
use ReflectionException;
use App\Models\Repository;
use Saloon\Contracts\Response;
use App\Actions\Github\GithubAction;
use Saloon\Exceptions\PendingRequestException;
Expand All @@ -20,12 +18,15 @@ class CreateWebhook
* @throws ReflectionException
* @throws PendingRequestException
*/
public function execute(Repository $repository): Response
{
public function execute(
string $username,
string $repository,
string $secret,
): Response {
$request = new CreateRepositoryWebhook(
username: $repository->username,
repository: $repository->repository,
secret: $repository->node_id
username: $username,
repository: $repository,
secret: $secret,
);

return $this->connector->send($request);
Expand Down
37 changes: 37 additions & 0 deletions app/Actions/Github/Repository/CreateWebhookFromRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Actions\Github\Repository;

use Exception;
use App\Models\Webhook;
use App\Models\Repository;
use App\Actions\Github\GithubAction;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\Eloquent\Model;

class CreateWebhookFromRepository
{
use GithubAction;

public function execute(Repository $repository): Model|bool
{
try {
$webhookData = app(CreateWebhook::class)
->execute(
username: $repository->username,
repository: $repository->repository_name,
secret: Hash::make($repository->node_id)
)
->dtoOrFail();

return $repository->webhooks()->save(
new Webhook([
'title' => $repository->full_name . ' hook',
'hook_id' => $webhookData->id,
])
);
} catch (Exception) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion app/Actions/Github/Webhook/FetchWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function getUsernameAndRepository(Repository|PullRequest $model): array
if ($model instanceof Repository) {
return [
'username' => $model->username,
'repository' => $model->repository,
'repository' => $model->repository_name,
];
}
return app(PullRequestService::class)->getRegexMatch($model->html_url);
Expand Down
4 changes: 3 additions & 1 deletion app/Actions/Repository/CreateRepositoryByPullRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public function __construct(protected RepositoryService $repositoryService)
*/
public function execute(PullRequest $pullRequest): Repository
{
$repository = $this->repositoryService->getRepositoryByFullName($pullRequest->repository);
$repository = $this->repositoryService->getRepositoryByFullName(
$pullRequest->repository_name
);
if (!is_null($repository)) {
return $repository;
}
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Commands/Discord/RunDiscordBotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Discord\WebSockets\Intents;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class RunDiscordBotCommand extends Command
{
Expand Down Expand Up @@ -64,6 +65,7 @@ protected function messageRouting(Message $inputMessage, Discord $discord)
}
continue;
}
$inputMessage->reply('Your command is not valid.');
}

private function storeProcess()
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Commands/Discord/TerminateDiscordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function handle(): void
posix_strerror(posix_get_last_error()) .
')'
);
return;
}
cache()->delete(config('cache.discord.process_id'));
}
}
1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protected function schedule(Schedule $schedule): void
$schedule->command('github:fetch-pull-request')->hourly();
$schedule->command('github:disable-unavailable-webhooks')->daily();
$schedule->command('queue:flush --hours=24')->daily();
$schedule->command('horizon:snapshot')->everyFiveMinutes();
}

/**
Expand Down
23 changes: 23 additions & 0 deletions app/DTO/Discord/ChannelData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\DTO\Discord;

use Spatie\LaravelData\Data;
use App\Enums\Discord\ChannelType;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\LaravelData\Attributes\MapOutputName;

#[MapInputName(SnakeCaseMapper::class)]
#[MapOutputName(SnakeCaseMapper::class)]
class ChannelData extends Data
{
public function __construct(
#[MapInputName('id')]
public string $channelId,
public ChannelType $type,
public string $name,
public ?string $parentId,
public string $guildId,
) {}
}
23 changes: 23 additions & 0 deletions app/DTO/Discord/ForumThreadData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\DTO\Discord;

use Spatie\LaravelData\Data;
use App\Enums\Discord\ChannelType;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\LaravelData\Attributes\MapOutputName;

#[MapInputName(SnakeCaseMapper::class)]
#[MapOutputName(SnakeCaseMapper::class)]
class ForumThreadData extends Data
{
public function __construct(
#[MapInputName('id')] public string $channelId,
public ChannelType $type,
public string $name,
public ?string $parentId,
public string $guildId
) {
}
}
18 changes: 18 additions & 0 deletions app/DTO/Discord/MessageObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\DTO\Discord;

use Spatie\LaravelData\Data;
use App\Enums\Discord\ChannelType;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\LaravelData\Attributes\MapOutputName;

#[MapInputName(SnakeCaseMapper::class)]
#[MapOutputName(SnakeCaseMapper::class)]
class MessageObject extends Data
{
public function __construct(public string $content, public ?array $embeds = null)
{
}
}
1 change: 1 addition & 0 deletions app/DTO/RepositoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
public ?string $description,
public ?string $language,
public string $htmlUrl,
public array $topics,
public OwnerData $owner
) {
}
Expand Down
Loading