From d35c5b0d45c2389911e8fa57f5a409dc7eb4eba9 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Wed, 18 Mar 2020 23:33:26 -0400 Subject: use token for workflow repo --- src/git-source-provider.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/git-source-provider.ts') diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index 4bce702..c1859a6 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -14,17 +14,21 @@ import {IGitSourceSettings} from './git-source-settings' const hostname = 'github.com' export async function getSource(settings: IGitSourceSettings): Promise { - // Repository URL core.info( `Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}` ) - const repositoryUrl = settings.sshKey - ? `git@${hostname}:${encodeURIComponent( - settings.repositoryOwner - )}/${encodeURIComponent(settings.repositoryName)}.git` - : `https://${hostname}/${encodeURIComponent( - settings.repositoryOwner - )}/${encodeURIComponent(settings.repositoryName)}` + + // Remote URL + const httpsUrl = `https://${hostname}/${encodeURIComponent( + settings.repositoryOwner + )}/${encodeURIComponent(settings.repositoryName)}` + const sshUrl = `git@${hostname}:${encodeURIComponent( + settings.repositoryOwner + )}/${encodeURIComponent(settings.repositoryName)}.git` + + // Always fetch the workflow repository using the token, not the SSH key + const initialRemoteUrl = + !settings.sshKey || settings.isWorkflowRepository ? httpsUrl : sshUrl // Remove conflicting file path if (fsHelper.fileExistsSync(settings.repositoryPath)) { @@ -46,7 +50,8 @@ export async function getSource(settings: IGitSourceSettings): Promise { await gitDirectoryHelper.prepareExistingDirectory( git, settings.repositoryPath, - repositoryUrl, + initialRemoteUrl, + [httpsUrl, sshUrl], settings.clean ) } @@ -86,7 +91,7 @@ export async function getSource(settings: IGitSourceSettings): Promise { !fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git')) ) { await git.init() - await git.remoteAdd('origin', repositoryUrl) + await git.remoteAdd('origin', initialRemoteUrl) } // Disable automatic garbage collection @@ -124,6 +129,11 @@ export async function getSource(settings: IGitSourceSettings): Promise { await git.lfsFetch(checkoutInfo.startPoint || checkoutInfo.ref) } + // Fix URL when using SSH + if (settings.sshKey && initialRemoteUrl != sshUrl) { + await git.setRemoteUrl(sshUrl) + } + // Checkout await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint) -- cgit v1.2.3