1
0
Fork 0
mirror of synced 2024-10-01 01:37:56 +13:00

Update execution model, add detection to repository

This commit is contained in:
Matej Bačo 2023-07-30 09:17:23 +02:00
parent 2e177873c0
commit a718f09f74
14 changed files with 96 additions and 25 deletions

View file

@ -3681,9 +3681,9 @@ $collections = [
'orders' => [Database::ORDER_ASC],
],
[
'$id' => ID::custom('_key_statusCode'),
'$id' => ID::custom('_key_responseStatusCode'),
'type' => Database::INDEX_KEY,
'attributes' => ['statusCode'],
'attributes' => ['responseStatusCode'],
'lengths' => [],
'orders' => [Database::ORDER_ASC],
],

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1430,7 +1430,12 @@ App::post('/v1/functions/:functionId/executions')
}
}
// TODO: Filter headers
$headersFiltered = [];
foreach ($headers as $key => $value) {
if(\in_array($key, FUNCTION_WHITELIST_HEADERS_REQUEST)) {
$headersFiltered[] = [ 'key' => $key, 'value' => $value ];
}
}
$executionId = ID::unique();
@ -1447,7 +1452,7 @@ App::post('/v1/functions/:functionId/executions')
'responseHeaders' => [],
'requestPath' => $path,
'requestMethod' => $method,
'requestHeaders' => $headers,
'requestHeaders' => $headersFiltered,
'errors' => '',
'logs' => '',
'duration' => 0.0,
@ -1530,13 +1535,18 @@ App::post('/v1/functions/:functionId/executions')
runtimeEntrypoint: 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $command . '"'
);
// TODO: Filter headers
$headersFiltered = [];
foreach ($executionResponse['headers'] as $key => $value) {
if(\in_array($key, FUNCTION_WHITELIST_HEADERS_REQUEST)) {
$headersFiltered[] = [ 'key' => $key, 'value' => $value ];
}
}
/** Update execution status */
$status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed';
$execution->setAttribute('status', $status);
$execution->setAttribute('responseStatusCode', $executionResponse['statusCode']);
$execution->setAttribute('responseHeaders', $executionResponse['headers']);
$execution->setAttribute('responseHeaders', $headersFiltered);
$execution->setAttribute('logs', $executionResponse['logs']);
$execution->setAttribute('errors', $executionResponse['errors']);
$execution->setAttribute('duration', $executionResponse['duration']);

View file

