github-api-helper.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as assert from 'assert'
  2. import * as core from '@actions/core'
  3. import * as exec from '@actions/exec'
  4. import * as fs from 'fs'
  5. import * as github from '@actions/github'
  6. import * as io from '@actions/io'
  7. import * as path from 'path'
  8. import {ReposGetArchiveLinkParams} from '@octokit/rest'
  9. import {defaultCoreCipherList} from 'constants'
  10. import {ExecOptions} from '@actions/exec/lib/interfaces'
  11. const IS_WINDOWS = process.platform === 'win32'
  12. export async function downloadRepository(
  13. accessToken: string,
  14. owner: string,
  15. repo: string,
  16. ref: string,
  17. repositoryPath: string
  18. ): Promise<void> {
  19. const octokit = new github.GitHub(accessToken)
  20. const params: ReposGetArchiveLinkParams = {
  21. archive_format: IS_WINDOWS ? 'zipball' : 'tarball',
  22. owner: owner,
  23. repo: repo,
  24. ref: ref
  25. }
  26. // todo: retry
  27. const response = await octokit.repos.getArchiveLink(params)
  28. if (response.status != 200) {
  29. throw new Error(
  30. `Unexpected response from GitHub API. Status: '${response.status}'; Data: '${response.data}'`
  31. )
  32. }
  33. console.log(`status=${response.status}`)
  34. console.log(`headers=${JSON.stringify(response.headers)}`)
  35. // console.log(`data=${response.data}`)
  36. // console.log(`data=${JSON.stringify(response.data)}`)
  37. // for (const key of Object.keys(response.data)) {
  38. // console.log(`data['${key}']=${response.data[key]}`)
  39. // }
  40. const runnerTemp = process.env['RUNNER_TEMP'] as string
  41. assert.ok(runnerTemp, 'RUNNER_TEMP not defined')
  42. const archiveFile = path.join(runnerTemp, 'checkout-archive.tar.gz')
  43. await io.rmRF(archiveFile)
  44. await fs.promises.writeFile(archiveFile, new Buffer(response.data))
  45. await exec.exec(`ls -la "${archiveFile}"`, [], {
  46. cwd: repositoryPath
  47. } as ExecOptions)
  48. const extractPath = path.join(runnerTemp, 'checkout-archive')
  49. await io.rmRF(extractPath)
  50. await io.mkdirP(extractPath)
  51. await exec.exec(`tar -xzf "${archiveFile}"`, [], {
  52. cwd: extractPath
  53. } as ExecOptions)
  54. // Determine the real directory to copy (ignore extra dir at root of the archive)
  55. const archiveFileNames = await fs.promises.readdir(extractPath)
  56. assert.ok(
  57. archiveFileNames.length == 1,
  58. 'Expected exactly one directory inside archive'
  59. )
  60. const extraDirectoryName = archiveFileNames[0]
  61. core.info(`Resolved ${extraDirectoryName}`) // contains the short SHA
  62. const tempRepositoryPath = path.join(extractPath, extraDirectoryName)
  63. // Move the files
  64. for (const fileName of await fs.promises.readdir(tempRepositoryPath)) {
  65. const sourcePath = path.join(tempRepositoryPath, fileName)
  66. const targetPath = path.join(repositoryPath, fileName)
  67. await io.mv(sourcePath, targetPath)
  68. }
  69. await exec.exec(`find .`, [], {
  70. cwd: repositoryPath
  71. } as ExecOptions)
  72. }