Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/save/index.js
/** | /** | ||||
* Copyright (C) 2018-2020 The Software Heritage developers | * Copyright (C) 2018-2020 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
import {handleFetchError, csrfPost, isGitRepoUrl, htmlAlert, removeUrlFragment} from 'utils/functions'; | import {handleFetchError, csrfPost, isGitRepoUrl, htmlAlert, removeUrlFragment} from 'utils/functions'; | ||||
import {swhSpinnerSrc} from 'utils/constants'; | import {swhSpinnerSrc} from 'utils/constants'; | ||||
import {validate} from 'validate.js'; | |||||
let saveRequestsTable; | let saveRequestsTable; | ||||
function originSaveRequest(originType, originUrl, | function originSaveRequest(originType, originUrl, | ||||
acceptedCallback, pendingCallback, errorCallback) { | acceptedCallback, pendingCallback, errorCallback) { | ||||
let addSaveOriginRequestUrl = Urls.origin_save_request(originType, originUrl); | let addSaveOriginRequestUrl = Urls.origin_save_request(originType, originUrl); | ||||
let headers = { | let headers = { | ||||
'Accept': 'application/json', | 'Accept': 'application/json', | ||||
▲ Show 20 Lines • Show All 214 Lines • ▼ Show 20 Lines | if (window.location.hash === '#requests') { | ||||
$('.nav-tabs a[href="#swh-origin-save-requests-list"]').tab('show'); | $('.nav-tabs a[href="#swh-origin-save-requests-list"]').tab('show'); | ||||
} | } | ||||
}); | }); | ||||
} | } | ||||
export function validateSaveOriginUrl(input) { | export function validateSaveOriginUrl(input) { | ||||
let originUrl = input.value.trim(); | let originType = $('#swh-input-visit-type').val(); | ||||
let validUrl = validate({website: originUrl}, { | let originUrl = null; | ||||
website: { | let validUrl = true; | ||||
url: { | |||||
schemes: ['http', 'https', 'svn', 'git'] | try { | ||||
originUrl = new URL(input.value.trim()); | |||||
} catch (TypeError) { | |||||
validUrl = false; | |||||
} | } | ||||
if (validUrl) { | |||||
let allowedProtocols = ['http:', 'https:', 'svn:', 'git:']; | |||||
validUrl = ( | |||||
allowedProtocols.find(protocol => protocol === originUrl.protocol) !== undefined | |||||
); | |||||
} | } | ||||
}) === undefined; | |||||
let originType = $('#swh-input-visit-type').val(); | if (validUrl && originType === 'git') { | ||||
if (originType === 'git' && validUrl) { | |||||
// additional checks for well known code hosting providers | // additional checks for well known code hosting providers | ||||
let githubIdx = originUrl.indexOf('://github.com'); | switch (originUrl.hostname) { | ||||
let gitlabIdx = originUrl.indexOf('://gitlab.'); | case 'github.com': | ||||
let gitSfIdx = originUrl.indexOf('://git.code.sf.net'); | validUrl = isGitRepoUrl(originUrl.toString(), 'github.com'); | ||||
let bitbucketIdx = originUrl.indexOf('://bitbucket.org'); | break; | ||||
if (githubIdx !== -1 && githubIdx <= 5) { | |||||
validUrl = isGitRepoUrl(originUrl, 'github.com'); | case 'git.code.sf.net': | ||||
} else if (gitlabIdx !== -1 && gitlabIdx <= 5) { | validUrl = isGitRepoUrl(originUrl.toString(), 'git.code.sf.net/p'); | ||||
let startIdx = gitlabIdx + 3; | break; | ||||
let idx = originUrl.indexOf('/', startIdx); | |||||
if (idx !== -1) { | case 'bitbucket.org': | ||||
let gitlabDomain = originUrl.substr(startIdx, idx - startIdx); | validUrl = isGitRepoUrl(originUrl.toString(), 'bitbucket.org'); | ||||
validUrl = isGitRepoUrl(originUrl, gitlabDomain); | break; | ||||
} else { | |||||
validUrl = false; | default: | ||||
if (originUrl.hostname.startsWith('gitlab.')) { | |||||
validUrl = isGitRepoUrl(originUrl.toString(), originUrl.hostname); | |||||
} | } | ||||
} else if (gitSfIdx !== -1 && gitSfIdx <= 5) { | break; | ||||
validUrl = isGitRepoUrl(originUrl, 'git.code.sf.net/p'); | |||||
} else if (bitbucketIdx !== -1 && bitbucketIdx <= 5) { | |||||
validUrl = isGitRepoUrl(originUrl, 'bitbucket.org'); | |||||
} | } | ||||
} | } | ||||
if (validUrl) { | if (validUrl) { | ||||
input.setCustomValidity(''); | input.setCustomValidity(''); | ||||
} else { | } else { | ||||
input.setCustomValidity('The origin url is not valid or does not reference a code repository'); | input.setCustomValidity('The origin url is not valid or does not reference a code repository'); | ||||
} | } | ||||
} | } | ||||
export function initTakeNewSnapshot() { | export function initTakeNewSnapshot() { | ||||
▲ Show 20 Lines • Show All 191 Lines • Show Last 20 Lines |