input-helper.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  65. // SHA?
  66. else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
  67. result.commit = result.ref
  68. result.ref = ''
  69. }
  70. core.debug(`ref = '${result.ref}'`)
  71. core.debug(`commit = '${result.commit}'`)
  72. // Clean
  73. result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
  74. core.debug(`clean = ${result.clean}`)
  75. // Fetch depth
  76. result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1'))
  77. if (isNaN(result.fetchDepth) || result.fetchDepth < 0) {
  78. result.fetchDepth = 0
  79. }
  80. core.debug(`fetch depth = ${result.fetchDepth}`)
  81. // LFS
  82. result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'
  83. core.debug(`lfs = ${result.lfs}`)
  84. // Submodules
  85. result.submodules = false
  86. result.nestedSubmodules = false
  87. const submodulesString = (core.getInput('submodules') || '').toUpperCase()
  88. if (submodulesString == 'RECURSIVE') {
  89. result.submodules = true
  90. result.nestedSubmodules = true
  91. } else if (submodulesString == 'TRUE') {
  92. result.submodules = true
  93. }
  94. core.debug(`submodules = ${result.submodules}`)
  95. core.debug(`recursive submodules = ${result.nestedSubmodules}`)
  96. // Auth token
  97. result.authToken = core.getInput('token', {required: true})
  98. // SSH
  99. result.sshKey = core.getInput('ssh-key')
  100. result.sshKnownHosts = core.getInput('ssh-known-hosts')
  101. result.sshStrict =
  102. (core.getInput('ssh-strict') || 'true').toUpperCase() === 'TRUE'
  103. // Persist credentials
  104. result.persistCredentials =
  105. (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE'
  106. return result
  107. }