From 8f039f9584c19569ccc3501176a68e95f662d6b8 Mon Sep 17 00:00:00 2001 From: YurgenUA Date: Fri, 16 Aug 2019 11:48:00 +0300 Subject: [PATCH 1/5] tackle sharedAPI full rewrite logic --- src/documentation.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/documentation.js b/src/documentation.js index 4862832..b718ad7 100644 --- a/src/documentation.js +++ b/src/documentation.js @@ -92,8 +92,13 @@ module.exports = function() { } }); }, + + _isSharedApi: function _isSharedApi(){ + return !!this.serverless.service.provider.apiGateway.restApiId; + }, _updateDocumentation: function _updateDocumentation() { + const serverlessServicePropertyKey = 'serverless-service'; const aws = this.serverless.providers.aws; return aws.request('APIGateway', 'getDocumentationVersion', { restApiId: this.restApiId, @@ -116,7 +121,14 @@ module.exports = function() { limit: 9999, }) ) - .then(results => results.items.map( + .then(results => results.items.filter(part => { + if (this._isSharedApi()){ + console.log('---- filtering docpart with properties', part.properties); + return JSON.parse(part.properties)[serverlessServicePropertyKey] === this.serverless.service.service; + } + return true; + }) + .map( part => aws.request('APIGateway', 'deleteDocumentationPart', { documentationPartId: part.id, restApiId: this.restApiId, @@ -125,7 +137,11 @@ module.exports = function() { .then(promises => Promise.all(promises)) .then(() => this.documentationParts.reduce((promise, part) => { return promise.then(() => { + if (this._isSharedApi()) { + part.properties[serverlessServicePropertyKey] = this.serverless.service.service; + } part.properties = JSON.stringify(part.properties); + console.log('---- sending to createDocumentationPart', part.properties); return aws.request('APIGateway', 'createDocumentationPart', part); }); }, Promise.resolve())) From 09e777532d2c716db8e2e4e8334e38bfecf2700c Mon Sep 17 00:00:00 2001 From: YurgenUA Date: Mon, 19 Aug 2019 11:20:20 +0300 Subject: [PATCH 2/5] remove console.log --- src/documentation.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/documentation.js b/src/documentation.js index b718ad7..b4f95d3 100644 --- a/src/documentation.js +++ b/src/documentation.js @@ -123,7 +123,6 @@ module.exports = function() { ) .then(results => results.items.filter(part => { if (this._isSharedApi()){ - console.log('---- filtering docpart with properties', part.properties); return JSON.parse(part.properties)[serverlessServicePropertyKey] === this.serverless.service.service; } return true; @@ -141,7 +140,6 @@ module.exports = function() { part.properties[serverlessServicePropertyKey] = this.serverless.service.service; } part.properties = JSON.stringify(part.properties); - console.log('---- sending to createDocumentationPart', part.properties); return aws.request('APIGateway', 'createDocumentationPart', part); }); }, Promise.resolve())) From 8016f148b6702321fc6fa1e698c18dd129ec566e Mon Sep 17 00:00:00 2001 From: YurgenUA Date: Mon, 19 Aug 2019 15:01:33 +0300 Subject: [PATCH 3/5] fix when 'apiGateway' is not defined in yml --- src/documentation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/documentation.js b/src/documentation.js index b4f95d3..bae8081 100644 --- a/src/documentation.js +++ b/src/documentation.js @@ -94,7 +94,7 @@ module.exports = function() { }, _isSharedApi: function _isSharedApi(){ - return !!this.serverless.service.provider.apiGateway.restApiId; + return this.serverless.service.provider.apiGateway ? !!this.serverless.service.provider.apiGateway.restApiId : false; }, _updateDocumentation: function _updateDocumentation() { From 71f3e49b58db5ad904d1315dfb866d23b12dec68 Mon Sep 17 00:00:00 2001 From: YurgenUA Date: Wed, 20 Nov 2019 10:59:27 +0200 Subject: [PATCH 4/5] add unit test --- src/index.spec.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/index.spec.js b/src/index.spec.js index 0118ad3..32d6745 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -3313,6 +3313,52 @@ describe('ServerlessAWSDocumentation', function () { }); }); + it('should not delete another documentation parts in case of shared API usage', function (done) { + this.serverlessMock.service.provider = + { + ... this.serverlessMock.service.provider, + apiGateway: { + restApiId: 'superid', + }, + }; + + this.serverlessMock.providers.aws.request.and.callFake((api, method) => { + switch (method) { + case 'describeStacks': + return Promise.resolve({ + Stacks: [{ + Outputs: [{ + OutputKey: 'ApiId', + OutputValue: 'superid', + }], + }], + }); + case 'getDocumentationParts': + return Promise.resolve({ + items: [{ + id: '123', + properties: JSON.stringify({'serverless-service': 'service1'}) + }, { + id: '456', + properties: JSON.stringify({'serverless-service': 'service2'}) + }], + }); + case 'getDocumentationVersion': + return Promise.reject(new Error('Invalid Documentation version specified')); + case 'deleteDocumentationPart': + return Promise.reject(); + default: + return Promise.resolve(); + } + }); + this.serverlessMock.providers.aws.naming.getStackName.and.returnValue('superstack'); + + this.plugin.afterDeploy().then(() => { + expect(this.serverlessMock.providers.aws.request).not.toHaveBeenCalledWith('APIGateway', 'deleteDocumentationPart', jasmine.any(Object)); + done(); + }); + }); + it('should generate different documentation versions for different documentation content', function() { this.serverlessMock.variables.service.custom.documentation.api = { description: 'this is an api', From d3c6373be45406ee3c06dd8f9fb57bf32548178f Mon Sep 17 00:00:00 2001 From: YurgenUA Date: Wed, 20 Nov 2019 11:07:38 +0200 Subject: [PATCH 5/5] amend unit test to plase Travis CI --- src/index.spec.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/index.spec.js b/src/index.spec.js index 32d6745..85745ca 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -3314,12 +3314,8 @@ describe('ServerlessAWSDocumentation', function () { }); it('should not delete another documentation parts in case of shared API usage', function (done) { - this.serverlessMock.service.provider = - { - ... this.serverlessMock.service.provider, - apiGateway: { - restApiId: 'superid', - }, + this.serverlessMock.service.provider.apiGateway = { + restApiId: 'superid', }; this.serverlessMock.providers.aws.request.and.callFake((api, method) => { @@ -3337,10 +3333,10 @@ describe('ServerlessAWSDocumentation', function () { return Promise.resolve({ items: [{ id: '123', - properties: JSON.stringify({'serverless-service': 'service1'}) + properties: JSON.stringify({ 'serverless-service': 'service1' }) }, { id: '456', - properties: JSON.stringify({'serverless-service': 'service2'}) + properties: JSON.stringify({ 'serverless-service': 'service2' }) }], }); case 'getDocumentationVersion':