2
0

input-helper.ts 5.0 KB

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