Skip to content
Snippets Groups Projects
Commit 4ce23c9a authored by Antoine Lambert's avatar Antoine Lambert Committed by Antoine Lambert
Browse files

browse/search: Handle visit type when search pattern is an origin URL

When searching for origins with the Web UI, if the search pattern is equal
to the URL of an archived origin then user gets automatically redirected to
the interface for browsing it.

Previously the visit type was not taken into account prior performing the
redirection so ensure to redirect only if the origin has a visit type
that matches the selected one.
parent f98b1824
No related branches found
No related tags found
1 merge request!1252browse/search: Handle visit type when search pattern is an origin URL
Pipeline #7801 passed
......@@ -115,13 +115,18 @@ export function validateUrl(url, allowedProtocols = []) {
return validUrl ? originUrl : null;
}
export async function isArchivedOrigin(originPath) {
export async function isArchivedOrigin(originPath, visitType) {
if (!validateUrl(originPath)) {
// Not a valid URL, return immediately
return false;
} else {
const response = await fetch(Urls.api_1_origin(originPath));
return response.ok && response.status === 200; // Success response represents an archived origin
if (!response.ok || response.status !== 200) {
return false;
} else {
const originData = await response.json();
return !visitType || visitType === 'any' || originData.visit_types.includes(visitType);
}
}
}
......
......@@ -339,6 +339,21 @@ describe('Test origin-search', function() {
});
});
it('should redirect to browse origin with selected visit type when search pattern is equal to origin URL', function() {
const searchPattern = 'https://example.org/project/multiple/visit/types';
for (const visitType of ['git', 'git-checkout']) {
cy.get('#swh-origins-url-patterns')
.type(searchPattern);
cy.get('#swh-search-visit-type')
.select(visitType);
cy.get('.swh-search-icon')
.click();
cy.url().should('include', `/browse/origin/directory/?origin_url=${searchPattern}&visit_type=${visitType}`);
cy.get('#swh-origin-visit-type').should('have.value', visitType);
cy.visit(url);
}
});
it('should encode ? in origin URL provided as argument in latest visit URL queried by XHR', function() {
// origin added in tests data by Python
const originUrl = 'https://example.org/project/download.php?version=2.0';
......
......@@ -179,6 +179,7 @@ async function searchOrigins(searchUrl) {
async function doSearch() {
$('#swh-no-result').hide();
const searchQueryText = $('#swh-origins-url-patterns').val();
const visitType = $('#swh-search-visit-type').val();
inSearch = true;
if (searchQueryText.startsWith('swh:')) {
try {
......@@ -199,10 +200,14 @@ async function doSearch() {
$('#swh-no-result').text(data.reason);
$('#swh-no-result').show();
}
} else if (await isArchivedOrigin(searchQueryText)) {
} else if (await isArchivedOrigin(searchQueryText, visitType)) {
// redirect to the browse origin
window.location.href =
let browseOriginUrl =
`${Urls.browse_origin()}?origin_url=${encodeURIComponent(searchQueryText)}`;
if (visitType && visitType !== 'any') {
browseOriginUrl += `&visit_type=${visitType}`;
}
window.location.href = browseOriginUrl;
} else {
// otherwise, proceed with origins search irrespective of the error
$('#swh-origin-search-results').show();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment