Skip to content

Commit

Permalink
Add self hosted gitlab as fallback (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm authored Nov 17, 2024
1 parent 1f96ee3 commit 3bfe75e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ToCloneUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ public static function fromRepoAndToken(string $repo, string $authToken) : strin
);
}
break;

default:
$port = 443;
if ($repo_parsed['scheme'] === 'http') {
$port = 80;
}
if (!empty($repo_parsed["port"])) {
$port = $repo_parsed["port"];
}
$repo_path = sprintf(
'%s://oauth2:%s@%s:%d%s',
$repo_parsed["scheme"],
$authToken,
$repo_parsed["host"],
$port,
$repo_parsed["path"]
);
// If using a more standard way, meaning the scheme
// matches its default port so to speak, we can just
// use the host and path.
if ($port === 443 && $repo_parsed['scheme'] === 'https') {
$repo_path = sprintf(
'https://oauth2:%s@%s%s',
$authToken,
$repo_parsed["host"],
$repo_parsed["path"]
);
}
// Same for 80 and http.
if ($port === 80 && $repo_parsed['scheme'] === 'http') {
$repo_path = sprintf(
'http://oauth2:%s@%s%s',
$authToken,
$repo_parsed["host"],
$repo_parsed["path"]
);
}
break;
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public static function provideAllCases() : array
'user:token123',
'https://user:[email protected]/user/repo.git',
],
[
'https://gitlab.acme.com/user/repo.git',
'mytoken',
'https://oauth2:[email protected]/user/repo.git',
],
[
'http://gitlab.acme.com/user/repo.git',
'mytoken',
'http://oauth2:[email protected]/user/repo.git',
],
[
'https://gitlab.acme.com:9977/user/repo.git',
'mytoken',
'https://oauth2:[email protected]:9977/user/repo.git',
],
[
'http://gitlab.acme.com:8877/user/repo.git',
'mytoken',
'http://oauth2:[email protected]:8877/user/repo.git',
],
];
}
}

0 comments on commit 3bfe75e

Please sign in to comment.