From ec36e1a493eb1613e8eb87c1af7a8e41317b801d Mon Sep 17 00:00:00 2001 From: Jon Eugster Date: Sun, 25 Aug 2024 22:28:11 +0200 Subject: [PATCH] improve URL parsing in tooltip --- client/src/Popups/Tools.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/client/src/Popups/Tools.tsx b/client/src/Popups/Tools.tsx index 53c123bc..f4e3ae3a 100644 --- a/client/src/Popups/Tools.tsx +++ b/client/src/Popups/Tools.tsx @@ -89,8 +89,17 @@ const ToolTip: FC<{ if (loaded) { return } setLoaded(true) - // Hack: construct github api URL from repo URL - let githubUrl = pkg.url.replace('github.com/', 'api.github.com/repos/') + `/commits/${pkg.rev}` + // construct github api URL from repo URL + let m = pkg.url.match(/github.com\/([^\/]+)\/([^\/\.]+)/i) // exclude '\.' to strip potential '.git' at the end + if (!m || m.length < 2) { + console.warn(`[LeanWeb]: cannot parse package url`, pkg.url) + setError('Not Found') + return + } + + let githubUrl = `https://api.github.com/repos/${m![1]}/${m![2]}/commits/${pkg.rev}` + + pkg.url.replace('github.com/', 'api.github.com/repos/') + `/commits/${pkg.rev}` console.debug(`[LeanWeb]: fetch from ${githubUrl}`) fetch(githubUrl).then(response => {