Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions __test__/git-auth-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,91 @@ describe('git-auth-helper tests', () => {
).toBe(false)
expect((authHelper as any).testCredentialsConfigPath('')).toBe(false)
})

const configureAuth_uses_default_git_user_when_sshUser_empty =
'configureAuth uses default git user when sshUser is empty'
it(configureAuth_uses_default_git_user_when_sshUser_empty, async () => {
// Arrange
await setup(configureAuth_uses_default_git_user_when_sshUser_empty)
settings.sshUser = ''
settings.sshKey = ''
const authHelper = gitAuthHelper.createAuthHelper(git, settings)

// Act
await authHelper.configureAuth()
await authHelper.configureGlobalAuth()

// Assert - verify that git@github.com is in the insteadOf config
const home = git.env['HOME'] || tempHomedir
const configContent = (
await fs.promises.readFile(path.join(home, '.gitconfig'))
).toString()
expect(configContent.indexOf('url.https://github.com/.insteadOf git@github.com')).toBeGreaterThanOrEqual(0)
})

const configureAuth_uses_custom_ssh_user_when_sshUser_provided =
'configureAuth uses custom SSH user when sshUser is provided'
it(configureAuth_uses_custom_ssh_user_when_sshUser_provided, async () => {
// Arrange
await setup(configureAuth_uses_custom_ssh_user_when_sshUser_provided)
settings.sshUser = 'customuser'
settings.sshKey = ''
const authHelper = gitAuthHelper.createAuthHelper(git, settings)

// Act
await authHelper.configureAuth()
await authHelper.configureGlobalAuth()

// Assert - verify that customuser@github.com is in the insteadOf config
const home = git.env['HOME'] || tempHomedir
const configContent = (
await fs.promises.readFile(path.join(home, '.gitconfig'))
).toString()
expect(configContent.indexOf('url.https://github.com/.insteadOf customuser@github.com')).toBeGreaterThanOrEqual(0)
})

const configureGlobalAuth_with_custom_sshUser =
'configureGlobalAuth with custom sshUser'
it(configureGlobalAuth_with_custom_sshUser, async () => {
// Arrange
await setup(configureGlobalAuth_with_custom_sshUser)
settings.sshUser = 'admin'
settings.sshKey = ''
const authHelper = gitAuthHelper.createAuthHelper(git, settings)

// Act
await authHelper.configureAuth()
await authHelper.configureGlobalAuth()

// Assert - verify the .insteadOf config contains the custom user
const home = git.env['HOME'] || tempHomedir
const configContent = (
await fs.promises.readFile(path.join(home, '.gitconfig'))
).toString()
expect(configContent.indexOf('url.https://github.com/.insteadOf admin@github.com')).toBeGreaterThanOrEqual(0)
})

const configureSubmoduleAuth_with_custom_sshUser =
'configureSubmoduleAuth with custom sshUser'
it(configureSubmoduleAuth_with_custom_sshUser, async () => {
// Arrange
await setup(configureSubmoduleAuth_with_custom_sshUser)
settings.sshUser = 'deploy'
settings.sshKey = ''
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
await authHelper.configureAuth()
const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
mockSubmoduleForeach.mockClear()

// Act
await authHelper.configureSubmoduleAuth()

// Assert - verify the insteadOf config uses the custom user
expect(mockSubmoduleForeach).toHaveBeenCalledTimes(3)
expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(
/url.*insteadOf.*deploy@github.com:/
)
})
})

async function setup(testName: string): Promise<void> {
Expand Down
5 changes: 4 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ class GitAuthHelper {
this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}`;
// Instead of SSH URL
this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf`; // "origin" is SCHEME://HOSTNAME[:PORT]
this.insteadOfValues.push(`git@${serverUrl.hostname}:`);
const sshUser = this.settings.sshUser && this.settings.sshUser.length > 0
? this.settings.sshUser
: 'git';
this.insteadOfValues.push(`${sshUser}@${serverUrl.hostname}:`);
if (this.settings.workflowOrganizationId) {
this.insteadOfValues.push(`org-${this.settings.workflowOrganizationId}@github.com:`);
}
Expand Down
6 changes: 5 additions & 1 deletion src/git-auth-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ class GitAuthHelper {

// Instead of SSH URL
this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf` // "origin" is SCHEME://HOSTNAME[:PORT]
this.insteadOfValues.push(`git@${serverUrl.hostname}:`)
const sshUser =
this.settings.sshUser && this.settings.sshUser.length > 0
? this.settings.sshUser
: 'git'
this.insteadOfValues.push(`${sshUser}@${serverUrl.hostname}:`)
if (this.settings.workflowOrganizationId) {
this.insteadOfValues.push(
`org-${this.settings.workflowOrganizationId}@github.com:`
Expand Down