Skip to content

Commit

Permalink
fix: refactor and resolve parsing issue with Git data for HTTP schema…
Browse files Browse the repository at this point in the history
… ending with a slash (/)
  • Loading branch information
ANGkeith committed Dec 17, 2024
1 parent 8ec9299 commit 7179973
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/git-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ export class GitData {
gitRemote = res.stdout;
}

if (gitRemote.startsWith("http")) {
gitRemoteMatch = /(?<schema>https?):\/\/(?:([^:]+):([^@]+)@)?(?<host>[^/:]+):?(?<port>\d+)?\/(?<group>\S+)\/(?<project>\S+)\.git/.exec(gitRemote); // regexr.com/7ve8l
const normalizedGitRemote = gitRemote
.replace(/\/$/, "")
.replace(/\.git$/, "");

if (normalizedGitRemote.startsWith("http")) {
gitRemoteMatch = /(?<schema>https?):\/\/(?:([^:]+):([^@]+)@)?(?<host>[^/:]+):?(?<port>\d+)?\/(?<group>\S+)\/(?<project>\S+)/.exec(normalizedGitRemote); // regexr.com/7ve8l
assert(gitRemoteMatch?.groups != null, "git remote get-url origin didn't provide valid matches");

let port = "443";
Expand All @@ -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 = /(?<schema>ssh):\/\/(\w+)@(?<host>[^/:]+):?(?<port>\d+)?\/(?<group>\S+)\/(?<project>\S+)\.git/.exec(gitRemote); // regexr.com/7vjq4
} else if (normalizedGitRemote.startsWith("ssh://")) {
gitRemoteMatch = /(?<schema>ssh):\/\/(\w+)@(?<host>[^/:]+):?(?<port>\d+)?\/(?<group>\S+)\/(?<project>\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;
Expand All @@ -124,14 +128,14 @@ export class GitData {
this.remote.schema = gitRemoteMatch.groups.schema as GitSchema;
this.remote.port = gitRemoteMatch.groups.port ?? "22";
} else {
gitRemoteMatch = /(?<username>\S+)@(?<host>[^:]+):(?<group>\S+)\/(?<project>\S+)/.exec(gitRemote); // regexr.com/7vjoq
gitRemoteMatch = /(?<username>\S+)@(?<host>[^:]+):(?<group>\S+)\/(?<project>\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;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/git-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 7179973

Please sign in to comment.