2023-05-25 18:22:24 +00:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import path from 'path'
|
2023-06-17 02:33:42 +00:00
|
|
|
import {getAllDiffFiles, getRenamedFiles} from './changedFiles'
|
2023-06-16 06:17:13 +00:00
|
|
|
import {setChangedFilesOutput} from './changedFilesOutput'
|
2023-06-14 18:45:32 +00:00
|
|
|
import {
|
|
|
|
DiffResult,
|
2023-05-25 18:22:24 +00:00
|
|
|
getSHAForPullRequestEvent,
|
2023-06-14 18:45:32 +00:00
|
|
|
getSHAForPushEvent
|
2023-05-25 18:22:24 +00:00
|
|
|
} from './commitSha'
|
|
|
|
import {getEnv} from './env'
|
|
|
|
import {getInputs} from './inputs'
|
|
|
|
import {
|
|
|
|
getFilePatterns,
|
|
|
|
getSubmodulePath,
|
2023-06-16 06:17:13 +00:00
|
|
|
getYamlFilePatterns,
|
2023-05-25 18:22:24 +00:00
|
|
|
isRepoShallow,
|
|
|
|
setOutput,
|
|
|
|
submoduleExists,
|
|
|
|
updateGitGlobalConfig,
|
|
|
|
verifyMinimumGitVersion
|
|
|
|
} from './utils'
|
|
|
|
|
|
|
|
export async function run(): Promise<void> {
|
|
|
|
core.startGroup('changed-files')
|
|
|
|
|
|
|
|
const env = await getEnv()
|
|
|
|
core.debug(`Env: ${JSON.stringify(env, null, 2)}`)
|
|
|
|
const inputs = getInputs()
|
|
|
|
core.debug(`Inputs: ${JSON.stringify(inputs, null, 2)}`)
|
|
|
|
|
|
|
|
await verifyMinimumGitVersion()
|
|
|
|
|
|
|
|
let quotePathValue = 'on'
|
|
|
|
|
|
|
|
if (!inputs.quotePath) {
|
|
|
|
quotePathValue = 'off'
|
|
|
|
}
|
|
|
|
|
|
|
|
await updateGitGlobalConfig({
|
|
|
|
name: 'core.quotepath',
|
|
|
|
value: quotePathValue
|
|
|
|
})
|
|
|
|
|
|
|
|
if (inputs.diffRelative) {
|
|
|
|
await updateGitGlobalConfig({
|
|
|
|
name: 'diff.relative',
|
|
|
|
value: 'true'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const workingDirectory = path.resolve(
|
|
|
|
env.GITHUB_WORKSPACE || process.cwd(),
|
|
|
|
inputs.path
|
|
|
|
)
|
|
|
|
const isShallow = await isRepoShallow({cwd: workingDirectory})
|
|
|
|
const hasSubmodule = await submoduleExists({cwd: workingDirectory})
|
2023-06-14 19:22:47 +00:00
|
|
|
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules']
|
2023-05-25 18:22:24 +00:00
|
|
|
const isTag = env.GITHUB_REF?.startsWith('refs/tags/')
|
2023-06-14 19:59:31 +00:00
|
|
|
const outputRenamedFilesAsDeletedAndAdded =
|
|
|
|
inputs.outputRenamedFilesAsDeletedAndAdded
|
2023-05-25 21:53:58 +00:00
|
|
|
let submodulePaths: string[] = []
|
2023-05-25 21:43:31 +00:00
|
|
|
|
|
|
|
if (hasSubmodule) {
|
2023-05-25 21:53:58 +00:00
|
|
|
submodulePaths = await getSubmodulePath({cwd: workingDirectory})
|
2023-05-25 21:43:31 +00:00
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
|
|
|
|
if (isTag) {
|
2023-06-14 19:22:47 +00:00
|
|
|
gitFetchExtraArgs = ['--prune', '--no-recurse-submodules']
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let diffResult: DiffResult
|
|
|
|
|
|
|
|
if (!env.GITHUB_EVENT_PULL_REQUEST_BASE_REF) {
|
2023-06-08 12:12:03 +00:00
|
|
|
core.info(`Running on a ${env.GITHUB_EVENT_NAME || 'push'} event...`)
|
2023-05-25 18:22:24 +00:00
|
|
|
diffResult = await getSHAForPushEvent(
|
|
|
|
inputs,
|
|
|
|
env,
|
|
|
|
workingDirectory,
|
|
|
|
isShallow,
|
|
|
|
hasSubmodule,
|
2023-06-14 19:22:47 +00:00
|
|
|
gitFetchExtraArgs,
|
2023-05-25 18:22:24 +00:00
|
|
|
isTag
|
|
|
|
)
|
|
|
|
} else {
|
2023-06-08 12:12:03 +00:00
|
|
|
core.info(
|
2023-06-16 06:17:13 +00:00
|
|
|
`Running on a ${env.GITHUB_EVENT_NAME || 'pull_request'} (${
|
|
|
|
env.GITHUB_EVENT_ACTION
|
|
|
|
}) event...`
|
2023-06-08 12:12:03 +00:00
|
|
|
)
|
2023-05-25 18:22:24 +00:00
|
|
|
diffResult = await getSHAForPullRequestEvent(
|
|
|
|
inputs,
|
|
|
|
env,
|
|
|
|
workingDirectory,
|
|
|
|
isShallow,
|
|
|
|
hasSubmodule,
|
2023-06-14 19:22:47 +00:00
|
|
|
gitFetchExtraArgs
|
2023-05-25 18:22:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-26 16:48:32 +00:00
|
|
|
if (diffResult.initialCommit) {
|
|
|
|
core.info('This is the first commit for this repository; exiting...')
|
|
|
|
core.endGroup()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:22:24 +00:00
|
|
|
core.info(
|
|
|
|
`Retrieving changes between ${diffResult.previousSha} (${diffResult.targetBranch}) → ${diffResult.currentSha} (${diffResult.currentBranch})`
|
|
|
|
)
|
|
|
|
|
2023-06-14 18:45:32 +00:00
|
|
|
const allDiffFiles = await getAllDiffFiles({
|
2023-05-25 18:22:24 +00:00
|
|
|
workingDirectory,
|
|
|
|
hasSubmodule,
|
|
|
|
diffResult,
|
2023-06-14 19:59:31 +00:00
|
|
|
submodulePaths,
|
|
|
|
outputRenamedFilesAsDeletedAndAdded
|
2023-05-25 18:22:24 +00:00
|
|
|
})
|
2023-06-14 18:45:32 +00:00
|
|
|
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
|
2023-06-16 06:17:13 +00:00
|
|
|
core.info('All Done!')
|
|
|
|
core.endGroup()
|
2023-06-14 18:45:32 +00:00
|
|
|
|
2023-06-16 06:17:13 +00:00
|
|
|
const filePatterns = await getFilePatterns({
|
2023-05-25 18:22:24 +00:00
|
|
|
inputs,
|
2023-06-16 06:17:13 +00:00
|
|
|
workingDirectory
|
2023-05-25 18:22:24 +00:00
|
|
|
})
|
2023-06-16 06:17:13 +00:00
|
|
|
core.debug(`File patterns: ${filePatterns}`)
|
2023-05-25 18:22:24 +00:00
|
|
|
|
2023-06-16 06:17:13 +00:00
|
|
|
if (filePatterns.length > 0) {
|
|
|
|
core.startGroup('changed-files-patterns')
|
|
|
|
await setChangedFilesOutput({
|
|
|
|
allDiffFiles,
|
|
|
|
filePatterns,
|
2023-06-17 02:33:42 +00:00
|
|
|
inputs,
|
|
|
|
workingDirectory,
|
|
|
|
diffResult
|
2023-06-16 06:17:13 +00:00
|
|
|
})
|
|
|
|
core.info('All Done!')
|
|
|
|
core.endGroup()
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
|
2023-06-16 06:17:13 +00:00
|
|
|
const yamlFilePatterns = await getYamlFilePatterns({
|
2023-05-25 18:22:24 +00:00
|
|
|
inputs,
|
2023-06-16 06:17:13 +00:00
|
|
|
workingDirectory
|
2023-05-25 18:22:24 +00:00
|
|
|
})
|
2023-06-16 06:17:13 +00:00
|
|
|
core.debug(`Yaml file patterns: ${JSON.stringify(yamlFilePatterns)}`)
|
|
|
|
|
|
|
|
if (Object.keys(yamlFilePatterns).length > 0) {
|
|
|
|
for (const key of Object.keys(yamlFilePatterns)) {
|
|
|
|
core.startGroup(`changed-files-yaml-${key}`)
|
|
|
|
await setChangedFilesOutput({
|
|
|
|
allDiffFiles,
|
|
|
|
filePatterns: yamlFilePatterns[key],
|
2023-06-17 02:33:42 +00:00
|
|
|
outputPrefix: key,
|
2023-06-16 06:17:13 +00:00
|
|
|
inputs,
|
2023-06-17 02:33:42 +00:00
|
|
|
workingDirectory,
|
|
|
|
diffResult
|
2023-06-16 06:17:13 +00:00
|
|
|
})
|
|
|
|
core.info('All Done!')
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
|
2023-06-16 06:17:13 +00:00
|
|
|
if (filePatterns.length === 0 && Object.keys(yamlFilePatterns).length === 0) {
|
|
|
|
core.startGroup('changed-files-all')
|
|
|
|
await setChangedFilesOutput({
|
|
|
|
allDiffFiles,
|
2023-06-17 02:33:42 +00:00
|
|
|
inputs,
|
|
|
|
workingDirectory,
|
|
|
|
diffResult
|
2023-06-16 06:17:13 +00:00
|
|
|
})
|
|
|
|
core.info('All Done!')
|
|
|
|
core.endGroup()
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
|
|
|
|
if (inputs.includeAllOldNewRenamedFiles) {
|
2023-06-16 06:17:13 +00:00
|
|
|
core.startGroup('changed-files-all-old-new-renamed-files')
|
2023-05-25 18:22:24 +00:00
|
|
|
const allOldNewRenamedFiles = await getRenamedFiles({
|
|
|
|
inputs,
|
|
|
|
workingDirectory,
|
|
|
|
hasSubmodule,
|
|
|
|
diffResult,
|
|
|
|
submodulePaths
|
|
|
|
})
|
|
|
|
core.debug(`All old new renamed files: ${allOldNewRenamedFiles}`)
|
|
|
|
await setOutput({
|
|
|
|
key: 'all_old_new_renamed_files',
|
|
|
|
value: allOldNewRenamedFiles,
|
|
|
|
inputs
|
|
|
|
})
|
2023-06-16 06:17:13 +00:00
|
|
|
core.info('All Done!')
|
|
|
|
core.endGroup()
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (!process.env.TESTING) {
|
|
|
|
// eslint-disable-next-line github/no-then
|
|
|
|
run().catch(e => {
|
|
|
|
core.setFailed(e.message || e)
|
|
|
|
})
|
|
|
|
}
|