input-helper.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Fetch depth
  77. result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1'))
  78. if (isNaN(result.fetchDepth) || result.fetchDepth < 0) {
  79. result.fetchDepth = 0
  80. }
  81. core.debug(`fetch depth = ${result.fetchDepth}`)
  82. // LFS
  83. result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'
  84. core.debug(`lfs = ${result.lfs}`)
  85. // Submodules
  86. result.submodules = false
  87. result.nestedSubmodules = false
  88. const submodulesString = (core.getInput('submodules') || '').toUpperCase()
  89. if (submodulesString == 'RECURSIVE') {
  90. result.submodules = true
  91. result.nestedSubmodules = true
  92. } else if (submodulesString == 'TRUE') {
  93. result.submodules = true
  94. }
  95. core.debug(`submodules = ${result.submodules}`)
  96. core.debug(`recursive submodules = ${result.nestedSubmodules}`)
  97. // Auth token
  98. result.authToken = core.getInput('token', {required: true})
  99. // SSH
  100. result.sshKey = core.getInput('ssh-key')
  101. result.sshKnownHosts = core.getInput('ssh-known-hosts')
  102. result.sshStrict =
  103. (core.getInput('ssh-strict') || 'true').toUpperCase() === 'TRUE'
  104. // Persist credentials
  105. result.persistCredentials =
  106. (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE'
  107. // Workflow organization ID
  108. result.workflowOrganizationId = await workflowContextHelper.getOrganizationId()
  109. // Set safe.directory in git global config.
  110. result.setSafeDirectory =
  111. (core.getInput('set-safe-directory') || 'true').toUpperCase() === 'TRUE'
  112. // Determine the GitHub URL that the repository is being hosted from
  113. result.githubServerUrl = core.getInput('github-server-url')
  114. core.debug(`GitHub Host URL = ${result.githubServerUrl}`)
  115. return result
  116. }