github-api-helper.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.tar.gz')
  43. await fs.promises.writeFile(archiveFile, new Buffer(response.data))
  44. await exec.exec(`ls -la "${archiveFile}"`, [], {
  45. cwd: repositoryPath
  46. } as ExecOptions)
  47. await exec.exec(`tar -xzf "${archiveFile}"`, [], {
  48. cwd: repositoryPath
  49. } as ExecOptions)
  50. await exec.exec(`find .`, [], {
  51. cwd: repositoryPath
  52. } as ExecOptions)
  53. }