From 76da7716f276c2f0e910bce617f99fa84599493e Mon Sep 17 00:00:00 2001 From: Sergey Grigoriev Date: Fri, 13 Sep 2024 09:41:02 +0200 Subject: [PATCH 1/2] fix: JavaScript refactoring and fixes for test runs support --- .../ConverterInternalController.java | 2 +- .../pdf-exporter/js/modules/ExportContext.js | 37 +++++++++++++------ .../webapp/pdf-exporter/js/pdf-exporter.js | 8 ++-- src/test/js/ExportContextTest.js | 21 +++++++++-- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/ConverterInternalController.java b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/ConverterInternalController.java index 6a497c4..5377d01 100644 --- a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/ConverterInternalController.java +++ b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/ConverterInternalController.java @@ -327,7 +327,7 @@ private void validateExportParameters(ExportParams exportParams) { if (exportParams.getDocumentType() == DocumentType.LIVE_DOC && exportParams.getProjectId() == null) { throw new BadRequestException("Parameter 'projectId' should be provided"); } - if (exportParams.getLocationPath() == null) { + if (exportParams.getLocationPath() == null && exportParams.getDocumentType() != DocumentType.TEST_RUN) { throw new BadRequestException("Parameter 'locationPath' should be provided"); } } diff --git a/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js b/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js index bdca070..0bc7200 100644 --- a/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js +++ b/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js @@ -18,8 +18,10 @@ export default class ExportContext { this.projectId = getProjectId(scope); this.locationPath = getPath(normalizedPolarionLocationHash, scope); - if (this.locationPath === "testrun") { + // if "testrun" or "testruns" is present return undefined + if (this.locationPath?.startsWith("testrun")) { this.documentType = ExportParams.DocumentType.TEST_RUN; + this.locationPath = undefined; } this.urlQueryParameters = getQueryParams(searchParameters); @@ -58,13 +60,14 @@ export default class ExportContext { function getPath(locationHash, scope) { if (scope) { - const pathPattern = /project\/[^/]+\/(wiki\/([^?#]+)|testrun)/; + const pathPattern = /project\/[^\/]+\/(wiki\/([^?#]+)|testruns|testrun)/; const pathMatch = pathPattern.exec(locationHash); - return pathMatch ? addDefaultSpaceIfRequired(pathMatch[2] || "testrun") : ""; + const extractedPath = pathMatch ? (pathMatch[2] || pathMatch[1]) : undefined; + return pathMatch ? addDefaultSpaceIfRequired(extractedPath) : undefined; } else { const globalPathPattern = /wiki\/([^/?#]+)/; const pathMatch = globalPathPattern.exec(locationHash); - return pathMatch ? addDefaultSpaceIfRequired(pathMatch[1]) : ""; + return pathMatch ? addDefaultSpaceIfRequired(pathMatch[1]) : undefined; } } @@ -72,13 +75,17 @@ export default class ExportContext { if (!extractedPath) { return ""; } - // if contains a '/' or is exactly 'testrun', return it as it is - if (extractedPath.includes("/") || extractedPath === "testrun") { + // if "testrun" or "testruns" is present return undefined + if (extractedPath.startsWith("testrun")) { + return extractedPath; + } + // if contains a '/' return it as it is + if (extractedPath.includes("/")) { return extractedPath; } // otherwise, prepend '_default/' to the path return `_default/${extractedPath}`; - } + }; function getQueryParams(searchParams) { if (!searchParameters) { @@ -116,13 +123,21 @@ export default class ExportContext { } getSpaceId() { - const pathParts = this.locationPath.split("/"); - return pathParts && pathParts.length > 0 && pathParts[0]; + if (this.locationPath?.includes("/")) { + const pathParts = this.locationPath.split("/"); + return pathParts && pathParts.length > 0 && pathParts[0]; + } else { + return undefined; + } } getDocumentName() { - const pathParts = this.locationPath.split("/"); - return pathParts && pathParts.length > 1 && pathParts[1]; + if (this.locationPath?.includes("/")) { + const pathParts = this.locationPath.split("/"); + return pathParts && pathParts.length > 1 && pathParts[1]; + } else { + return undefined; + } } toExportParams() { diff --git a/src/main/resources/webapp/pdf-exporter/js/pdf-exporter.js b/src/main/resources/webapp/pdf-exporter/js/pdf-exporter.js index 50b3a6e..a058a57 100644 --- a/src/main/resources/webapp/pdf-exporter/js/pdf-exporter.js +++ b/src/main/resources/webapp/pdf-exporter/js/pdf-exporter.js @@ -43,17 +43,17 @@ const PdfExporter = { }, openPopup: function (params) { + this.exportContext = params?.exportContext ? params.exportContext : new ExportContext(); + this.hideAlerts(); - this.loadFormData(params); + this.loadFormData(); const reportContext = this.exportContext.getDocumentType() === ExportParams.DocumentType.LIVE_REPORT || this.exportContext.getDocumentType() === ExportParams.DocumentType.TEST_RUN; document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.only-live-doc") .forEach(propertyBlock => propertyBlock.style.display = (reportContext ? "none" : "flex")); MicroModal.show('pdf-export-modal-popup'); }, - loadFormData: function (params) { - this.exportContext = params?.exportContext ? params.exportContext : new ExportContext(); - + loadFormData: function () { this.actionInProgress({inProgress: true, message: "Loading form data"}); Promise.all([ diff --git a/src/test/js/ExportContextTest.js b/src/test/js/ExportContextTest.js index a85408b..1ed4970 100644 --- a/src/test/js/ExportContextTest.js +++ b/src/test/js/ExportContextTest.js @@ -79,12 +79,12 @@ describe('ExportContext Class', function () { expect(exportContext.documentType).to.equal(ExportParams.DocumentType.TEST_RUN); expect(exportContext.projectId).to.equal('elibrary'); - expect(exportContext.locationPath).to.equal('testrun'); + expect(exportContext.locationPath).to.be.undefined; expect(exportContext.revision).to.be.undefined; expect(exportContext.urlQueryParameters).to.deep.equal({ id: 'elibrary_20231026-163136654' }); - expect(exportContext.getSpaceId()).to.equal('testrun'); - expect(exportContext.getDocumentName()).to.be.false; // No document name for testrun + expect(exportContext.getSpaceId()).to.be.undefined; + expect(exportContext.getDocumentName()).to.be.undefined; }); it('URL: #/project/elibrary/wiki/Reports/LiveReport%20with%20params?stringParameter=asd&workItemType=changerequest&yesnoParameter=yes', function () { @@ -104,4 +104,19 @@ describe('ExportContext Class', function () { expect(exportContext.getSpaceId()).to.equal('Reports'); expect(exportContext.getDocumentName()).to.equal('LiveReport with params'); }); + + it('URL: #/project/elibrary/testruns', function () { + const locationHash = "#/project/elibrary/testruns"; + const exportContext = new ExportContext(ExportParams.DocumentType.LIVE_REPORT, locationHash); + + expect(exportContext.documentType).to.equal(ExportParams.DocumentType.TEST_RUN); + expect(exportContext.projectId).to.equal('elibrary'); + expect(exportContext.locationPath).to.be.undefined; + expect(exportContext.revision).to.be.undefined; + expect(exportContext.urlQueryParameters).to.be.undefined; + + expect(exportContext.getSpaceId()).to.be.undefined; + expect(exportContext.getDocumentName()).to.be.undefined; + }); + }); From 89fb73f83faab427e0d9611d5d63ce8ce1b30a71 Mon Sep 17 00:00:00 2001 From: Sergey Grigoriev Date: Fri, 13 Sep 2024 09:43:26 +0200 Subject: [PATCH 2/2] fix: JavaScript refactoring and fixes for test runs support --- .../resources/webapp/pdf-exporter/js/modules/ExportContext.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js b/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js index 0bc7200..4091153 100644 --- a/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js +++ b/src/main/resources/webapp/pdf-exporter/js/modules/ExportContext.js @@ -60,7 +60,7 @@ export default class ExportContext { function getPath(locationHash, scope) { if (scope) { - const pathPattern = /project\/[^\/]+\/(wiki\/([^?#]+)|testruns|testrun)/; + const pathPattern = /project\/[^/]+\/(wiki\/([^?#]+)|testruns|testrun)/; const pathMatch = pathPattern.exec(locationHash); const extractedPath = pathMatch ? (pathMatch[2] || pathMatch[1]) : undefined; return pathMatch ? addDefaultSpaceIfRequired(extractedPath) : undefined;