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