diff --git a/assets/src/bundles/save/index.js b/assets/src/bundles/save/index.js --- a/assets/src/bundles/save/index.js +++ b/assets/src/bundles/save/index.js @@ -46,6 +46,23 @@ }); } +function addArtifactVersionAutofillHandler(formId) { + // autofill artifact version input with the filename from + // the artifact url without extensions + $(`#swh-input-artifact-url-${formId}`).on('input', function(event) { + const artifactUrl = $(this).val().trim(); + let filename = artifactUrl.split('/').slice(-1)[0]; + if (filename !== artifactUrl) { + filename = filename.replace(/tar.*$/, 'tar'); + const filenameNoExt = filename.split('.').slice(0, -1).join('.'); + const artifactVersion = $(`#swh-input-artifact-version-${formId}`); + if (filenameNoExt !== filename) { + artifactVersion.val(filenameNoExt); + } + } + }); +} + export function maybeRequireExtraInputs() { // Read the actual selected value and depending on the origin type, display some extra // inputs or hide them. This makes the extra inputs disabled when not displayed. @@ -68,16 +85,19 @@ // insert first artifact row when the archives visit type is selected for the first time $('.swh-save-origin-archives-form').last().after( artifactFormRowTemplate({deletableRow: false, formId: 0})); + addArtifactVersionAutofillHandler(0); } } export function addArtifactFormRow() { + const formId = $('.swh-save-origin-artifact-form').length; $('.swh-save-origin-artifact-form').last().after( artifactFormRowTemplate({ deletableRow: true, - formId: $('.swh-save-origin-artifact-form').length + formId: formId }) ); + addArtifactVersionAutofillHandler(formId); } export function deleteArtifactFormRow(event) { diff --git a/cypress/integration/origin-save.spec.js b/cypress/integration/origin-save.spec.js --- a/cypress/integration/origin-save.spec.js +++ b/cypress/integration/origin-save.spec.js @@ -538,6 +538,7 @@ .get('#swh-input-artifact-url-0') .type(artifactUrl) .get('#swh-input-artifact-version-0') + .clear() .type(artifactVersion) .get('#swh-save-origin-form') .submit(); @@ -567,6 +568,7 @@ cy.get('#swh-input-artifact-url-0') .type(artifactUrl) .get('#swh-input-artifact-version-0') + .clear() .type(artifactVersion); // add new artifact form row @@ -593,6 +595,7 @@ cy.get('#swh-input-artifact-url-1') .type(artifact2Url) .get('#swh-input-artifact-version-1') + .clear() .type(artifact2Version); // setup request interceptor to check POST data and stub response @@ -625,4 +628,40 @@ }); + it('should autofill artifact version when pasting artifact url', function() { + let originUrl = 'https://ftp.gnu.org/pub/pub/gnu/3dldf'; + let artifactUrl = 'https://ftp.gnu.org/pub/pub/gnu/3dldf/3DLDF-1.1.4.tar.gz'; + let artifactVersion = '3DLDF-1.1.4'; + let artifact2Url = 'https://example.org/artifact/test/1.3.0.zip'; + let artifact2Version = '1.3.0'; + + cy.ambassadorLogin(); + cy.visit(url); + + cy.get('#swh-input-origin-url') + .type(originUrl) + .get('#swh-input-visit-type') + .select('archives'); + + // fill first artifact info + cy.get('#swh-input-artifact-url-0') + .type(artifactUrl); + + // check autofilled version + cy.get('#swh-input-artifact-version-0') + .should('have.value', artifactVersion); + + // add new artifact form row + cy.get('#swh-add-archive-artifact') + .click(); + + // fill second artifact info + cy.get('#swh-input-artifact-url-1') + .type(artifact2Url); + + // check autofilled version + cy.get('#swh-input-artifact-version-1') + .should('have.value', artifact2Version); + }); + });