Check for repo instead of domain

When checking if url is GitLab proof, check if it points to a
gitlab_proof repo instad of the gitlab.com domain. This way self hosted
instances could be added.

Also go back to searching for user first and then for the repo since
repo search is unreliable.
This commit is contained in:
Dušan Simić 2020-08-19 20:05:41 +02:00
parent 5df81166ed
commit be2cf693b1

View file

@ -616,24 +616,38 @@ async function verifyProof(url, fingerprint) {
} }
} }
// GitLab // GitLab
if (/^https:\/\/gitlab.com/.test(url)) { if (/\/gitlab_proof$/.test(url)) {
output.type = "gitlab"; output.type = "gitlab";
match = url.match(/https:\/\/gitlab.com\/(.*)\/(.*)/); match = url.match(/https:\/\/(.*)\/(.*)\/gitlab_proof/);
output.display = match[1]; output.display = match[2];
output.url = `https://gitlab.com/${match[1]}`; output.url = `https://${match[1]}/${match[2]}`;
output.proofUrlFetch = `https://gitlab.com/api/v4/projects?custom_attributes[search]=${match[1]}/${match[2]}&custom_attributes[search_namespaces]=true`; output.proofUrlFetch = `https://gitlab.com/api/v4/users?username=${match[2]}`;
// output.proofUrlFetch = `https://gitlab.com/api/v4/projects?custom_attributes[search]=${match[2]}/gitlab_proof&custom_attributes[search_namespaces]=true`;
try { try {
response = await fetch(output.proofUrlFetch, { const opts = {
headers: { headers: {
Accept: 'application/json' Accept: 'application/json'
}, },
credentials: 'omit' credentials: 'omit'
}); };
// Get user
response = await fetch(output.proofUrlFetch, opts);
if (!response.ok) { if (!response.ok) {
throw new Error('Response failed: ' + response.status); throw new Error('Response failed: ' + response.status);
} }
json = await response.json(); json = await response.json();
let project = json.find(proj => proj.web_url === url); const user = json.find(user => user.username === match[2]);
if (!user) {
throw new Error('No user with username ' + match[2]);
}
// Get project
output.proofUrlFetch = `https://gitlab.com/api/v4/users/${user.id}/projects`;
response = await fetch(output.proofUrlFetch, opts);
if (!response.ok) {
throw new Error('Response failed: ' + response.status);
}
json = await response.json();
const project = json.find(proj => proj.path === 'gitlab_proof');
if (!project) { if (!project) {
throw new Error('No project at ' + url); throw new Error('No project at ' + url);
} }