mirror of
				https://github.com/actions/checkout.git
				synced 2025-11-04 05:58:40 +08:00 
			
		
		
		
	Fix getting default branch when using SSH
This commit is contained in:
		
							parent
							
								
									8aabf05e48
								
							
						
					
					
						commit
						6e3d2777af
					
				
							
								
								
									
										49
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										49
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							| 
						 | 
					@ -5827,6 +5827,33 @@ class GitCommandManager {
 | 
				
			||||||
            }));
 | 
					            }));
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    getDefaultBranch(repositoryUrl) {
 | 
				
			||||||
 | 
					        return __awaiter(this, void 0, void 0, function* () {
 | 
				
			||||||
 | 
					            let output;
 | 
				
			||||||
 | 
					            yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
 | 
				
			||||||
 | 
					                output = yield this.execGit([
 | 
				
			||||||
 | 
					                    'ls-remote',
 | 
				
			||||||
 | 
					                    '--quiet',
 | 
				
			||||||
 | 
					                    '--exit-code',
 | 
				
			||||||
 | 
					                    '--symref',
 | 
				
			||||||
 | 
					                    repositoryUrl,
 | 
				
			||||||
 | 
					                    'HEAD'
 | 
				
			||||||
 | 
					                ]);
 | 
				
			||||||
 | 
					            }));
 | 
				
			||||||
 | 
					            if (output) {
 | 
				
			||||||
 | 
					                // Satisfy compiler, will always be set
 | 
				
			||||||
 | 
					                for (let line of output.stdout.trim().split('\n')) {
 | 
				
			||||||
 | 
					                    line = line.trim();
 | 
				
			||||||
 | 
					                    if (line.startsWith('ref:') || line.endsWith('HEAD')) {
 | 
				
			||||||
 | 
					                        return line
 | 
				
			||||||
 | 
					                            .substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length)
 | 
				
			||||||
 | 
					                            .trim();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            throw new Error('Unexpected output when retrieving default branch');
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    getWorkingDirectory() {
 | 
					    getWorkingDirectory() {
 | 
				
			||||||
        return this.workingDirectory;
 | 
					        return this.workingDirectory;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -6114,12 +6141,6 @@ function getSource(settings) {
 | 
				
			||||||
        // Repository URL
 | 
					        // Repository URL
 | 
				
			||||||
        core.info(`Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}`);
 | 
					        core.info(`Syncing repository: ${settings.repositoryOwner}/${settings.repositoryName}`);
 | 
				
			||||||
        const repositoryUrl = urlHelper.getFetchUrl(settings);
 | 
					        const repositoryUrl = urlHelper.getFetchUrl(settings);
 | 
				
			||||||
        // Determine the default branch
 | 
					 | 
				
			||||||
        if (!settings.ref && !settings.commit) {
 | 
					 | 
				
			||||||
            core.startGroup('Determining the default branch');
 | 
					 | 
				
			||||||
            settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName);
 | 
					 | 
				
			||||||
            core.endGroup();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        // Remove conflicting file path
 | 
					        // Remove conflicting file path
 | 
				
			||||||
        if (fsHelper.fileExistsSync(settings.repositoryPath)) {
 | 
					        if (fsHelper.fileExistsSync(settings.repositoryPath)) {
 | 
				
			||||||
            yield io.rmRF(settings.repositoryPath);
 | 
					            yield io.rmRF(settings.repositoryPath);
 | 
				
			||||||
| 
						 | 
					@ -6172,6 +6193,17 @@ function getSource(settings) {
 | 
				
			||||||
            core.startGroup('Setting up auth');
 | 
					            core.startGroup('Setting up auth');
 | 
				
			||||||
            yield authHelper.configureAuth();
 | 
					            yield authHelper.configureAuth();
 | 
				
			||||||
            core.endGroup();
 | 
					            core.endGroup();
 | 
				
			||||||
 | 
					            // Determine the default branch
 | 
				
			||||||
 | 
					            if (!settings.ref && !settings.commit) {
 | 
				
			||||||
 | 
					                core.startGroup('Determining the default branch');
 | 
				
			||||||
 | 
					                if (settings.sshKey) {
 | 
				
			||||||
 | 
					                    settings.ref = yield git.getDefaultBranch(repositoryUrl);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else {
 | 
				
			||||||
 | 
					                    settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                core.endGroup();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            // LFS install
 | 
					            // LFS install
 | 
				
			||||||
            if (settings.lfs) {
 | 
					            if (settings.lfs) {
 | 
				
			||||||
                yield git.lfsInstall();
 | 
					                yield git.lfsInstall();
 | 
				
			||||||
| 
						 | 
					@ -9531,6 +9563,11 @@ const v4_1 = __importDefault(__webpack_require__(826));
 | 
				
			||||||
const IS_WINDOWS = process.platform === 'win32';
 | 
					const IS_WINDOWS = process.platform === 'win32';
 | 
				
			||||||
function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) {
 | 
					function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) {
 | 
				
			||||||
    return __awaiter(this, void 0, void 0, function* () {
 | 
					    return __awaiter(this, void 0, void 0, function* () {
 | 
				
			||||||
 | 
					        // Determine the default branch
 | 
				
			||||||
 | 
					        if (!ref && !commit) {
 | 
				
			||||||
 | 
					            core.info('Determining the default branch');
 | 
				
			||||||
 | 
					            ref = yield getDefaultBranch(authToken, owner, repo);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        // Download the archive
 | 
					        // Download the archive
 | 
				
			||||||
        let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
 | 
					        let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
 | 
				
			||||||
            core.info('Downloading the archive');
 | 
					            core.info('Downloading the archive');
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,6 +25,7 @@ export interface IGitCommandManager {
 | 
				
			||||||
  ): Promise<void>
 | 
					  ): Promise<void>
 | 
				
			||||||
  configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
 | 
					  configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
 | 
				
			||||||
  fetch(refSpec: string[], fetchDepth?: number): Promise<void>
 | 
					  fetch(refSpec: string[], fetchDepth?: number): Promise<void>
 | 
				
			||||||
 | 
					  getDefaultBranch(repositoryUrl: string): Promise<string>
 | 
				
			||||||
  getWorkingDirectory(): string
 | 
					  getWorkingDirectory(): string
 | 
				
			||||||
  init(): Promise<void>
 | 
					  init(): Promise<void>
 | 
				
			||||||
  isDetached(): Promise<boolean>
 | 
					  isDetached(): Promise<boolean>
 | 
				
			||||||
| 
						 | 
					@ -195,6 +196,34 @@ class GitCommandManager {
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async getDefaultBranch(repositoryUrl: string): Promise<string> {
 | 
				
			||||||
 | 
					    let output: GitOutput | undefined
 | 
				
			||||||
 | 
					    await retryHelper.execute(async () => {
 | 
				
			||||||
 | 
					      output = await this.execGit([
 | 
				
			||||||
 | 
					        'ls-remote',
 | 
				
			||||||
 | 
					        '--quiet',
 | 
				
			||||||
 | 
					        '--exit-code',
 | 
				
			||||||
 | 
					        '--symref',
 | 
				
			||||||
 | 
					        repositoryUrl,
 | 
				
			||||||
 | 
					        'HEAD'
 | 
				
			||||||
 | 
					      ])
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (output) {
 | 
				
			||||||
 | 
					      // Satisfy compiler, will always be set
 | 
				
			||||||
 | 
					      for (let line of output.stdout.trim().split('\n')) {
 | 
				
			||||||
 | 
					        line = line.trim()
 | 
				
			||||||
 | 
					        if (line.startsWith('ref:') || line.endsWith('HEAD')) {
 | 
				
			||||||
 | 
					          return line
 | 
				
			||||||
 | 
					            .substr('ref:'.length, line.length - 'ref:'.length - 'HEAD'.length)
 | 
				
			||||||
 | 
					            .trim()
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    throw new Error('Unexpected output when retrieving default branch')
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  getWorkingDirectory(): string {
 | 
					  getWorkingDirectory(): string {
 | 
				
			||||||
    return this.workingDirectory
 | 
					    return this.workingDirectory
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,17 +19,6 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
 | 
				
			||||||
  )
 | 
					  )
 | 
				
			||||||
  const repositoryUrl = urlHelper.getFetchUrl(settings)
 | 
					  const repositoryUrl = urlHelper.getFetchUrl(settings)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Determine the default branch
 | 
					 | 
				
			||||||
  if (!settings.ref && !settings.commit) {
 | 
					 | 
				
			||||||
    core.startGroup('Determining the default branch')
 | 
					 | 
				
			||||||
    settings.ref = await githubApiHelper.getDefaultBranch(
 | 
					 | 
				
			||||||
      settings.authToken,
 | 
					 | 
				
			||||||
      settings.repositoryOwner,
 | 
					 | 
				
			||||||
      settings.repositoryName
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
    core.endGroup()
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Remove conflicting file path
 | 
					  // Remove conflicting file path
 | 
				
			||||||
  if (fsHelper.fileExistsSync(settings.repositoryPath)) {
 | 
					  if (fsHelper.fileExistsSync(settings.repositoryPath)) {
 | 
				
			||||||
    await io.rmRF(settings.repositoryPath)
 | 
					    await io.rmRF(settings.repositoryPath)
 | 
				
			||||||
| 
						 | 
					@ -114,6 +103,21 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
 | 
				
			||||||
    await authHelper.configureAuth()
 | 
					    await authHelper.configureAuth()
 | 
				
			||||||
    core.endGroup()
 | 
					    core.endGroup()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Determine the default branch
 | 
				
			||||||
 | 
					    if (!settings.ref && !settings.commit) {
 | 
				
			||||||
 | 
					      core.startGroup('Determining the default branch')
 | 
				
			||||||
 | 
					      if (settings.sshKey) {
 | 
				
			||||||
 | 
					        settings.ref = await git.getDefaultBranch(repositoryUrl)
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        settings.ref = await githubApiHelper.getDefaultBranch(
 | 
				
			||||||
 | 
					          settings.authToken,
 | 
				
			||||||
 | 
					          settings.repositoryOwner,
 | 
				
			||||||
 | 
					          settings.repositoryName
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      core.endGroup()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // LFS install
 | 
					    // LFS install
 | 
				
			||||||
    if (settings.lfs) {
 | 
					    if (settings.lfs) {
 | 
				
			||||||
      await git.lfsInstall()
 | 
					      await git.lfsInstall()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,7 +8,6 @@ import * as retryHelper from './retry-helper'
 | 
				
			||||||
import * as toolCache from '@actions/tool-cache'
 | 
					import * as toolCache from '@actions/tool-cache'
 | 
				
			||||||
import {default as uuid} from 'uuid/v4'
 | 
					import {default as uuid} from 'uuid/v4'
 | 
				
			||||||
import {Octokit} from '@octokit/rest'
 | 
					import {Octokit} from '@octokit/rest'
 | 
				
			||||||
import {Console} from 'console'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
const IS_WINDOWS = process.platform === 'win32'
 | 
					const IS_WINDOWS = process.platform === 'win32'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,6 +19,12 @@ export async function downloadRepository(
 | 
				
			||||||
  commit: string,
 | 
					  commit: string,
 | 
				
			||||||
  repositoryPath: string
 | 
					  repositoryPath: string
 | 
				
			||||||
): Promise<void> {
 | 
					): Promise<void> {
 | 
				
			||||||
 | 
					  // Determine the default branch
 | 
				
			||||||
 | 
					  if (!ref && !commit) {
 | 
				
			||||||
 | 
					    core.info('Determining the default branch')
 | 
				
			||||||
 | 
					    ref = await getDefaultBranch(authToken, owner, repo)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Download the archive
 | 
					  // Download the archive
 | 
				
			||||||
  let archiveData = await retryHelper.execute(async () => {
 | 
					  let archiveData = await retryHelper.execute(async () => {
 | 
				
			||||||
    core.info('Downloading the archive')
 | 
					    core.info('Downloading the archive')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user