diff --git a/__test__/create-or-update-branch.int.test.ts b/__test__/create-or-update-branch.int.test.ts index 09fa8ae..0f12254 100644 --- a/__test__/create-or-update-branch.int.test.ts +++ b/__test__/create-or-update-branch.int.test.ts @@ -8,7 +8,7 @@ import {GitCommandManager} from '../lib/git-command-manager' import * as path from 'path' import {v4 as uuidv4} from 'uuid' -const REPO_PATH = '/git/local/test-base' +const REPO_PATH = '/git/local/repos/test-base' const REMOTE_NAME = 'origin' const TRACKED_FILE = 'a/tracked-file.txt' @@ -22,7 +22,7 @@ const INIT_COMMIT_MESSAGE = 'Add file to be a tracked file for tests' const BRANCH = 'tests/create-pull-request/patch' const BASE = DEFAULT_BRANCH -const FORK_REMOTE_URL = 'git://127.0.0.1/test-fork.git' +const FORK_REMOTE_URL = 'git://127.0.0.1/repos/test-fork.git' const FORK_REMOTE_NAME = 'fork' const ADD_PATHS_DEFAULT = [] diff --git a/__test__/entrypoint.sh b/__test__/entrypoint.sh index 7dc7ccd..fa08b90 100755 --- a/__test__/entrypoint.sh +++ b/__test__/entrypoint.sh @@ -5,18 +5,18 @@ set -euo pipefail WORKINGDIR=$PWD # Create and serve a remote repo -mkdir -p /git/remote +mkdir -p /git/remote/repos git config --global init.defaultBranch main -git init --bare /git/remote/test-base.git +git init --bare /git/remote/repos/test-base.git git daemon --verbose --enable=receive-pack --base-path=/git/remote --export-all /git/remote &>/dev/null & # Give the daemon time to start sleep 2 # Create a local clone and make an initial commit -mkdir -p /git/local -git clone git://127.0.0.1/test-base.git /git/local/test-base -cd /git/local/test-base +mkdir -p /git/local/repos +git clone git://127.0.0.1/repos/test-base.git /git/local/repos/test-base +cd /git/local/repos/test-base git config --global user.email "you@example.com" git config --global user.name "Your Name" echo "#test-base" > README.md @@ -30,8 +30,8 @@ git config -l # Clone a server-side fork of the base repo cd $WORKINGDIR -git clone --mirror git://127.0.0.1/test-base.git /git/remote/test-fork.git -cd /git/remote/test-fork.git +git clone --mirror git://127.0.0.1/repos/test-base.git /git/remote/repos/test-fork.git +cd /git/remote/repos/test-fork.git git log -1 --pretty=oneline # Restore the working directory diff --git a/__test__/git-config-helper.int.test.ts b/__test__/git-config-helper.int.test.ts index 08d3beb..040a080 100644 --- a/__test__/git-config-helper.int.test.ts +++ b/__test__/git-config-helper.int.test.ts @@ -1,9 +1,9 @@ import {GitCommandManager} from '../lib/git-command-manager' import {GitConfigHelper} from '../lib/git-config-helper' -const REPO_PATH = '/git/local/test-base' +const REPO_PATH = '/git/local/repos/test-base' -const extraheaderConfigKey = 'http.https://github.com/.extraheader' +const extraheaderConfigKey = 'http.https://127.0.0.1/.extraheader' describe('git-config-helper integration tests', () => { let git: GitCommandManager @@ -11,22 +11,22 @@ describe('git-config-helper integration tests', () => { beforeAll(async () => { git = await GitCommandManager.create(REPO_PATH) - gitConfigHelper = await GitConfigHelper.create(git) }) it('tests save and restore with no persisted auth', async () => { - await gitConfigHelper.savePersistedAuth() - await gitConfigHelper.restorePersistedAuth() + const gitConfigHelper = await GitConfigHelper.create(git) + await gitConfigHelper.close() }) it('tests configure and removal of auth', async () => { + const gitConfigHelper = await GitConfigHelper.create(git) await gitConfigHelper.configureToken('github-token') expect(await git.configExists(extraheaderConfigKey)).toBeTruthy() expect(await git.getConfigValue(extraheaderConfigKey)).toEqual( 'AUTHORIZATION: basic eC1hY2Nlc3MtdG9rZW46Z2l0aHViLXRva2Vu' ) - await gitConfigHelper.removeAuth() + await gitConfigHelper.close() expect(await git.configExists(extraheaderConfigKey)).toBeFalsy() }) @@ -34,37 +34,53 @@ describe('git-config-helper integration tests', () => { const extraheaderConfigValue = 'AUTHORIZATION: basic ***persisted-auth***' await git.config(extraheaderConfigKey, extraheaderConfigValue) - await gitConfigHelper.savePersistedAuth() + const gitConfigHelper = await GitConfigHelper.create(git) const exists = await git.configExists(extraheaderConfigKey) expect(exists).toBeFalsy() - await gitConfigHelper.restorePersistedAuth() + await gitConfigHelper.close() const configValue = await git.getConfigValue(extraheaderConfigKey) expect(configValue).toEqual(extraheaderConfigValue) - await gitConfigHelper.removeAuth() + const unset = await git.tryConfigUnset( + extraheaderConfigKey, + '^AUTHORIZATION:' + ) + expect(unset).toBeTruthy() + }) + + it('tests not adding/removing the safe.directory config when it already exists', async () => { + await git.config('safe.directory', '/another-value', true, true) + + const gitConfigHelper = await GitConfigHelper.create(git) + + expect( + await git.configExists('safe.directory', '/another-value', true) + ).toBeTruthy() + + await gitConfigHelper.close() + + const unset = await git.tryConfigUnset( + 'safe.directory', + '/another-value', + true + ) + expect(unset).toBeTruthy() }) it('tests adding and removing the safe.directory config', async () => { - await git.config('safe.directory', '/another-value', true, true) - - await gitConfigHelper.removeSafeDirectory() - await gitConfigHelper.addSafeDirectory() + const gitConfigHelper = await GitConfigHelper.create(git) expect( await git.configExists('safe.directory', REPO_PATH, true) ).toBeTruthy() - await gitConfigHelper.addSafeDirectory() - await gitConfigHelper.removeSafeDirectory() + await gitConfigHelper.close() expect( await git.configExists('safe.directory', REPO_PATH, true) ).toBeFalsy() - expect( - await git.configExists('safe.directory', '/another-value', true) - ).toBeTruthy() }) }) diff --git a/__test__/git-config-helper.unit.test.ts b/__test__/git-config-helper.unit.test.ts index 083b5f9..8527471 100644 --- a/__test__/git-config-helper.unit.test.ts +++ b/__test__/git-config-helper.unit.test.ts @@ -68,6 +68,16 @@ describe('git-config-helper unit tests', () => { expect(remote3.repository).toEqual('peter-evans/create-pull-request') }) + test('parseGitRemote successfully parses GIT remote URLs', async () => { + // Unauthenticated git protocol for integration tests only + const remote1 = GitConfigHelper.parseGitRemote( + 'git://127.0.0.1/repos/test-base.git' + ) + expect(remote1.hostname).toEqual('127.0.0.1') + expect(remote1.protocol).toEqual('GIT') + expect(remote1.repository).toEqual('repos/test-base') + }) + test('parseGitRemote fails to parse a remote URL', async () => { const remoteUrl = 'https://github.com/peter-evans' try { diff --git a/dist/index.js b/dist/index.js index a3a6a61..1089e16 100644 --- a/dist/index.js +++ b/dist/index.js @@ -929,7 +929,6 @@ class GitConfigHelper { } static parseGitRemote(remoteUrl) { const httpsUrlPattern = new RegExp('^(https?)://(?:.+@)?(.+?)/(.+/.+?)(\\.git)?$', 'i'); - const sshUrlPattern = new RegExp('^git@(.+?):(.+/.+)\\.git$', 'i'); const httpsMatch = remoteUrl.match(httpsUrlPattern); if (httpsMatch) { return { @@ -938,6 +937,7 @@ class GitConfigHelper { repository: httpsMatch[3] }; } + const sshUrlPattern = new RegExp('^git@(.+?):(.+/.+)\\.git$', 'i'); const sshMatch = remoteUrl.match(sshUrlPattern); if (sshMatch) { return { @@ -946,6 +946,16 @@ class GitConfigHelper { repository: sshMatch[2] }; } + // Unauthenticated git protocol for integration tests only + const gitUrlPattern = new RegExp('^git://(.+?)/(.+/.+)\\.git$', 'i'); + const gitMatch = remoteUrl.match(gitUrlPattern); + if (gitMatch) { + return { + hostname: gitMatch[1], + protocol: 'GIT', + repository: gitMatch[2] + }; + } throw new Error(`The format of '${remoteUrl}' is not a valid GitHub repository URL`); } savePersistedAuth() { diff --git a/src/git-config-helper.ts b/src/git-config-helper.ts index 6c515e0..f5f484b 100644 --- a/src/git-config-helper.ts +++ b/src/git-config-helper.ts @@ -83,8 +83,6 @@ export class GitConfigHelper { '^(https?)://(?:.+@)?(.+?)/(.+/.+?)(\\.git)?$', 'i' ) - const sshUrlPattern = new RegExp('^git@(.+?):(.+/.+)\\.git$', 'i') - const httpsMatch = remoteUrl.match(httpsUrlPattern) if (httpsMatch) { return { @@ -94,6 +92,7 @@ export class GitConfigHelper { } } + const sshUrlPattern = new RegExp('^git@(.+?):(.+/.+)\\.git$', 'i') const sshMatch = remoteUrl.match(sshUrlPattern) if (sshMatch) { return { @@ -103,6 +102,17 @@ export class GitConfigHelper { } } + // Unauthenticated git protocol for integration tests only + const gitUrlPattern = new RegExp('^git://(.+?)/(.+/.+)\\.git$', 'i') + const gitMatch = remoteUrl.match(gitUrlPattern) + if (gitMatch) { + return { + hostname: gitMatch[1], + protocol: 'GIT', + repository: gitMatch[2] + } + } + throw new Error( `The format of '${remoteUrl}' is not a valid GitHub repository URL` )