diff --git a/assets/src/utils/functions.js b/assets/src/utils/functions.js --- a/assets/src/utils/functions.js +++ b/assets/src/utils/functions.js @@ -100,6 +100,14 @@ } } +async function getCanonicalGithubOriginURL(ownerRepo) { + const ghApiResponse = await fetch(`https://api.github.com/repos/${ownerRepo}`); + if (ghApiResponse.ok && ghApiResponse.status === 200) { + const ghApiResponseData = await ghApiResponse.json(); + return ghApiResponseData.html_url; + } +} + export async function getCanonicalOriginURL(originUrl) { let originUrlLower = originUrl.toLowerCase(); // github.com URL processing @@ -116,11 +124,22 @@ // extract {owner}/{repo} const ownerRepo = originUrlLower.replace(ghUrlRegex, ''); // fetch canonical URL from github Web API - const ghApiResponse = await fetch(`https://api.github.com/repos/${ownerRepo}`); - if (ghApiResponse.ok && ghApiResponse.status === 200) { - const ghApiResponseData = await ghApiResponse.json(); - return ghApiResponseData.html_url; + const url = getCanonicalGithubOriginURL(ownerRepo); + if (url) { + return url; + } + } + + const ghpagesUrlRegex = /^http[s]*:\/\/(?[^/]+).github.io\/(?[^/]+)\/?.*/; + const parsedUrl = originUrlLower.match(ghpagesUrlRegex); + if (parsedUrl) { + const ownerRepo = `${parsedUrl.groups.owner}/${parsedUrl.groups.repo}`; + // fetch canonical URL from github Web API + const url = getCanonicalGithubOriginURL(ownerRepo); + if (url) { + return url; } } + return originUrl; } 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 @@ -731,7 +731,9 @@ for (const originUrl of ['https://github.com/BiC-MnI/MnI_AuToReG', 'https://github.com/BiC-MnI/MnI_AuToReG.git', - 'https://github.com/BiC-MnI/MnI_AuToReG/']) { + 'https://github.com/BiC-MnI/MnI_AuToReG/', + 'https://BiC-MnI.github.io/MnI_AuToReG/' + ]) { // enter non canonical URL of github repo cy.get('#swh-input-origin-url')