Skip to content

Commit

Permalink
#39: Look into support for new GitHub events (#193)
Browse files Browse the repository at this point in the history
* Remove unnecessary constructor

* Update supported events list

* Create components for new GitHub events

* Fix data expectations for new events

* Add CommitCommentEvent

* Fix CommitCommentEvent

* Cover ReleaseEvent

* Update CHANGELOG
  • Loading branch information
JSn1nj4 authored Apr 17, 2024
1 parent 7a31adf commit efd17a2
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
### Updates

- [#183][is_183]: Upgrade to Laravel 11 ([#190][pr_190])
- [#39][is_39]: Look into support for new GitHub events ([#193][pr_193])

### Development

- Purge old VS Code configs

[is_39]: https://github.com/JSn1nj4/ElliotDerhay.com/issues/39

[is_183]: https://github.com/JSn1nj4/ElliotDerhay.com/issues/183

[pr_190]: https://github.com/JSn1nj4/ElliotDerhay.com/pull/190

[pr_193]: https://github.com/JSn1nj4/ElliotDerhay.com/pull/193

## Version 2.8.6

### Fix
Expand Down
14 changes: 2 additions & 12 deletions app/Console/Commands/GithubEventPullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ class GithubEventPullCommand extends Command
*/
protected $description = 'Fetch events from GitHub\'s events API.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
Expand All @@ -60,13 +50,13 @@ public function handle(GithubService $github): int

$events = $github->getEvents('JSn1nj4', $this->option('count'));

if($this->option('file')) {
if ($this->option('file')) {
Storage::disk('debug')->put($this->option('file'), $events->toJson());

return self::SUCCESS;
}

if($this->option('debug')) {
if ($this->option('debug')) {
dd($events);
}

Expand Down
61 changes: 43 additions & 18 deletions app/DataTransferObjects/GithubEventDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,61 @@

use Illuminate\Support\Carbon;

class GithubEventDTO
readonly class GithubEventDTO
{
public function __construct(
public readonly string $id,
public readonly string $type,
public readonly ?string $action,
public readonly string $date,
public readonly GithubUserDTO $user,
public readonly ?string $source,
public readonly string $repo,
public string $id,
public string $type,
public string|null $action,
public string $date,
public GithubUserDTO $user,
public string|null $source,
public string $repo,
) {}

public static function getAction(array $data): ?string
public static function getAction(array $data): string|null
{
return match ($data['type']) {
'IssuesEvent' => \optional($data['payload'])['action'],
'PullRequestEvent' => (\optional($data['payload'])['merged']
? 'merged'
: \optional($data['payload'])['action']),
'CommitCommentEvent' => match (optional($data['payload'])['action']) {
'created' => 'commented on',
default => 'deleted a comment on',
},

'IssuesEvent' => optional($data['payload'])['action'],

'PullRequestEvent' => match (true) {
optional($data['payload'])['merged'] !== null => 'merged',
default => optional($data['payload'])['action'],
},

'PullRequestReviewCommentEvent' => match (optional($data['payload'])['action']) {
'created' => 'commented on a review of',
'deleted' => 'deleted a review comment on',
default => 'updated a review comment on',
},

'PullRequestReviewEvent' => 'started a review on',

'ReleaseEvent' => match (optional($data['payload'])['action']) {
null => 'updated',
default => optional($data['payload'])['action'],
},

default => null,
};
}

public static function getEventSource(array $data): ?string
public static function getEventSource(array $data): string|null
{
return match ($data['type']) {
'CreateEvent', 'DeleteEvent', 'PushEvent' => $data['payload']['ref'],
'ForkEvent' => $data['payload']['forkee']['full_name'],
'IssueCommentEvent', 'IssuesEvent' => $data['payload']['issue']['number'],
'PullRequestEvent' => $data['payload']['pull_request']['number'],
'CommitCommentEvent' => $data['payload']['comment']['commit_id'],
'CreateEvent', 'DeleteEvent', 'PushEvent' => $data['payload']['ref'],
'ForkEvent' => $data['payload']['forkee']['full_name'],
'IssueCommentEvent', 'IssuesEvent' => $data['payload']['issue']['number'],
'PullRequestEvent',
'PullRequestReviewCommentEvent',
'PullRequestReviewEvent' => $data['payload']['pull_request']['number'],
'ReleaseEvent' => $data['payload']['release']['html_url'],
default => null,
};
}
Expand Down
16 changes: 10 additions & 6 deletions app/Services/Github/GithubService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ class GithubService implements GitHostService
* Reference: https://docs.github.com/en/developers/webhooks-and-events/events/github-event-types
*/
private array $supportedEventTypes = [
'CommitCommentEvent',
'CreateEvent',
'DeleteEvent',
'ForkEvent',
'IssueCommentEvent',
'IssuesEvent',
'PublicEvent',
'PullRequestEvent',
'PullRequestReviewCommentEvent',
'PullRequestReviewEvent',
'PushEvent',
'ReleaseEvent',
'WatchEvent',
];

Expand All @@ -51,10 +55,10 @@ class GithubService implements GitHostService
public function __construct()
{
foreach ([
'services.github.token',
'mail.to.name',
'mail.to.address',
] as $key) {
'services.github.token',
'mail.to.name',
'mail.to.address',
] as $key) {
if (config($key) === null) {
throw new Exception("Config option '{$key}' not set.");
}
Expand Down Expand Up @@ -142,11 +146,11 @@ public function filterEventTypes(Response $response): Collection
*/
public function getEvents(string $user, int $count): Collection
{
if($count < 1) {
if ($count < 1) {
throw new Exception("'\$count' value must be 1 or higher. Value is '{$count}'.");
}

if($count > 100) {
if ($count > 100) {
throw new Exception("'\$count' value must be 100 or less. Value is '{$count}'.");
}

Expand Down
31 changes: 31 additions & 0 deletions app/View/Components/Github/EventTypes/CommitCommentEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\View\Components\Github\EventTypes;

use App\Models\GithubEvent;
use App\Traits\CanHaveGitRef;
use App\Traits\CanHavePreposition;

class CommitCommentEvent extends BaseComponent
{
use CanHaveGitRef,
CanHavePreposition;

public function __construct(GithubEvent $event)
{
parent::__construct(
action: $event->action,
icon: 'fas fa-comment',
event: $event,
);

if ($this->refNull($this->event->source)) return;

$this->preposition = 'at';
$this->setRefName(str($this->event->source)->limit(7, ''));

if (str($this->action)->contains('deleted', true)) return;

$this->setRefUrl($this->repoUrl());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\View\Components\Github\EventTypes;

use App\Models\GithubEvent;
use App\Traits\CanHavePreposition;
use App\Traits\HasPullRequestNumber;

class PullRequestReviewCommentEvent extends BaseComponent
{
use CanHavePreposition,
HasPullRequestNumber;

/**
* Create a new component instance.
*
* @return void
*/
public function __construct(GithubEvent $event)
{
parent::__construct(
$event->action ?? 'commented on a review of',
'fas fa-comment-alt',
$event
);

$this->preposition = 'at';
$this->setPullRequestNumberText($this->event->source);
}
}
30 changes: 30 additions & 0 deletions app/View/Components/Github/EventTypes/PullRequestReviewEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\View\Components\Github\EventTypes;

use App\Models\GithubEvent;
use App\Traits\CanHavePreposition;
use App\Traits\HasPullRequestNumber;

class PullRequestReviewEvent extends BaseComponent
{
use CanHavePreposition,
HasPullRequestNumber;

/**
* Create a new component instance.
*
* @return void
*/
public function __construct(GithubEvent $event)
{
parent::__construct(
$event->action ?? 'reviewed',
'fas fa-edit',
$event
);

$this->preposition = 'at';
$this->setPullRequestNumberText($this->event->source);
}
}
31 changes: 31 additions & 0 deletions app/View/Components/Github/EventTypes/ReleaseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\View\Components\Github\EventTypes;

use App\Models\GithubEvent;
use App\Traits\CanHavePreposition;

class ReleaseEvent extends BaseComponent
{
use CanHavePreposition;

public string $releaseText;

public string $releaseUrl;

/**
* Create a new component instance.
*/
public function __construct(GithubEvent $event)
{
parent::__construct(
action: $event->action,
icon: 'fas fa-tag',
event: $event,
);

$this->preposition = 'at';
$this->releaseUrl = $event->source;
$this->releaseText = str($event->source)->after('releases/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<div class="flex flex-row relative">
<div class="text-neutral-800 dark:text-neutral-500 text-center flex-none {{ $icon }}"
style="width: 2rem; font-size: 22px;"></div>

<div class="pl-4 flex-grow relative">
<p class="text-neutral-800 dark:text-neutral-500">
{{ $timeElapsed }}
</p>

<p class="font-white mt-1 text-sm leading-none">
<strong>
<a href="{{ $profileUrl() }}" target="_blank">
{{ $event->user->display_login }}
</a>

{{ $action }}

{{-- no refUrl assumes it was deleted --}}
@if($refUrl === null)
<span class="text-caribbeanGreen-600 dark:text-caribbeanGreen-800">
{{ $refName }}
</span>
@else
<a href="{{ $refUrl }}" target="_blank">
{{ $refName }}
</a>
@endif


{{ $preposition }}

<a href="{{ $repoUrl() }}" target="_blank">
{{ $event->repo }}
</a>
</strong>
</p>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="flex flex-row relative">
<div class="text-neutral-800 dark:text-neutral-500 text-center flex-none {{ $icon }}"
style="width: 2rem; font-size: 22px;"></div>

<div class="pl-4 flex-grow relative">
<p class="text-neutral-800 dark:text-neutral-500">
{{ $timeElapsed }}
</p>

<p class="font-white mt-1 text-sm leading-none">
<strong>
<a href="{{ $profileUrl() }}" target="_blank">
{{ $event->user->display_login }}
</a>

{{ $action }}

<a href="{{ $repoUrl() }}/pull/{{ $event->source }}" target="_blank"
class="text-caribbeanGreen-600 dark:text-caribbeanGreen-500">
{{ $pullRequestNumberText }}
</a>

{{ $preposition }}

<a href="{{ $repoUrl() }}" target="_blank">
{{ $event->repo }}
</a>
</strong>
</p>
</div>
</div>
Loading

0 comments on commit efd17a2

Please sign in to comment.