input-helper.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import * as core from '@actions/core'
  2. import * as fsHelper from './fs-helper'
  3. import * as github from '@actions/github'
  4. import * as path from 'path'
  5. import * as unsafePrCheckoutHelper from './unsafe-pr-checkout-helper'
  6. import * as workflowContextHelper from './workflow-context-helper'
  7. import {IGitSourceSettings} from './git-source-settings'
  8. export async function getInputs(): Promise<IGitSourceSettings> {
  9. const result = ({} as unknown) as IGitSourceSettings
  10. // GitHub workspace
  11. let githubWorkspacePath = process.env['GITHUB_WORKSPACE']
  12. if (!githubWorkspacePath) {
  13. throw new Error('GITHUB_WORKSPACE not defined')
  14. }
  15. githubWorkspacePath = path.resolve(githubWorkspacePath)
  16. core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
  17. fsHelper.directoryExistsSync(githubWorkspacePath, true)
  18. // Qualified repository
  19. const qualifiedRepository =
  20. core.getInput('repository') ||
  21. `${github.context.repo.owner}/${github.context.repo.repo}`
  22. core.debug(`qualified repository = '${qualifiedRepository}'`)
  23. const splitRepository = qualifiedRepository.split('/')
  24. if (
  25. splitRepository.length !== 2 ||
  26. !splitRepository[0] ||
  27. !splitRepository[1]
  28. ) {
  29. throw new Error(
  30. `Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`
  31. )
  32. }
  33. result.repositoryOwner = splitRepository[0]
  34. result.repositoryName = splitRepository[1]
  35. // Repository path
  36. result.repositoryPath = core.getInput('path') || '.'
  37. result.repositoryPath = path.resolve(
  38. githubWorkspacePath,
  39. result.repositoryPath
  40. )
  41. if (
  42. !(result.repositoryPath + path.sep).startsWith(
  43. githubWorkspacePath + path.sep
  44. )
  45. ) {
  46. throw new Error(
  47. `Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`
  48. )
  49. }
  50. // Workflow repository?
  51. const isWorkflowRepository =
  52. qualifiedRepository.toUpperCase() ===
  53. `${github.context.repo.owner}/${github.context.repo.repo}`.toUpperCase()
  54. // Source branch, source version
  55. result.ref = core.getInput('ref')
  56. if (!result.ref) {
  57. if (isWorkflowRepository) {
  58. result.ref = github.context.ref
  59. result.commit = github.context.sha
  60. // Some events have an unqualifed ref. For example when a PR is merged (pull_request closed event),
  61. // the ref is unqualifed like "main" instead of "refs/heads/main".
  62. if (result.commit && result.ref && !result.ref.startsWith('refs/')) {
  63. result.ref = `refs/heads/${result.ref}`
  64. }
  65. }
  66. }
  67. // SHA?
  68. else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
  69. result.commit = result.ref
  70. result.ref = ''
  71. }
  72. core.debug(`ref = '${result.ref}'`)
  73. core.debug(`commit = '${result.commit}'`)
  74. // Clean
  75. result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
  76. core.debug(`clean = ${result.clean}`)
  77. // Sparse checkout
  78. const sparseCheckout = core.getMultilineInput('sparse-checkout')
  79. if (sparseCheckout.length) {
  80. result.sparseCheckout = sparseCheckout
  81. core.debug(`sparse checkout = ${result.sparseCheckout}`)
  82. }
  83. result.sparseCheckoutConeMode =
  84. (core.getInput('sparse-checkout-cone-mode') || 'true').toUpperCase() ===
  85. 'TRUE'
  86. // Fetch depth
  87. result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1'))
  88. if (isNaN(result.fetchDepth) || result.fetchDepth < 0) {
  89. result.fetchDepth = 0
  90. }
  91. core.debug(`fetch depth = ${result.fetchDepth}`)
  92. // Fetch tags
  93. result.fetchTags =
  94. (core.getInput('fetch-tags') || 'false').toUpperCase() === 'TRUE'
  95. core.debug(`fetch tags = ${result.fetchTags}`)
  96. // LFS
  97. result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'
  98. core.debug(`lfs = ${result.lfs}`)
  99. // Submodules
  100. result.submodules = false
  101. result.nestedSubmodules = false
  102. const submodulesString = (core.getInput('submodules') || '').toUpperCase()
  103. if (submodulesString == 'RECURSIVE') {
  104. result.submodules = true
  105. result.nestedSubmodules = true
  106. } else if (submodulesString == 'TRUE') {
  107. result.submodules = true
  108. }
  109. core.debug(`submodules = ${result.submodules}`)
  110. core.debug(`recursive submodules = ${result.nestedSubmodules}`)
  111. // Auth token
  112. result.authToken = core.getInput('token', {required: true})
  113. // SSH
  114. result.sshKey = core.getInput('ssh-key')
  115. result.sshKnownHosts = core.getInput('ssh-known-hosts')
  116. result.sshStrict =
  117. (core.getInput('ssh-strict') || 'true').toUpperCase() === 'TRUE'
  118. // Persist credentials
  119. result.persistCredentials =
  120. (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE'
  121. // Workflow organization ID
  122. result.workflowOrganizationId = await workflowContextHelper.getOrganizationId()
  123. // Set safe.directory in git global config.
  124. result.setSafeDirectory =
  125. (core.getInput('set-safe-directory') || 'true').toUpperCase() === 'TRUE'
  126. // Determine the GitHub URL that the repository is being hosted from
  127. result.githubServerUrl = core.getInput('github-server-url')
  128. core.debug(`GitHub Host URL = ${result.githubServerUrl}`)
  129. // Allow unsafe PR checkout (opt-in for pull_request_target / workflow_run fork PRs)
  130. result.allowUnsafePrCheckout =
  131. (core.getInput('allow-unsafe-pr-checkout') || 'false').toUpperCase() ===
  132. 'TRUE'
  133. core.debug(`allow unsafe PR checkout = ${result.allowUnsafePrCheckout}`)
  134. unsafePrCheckoutHelper.assertSafePrCheckout({
  135. qualifiedRepository,
  136. ref: result.ref,
  137. commit: result.commit,
  138. allowUnsafePrCheckout: result.allowUnsafePrCheckout
  139. })
  140. return result
  141. }