Skip to content

Commit 4cad255

Browse files
committed
fix: update workflow and aggregate event
1 parent fccb450 commit 4cad255

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

src/Actions/WebhookAction.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@ public function __invoke(): JsonResponse
4141

4242
$message = view('github-project::md.comment', compact('payload'))->render();
4343

44-
$response = $this->githubService->handleComment((string) $payload['projects_v2_item']['content_node_id'], $message);
44+
$this->githubService->handleComment((string) $payload['projects_v2_item']['content_node_id'], $message);
4545

46-
return response()->json([
47-
'message' => __('github-project::github-project.success.message'),
48-
'response' => $response,
49-
]);
46+
return response()->json(['message' => __('github-project::github-project.success.message')]);
5047
}
5148
}

src/Jobs/ProcessAggregatedEvents.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,57 @@
22

33
namespace CSlant\GitHubProject\Jobs;
44

5+
use Illuminate\Bus\Queueable;
56
use Illuminate\Contracts\Queue\ShouldQueue;
6-
use Illuminate\Foundation\Queue\Queueable;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Queue\SerializesModels;
10+
use Illuminate\Support\Facades\Cache;
11+
use Illuminate\Support\Facades\Mail;
712

813
class ProcessAggregatedEvents implements ShouldQueue
914
{
15+
use Dispatchable;
16+
use InteractsWithQueue;
1017
use Queueable;
18+
use SerializesModels;
19+
20+
protected string $nodeId;
1121

1222
/**
1323
* Create a new job instance.
1424
*/
15-
public function __construct()
25+
public function __construct(string $nodeId)
1626
{
17-
//
27+
$this->nodeId = $nodeId;
1828
}
1929

2030
/**
2131
* Execute the job.
2232
*/
2333
public function handle(): void
2434
{
25-
//
35+
$commentAggregationCacheKey = "comment_aggregation_{$this->nodeId}";
36+
$events = Cache::pull($commentAggregationCacheKey, []);
37+
38+
if (!empty($events)) {
39+
$message = $this->aggregateMessages($events);
40+
}
41+
}
42+
43+
/**
44+
* Aggregate messages from events.
45+
*
46+
* @param array $events
47+
*
48+
* @return string
49+
*/
50+
protected function aggregateMessages(array $events): string
51+
{
52+
$messages = array_map(function ($event) {
53+
return $event['message'];
54+
}, $events);
55+
56+
return implode("\n", $messages);
2657
}
2758
}

src/Jobs/ProcessWebhookEvent.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ public function __construct(array $eventData)
3131
*/
3232
public function handle(): void
3333
{
34-
$commentAggregationCacheKey = (string) config('github-project.comment_aggregation_cache_key');
34+
$nodeId = $this->eventData['projects_v2_item']['content_node_id'];
35+
$commentAggregationCacheKey = "comment_aggregation_{$nodeId}";
3536
$commentAggregationTime = (int) config('github-project.comment_aggregation_time');
3637

3738
$events = Cache::get($commentAggregationCacheKey, []);
3839
$events[] = $this->eventData;
3940
Cache::put($commentAggregationCacheKey, $events, now()->addSeconds($commentAggregationTime));
4041

4142
if (count($events) === 1) {
42-
ProcessAggregatedEvents::dispatch()->delay(now()->addSeconds($commentAggregationTime));
43+
ProcessAggregatedEvents::dispatch($nodeId)->delay(now()->addSeconds($commentAggregationTime));
4344
}
4445
}
4546
}

src/Services/GithubService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CSlant\GitHubProject\Services;
44

5+
use CSlant\GitHubProject\Jobs\ProcessWebhookEvent;
56
use Github\Client;
67

78
class GithubService
@@ -39,12 +40,12 @@ public function commentOnNode(string $contentNodeId, string $message): array
3940
return $client->graphql()->execute($query, $variables);
4041
}
4142

42-
public function handleComment(string $contentNodeId, string $message): array
43+
public function handleComment(string $contentNodeId, string $message): void
4344
{
4445
if (config('github-project.is_queue_enabled')) {
45-
//
46+
ProcessWebhookEvent::dispatch($contentNodeId, $message);
4647
}
4748

48-
return $this->commentOnNode($contentNodeId, $message);
49+
$this->commentOnNode($contentNodeId, $message);
4950
}
5051
}

0 commit comments

Comments
 (0)