From f22477a2f11374ba8074ddc1a82786a1eaa56aa6 Mon Sep 17 00:00:00 2001 From: Kilian PAQUIER Date: Thu, 2 May 2024 20:48:07 +0000 Subject: [PATCH] fix(auth modificator): add test around port and add replacer in case an user is already in parsed url --- lib/auth-modificator.ts | 3 ++- lib/git.ts | 2 +- test/auth-modificator.test.ts | 17 ++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/auth-modificator.ts b/lib/auth-modificator.ts index 66973b4..56e15a5 100644 --- a/lib/auth-modificator.ts +++ b/lib/auth-modificator.ts @@ -21,5 +21,6 @@ export const getUser = (platform: Platform): string => { export const authModificator = (url: GitUrl, platform: Platform, token: string): string => { const origin = url.toString("https") // simple replace to add the authentication after toString - return origin.replace("https://", `https://${getUser(platform)}:${token}@`) + return origin.replace(`https://${url.user}@`, "https://"). + replace("https://", `https://${getUser(platform)}:${token}@`) } \ No newline at end of file diff --git a/lib/git.ts b/lib/git.ts index 746c530..f88942d 100644 --- a/lib/git.ts +++ b/lib/git.ts @@ -132,7 +132,7 @@ export const mergeBranch = async (context: Partial, con return await createPullRequest(config, info, from, to) } - const push = ["push", authModificator(info, config.platform, config.token), `HEAD:${to}`] + const push = ["push", authModificator(info, config.platform, config.token)] if (config.dryRun) { context.logger?.log(`Running with --dry-run, push from '${from}' into '${to}' with commit '${commit}' will not update ${remote}.`) push.push("--dry-run") diff --git a/test/auth-modificator.test.ts b/test/auth-modificator.test.ts index abbf7a8..a282be7 100644 --- a/test/auth-modificator.test.ts +++ b/test/auth-modificator.test.ts @@ -28,6 +28,17 @@ describe("modificator", () => { expect(url).toEqual("https://x-token-auth:token@github.com/kilianpaquier/semantic-release-backmerge.git") }) + test("should return a valid authenticated git url with bitbucket and port", () => { + // Arrange + const info = gitUrlParse("https://github.com:7099/kilianpaquier/semantic-release-backmerge.git") + + // Act + const url = authModificator(info, Platform.BITBUCKET_CLOUD, "token") + + // Assert + expect(url).toEqual("https://x-token-auth:token@github.com:7099/kilianpaquier/semantic-release-backmerge.git") + }) + test("should return a valid authenticated git url with gitea", () => { // Arrange const info = gitUrlParse("git+https://github.com/kilianpaquier/semantic-release-backmerge.git") @@ -41,7 +52,7 @@ describe("modificator", () => { test("should return a valid authenticated git url with github", () => { // Arrange - const info = gitUrlParse("git@github.com:kilianpaquier/semantic-release-backmerge.git") + const info = gitUrlParse("https://some-user:token@github.com/kilianpaquier/semantic-release-backmerge.git") // Act const url = authModificator(info, Platform.GITHUB, "token") @@ -52,12 +63,12 @@ describe("modificator", () => { test("should return a valid authenticated git url with gitlab", () => { // Arrange - const info = gitUrlParse("git@github.com:kilianpaquier/semantic-release-backmerge.git") + const info = gitUrlParse("git@github.com:kilianpaquier/subgroup/semantic-release-backmerge.git") // Act const url = authModificator(info, Platform.GITLAB, "token") // Assert - expect(url).toEqual("https://gitlab-ci-token:token@github.com/kilianpaquier/semantic-release-backmerge.git") + expect(url).toEqual("https://gitlab-ci-token:token@github.com/kilianpaquier/subgroup/semantic-release-backmerge.git") }) }) \ No newline at end of file