diff --git a/app/utils/mention.ts b/app/utils/mention.ts index fcd7acedf5..c86b0252d2 100644 --- a/app/utils/mention.ts +++ b/app/utils/mention.ts @@ -30,9 +30,15 @@ export const isURLMentionable = ({ case IntegrationService.GitLab: { const settings = integration.settings as IntegrationSettings; - const gitlabHostname = settings.gitlab?.url - ? new URL(settings.gitlab?.url).hostname - : undefined; + let gitlabHostname: string | undefined; + try { + gitlabHostname = settings.gitlab?.url + ? new URL(settings.gitlab.url).hostname + : undefined; + } catch { + // Invalid URL stored in settings + return false; + } return hostname === "gitlab.com" || hostname === gitlabHostname; } @@ -68,11 +74,21 @@ export const determineMentionType = ({ } case IntegrationService.GitLab: { - return pathname.includes("merge_requests") - ? MentionType.PullRequest - : pathname.includes("issues") - ? MentionType.Issue - : undefined; + const hasShowParam = url.searchParams.has("show"); + + if ( + /\/-\/merge_requests\/\d+/.test(pathname) || + (/\/-\/merge_requests\/?$/.test(pathname) && hasShowParam) + ) { + return MentionType.PullRequest; + } + if ( + /\/-\/issues\/\d+/.test(pathname) || + (/\/-\/issues\/?$/.test(pathname) && hasShowParam) + ) { + return MentionType.Issue; + } + return undefined; } default: diff --git a/plugins/gitlab/shared/GitLabUtils.test.ts b/plugins/gitlab/shared/GitLabUtils.test.ts index fbf3c47875..6ceeea2614 100644 --- a/plugins/gitlab/shared/GitLabUtils.test.ts +++ b/plugins/gitlab/shared/GitLabUtils.test.ts @@ -60,6 +60,21 @@ describe("GitLabUtils.parseUrl", () => { ); expect(result).toBeUndefined(); }); + + it("should return undefined for an issues list URL without an ID", () => { + const result = GitLabUtils.parseUrl( + "https://gitlab.com/speak/purser/-/issues" + ); + expect(result).toBeUndefined(); + }); + + it("should return undefined for an invalid custom URL", () => { + const result = GitLabUtils.parseUrl( + "https://gitlab.example.com/team/project/-/issues/10", + "not-a-valid-url" + ); + expect(result).toBeUndefined(); + }); }); describe("base64 show parameter URLs", () => { diff --git a/plugins/gitlab/shared/GitLabUtils.ts b/plugins/gitlab/shared/GitLabUtils.ts index d2388219a2..47f04189ed 100644 --- a/plugins/gitlab/shared/GitLabUtils.ts +++ b/plugins/gitlab/shared/GitLabUtils.ts @@ -104,21 +104,57 @@ export class GitLabUtils { * @returns An object containing resource identifiers or undefined if the URL is invalid. */ public static parseUrl(url: string, customUrl?: string) { - const parsed = new URL(url); - const urlHostname = new URL(this.getGitlabUrl(customUrl)).hostname; + try { + const parsed = new URL(url); + const urlHostname = new URL(this.getGitlabUrl(customUrl)).hostname; - if (parsed.hostname !== urlHostname) { - return; - } + if (parsed.hostname !== urlHostname) { + return; + } - const parts = parsed.pathname.split("/").filter(Boolean); + const parts = parsed.pathname.split("/").filter(Boolean); - // Try base64-encoded `show` query parameter first - // e.g. /owner/repo/-/issues?show=eyJ... - const showParam = parsed.searchParams.get("show"); - if (showParam && parts.length >= 4) { + // Try base64-encoded `show` query parameter first + // e.g. /owner/repo/-/issues?show=eyJ... + const showParam = parsed.searchParams.get("show"); + if (showParam && parts.length >= 4) { + const resourceType = parts.pop(); + parts.pop(); // separator ("-") + const repo = parts.pop(); + const owner = parts.join("/"); + + const type = + resourceType === "issues" + ? UnfurlResourceType.Issue + : resourceType === "merge_requests" + ? UnfurlResourceType.PR + : undefined; + + if (!type || !this.supportedResources.includes(type)) { + return; + } + + try { + const decoded = JSON.parse(atob(decodeURIComponent(showParam))); + const iid = Number(decoded.iid); + if (!iid) { + return; + } + return { owner, repo, type, id: iid, url }; + } catch { + return; + } + } + + if (parts.length < 5) { + return; + } + + // Direct URL: /owner/repo/-/issues/123 or /owner/repo/-/merge_requests/123 + const resourceId = parts.pop(); const resourceType = parts.pop(); parts.pop(); // separator ("-") + const repo = parts.pop(); const owner = parts.join("/"); @@ -133,48 +169,16 @@ export class GitLabUtils { return; } - try { - const decoded = JSON.parse(atob(decodeURIComponent(showParam))); - const iid = Number(decoded.iid); - if (!iid) { - return; - } - return { owner, repo, type, id: iid, url }; - } catch { - return; - } - } - - if (parts.length < 5) { + return { + owner, + repo, + type, + id: Number(resourceId), + url, + }; + } catch { return; } - - // Direct URL: /owner/repo/-/issues/123 or /owner/repo/-/merge_requests/123 - const resourceId = parts.pop(); - const resourceType = parts.pop(); - parts.pop(); // separator ("-") - - const repo = parts.pop(); - const owner = parts.join("/"); - - const type = - resourceType === "issues" - ? UnfurlResourceType.Issue - : resourceType === "merge_requests" - ? UnfurlResourceType.PR - : undefined; - - if (!type || !this.supportedResources.includes(type)) { - return; - } - - return { - owner, - repo, - type, - id: Number(resourceId), - url, - }; } /** diff --git a/shared/editor/components/Mentions.tsx b/shared/editor/components/Mentions.tsx index 5d679608cc..ace48399d9 100644 --- a/shared/editor/components/Mentions.tsx +++ b/shared/editor/components/Mentions.tsx @@ -311,14 +311,19 @@ export const MentionIssue = observer((props: IssuePrProps) => { } const issue = unfurl as UnfurlResponse[UnfurlResourceType.Issue]; - const url = new URL(issue.url); - const service = - url.hostname === "github.com" - ? IntegrationService.GitHub - : url.hostname === "linear.app" - ? IntegrationService.Linear - : IntegrationService.GitLab; + let service = IntegrationService.GitLab; + try { + const parsedUrl = new URL(issue.url); + service = + parsedUrl.hostname === "github.com" + ? IntegrationService.GitHub + : parsedUrl.hostname === "linear.app" + ? IntegrationService.Linear + : IntegrationService.GitLab; + } catch { + // Invalid URL in unfurl data, default to GitLab + } return (