From 7179973a77fafce9fc406f667b3626ec5a645d72 Mon Sep 17 00:00:00 2001 From: ANGkeith Date: Tue, 17 Dec 2024 22:05:01 +0800 Subject: [PATCH 1/2] fix: refactor and resolve parsing issue with Git data for HTTP schema ending with a slash (/) --- src/git-data.ts | 16 ++++++++++------ tests/git-data.test.ts | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/git-data.ts b/src/git-data.ts index b32b5d5d..4e0a5286 100644 --- a/src/git-data.ts +++ b/src/git-data.ts @@ -99,8 +99,12 @@ export class GitData { gitRemote = res.stdout; } - if (gitRemote.startsWith("http")) { - gitRemoteMatch = /(?https?):\/\/(?:([^:]+):([^@]+)@)?(?[^/:]+):?(?\d+)?\/(?\S+)\/(?\S+)\.git/.exec(gitRemote); // regexr.com/7ve8l + const normalizedGitRemote = gitRemote + .replace(/\/$/, "") + .replace(/\.git$/, ""); + + if (normalizedGitRemote.startsWith("http")) { + gitRemoteMatch = /(?https?):\/\/(?:([^:]+):([^@]+)@)?(?[^/:]+):?(?\d+)?\/(?\S+)\/(?\S+)/.exec(normalizedGitRemote); // regexr.com/7ve8l assert(gitRemoteMatch?.groups != null, "git remote get-url origin didn't provide valid matches"); let port = "443"; @@ -114,8 +118,8 @@ export class GitData { this.remote.project = gitRemoteMatch.groups.project; this.remote.schema = gitRemoteMatch.groups.schema as GitSchema; this.remote.port = port; - } else if (gitRemote.startsWith("ssh://")) { - gitRemoteMatch = /(?ssh):\/\/(\w+)@(?[^/:]+):?(?\d+)?\/(?\S+)\/(?\S+)\.git/.exec(gitRemote); // regexr.com/7vjq4 + } else if (normalizedGitRemote.startsWith("ssh://")) { + gitRemoteMatch = /(?ssh):\/\/(\w+)@(?[^/:]+):?(?\d+)?\/(?\S+)\/(?\S+)/.exec(normalizedGitRemote); // regexr.com/7vjq4 assert(gitRemoteMatch?.groups != null, "git remote get-url origin didn't provide valid matches"); this.remote.host = gitRemoteMatch.groups.host; @@ -124,14 +128,14 @@ export class GitData { this.remote.schema = gitRemoteMatch.groups.schema as GitSchema; this.remote.port = gitRemoteMatch.groups.port ?? "22"; } else { - gitRemoteMatch = /(?\S+)@(?[^:]+):(?\S+)\/(?\S+)/.exec(gitRemote); // regexr.com/7vjoq + gitRemoteMatch = /(?\S+)@(?[^:]+):(?\S+)\/(?\S+)/.exec(normalizedGitRemote); // regexr.com/7vjoq assert(gitRemoteMatch?.groups != null, "git remote get-url origin didn't provide valid matches"); const {stdout} = await Utils.spawn(["ssh", "-G", `${gitRemoteMatch.groups.username}@${gitRemoteMatch.groups.host}`]); const port = stdout.split("\n").filter((line) => line.startsWith("port "))[0].split(" ")[1]; this.remote.host = gitRemoteMatch.groups.host; this.remote.group = gitRemoteMatch.groups.group; - this.remote.project = Utils.trimSuffix(gitRemoteMatch.groups.project, ".git"); + this.remote.project = gitRemoteMatch.groups.project; this.remote.schema = "git"; this.remote.port = port; } diff --git a/tests/git-data.test.ts b/tests/git-data.test.ts index 92c5ab35..e8df0f94 100644 --- a/tests/git-data.test.ts +++ b/tests/git-data.test.ts @@ -34,6 +34,26 @@ const tests = [ project: "gitlab-ci-local", }, }, + { + input: "http://example.com/vendor/package", // does not need to end with .git + expected: { + schema: "http", + port: "80", + host: "example.com", + group: "vendor", + project: "package", + }, + }, + { + input: "http://example.com/vendor/package/", // can end with / + expected: { + schema: "http", + port: "80", + host: "example.com", + group: "vendor", + project: "package", + }, + }, { input: "git@github.com:firecow/gitlab-ci.local.git", // project can contain . expected: { From 48b784f5332841d92b4cffdaf5d62daabdf8344d Mon Sep 17 00:00:00 2001 From: ANGkeith Date: Wed, 18 Dec 2024 22:14:21 +0800 Subject: [PATCH 2/2] add comment --- src/git-data.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/git-data.ts b/src/git-data.ts index 4e0a5286..ee572f0e 100644 --- a/src/git-data.ts +++ b/src/git-data.ts @@ -99,6 +99,7 @@ export class GitData { gitRemote = res.stdout; } + // To simplify the regex. Stripping the trailing `/` or `.git` since they're both optional. const normalizedGitRemote = gitRemote .replace(/\/$/, "") .replace(/\.git$/, "");