From ed192b9cb15753fc2cdf5cb76e38eb1857a3307c Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 7 Nov 2022 20:57:31 +0100 Subject: [PATCH] Playground API - retrieveSample --- playground-api/handler.ts | 40 +++++++++++++++++++++++++++++++++++ playground-api/serverless.yml | 9 ++++++++ 2 files changed, 49 insertions(+) diff --git a/playground-api/handler.ts b/playground-api/handler.ts index cfe45aff91..c4ab5a904b 100644 --- a/playground-api/handler.ts +++ b/playground-api/handler.ts @@ -294,6 +294,45 @@ async function retrieveResult(request: HttpRequest): Promise { } } +async function retrieveSample(request: HttpRequest): Promise { + try { + const id = request.queryStringParameters.id; + const object = await s3.getObject({ + Bucket: 'phpstan-playground', + Key: 'api/results/' + id + '.json', + }).promise(); + const json = JSON.parse(object.Body as string); + const strictRules = typeof json.config.strictRules !== 'undefined' ? json.config.strictRules : false; + const bleedingEdge = typeof json.config.bleedingEdge !== 'undefined' ? json.config.bleedingEdge : false; + const treatPhpDocTypesAsCertain = typeof json.config.treatPhpDocTypesAsCertain !== 'undefined' ? json.config.treatPhpDocTypesAsCertain : true; + + const bodyJson: any = { + code: json.code, + errors: json.errors, + version: json.version, + level: json.level, + config: { + strictRules, + bleedingEdge, + treatPhpDocTypesAsCertain, + }, + }; + if (typeof json.versionedErrors !== 'undefined') { + bodyJson.versionedErrors = json.versionedErrors; + } else { + bodyJson.versionedErrors = [{errors: json.errors, title: 'PHP 7.4'}]; + } + return Promise.resolve({ + statusCode: 200, + body: JSON.stringify(bodyJson), + }); + } catch (e) { + console.error(e); + captureException(e); + return Promise.resolve({statusCode: 500}); + } +} + async function retrieveLegacyResult(request: HttpRequest): Promise { try { const id = request.queryStringParameters.id; @@ -343,5 +382,6 @@ const corsMiddleware = cors(); module.exports = { analyseResult: middy(analyseResult).use(corsMiddleware), retrieveResult: middy(retrieveResult).use(corsMiddleware), + retrieveSample: middy(retrieveSample).use(corsMiddleware), retrieveLegacyResult: middy(retrieveLegacyResult).use(corsMiddleware), }; diff --git a/playground-api/serverless.yml b/playground-api/serverless.yml index f99bfda7b1..1678cbc47c 100644 --- a/playground-api/serverless.yml +++ b/playground-api/serverless.yml @@ -29,6 +29,15 @@ functions: path: /result cors: true + retrieveSample: + handler: handler.retrieveSample + role: arn:aws:iam::928192134594:role/lambda-phpstan-playground + events: + - http: + method: get + path: /sample + cors: true + retrieveLegacyResult: handler: handler.retrieveLegacyResult role: arn:aws:iam::928192134594:role/lambda-phpstan-playground