payload debug

This commit is contained in:
shimon 2025-01-24 18:03:24 +02:00
parent 2c4f5d3daa
commit 0cc5088e8e

View file

@ -1,10 +1,14 @@
<?php <?php
use Appwrite\Auth\Auth;
use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Auth\OAuth2\Github as OAuth2Github;
use Appwrite\Event\Build; use Appwrite\Event\Build;
use Appwrite\Event\Delete; use Appwrite\Event\Delete;
use Appwrite\Extend\Exception; use Appwrite\Extend\Exception;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
use Appwrite\SDK\MethodType;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Database\Validator\Queries\Installations; use Appwrite\Utopia\Database\Validator\Queries\Installations;
use Appwrite\Utopia\Request; use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response; use Appwrite\Utopia\Response;
@ -34,7 +38,6 @@ use Utopia\Detector\Adapter\Python;
use Utopia\Detector\Adapter\Ruby; use Utopia\Detector\Adapter\Ruby;
use Utopia\Detector\Adapter\Swift; use Utopia\Detector\Adapter\Swift;
use Utopia\Detector\Detector; use Utopia\Detector\Detector;
use Utopia\Fetch\Client;
use Utopia\System\System; use Utopia\System\System;
use Utopia\Validator\Boolean; use Utopia\Validator\Boolean;
use Utopia\Validator\Host; use Utopia\Validator\Host;
@ -269,15 +272,22 @@ App::get('/v1/vcs/github/authorize')
->desc('Install GitHub app') ->desc('Install GitHub app')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.read') ->label('scope', 'vcs.read')
->label('sdk.namespace', 'vcs')
->label('error', __DIR__ . '/../../views/general/error.phtml') ->label('error', __DIR__ . '/../../views/general/error.phtml')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk', new Method(
->label('sdk.method', 'createGitHubInstallation') namespace: 'vcs',
->label('sdk.description', '') name: 'createGitHubInstallation',
->label('sdk.response.code', Response::STATUS_CODE_MOVED_PERMANENTLY) description: '/docs/references/vcs/create-github-installation.md',
->label('sdk.response.type', Response::CONTENT_TYPE_HTML) auth: [AuthType::ADMIN],
->label('sdk.methodType', 'webAuth') responses: [
->label('sdk.hide', true) new SDKResponse(
code: Response::STATUS_CODE_MOVED_PERMANENTLY,
model: Response::MODEL_NONE,
)
],
contentType: ContentType::HTML,
type: MethodType::WEBAUTH,
hide: true,
))
->param('success', '', fn ($clients) => new Host($clients), 'URL to redirect back to console after a successful installation attempt.', true, ['clients']) ->param('success', '', fn ($clients) => new Host($clients), 'URL to redirect back to console after a successful installation attempt.', true, ['clients'])
->param('failure', '', fn ($clients) => new Host($clients), 'URL to redirect back to console after a failed installation attempt.', true, ['clients']) ->param('failure', '', fn ($clients) => new Host($clients), 'URL to redirect back to console after a failed installation attempt.', true, ['clients'])
->inject('request') ->inject('request')
@ -444,13 +454,18 @@ App::get('/v1/vcs/github/installations/:installationId/providerRepositories/:pro
->desc('Get files and directories of a VCS repository') ->desc('Get files and directories of a VCS repository')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.read') ->label('scope', 'vcs.read')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'getRepositoryContents') name: 'getRepositoryContents',
->label('sdk.description', '') description: '/docs/references/vcs/get-repository-contents.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_VCS_CONTENT_LIST) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_VCS_CONTENT_LIST,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id')
->param('providerRootDirectory', '', new Text(256, 0), 'Path to get contents of nested directory', true) ->param('providerRootDirectory', '', new Text(256, 0), 'Path to get contents of nested directory', true)
@ -505,13 +520,18 @@ App::post('/v1/vcs/github/installations/:installationId/providerRepositories/:pr
->desc('Detect runtime settings from source code') ->desc('Detect runtime settings from source code')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.write') ->label('scope', 'vcs.write')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'createRepositoryDetection') name: 'createRepositoryDetection',
->label('sdk.description', '') description: '/docs/references/vcs/create-repository-detection.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_DETECTION) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_DETECTION,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id')
->param('providerRootDirectory', '', new Text(256, 0), 'Path to Root Directory', true) ->param('providerRootDirectory', '', new Text(256, 0), 'Path to Root Directory', true)
@ -577,13 +597,18 @@ App::get('/v1/vcs/github/installations/:installationId/providerRepositories')
->desc('List repositories') ->desc('List repositories')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.read') ->label('scope', 'vcs.read')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'listRepositories') name: 'listRepositories',
->label('sdk.description', '') description: '/docs/references/vcs/list-repositories.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_PROVIDER_REPOSITORY_LIST) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_PROVIDER_REPOSITORY_LIST,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->inject('gitHub') ->inject('gitHub')
@ -672,13 +697,18 @@ App::post('/v1/vcs/github/installations/:installationId/providerRepositories')
->desc('Create repository') ->desc('Create repository')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.write') ->label('scope', 'vcs.write')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'createRepository') name: 'createRepository',
->label('sdk.description', '') description: '/docs/references/vcs/create-repository.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_PROVIDER_REPOSITORY) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_PROVIDER_REPOSITORY,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->param('name', '', new Text(256), 'Repository name (slug)') ->param('name', '', new Text(256), 'Repository name (slug)')
->param('private', '', new Boolean(false), 'Mark repository public or private') ->param('private', '', new Boolean(false), 'Mark repository public or private')
@ -779,13 +809,18 @@ App::get('/v1/vcs/github/installations/:installationId/providerRepositories/:pro
->desc('Get repository') ->desc('Get repository')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.read') ->label('scope', 'vcs.read')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'getRepository') name: 'getRepository',
->label('sdk.description', '') description: '/docs/references/vcs/get-repository.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_PROVIDER_REPOSITORY) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_PROVIDER_REPOSITORY,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id')
->inject('gitHub') ->inject('gitHub')
@ -828,13 +863,18 @@ App::get('/v1/vcs/github/installations/:installationId/providerRepositories/:pro
->desc('List repository branches') ->desc('List repository branches')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.read') ->label('scope', 'vcs.read')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'listRepositoryBranches') name: 'listRepositoryBranches',
->label('sdk.description', '') description: '/docs/references/vcs/list-repository-branches.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_BRANCH_LIST) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_BRANCH_LIST,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id')
->inject('gitHub') ->inject('gitHub')
@ -885,55 +925,8 @@ App::post('/v1/vcs/github/events')
->inject('queueForBuilds') ->inject('queueForBuilds')
->action( ->action(
function (GitHub $github, Request $request, Response $response, Database $dbForPlatform, callable $getProjectDB, Build $queueForBuilds) use ($createGitDeployments) { function (GitHub $github, Request $request, Response $response, Database $dbForPlatform, callable $getProjectDB, Build $queueForBuilds) use ($createGitDeployments) {
$payload = $request->getRawPayload(); $payload = $request->getRawPayload();
$event = $request->getHeader('x-github-event', ''); var_dump(['payload' => $payload]);
$parsedPayload = $github->getEvent($event, $payload);
$providerRepositoryId = $parsedPayload["repositoryId"] ?? '';
$repository = Authorization::skip(fn () => $dbForPlatform->findOne('repositories', [
Query::equal('providerRepositoryId', [$providerRepositoryId]),
]));
$projectId = $repository->getAttribute('projectId');
var_dump([
'providerRepositoryId' => $providerRepositoryId,
'parsedPayload' => $parsedPayload,
'projectId' => $projectId,
]);
// if (empty($projectId)) {
// throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, "Project was not found");
// }
//
// $project = $dbForPlatform->getDocument('projects', $projectId);
//
// if($project->getAttribute('region') !== System::getEnv('_APP_REGION')){
// $networkDomains = [];
// $tmp = explode(',', System::getEnv('_APP_NETWORK_DOMAINS'));
// foreach ($tmp as $domain) {
// $parts = explode('=', $domain);
// $networkDomains[$parts[0]] = $parts[1];
// }
//
// $targetUrl = 'https://'.$networkDomains[$project->getAttribute('region')].'/v1/vcs/github/events';
// $client = new Client();
// foreach ($request->getHeaders() as $name => $value) {
// $client->addHeader($name, $value);
// }
//
// try {
// $client->fetch(
// url: $targetUrl,
// method: Client::METHOD_POST,
// body: $payload
// );
// } catch (Throwable $th) {
// throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, "Error while redirecting to $targetUrl");
// }
// }
$signatureRemote = $request->getHeader('x-hub-signature-256', ''); $signatureRemote = $request->getHeader('x-hub-signature-256', '');
$signatureLocal = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); $signatureLocal = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', '');
@ -943,10 +936,10 @@ App::post('/v1/vcs/github/events')
throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, "Invalid webhook payload signature. Please make sure the webhook secret has same value in your GitHub app and in the _APP_VCS_GITHUB_WEBHOOK_SECRET environment variable"); throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, "Invalid webhook payload signature. Please make sure the webhook secret has same value in your GitHub app and in the _APP_VCS_GITHUB_WEBHOOK_SECRET environment variable");
} }
$event = $request->getHeader('x-github-event', '');
$privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY');
$githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID');
$parsedPayload = $github->getEvent($event, $payload);
if ($event == $github::EVENT_PUSH) { if ($event == $github::EVENT_PUSH) {
$providerBranchCreated = $parsedPayload["branchCreated"] ?? false; $providerBranchCreated = $parsedPayload["branchCreated"] ?? false;
@ -954,8 +947,8 @@ App::post('/v1/vcs/github/events')
$providerBranchUrl = $parsedPayload["branchUrl"] ?? ''; $providerBranchUrl = $parsedPayload["branchUrl"] ?? '';
$providerRepositoryId = $parsedPayload["repositoryId"] ?? ''; $providerRepositoryId = $parsedPayload["repositoryId"] ?? '';
$providerRepositoryName = $parsedPayload["repositoryName"] ?? ''; $providerRepositoryName = $parsedPayload["repositoryName"] ?? '';
$providerRepositoryUrl = $parsedPayload["repositoryUrl"] ?? '';
$providerInstallationId = $parsedPayload["installationId"] ?? ''; $providerInstallationId = $parsedPayload["installationId"] ?? '';
$providerRepositoryUrl = $parsedPayload["repositoryUrl"] ?? '';
$providerCommitHash = $parsedPayload["commitHash"] ?? ''; $providerCommitHash = $parsedPayload["commitHash"] ?? '';
$providerRepositoryOwner = $parsedPayload["owner"] ?? ''; $providerRepositoryOwner = $parsedPayload["owner"] ?? '';
$providerCommitAuthor = $parsedPayload["headCommitAuthor"] ?? ''; $providerCommitAuthor = $parsedPayload["headCommitAuthor"] ?? '';
@ -1064,13 +1057,18 @@ App::get('/v1/vcs/installations')
->desc('List installations') ->desc('List installations')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.read') ->label('scope', 'vcs.read')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'listInstallations') name: 'listInstallations',
->label('sdk.description', '/docs/references/vcs/list-installations.md') description: '/docs/references/vcs/list-installations.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_INSTALLATION_LIST) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_INSTALLATION_LIST,
)
]
))
->param('queries', [], new Installations(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Installations::ALLOWED_ATTRIBUTES), true) ->param('queries', [], new Installations(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Installations::ALLOWED_ATTRIBUTES), true)
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->inject('response') ->inject('response')
@ -1130,13 +1128,18 @@ App::get('/v1/vcs/installations/:installationId')
->desc('Get installation') ->desc('Get installation')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.read') ->label('scope', 'vcs.read')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'getInstallation') name: 'getInstallation',
->label('sdk.description', '/docs/references/vcs/get-installation.md') description: '/docs/references/vcs/get-installation.md',
->label('sdk.response.code', Response::STATUS_CODE_OK) auth: [AuthType::ADMIN],
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) responses: [
->label('sdk.response.model', Response::MODEL_INSTALLATION) new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_INSTALLATION,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->inject('response') ->inject('response')
->inject('project') ->inject('project')
@ -1159,12 +1162,19 @@ App::delete('/v1/vcs/installations/:installationId')
->desc('Delete installation') ->desc('Delete installation')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.write') ->label('scope', 'vcs.write')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'deleteInstallation') name: 'deleteInstallation',
->label('sdk.description', '/docs/references/vcs/delete-installation.md') description: '/docs/references/vcs/delete-installation.md',
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) auth: [AuthType::ADMIN],
->label('sdk.response.model', Response::MODEL_NONE) responses: [
new SDKResponse(
code: Response::STATUS_CODE_NOCONTENT,
model: Response::MODEL_NONE,
)
],
contentType: ContentType::NONE
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->inject('response') ->inject('response')
->inject('project') ->inject('project')
@ -1192,12 +1202,18 @@ App::patch('/v1/vcs/github/installations/:installationId/repositories/:repositor
->desc('Authorize external deployment') ->desc('Authorize external deployment')
->groups(['api', 'vcs']) ->groups(['api', 'vcs'])
->label('scope', 'vcs.write') ->label('scope', 'vcs.write')
->label('sdk.namespace', 'vcs') ->label('sdk', new Method(
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) namespace: 'vcs',
->label('sdk.method', 'updateExternalDeployments') name: 'updateExternalDeployments',
->label('sdk.description', '') description: '/docs/references/vcs/update-external-deployments.md',
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) auth: [AuthType::ADMIN],
->label('sdk.response.model', Response::MODEL_NONE) responses: [
new SDKResponse(
code: Response::STATUS_CODE_NOCONTENT,
model: Response::MODEL_NONE,
)
]
))
->param('installationId', '', new Text(256), 'Installation Id') ->param('installationId', '', new Text(256), 'Installation Id')
->param('repositoryId', '', new Text(256), 'VCS Repository Id') ->param('repositoryId', '', new Text(256), 'VCS Repository Id')
->param('providerPullRequestId', '', new Text(256), 'GitHub Pull Request Id') ->param('providerPullRequestId', '', new Text(256), 'GitHub Pull Request Id')