@ -377,12 +377,12 @@ App::get('/v1/vcs/github/installations/:installationId/repositories/:repositoryI
->label('sdk.response.model', Response::MODEL_DETECTION)
->param('installationId', '', new Text(256), 'Installation Id')
->param('repositoryId', '', new Text(256), 'Repository Id')
->param('rootDirectoryPath', '', new Text(256), 'Path to Root Directory', true)
->param('rootDirectory', '', new Text(256), 'Path to Root Directory', true)
->inject('gitHub')
->inject('response')
->inject('project')
->inject('dbForConsole')
->action(function (string $vcsInstallationId, string $repositoryId, string $rootDirectoryPath, GitHub $github, Response $response, Document $project, Database $dbForConsole) {
->action(function (string $vcsInstallationId, string $repositoryId, string $rootDirectory, GitHub $github, Response $response, Document $project, Database $dbForConsole) {
$installation = $dbForConsole->getDocument('vcsInstallations', $vcsInstallationId, [
Query::equal('projectInternalId', [$project->getInternalId()])
]);
@ -403,7 +403,7 @@ App::get('/v1/vcs/github/installations/:installationId/repositories/:repositoryI
throw new Exception(Exception::REPOSITORY_NOT_FOUND);
}
$files = $github->listRepositoryContents($owner, $repositoryName, $rootDirectoryPath);
$files = $github->listRepositoryContents($owner, $repositoryName, $rootDirectory);
$languages = $github->getRepositoryLanguages($owner, $repositoryName);
$detectorFactory = new Detector($files, $languages);
@ -511,11 +511,33 @@ App::get('/v1/vcs/github/installations/:installationId/repositories')
// Limit the maximum results to 5
$repos = \array_slice($repos, 0, 5);
$repos = \array_map(function ($repo) use ($installation) {
$repos = \array_map(function ($repo) use ($installation, $github) {
$repo['id'] = \strval($repo['id'] ?? '');
$repo['pushedAt'] = $repo['pushed_at'] ?? null;
$repo['provider'] = $installation->getAttribute('provider', '') ?? '';
$repo['organization'] = $installation->getAttribute('organization', '') ?? '';
$files = $github->listRepositoryContents($repo['organization'], $repo['name'], '');
$languages = $github->getRepositoryLanguages($repo['organization'], $repo['name']);
$detectorFactory = new Detector($files, $languages);
$detectorFactory
->addDetector(new JavaScript())
->addDetector(new PHP())
->addDetector(new Python())
->addDetector(new Dart())
->addDetector(new Swift())
->addDetector(new Ruby())
->addDetector(new Java())
->addDetector(new CPP())
->addDetector(new Deno())
->addDetector(new Dotnet());
$runtime = $detectorFactory->detect();
$repo['runtime'] = $runtime;
return new Document($repo);
}, $repos);

View file

@ -178,6 +178,9 @@ const APP_AUTH_TYPE_KEY = 'Key';
const APP_AUTH_TYPE_ADMIN = 'Admin';
// Response related
const MAX_OUTPUT_CHUNK_SIZE = 2 * 1024 * 1024; // 2MB
// Function headers
const FUNCTION_WHITELIST_HEADERS_REQUEST = ['content-type', 'agent'];
const FUNCTION_WHITELIST_HEADERS_RESPONSE = ['content-type', 'content-length'];
$register = new Registry();

View file

@ -86,7 +86,12 @@ Server::setResource('execute', function () {
/** Create execution or update execution status */
$execution = $dbForProject->getDocument('executions', $executionId ?? '');
if ($execution->isEmpty()) {
// TODO: Filter headers
$headersFiltered = [];
foreach ($headers as $key => $value) {
if(\in_array($key, FUNCTION_WHITELIST_HEADERS_REQUEST)) {
$headersFiltered[] = [ 'key' => $key, 'value' => $value ];
}
}
$executionId = ID::unique();
$execution = new Document([
@ -102,7 +107,7 @@ Server::setResource('execute', function () {
'responseHeaders' => [],
'requestPath' => $path,
'requestMethod' => $method,
'requestHeaders' => $headers,
'requestHeaders' => $headersFiltered,
'errors' => '',
'logs' => '',
'duration' => 0.0,
@ -182,13 +187,18 @@ Server::setResource('execute', function () {
$status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed';
// TODO: Filter headers
$headersFiltered = [];
foreach ($executionResponse['headers'] as $key => $value) {
if(\in_array($key, FUNCTION_WHITELIST_HEADERS_REQUEST)) {
$headersFiltered[] = [ 'key' => $key, 'value' => $value ];
}
}
/** Update execution status */
$execution
->setAttribute('status', $status)
->setAttribute('responseStatusCode', $executionResponse['statusCode'])
->setAttribute('responseHeaders', $executionResponse['headers'])
->setAttribute('responseHeaders', $headersFiltered)
->setAttribute('logs', $executionResponse['logs'])
->setAttribute('errors', $executionResponse['errors'])
->setAttribute('duration', $executionResponse['duration']);

View file

@ -69,8 +69,9 @@ class Execution extends Model
->addRule('requestHeaders', [
'type' => Response::MODEL_HEADERS,
'description' => 'HTTP request headers as a key-value object. This will return only whitelisted headers.',
'default' => new \stdClass(),
'example' => ['x-internal-timezone' => 'UTC'],
'default' => [],
'example' => [['Content-Type' => 'application/json']],
'array' => true,
])
->addRule('responseStatusCode', [
'type' => self::TYPE_INTEGER,
@ -87,8 +88,9 @@ class Execution extends Model
->addRule('responseheaders', [
'type' => Response::MODEL_HEADERS,
'description' => 'HTTP response headers as a key-value object. This will return only whitelisted headers. All headers are returned if execution is created as synchronous.',
'default' => new \stdClass(),
'example' => ['x-internal-timezone' => 'UTC'],
'default' => [],
'example' => [['Content-Type' => 'application/json']],
'array' => true,
])
->addRule('logs', [
'type' => self::TYPE_STRING,

View file

@ -3,9 +3,27 @@
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
class Headers extends Any
class Headers extends Model
{
public function __construct()
{
$this
->addRule('name', [
'type' => self::TYPE_STRING,
'description' => 'Header name.',
'default' => '',
'example' => 'Content-Type',
])
->addRule('value', [
'type' => self::TYPE_STRING,
'description' => 'Header value.',
'default' => '',
'example' => 'application/json',
]);
}
/**
* Get Name
*

View file

@ -41,6 +41,12 @@ class Repository extends Model
'default' => false,
'example' => true,
])
->addRule('runtime', [
'type' => self::TYPE_STRING,
'description' => 'Auto-detected runtime suggestion. Empty if getting response of getRuntime().',
'default' => '',
'example' => 'node',
])
->addRule('pushedAt', [
'type' => self::TYPE_DATETIME,
'description' => 'Last commit date in ISO 8601 format.',