2026-04-18 11:22:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Core PHP Framework
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the European Union Public Licence (EUPL) v1.2.
|
|
|
|
|
* See LICENSE file for details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Core\Mod\Agentic\Actions\Forge;
|
|
|
|
|
|
|
|
|
|
use Core\Actions\Action;
|
2026-04-23 18:21:28 +01:00
|
|
|
use Core\Mod\Agentic\Pipeline\ForgejoMetaReader;
|
|
|
|
|
use Core\Mod\Agentic\Pipeline\MetaReader;
|
2026-04-18 11:22:27 +00:00
|
|
|
use Core\Mod\Agentic\Services\ForgejoService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Evaluate and merge a Forgejo pull request when ready.
|
|
|
|
|
*
|
2026-04-23 18:21:28 +01:00
|
|
|
* Checks the PR state, mergeability, and CI status from MetaReader
|
|
|
|
|
* before attempting the merge. Returns a result array describing the
|
|
|
|
|
* outcome.
|
2026-04-18 11:22:27 +00:00
|
|
|
*
|
|
|
|
|
* Usage:
|
|
|
|
|
* $result = ManagePullRequest::run('core', 'app', 10);
|
|
|
|
|
*/
|
|
|
|
|
class ManagePullRequest
|
|
|
|
|
{
|
|
|
|
|
use Action;
|
|
|
|
|
|
2026-04-23 18:21:28 +01:00
|
|
|
public function __construct(
|
|
|
|
|
private ?MetaReader $metaReader = null,
|
|
|
|
|
) {}
|
|
|
|
|
|
2026-04-18 11:22:27 +00:00
|
|
|
/**
|
|
|
|
|
* @return array{merged: bool, pr_number?: int, reason?: string}
|
|
|
|
|
*/
|
|
|
|
|
public function handle(string $owner, string $repo, int $prNumber): array
|
|
|
|
|
{
|
|
|
|
|
$forge = app(ForgejoService::class);
|
2026-04-23 18:21:28 +01:00
|
|
|
$metaReader = $this->resolveMetaReader($owner, $repo);
|
|
|
|
|
$prMeta = $metaReader->getPRMeta($prNumber);
|
2026-04-18 11:22:27 +00:00
|
|
|
|
2026-04-23 18:21:28 +01:00
|
|
|
if ($prMeta->state !== 'open') {
|
2026-04-18 11:22:27 +00:00
|
|
|
return ['merged' => false, 'reason' => 'not_open'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 18:21:28 +01:00
|
|
|
if ($prMeta->mergeability !== 'mergeable') {
|
2026-04-18 11:22:27 +00:00
|
|
|
return ['merged' => false, 'reason' => 'conflicts'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 18:21:28 +01:00
|
|
|
if (! $this->checksHavePassed($prMeta->checkStatuses)) {
|
2026-04-18 11:22:27 +00:00
|
|
|
return ['merged' => false, 'reason' => 'checks_pending'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$forge->mergePullRequest($owner, $repo, $prNumber);
|
|
|
|
|
|
|
|
|
|
return ['merged' => true, 'pr_number' => $prNumber];
|
|
|
|
|
}
|
2026-04-23 18:21:28 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<int, array{name: string, conclusion: string|null, status: string|null}> $checkStatuses
|
|
|
|
|
*/
|
|
|
|
|
private function checksHavePassed(array $checkStatuses): bool
|
|
|
|
|
{
|
|
|
|
|
if ($checkStatuses === []) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($checkStatuses as $checkStatus) {
|
|
|
|
|
if (($checkStatus['status'] ?? null) !== 'completed') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($checkStatus['conclusion'] ?? null) !== 'success') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function resolveMetaReader(string $owner, string $repo): MetaReader
|
|
|
|
|
{
|
|
|
|
|
if ($this->metaReader instanceof MetaReader) {
|
|
|
|
|
return $this->metaReader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var MetaReader $metaReader */
|
|
|
|
|
$metaReader = app()->makeWith(ForgejoMetaReader::class, [
|
|
|
|
|
'owner' => $owner,
|
|
|
|
|
'repo' => $repo,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $metaReader;
|
|
|
|
|
}
|
2026-04-18 11:22:27 +00:00
|
|
|
}
|