Skip to content

Commit

Permalink
feat(auth): implement SSH auth modificator (nothing special outside o…
Browse files Browse the repository at this point in the history
…f adding `ssh://` if not provided) - fixes #34

Signed-off-by: kilianpaquier <kilian@kilianpaquier.com>
  • Loading branch information
kilianpaquier committed Nov 22, 2024
1 parent 80b85c8 commit 78783f4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
18 changes: 12 additions & 6 deletions lib/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ const deblog = debug("semantic-release:backmerge")
* @param user being the oauth prefix to use (specific depending on the current git platform).
* @param token to use instead of present token in url.
*
* @throws SemanticReleaseError in case the platform isn't an implemented platform or is not a platform at all.
* @throws an error in case the repositoryUrl is neither ssh, http or https
*
* @returns the computed remote url with all modifications' done.
*/
export const authModificator = (url: GitUrl, user: string, token: string): string => {
const proto = url.protocol === "http" ? "http" : "https"

const proto = url.protocols.length > 1 ? url.protocols[url.protocols.length - 1] : url.protocols[0]
const origin = url.toString(proto)
// simple replace to add the authentication after toString
return origin.replace(`${proto}://${url.user}@`, `${proto}://`).
replace(`${proto}://`, `${proto}://${user}:${token}@`)
switch (proto) {
case "ssh":
return origin.startsWith(`${proto}://`) ? origin : origin.replace(`${url.user}@`, `${proto}://${url.user}@`)
case "http":
case "https":
return origin.replace(`${proto}://${url.user}@`, `${proto}://`).
replace(`${proto}://`, `${proto}://${user}:${token}@`)
default:
throw new Error(`Invalid repositoryUrl with protocol '${proto}', please open an issue for us to implement auth modification with this protocol.`)
}
}

/**
Expand Down
7 changes: 3 additions & 4 deletions test/backmerge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ describe("backmerge", () => {
afterEach(() => mock.restore())

const repositoryUrl = "git@github.com:kilianpaquier/semantic-release-backmerge.git"
const expectedRepositoryUrl = "ssh://git@github.com:kilianpaquier/semantic-release-backmerge.git"

const context = getContext("main")
const release: Branch = { hash: "", name: "main" }
Expand Down Expand Up @@ -239,8 +240,6 @@ describe("backmerge", () => {

test("should succeed to merge and push a branch", async () => {
// Arrange
const expectedUrl = "https://test-user:some-token@github.com/kilianpaquier/semantic-release-backmerge.git"

const checkouts: Branch[] = []
const merge: { commit?: string, from?: string }[] = []
const push: { branch?: string, dryRun?: boolean, remote?: string }[] = []
Expand All @@ -260,8 +259,8 @@ describe("backmerge", () => {
{ commit: "chore(release): merge branch main into develop [skip ci]", from: "main" }
])
expect(push).toEqual([
{ branch: "staging", dryRun: false, remote: expectedUrl },
{ branch: "develop", dryRun: false, remote: expectedUrl }
{ branch: "staging", dryRun: false, remote: expectedRepositoryUrl },
{ branch: "develop", dryRun: false, remote: expectedRepositoryUrl }
])
})
})
32 changes: 16 additions & 16 deletions test/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,62 @@ describe("authModificator", () => {
const url = authModificator(info, "user", "token")

// Assert
expect(url).toEqual("https://user:token@github.com:7099/kilianpaquier/semantic-release-backmerge.git")
expect(url).toEqual("ssh://git@github.com:7099/kilianpaquier/semantic-release-backmerge.git")
})

test("should return a valid authenticated git URL from one with a port", () => {
test("should return a valid authenticated git URL with SSH", () => {
// Arrange
const info = parse("https://github.com:7099/kilianpaquier/semantic-release-backmerge.git")
const info = parse("git@github.com:kilianpaquier/subgroup/semantic-release-backmerge.git")

// Act
const url = authModificator(info, "user", "token")

// Assert
expect(url).toEqual("https://user:token@github.com:7099/kilianpaquier/semantic-release-backmerge.git")
expect(url).toEqual("ssh://git@github.com:kilianpaquier/subgroup/semantic-release-backmerge.git")
})

test("should return a valid authenticated git URL from git https one", () => {
test("should return a valid authenticated git URL with a port", () => {
// Arrange
const info = parse("git+https://github.com/kilianpaquier/semantic-release-backmerge.git")
const info = parse("https://github.com:7099/kilianpaquier/semantic-release-backmerge.git")

// Act
const url = authModificator(info, "user", "token")

// Assert
expect(url).toEqual("https://user:token@github.com/kilianpaquier/semantic-release-backmerge.git")
expect(url).toEqual("https://user:token@github.com:7099/kilianpaquier/semantic-release-backmerge.git")
})

test("should return a valid authenticated git URL from already authenticated one", () => {
test("should return a valid authenticated git URL with git specific HTTPS", () => {
// Arrange
const info = parse("https://gitlab-ci-token:glpat-RKuRfmL9gfDnw@gitlab.example.com/my-group/my-project.git")
const info = parse("git+https://github.com/kilianpaquier/semantic-release-backmerge.git")

// Act
const url = authModificator(info, "user", "token")

// Assert
expect(url).toEqual("https://user:token@gitlab.example.com/my-group/my-project.git")
expect(url).toEqual("https://user:token@github.com/kilianpaquier/semantic-release-backmerge.git")
})

test("should return a valid authenticated git URL from http one", () => {
test("should return a valid authenticated git URL with already authenticated one", () => {
// Arrange
const info = parse("http://github.com/kilianpaquier/semantic-release-backmerge.git")
const info = parse("https://gitlab-ci-token:glpat-RKuRfmL9gfDnw@gitlab.example.com/my-group/my-project.git")

// Act
const url = authModificator(info, "user", "token")

// Assert
expect(url).toEqual("http://user:token@github.com/kilianpaquier/semantic-release-backmerge.git")
expect(url).toEqual("https://user:token@gitlab.example.com/my-group/my-project.git")
})

test("should return a valid authenticated git URL from ssh one", () => {
test("should return a valid authenticated git URL with HTTP", () => {
// Arrange
const info = parse("git@github.com:kilianpaquier/subgroup/semantic-release-backmerge.git")
const info = parse("http://github.com/kilianpaquier/semantic-release-backmerge.git")

// Act
const url = authModificator(info, "user", "token")

// Assert
expect(url).toEqual("https://user:token@github.com/kilianpaquier/subgroup/semantic-release-backmerge.git")
expect(url).toEqual("http://user:token@github.com/kilianpaquier/semantic-release-backmerge.git")
})
})

Expand Down

0 comments on commit 78783f4

Please sign in to comment.