Skip to content

Commit

Permalink
fix(auth modificator): add test around port and add replacer in case …
Browse files Browse the repository at this point in the history
…an user is already in parsed url
  • Loading branch information
kilianpaquier committed May 2, 2024
1 parent f725b11 commit f22477a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/auth-modificator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}@`)
}
2 changes: 1 addition & 1 deletion lib/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const mergeBranch = async (context: Partial<VerifyConditionsContext>, 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")
Expand Down
17 changes: 14 additions & 3 deletions test/auth-modificator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
})
})

0 comments on commit f22477a

Please sign in to comment.