diff --git a/dist/cli/index.js b/dist/cli/index.js index 5a84866..2f6ab30 100755 --- a/dist/cli/index.js +++ b/dist/cli/index.js @@ -493,8 +493,9 @@ class GitCLIService { * @param remoteURL remote link, e.g., https://github.com/kiegroup/git-backporting-example.git */ remoteWithAuth(remoteURL) { - if (this.auth && this.gitData.user) { - return remoteURL.replace("://", `://${this.gitData.user}:${this.auth}@`); + if (this.auth) { + // Anything will work as a username. + return remoteURL.replace("://", `://token:${this.auth}@`); } // return remote as it is return remoteURL; diff --git a/dist/gha/index.js b/dist/gha/index.js index 9f2c8e4..60b0fea 100755 --- a/dist/gha/index.js +++ b/dist/gha/index.js @@ -463,8 +463,9 @@ class GitCLIService { * @param remoteURL remote link, e.g., https://github.com/kiegroup/git-backporting-example.git */ remoteWithAuth(remoteURL) { - if (this.auth && this.gitData.user) { - return remoteURL.replace("://", `://${this.gitData.user}:${this.auth}@`); + if (this.auth) { + // Anything will work as a username. + return remoteURL.replace("://", `://token:${this.auth}@`); } // return remote as it is return remoteURL; diff --git a/src/service/git/git-cli.ts b/src/service/git/git-cli.ts index 76f6fc3..5eb7678 100644 --- a/src/service/git/git-cli.ts +++ b/src/service/git/git-cli.ts @@ -35,8 +35,9 @@ export default class GitCLIService { * @param remoteURL remote link, e.g., https://github.com/kiegroup/git-backporting-example.git */ private remoteWithAuth(remoteURL: string): string { - if (this.auth && this.gitData.user) { - return remoteURL.replace("://", `://${this.gitData.user}:${this.auth}@`); + if (this.auth) { + // Anything will work as a username. + return remoteURL.replace("://", `://token:${this.auth}@`); } // return remote as it is diff --git a/test/service/git/git-cli.test.ts b/test/service/git/git-cli.test.ts index 2b9c364..8c71609 100644 --- a/test/service/git/git-cli.test.ts +++ b/test/service/git/git-cli.test.ts @@ -123,4 +123,22 @@ describe("git cli service", () => { const post = spawnSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd }).stdout.toString().trim(); expect(post).toEqual("tbranch"); }); + + test("git clone set url with auth correctly for API token", async () => { + const git2 = new GitCLIService("api-token", { + user: "Backporting bot", + email: "bot@example.com", + }); + const cwd2 = `${__dirname}/test-api-token`; + + try { + await git2.clone(`file://${cwd}`, cwd2, "main"); + const remoteURL = spawnSync("git", ["remote", "get-url", "origin"], { cwd: cwd2 }).stdout.toString().trim(); + + expect(remoteURL).toContain("api-token"); + expect(remoteURL).not.toContain("Backporting bot"); + } finally { + fs.rmSync(cwd2, { recursive: true, force: true }); + } + }); }); \ No newline at end of file