From 1284449765a8b2a32ca06376e9107d66f48944cf Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Sat, 19 Aug 2023 18:21:27 +0530 Subject: [PATCH] Fixed GitHub comments --- app/console | 2 +- app/controllers/api/vcs.php | 123 ++++++++++++++++-------------------- 2 files changed, 57 insertions(+), 68 deletions(-) diff --git a/app/console b/app/console index 451e591a1d..f0f0b050fc 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 451e591a1d6c0843fdaef131f1064fd6cdbb2a3b +Subproject commit f0f0b050fc51871206a4bb729f1c11592f7e8c2f diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index 0f8ee9f5f4..234f1a4f52 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -64,34 +64,6 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId $activate = true; } - $latestCommentId = ''; - - if (!empty($providerPullRequestId)) { - $latestComment = Authorization::skip(fn () => $dbForConsole->findOne('vcsComments', [ - Query::equal('installationInternalId', [$installationInternalId]), - Query::equal('projectInternalId', [$project->getInternalId()]), - Query::equal('providerRepositoryId', [$providerRepositoryId]), - Query::equal('providerPullRequestId', [$providerPullRequestId]), - Query::orderDesc('$createdAt'), - ])); - - if ($latestComment !== false && !$latestComment->isEmpty()) { - $latestCommentId = $latestComment->getAttribute('commentId', ''); - } - } elseif (!empty($providerBranch)) { - $latestComment = Authorization::skip(fn () => $dbForConsole->findOne('vcsComments', [ - Query::equal('installationInternalId', [$installationInternalId]), - Query::equal('projectInternalId', [$project->getInternalId()]), - Query::equal('providerRepositoryId', [$providerRepositoryId]), - Query::equal('providerBranch', [$providerBranch]), - Query::orderDesc('$createdAt'), - ])); - - if ($latestComment !== false && !$latestComment->isEmpty()) { - $latestCommentId = $latestComment->getAttribute('commentId', ''); - } - } - $owner = $github->getOwnerName($providerInstallationId) ?? ''; $repositoryName = $github->getRepositoryName($providerRepositoryId) ?? ''; @@ -113,48 +85,65 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId $action = $isAuthorized ? ['type' => 'logs'] : ['type' => 'authorize', 'url' => $authorizeUrl]; - if (empty($latestCommentId)) { - $comment = new Comment(); - $comment->addBuild($project, $function, $commentStatus, $deploymentId, $action); + $latestCommentId = ''; - if (!empty($providerPullRequestId)) { + if (!empty($providerPullRequestId)) { + $latestComment = Authorization::skip(fn () => $dbForConsole->findOne('vcsComments', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::equal('providerPullRequestId', [$providerPullRequestId]), + Query::orderDesc('$createdAt'), + ])); + + if ($latestComment !== false && !$latestComment->isEmpty()) { + $latestCommentId = $latestComment->getAttribute('providerCommentId', ''); + $comment = new Comment(); + $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); + $comment->addBuild($project, $function, $commentStatus, $deploymentId, $action); + + $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); + } else { + $comment = new Comment(); + $comment->addBuild($project, $function, $commentStatus, $deploymentId, $action); $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); - } elseif (!empty($providerBranch)) { - $gitPullRequest = $github->getPullRequestFromBranch($owner, $repositoryName, $providerBranch); - $providerPullRequestId = \strval($gitPullRequest['number'] ?? ''); - if (!empty($providerPullRequestId)) { - $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); + + if (!empty($latestCommentId)) { + $teamId = $project->getAttribute('teamId', ''); + + $latestComment = Authorization::skip(fn () => $dbForConsole->createDocument('vcsComments', new Document([ + '$id' => ID::unique(), + '$permissions' => [ + Permission::read(Role::team(ID::custom($teamId))), + Permission::update(Role::team(ID::custom($teamId), 'owner')), + Permission::update(Role::team(ID::custom($teamId), 'developer')), + Permission::delete(Role::team(ID::custom($teamId), 'owner')), + Permission::delete(Role::team(ID::custom($teamId), 'developer')), + ], + 'installationInternalId' => $installationInternalId, + 'installationId' => $installationId, + 'projectInternalId' => $project->getInternalId(), + 'projectId' => $project->getId(), + 'providerRepositoryId' => $providerRepositoryId, + 'providerBranch' => $providerBranch, + 'providerPullRequestId' => $providerPullRequestId, + 'providerCommentId' => $latestCommentId + ]))); } } + } elseif (!empty($providerBranch)) { + $latestComments = Authorization::skip(fn () => $dbForConsole->find('vcsComments', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::equal('providerBranch', [$providerBranch]), + Query::orderDesc('$createdAt'), + ])); - if (!empty($latestCommentId)) { - $teamId = $project->getAttribute('teamId', ''); + foreach ($latestComments as $comment) { + $latestCommentId = $comment->getAttribute('providerCommentId', ''); + $comment = new Comment(); + $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); + $comment->addBuild($project, $function, $commentStatus, $deploymentId, $action); - $latestComment = Authorization::skip(fn () => $dbForConsole->createDocument('vcsComments', new Document([ - '$id' => ID::unique(), - '$permissions' => [ - Permission::read(Role::team(ID::custom($teamId))), - Permission::update(Role::team(ID::custom($teamId), 'owner')), - Permission::update(Role::team(ID::custom($teamId), 'developer')), - Permission::delete(Role::team(ID::custom($teamId), 'owner')), - Permission::delete(Role::team(ID::custom($teamId), 'developer')), - ], - 'installationInternalId' => $installationInternalId, - 'installationId' => $installationId, - 'projectInternalId' => $project->getInternalId(), - 'projectId' => $project->getId(), - 'providerRepositoryId' => $providerRepositoryId, - 'providerBranch' => $providerBranch, - 'providerPullRequestId' => $providerPullRequestId, - 'providerCommentId' => $latestCommentId - ]))); + $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } - } else { - $comment = new Comment(); - $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); - $comment->addBuild($project, $function, $commentStatus, $deploymentId, $action); - - $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } if (!$isAuthorized) { @@ -722,10 +711,6 @@ App::post('/v1/vcs/github/installations/:installationId/providerRepositories') } } - if (isset($repository['message'])) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Provider Error: ' . $repository['message']); - } - if (isset($repository['errors'])) { $message = $repository['message'] ?? 'Unknown error.'; if (isset($repository['errors'][0])) { @@ -734,6 +719,10 @@ App::post('/v1/vcs/github/installations/:installationId/providerRepositories') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Provider Error: ' . $message); } + if (isset($repository['message'])) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Provider Error: ' . $repository['message']); + } + $repository['id'] = \strval($repository['id']) ?? ''; $repository['pushedAt'] = $repository['pushed_at'] ?? ''; $repository['organization'] = $installation->getAttribute('organization', '');