diff options
Diffstat (limited to 'src/git-source-provider.ts')
| -rw-r--r-- | src/git-source-provider.ts | 177 |
1 files changed, 93 insertions, 84 deletions
diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index 42a12e0..0913229 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -36,68 +36,77 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> { const git = await getGitCommandManager(settings) core.endGroup() - // Prepare existing directory, otherwise recreate - if (isExisting) { - await gitDirectoryHelper.prepareExistingDirectory( - git, - settings.repositoryPath, - repositoryUrl, - settings.clean, - settings.ref - ) - } + let authHelper: gitAuthHelper.IGitAuthHelper | null = null + try { + if (git) { + authHelper = gitAuthHelper.createAuthHelper(git, settings) + await authHelper.configureTempGlobalConfig() + } - if (!git) { - // Downloading using REST API - core.info(`The repository will be downloaded using the GitHub REST API`) - core.info( - `To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH` - ) - if (settings.submodules) { - throw new Error( - `Input 'submodules' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.` + // Prepare existing directory, otherwise recreate + if (isExisting) { + await gitDirectoryHelper.prepareExistingDirectory( + git, + settings.repositoryPath, + repositoryUrl, + settings.clean, + settings.ref ) - } else if (settings.sshKey) { - throw new Error( - `Input 'ssh-key' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.` + } + + if (!git) { + // Downloading using REST API + core.info(`The repository will be downloaded using the GitHub REST API`) + core.info( + `To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH` ) + if (settings.submodules) { + throw new Error( + `Input 'submodules' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.` + ) + } else if (settings.sshKey) { + throw new Error( + `Input 'ssh-key' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.` + ) + } + + await githubApiHelper.downloadRepository( + settings.authToken, + settings.repositoryOwner, + settings.repositoryName, + settings.ref, + settings.commit, + settings.repositoryPath + ) + return } - await githubApiHelper.downloadRepository( - settings.authToken, - settings.repositoryOwner, - settings.repositoryName, - settings.ref, - settings.commit, - settings.repositoryPath - ) - return - } + // Save state for POST action + stateHelper.setRepositoryPath(settings.repositoryPath) - // Save state for POST action - stateHelper.setRepositoryPath(settings.repositoryPath) + // Initialize the repository + if ( + !fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git')) + ) { + core.startGroup('Initializing the repository') + await git.init() + await git.remoteAdd('origin', repositoryUrl) + core.endGroup() + } - // Initialize the repository - if ( - !fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git')) - ) { - core.startGroup('Initializing the repository') - await git.init() - await git.remoteAdd('origin', repositoryUrl) + // Disable automatic garbage collection + core.startGroup('Disabling automatic garbage collection') + if (!(await git.tryDisableAutomaticGarbageCollection())) { + core.warning( + `Unable to turn off git automatic garbage collection. The git fetch operation may trigger garbage collection and cause a delay.` + ) + } core.endGroup() - } - // Disable automatic garbage collection - core.startGroup('Disabling automatic garbage collection') - if (!(await git.tryDisableAutomaticGarbageCollection())) { - core.warning( - `Unable to turn off git automatic garbage collection. The git fetch operation may trigger garbage collection and cause a delay.` - ) - } - core.endGroup() - - const authHelper = gitAuthHelper.createAuthHelper(git, settings) - try { + // If we didn't initialize it above, do it now + if (!authHelper) { + authHelper = gitAuthHelper.createAuthHelper(git, settings) + } // Configure auth core.startGroup('Setting up auth') await authHelper.configureAuth() @@ -170,34 +179,26 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> { // Submodules if (settings.submodules) { - try { - // Temporarily override global config - core.startGroup('Setting up auth for fetching submodules') - await authHelper.configureGlobalAuth() - core.endGroup() + // Temporarily override global config + core.startGroup('Setting up auth for fetching submodules') + await authHelper.configureGlobalAuth() + core.endGroup() - // Checkout submodules - core.startGroup('Fetching submodules') - await git.submoduleSync(settings.nestedSubmodules) - await git.submoduleUpdate( - settings.fetchDepth, - settings.nestedSubmodules - ) - await git.submoduleForeach( - 'git config --local gc.auto 0', - settings.nestedSubmodules - ) - core.endGroup() + // Checkout submodules + core.startGroup('Fetching submodules') + await git.submoduleSync(settings.nestedSubmodules) + await git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules) + await git.submoduleForeach( + 'git config --local gc.auto 0', + settings.nestedSubmodules + ) + core.endGroup() - // Persist credentials - if (settings.persistCredentials) { - core.startGroup('Persisting credentials for submodules') - await authHelper.configureSubmoduleAuth() - core.endGroup() - } - } finally { - // Remove temporary global config override - await authHelper.removeGlobalAuth() + // Persist credentials + if (settings.persistCredentials) { + core.startGroup('Persisting credentials for submodules') + await authHelper.configureSubmoduleAuth() + core.endGroup() } } @@ -218,10 +219,13 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> { ) } finally { // Remove auth - if (!settings.persistCredentials) { - core.startGroup('Removing auth') - await authHelper.removeAuth() - core.endGroup() + if (authHelper) { + if (!settings.persistCredentials) { + core.startGroup('Removing auth') + await authHelper.removeAuth() + core.endGroup() + } + authHelper.removeGlobalConfig() } } } @@ -244,7 +248,12 @@ export async function cleanup(repositoryPath: string): Promise<void> { // Remove auth const authHelper = gitAuthHelper.createAuthHelper(git) - await authHelper.removeAuth() + try { + await authHelper.configureTempGlobalConfig(repositoryPath) + await authHelper.removeAuth() + } finally { + await authHelper.removeGlobalConfig() + } } async function getGitCommandManager( |
