diff options
| author | eric sciple <ericsciple@users.noreply.github.com> | 2019-12-12 13:49:26 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-12 13:49:26 -0500 |
| commit | ca3a763d5ed9e04331c8a6aea85ddabcb3a6aa4d (patch) | |
| tree | 236058d4a2be4208412dc3ed294e2db566aab7d6 /src | |
| parent | 25ac60e43ded675f082e10f444b0ad95ea43622e (diff) | |
add input persist-credentials (#107)
Diffstat (limited to 'src')
| -rw-r--r-- | src/git-command-manager.ts | 13 | ||||
| -rw-r--r-- | src/git-source-provider.ts | 99 | ||||
| -rw-r--r-- | src/github-api-helper.ts | 8 | ||||
| -rw-r--r-- | src/input-helper.ts | 8 |
4 files changed, 67 insertions, 61 deletions
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index ddd5ec6..f86b8c0 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -116,7 +116,7 @@ class GitCommandManager { } async config(configKey: string, configValue: string): Promise<void> { - await this.execGit(['config', configKey, configValue]) + await this.execGit(['config', '--local', configKey, configValue]) } async configExists(configKey: string): Promise<boolean> { @@ -124,7 +124,7 @@ class GitCommandManager { return `\\${x}` }) const output = await this.execGit( - ['config', '--name-only', '--get-regexp', pattern], + ['config', '--local', '--name-only', '--get-regexp', pattern], true ) return output.exitCode === 0 @@ -211,20 +211,23 @@ class GitCommandManager { async tryConfigUnset(configKey: string): Promise<boolean> { const output = await this.execGit( - ['config', '--unset-all', configKey], + ['config', '--local', '--unset-all', configKey], true ) return output.exitCode === 0 } async tryDisableAutomaticGarbageCollection(): Promise<boolean> { - const output = await this.execGit(['config', 'gc.auto', '0'], true) + const output = await this.execGit( + ['config', '--local', 'gc.auto', '0'], + true + ) return output.exitCode === 0 } async tryGetFetchUrl(): Promise<string> { const output = await this.execGit( - ['config', '--get', 'remote.origin.url'], + ['config', '--local', '--get', 'remote.origin.url'], true ) diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index bf086ff..6b7a9f7 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -1,5 +1,4 @@ import * as core from '@actions/core' -import * as coreCommand from '@actions/core/lib/command' import * as fs from 'fs' import * as fsHelper from './fs-helper' import * as gitCommandManager from './git-command-manager' @@ -21,7 +20,8 @@ export interface ISourceSettings { clean: boolean fetchDepth: number lfs: boolean - accessToken: string + authToken: string + persistCredentials: boolean } export async function getSource(settings: ISourceSettings): Promise<void> { @@ -65,7 +65,7 @@ export async function getSource(settings: ISourceSettings): Promise<void> { `To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH` ) await githubApiHelper.downloadRepository( - settings.accessToken, + settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.ref, @@ -94,43 +94,43 @@ export async function getSource(settings: ISourceSettings): Promise<void> { // Remove possible previous extraheader await removeGitConfig(git, authConfigKey) - // Add extraheader (auth) - const base64Credentials = Buffer.from( - `x-access-token:${settings.accessToken}`, - 'utf8' - ).toString('base64') - core.setSecret(base64Credentials) - const authConfigValue = `AUTHORIZATION: basic ${base64Credentials}` - await git.config(authConfigKey, authConfigValue) + try { + // Config auth token + await configureAuthToken(git, settings.authToken) - // LFS install - if (settings.lfs) { - await git.lfsInstall() - } + // LFS install + if (settings.lfs) { + await git.lfsInstall() + } - // Fetch - const refSpec = refHelper.getRefSpec(settings.ref, settings.commit) - await git.fetch(settings.fetchDepth, refSpec) + // Fetch + const refSpec = refHelper.getRefSpec(settings.ref, settings.commit) + await git.fetch(settings.fetchDepth, refSpec) - // Checkout info - const checkoutInfo = await refHelper.getCheckoutInfo( - git, - settings.ref, - settings.commit - ) + // Checkout info + const checkoutInfo = await refHelper.getCheckoutInfo( + git, + settings.ref, + settings.commit + ) - // LFS fetch - // Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time). - // Explicit lfs fetch will fetch lfs objects in parallel. - if (settings.lfs) { - await git.lfsFetch(checkoutInfo.startPoint || checkoutInfo.ref) - } + // LFS fetch + // Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time). + // Explicit lfs fetch will fetch lfs objects in parallel. + if (settings.lfs) { + await git.lfsFetch(checkoutInfo.startPoint || checkoutInfo.ref) + } - // Checkout - await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint) + // Checkout + await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint) - // Dump some info about the checked out commit - await git.log1() + // Dump some info about the checked out commit + await git.log1() + } finally { + if (!settings.persistCredentials) { + await removeGitConfig(git, authConfigKey) + } + } } } @@ -255,6 +255,20 @@ async function prepareExistingDirectory( } } +async function configureAuthToken( + git: IGitCommandManager, + authToken: string +): Promise<void> { + // Add extraheader (auth) + const base64Credentials = Buffer.from( + `x-access-token:${authToken}`, + 'utf8' + ).toString('base64') + core.setSecret(base64Credentials) + const authConfigValue = `AUTHORIZATION: basic ${base64Credentials}` + await git.config(authConfigKey, authConfigValue) +} + async function removeGitConfig( git: IGitCommandManager, configKey: string @@ -264,21 +278,6 @@ async function removeGitConfig( !(await git.tryConfigUnset(configKey)) ) { // Load the config contents - core.warning( - `Failed to remove '${configKey}' from the git config. Attempting to remove the config value by editing the file directly.` - ) - const configPath = path.join(git.getWorkingDirectory(), '.git', 'config') - fsHelper.fileExistsSync(configPath) - let contents = fs.readFileSync(configPath).toString() || '' - - // Filter - only includes lines that do not contain the config key - const upperConfigKey = configKey.toUpperCase() - const split = contents - .split('\n') - .filter(x => !x.toUpperCase().includes(upperConfigKey)) - contents = split.join('\n') - - // Rewrite the config file - fs.writeFileSync(configPath, contents) + core.warning(`Failed to remove '${configKey}' from the git config`) } } diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts index b370d06..83a2b86 100644 --- a/src/github-api-helper.ts +++ b/src/github-api-helper.ts @@ -12,7 +12,7 @@ import {ReposGetArchiveLinkParams} from '@octokit/rest' const IS_WINDOWS = process.platform === 'win32' export async function downloadRepository( - accessToken: string, + authToken: string, owner: string, repo: string, ref: string, @@ -22,7 +22,7 @@ export async function downloadRepository( // Download the archive let archiveData = await retryHelper.execute(async () => { core.info('Downloading the archive') - return await downloadArchive(accessToken, owner, repo, ref, commit) + return await downloadArchive(authToken, owner, repo, ref, commit) }) // Write archive to disk @@ -68,13 +68,13 @@ export async function downloadRepository( } async function downloadArchive( - accessToken: string, + authToken: string, owner: string, repo: string, ref: string, commit: string ): Promise<Buffer> { - const octokit = new github.GitHub(accessToken) + const octokit = new github.GitHub(authToken) const params: ReposGetArchiveLinkParams = { owner: owner, repo: repo, diff --git a/src/input-helper.ts b/src/input-helper.ts index d7d8779..56b58f2 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -97,8 +97,12 @@ export function getInputs(): ISourceSettings { result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE' core.debug(`lfs = ${result.lfs}`) - // Access token - result.accessToken = core.getInput('token') + // Auth token + result.authToken = core.getInput('token') + + // Persist credentials + result.persistCredentials = + (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE' return result } |
