Skip to content

Commit

Permalink
fix: --auth when --git-user contains space
Browse files Browse the repository at this point in the history
Since --git-user is a user-facing name, it's common to include a space
in it. As such, it's not suitable to use as a username in a Git remote
URL.

GitLab documented that it doesn't (yet?) check for username [1], and
from my testing GitHub doesn't seem to care either. So just use an
arbitrary name as a username.

[1] https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html
  • Loading branch information
peat-psuwit committed Feb 21, 2024
1 parent d4dc510 commit 56531a5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 3 additions & 2 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions dist/gha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/service/git/git-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/service/git/git-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
});
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 });
}
});
});

0 comments on commit 56531a5

Please sign in to comment.