Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor and resolve parsing issue with Git data for HTTP schema ending with a slash (/) #1459

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/git-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ 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
// To simplify the regex. Stripping the trailing `/` or `.git` since they're both optional.
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 +119,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 +129,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: "[email protected]:firecow/gitlab-ci.local.git", // project can contain .
expected: {
Expand Down
Loading