input-helper.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 {IGitSourceSettings} from './git-source-settings'
  6. export function getInputs(): IGitSourceSettings {
  7. const result = ({} as unknown) as IGitSourceSettings
  8. // GitHub workspace
  9. let githubWorkspacePath = process.env['GITHUB_WORKSPACE']
  10. if (!githubWorkspacePath) {
  11. throw new Error('GITHUB_WORKSPACE not defined')
  12. }
  13. githubWorkspacePath = path.resolve(githubWorkspacePath)
  14. core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
  15. fsHelper.directoryExistsSync(githubWorkspacePath, true)
  16. // Qualified repository
  17. const qualifiedRepository =
  18. core.getInput('repository') ||
  19. `${github.context.repo.owner}/${github.context.repo.repo}`
  20. core.debug(`qualified repository = '${qualifiedRepository}'`)
  21. const splitRepository = qualifiedRepository.split('/')
  22. if (
  23. splitRepository.length !== 2 ||
  24. !splitRepository[0] ||
  25. !splitRepository[1]
  26. ) {
  27. throw new Error(
  28. `Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`
  29. )
  30. }
  31. result.repositoryOwner = splitRepository[0]
  32. result.repositoryName = splitRepository[1]
  33. // Repository path
  34. result.repositoryPath = core.getInput('path') || '.'
  35. result.repositoryPath = path.resolve(
  36. githubWorkspacePath,
  37. result.repositoryPath
  38. )
  39. if (
  40. !(result.repositoryPath + path.sep).startsWith(
  41. githubWorkspacePath + path.sep
  42. )
  43. ) {
  44. throw new Error(
  45. `Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`
  46. )
  47. }
  48. // Workflow repository?
  49. const isWorkflowRepository =
  50. qualifiedRepository.toUpperCase() ===
  51. `${github.context.repo.owner}/${github.context.repo.repo}`.toUpperCase()
  52. // Source branch, source version
  53. result.ref = core.getInput('ref')
  54. if (!result.ref) {
  55. if (isWorkflowRepository) {
  56. result.ref = github.context.ref
  57. result.commit = github.context.sha
  58. // Some events have an unqualifed ref. For example when a PR is merged (pull_request closed event),
  59. // the ref is unqualifed like "master" instead of "refs/heads/master".
  60. if (result.commit && result.ref && !result.ref.startsWith('refs/')) {
  61. result.ref = `refs/heads/${result.ref}`
  62. }
  63. }
  64. if (!result.ref && !result.commit) {
  65. result.ref = 'refs/heads/master'
  66. }
  67. }
  68. // SHA?
  69. else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
  70. result.commit = result.ref
  71. result.ref = ''
  72. }
  73. core.debug(`ref = '${result.ref}'`)
  74. core.debug(`commit = '${result.commit}'`)
  75. // Clean
  76. result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
  77. core.debug(`clean = ${result.clean}`)
  78. // Fetch depth
  79. result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1'))
  80. if (isNaN(result.fetchDepth) || result.fetchDepth < 0) {
  81. result.fetchDepth = 0
  82. }
  83. core.debug(`fetch depth = ${result.fetchDepth}`)
  84. // LFS
  85. result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'
  86. core.debug(`lfs = ${result.lfs}`)
  87. // Submodules
  88. result.submodules = false
  89. result.nestedSubmodules = false
  90. const submodulesString = (core.getInput('submodules') || '').toUpperCase()
  91. if (submodulesString == 'RECURSIVE') {
  92. result.submodules = true
  93. result.nestedSubmodules = true
  94. } else if (submodulesString == 'TRUE') {
  95. result.submodules = true
  96. }
  97. core.debug(`submodules = ${result.submodules}`)
  98. core.debug(`recursive submodules = ${result.nestedSubmodules}`)
  99. // Auth token
  100. result.authToken = core.getInput('token')
  101. // SSH
  102. result.sshKey = core.getInput('ssh-key')
  103. result.sshKnownHosts = core.getInput('ssh-known-hosts')
  104. result.sshStrict =
  105. (core.getInput('ssh-strict') || 'true').toUpperCase() === 'TRUE'
  106. // Persist credentials
  107. result.persistCredentials =
  108. (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE'
  109. return result
  110. }