input-helper.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 {ISourceSettings} from './git-source-provider'
  6. export function getInputs(): ISourceSettings {
  7. const result = ({} as unknown) as ISourceSettings
  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. }
  59. if (!result.ref && !result.commit) {
  60. result.ref = 'refs/heads/master'
  61. }
  62. }
  63. // SHA?
  64. else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
  65. result.commit = result.ref
  66. result.ref = ''
  67. }
  68. core.debug(`ref = '${result.ref}'`)
  69. core.debug(`commit = '${result.commit}'`)
  70. // Clean
  71. result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
  72. core.debug(`clean = ${result.clean}`)
  73. // Submodules
  74. if (core.getInput('submodules')) {
  75. throw new Error(
  76. "The input 'submodules' is not supported in actions/checkout@v2"
  77. )
  78. }
  79. // Fetch depth
  80. result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1'))
  81. if (isNaN(result.fetchDepth) || result.fetchDepth < 0) {
  82. result.fetchDepth = 0
  83. }
  84. core.debug(`fetch depth = ${result.fetchDepth}`)
  85. // LFS
  86. result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'
  87. core.debug(`lfs = ${result.lfs}`)
  88. // Access token
  89. result.accessToken = core.getInput('token')
  90. return result
  91. }