1
0
Fork 0
mirror of synced 2024-06-14 08:44:49 +12:00

Addressed PR comments

This commit is contained in:
Khushboo Verma 2023-10-27 19:38:33 +05:30
parent bb7278dd12
commit cba20d8b96
8 changed files with 124 additions and 21 deletions

2
.env
View file

@ -91,7 +91,7 @@ _APP_GRAPHQL_MAX_DEPTH=3
_APP_DOCKER_HUB_USERNAME=
_APP_DOCKER_HUB_PASSWORD=
_APP_VCS_GITHUB_APP_NAME=
_APP_VCS_GITHUB_PRIVATE_KEY=""
_APP_VCS_GITHUB_PRIVATE_KEY=disabled
_APP_VCS_GITHUB_APP_ID=
_APP_VCS_GITHUB_CLIENT_ID=
_APP_VCS_GITHUB_CLIENT_SECRET=

View file

@ -98,6 +98,7 @@ jobs:
Teams,
Users,
Webhooks,
VCS,
]
steps:

View file

@ -46,6 +46,7 @@ use Utopia\Validator\Boolean;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use MaxMind\Db\Reader;
use Utopia\VCS\Adapter\Git\GitHub;
use Utopia\VCS\Exception\RepositoryNotFound;
include_once __DIR__ . '/../shared/api.php';
@ -58,7 +59,14 @@ $redeployVcs = function (Request $request, Document $function, Document $project
$github->initializeVariables($providerInstallationId, $privateKey, $githubAppId);
$owner = $github->getOwnerName($providerInstallationId);
$providerRepositoryId = $function->getAttribute('providerRepositoryId', '');
$repositoryName = $github->getRepositoryName($providerRepositoryId);
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
$providerBranch = $function->getAttribute('providerBranch', 'main');
$authorUrl = "https://github.com/$owner";
$repositoryUrl = "https://github.com/$owner/$repositoryName";
@ -287,9 +295,8 @@ App::post('/v1/functions')
$ruleModel = new Rule();
$ruleCreate =
$queueForEvents
->setClass(Event::WEBHOOK_CLASS_NAME)
->setQueue(Event::WEBHOOK_QUEUE_NAME)
;
->setClass(Event::WEBHOOK_CLASS_NAME)
->setQueue(Event::WEBHOOK_QUEUE_NAME);
$ruleCreate
->setProject($project)
@ -870,8 +877,7 @@ App::get('/v1/functions/:functionId/deployments/:deploymentId/download')
->setContentType('application/gzip')
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
->addHeader('X-Peak', \memory_get_peak_usage())
->addHeader('Content-Disposition', 'attachment; filename="' . $deploymentId . '.tar.gz"')
;
->addHeader('Content-Disposition', 'attachment; filename="' . $deploymentId . '.tar.gz"');
$size = $deviceFunctions->getFileSize($path);
$rangeHeader = $request->getHeader('range');

View file

@ -66,7 +66,14 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId
}
$owner = $github->getOwnerName($providerInstallationId) ?? '';
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
@ -154,7 +161,14 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId
$message = 'Authorization required for external contributor.';
$providerRepositoryId = $resource->getAttribute('providerRepositoryId');
$repositoryName = $github->getRepositoryName($providerRepositoryId);
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
$owner = $github->getOwnerName($providerInstallationId);
$github->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'failure', $message, $authorizeUrl, $name);
continue;
@ -206,7 +220,14 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId
$message = 'Starting...';
$providerRepositoryId = $resource->getAttribute('providerRepositoryId');
$repositoryName = $github->getRepositoryName($providerRepositoryId);
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
$owner = $github->getOwnerName($providerInstallationId);
$providerTargetUrl = $request->getProtocol() . '://' . $request->getHostname() . "/console/project-$projectId/functions/function-$functionId";
@ -459,7 +480,10 @@ App::post('/v1/vcs/github/installations/:installationId/providerRepositories/:pr
$owner = $github->getOwnerName($providerInstallationId);
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId);
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
@ -722,6 +746,9 @@ App::get('/v1/vcs/github/installations/:installationId/providerRepositories/:pro
$owner = $github->getOwnerName($providerInstallationId) ?? '';
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
@ -768,6 +795,9 @@ App::get('/v1/vcs/github/installations/:installationId/providerRepositories/:pro
$owner = $github->getOwnerName($providerInstallationId) ?? '';
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
@ -1090,7 +1120,14 @@ App::patch('/v1/vcs/github/installations/:installationId/repositories/:repositor
$providerRepositoryId = $repository->getAttribute('providerRepositoryId');
$owner = $github->getOwnerName($providerInstallationId);
$repositoryName = $github->getRepositoryName($providerRepositoryId);
try {
$repositoryName = $github->getRepositoryName($providerRepositoryId) ?? '';
if (empty($repositoryName)) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
} catch (RepositoryNotFound $e) {
throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND);
}
$pullRequestResponse = $github->getPullRequest($owner, $repositoryName, $providerPullRequestId);
$providerBranch = \explode(':', $pullRequestResponse['head']['label'])[1] ?? '';

View file

@ -2,6 +2,16 @@
namespace Tests\E2E\Services\VCS;
use Utopia\App;
trait VCSBase
{
protected function setUp(): void
{
parent::setUp();
if (App::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY') === 'disabled') {
$this->markTestSkipped('VCS is not enabled.');
}
}
}

View file

@ -23,7 +23,7 @@ class VCSConsoleClientTest extends Scope
public string $providerRepositoryId = '705764267';
public string $providerRepositoryId2 = '708688544';
public function testGitHubAuthorize()
public function testGitHubAuthorize(): string
{
/**
* Test for SUCCESS
@ -43,7 +43,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testGetInstallation(string $installationId)
public function testGetInstallation(string $installationId): void
{
/**
* Test for SUCCESS
@ -61,7 +61,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testDetectRuntime(string $installationId)
public function testDetectRuntime(string $installationId): void
{
/**
* Test for SUCCESS
@ -88,7 +88,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testListRepositories(string $installationId)
public function testListRepositories(string $installationId): void
{
/**
* Test for SUCCESS
@ -131,7 +131,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testGetRepository(string $installationId)
public function testGetRepository(string $installationId): void
{
/**
* Test for SUCCESS
@ -169,7 +169,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testListRepositoryBranches(string $installationId)
public function testListRepositoryBranches(string $installationId): void
{
/**
* Test for SUCCESS
@ -198,7 +198,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testCreateFunctionUsingVCS(string $installationId)
public function testCreateFunctionUsingVCS(string $installationId): array
{
$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
@ -236,7 +236,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testCreateFunctionUsingVCS
*/
public function testUpdateFunctionUsingVCS(array $data)
public function testUpdateFunctionUsingVCS(array $data): string
{
$function = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'], array_merge([
'content-type' => 'application/json',
@ -271,7 +271,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testCreateRepository(string $installationId)
public function testCreateRepository(string $installationId): void
{
/**
* Test for SUCCESS

View file

@ -0,0 +1,24 @@
<?php
namespace Tests\E2E\Services\VCS;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\SideClient;
use Tests\E2E\Client;
class VCSCustomClientTest extends Scope
{
use VCSBase;
use ProjectCustom;
use SideClient;
public function testGetInstallation(string $installationId = 'randomString'): void
{
$installation = $this->client->call(Client::METHOD_GET, '/vcs/installations/' . $installationId, array_merge([
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(401, $installation['headers']['status-code']);
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace Tests\E2E\Services\VCS;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\SideServer;
use Tests\E2E\Client;
class VCSCustomServerTest extends Scope
{
use VCSBase;
use ProjectCustom;
use SideServer;
public function testGetInstallation(string $installationId = 'randomString'): void
{
$installation = $this->client->call(Client::METHOD_GET, '/vcs/installations/' . $installationId, array_merge([
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $this->getHeaders()));
$this->assertEquals(401, $installation['headers']['status-code']);
}
}