feat: add support for retrieving changed files via github rest api (#1289)
Co-authored-by: GitHub Action <action@github.com> Co-authored-by: tj-actions[bot] <109116665+tj-actions-bot@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
150
src/main.ts
150
src/main.ts
@@ -1,18 +1,23 @@
|
||||
import * as core from '@actions/core'
|
||||
import path from 'path'
|
||||
import {getAllDiffFiles, getRenamedFiles} from './changedFiles'
|
||||
import {
|
||||
getAllDiffFiles,
|
||||
getChangedFilesFromGithubAPI,
|
||||
getRenamedFiles
|
||||
} from './changedFiles'
|
||||
import {setChangedFilesOutput} from './changedFilesOutput'
|
||||
import {
|
||||
DiffResult,
|
||||
getSHAForPullRequestEvent,
|
||||
getSHAForPushEvent
|
||||
} from './commitSha'
|
||||
import {getEnv} from './env'
|
||||
import {getInputs} from './inputs'
|
||||
import {Env, getEnv} from './env'
|
||||
import {getInputs, Inputs} from './inputs'
|
||||
import {
|
||||
getFilePatterns,
|
||||
getSubmodulePath,
|
||||
getYamlFilePatterns,
|
||||
hasLocalGitDirectory,
|
||||
isRepoShallow,
|
||||
setOutput,
|
||||
submoduleExists,
|
||||
@@ -20,14 +25,15 @@ import {
|
||||
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)}`)
|
||||
|
||||
const getChangedFilesFromLocalGit = async ({
|
||||
inputs,
|
||||
env,
|
||||
workingDirectory
|
||||
}: {
|
||||
inputs: Inputs
|
||||
env: Env
|
||||
workingDirectory: string
|
||||
}): Promise<void> => {
|
||||
await verifyMinimumGitVersion()
|
||||
|
||||
let quotePathValue = 'on'
|
||||
@@ -48,10 +54,6 @@ export async function run(): Promise<void> {
|
||||
})
|
||||
}
|
||||
|
||||
const workingDirectory = path.resolve(
|
||||
env.GITHUB_WORKSPACE || process.cwd(),
|
||||
inputs.path
|
||||
)
|
||||
const isShallow = await isRepoShallow({cwd: workingDirectory})
|
||||
const hasSubmodule = await submoduleExists({cwd: workingDirectory})
|
||||
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules']
|
||||
@@ -196,6 +198,124 @@ export async function run(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
const getChangedFilesFromRESTAPI = async ({
|
||||
inputs,
|
||||
env,
|
||||
workingDirectory
|
||||
}: {
|
||||
inputs: Inputs
|
||||
env: Env
|
||||
workingDirectory: string
|
||||
}): Promise<void> => {
|
||||
const allDiffFiles = await getChangedFilesFromGithubAPI({
|
||||
inputs,
|
||||
env
|
||||
})
|
||||
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
|
||||
core.info('All Done!')
|
||||
|
||||
const filePatterns = await getFilePatterns({
|
||||
inputs,
|
||||
workingDirectory
|
||||
})
|
||||
core.debug(`File patterns: ${filePatterns}`)
|
||||
|
||||
if (filePatterns.length > 0) {
|
||||
core.startGroup('changed-files-patterns')
|
||||
await setChangedFilesOutput({
|
||||
allDiffFiles,
|
||||
filePatterns,
|
||||
inputs,
|
||||
workingDirectory
|
||||
})
|
||||
core.info('All Done!')
|
||||
core.endGroup()
|
||||
}
|
||||
|
||||
const yamlFilePatterns = await getYamlFilePatterns({
|
||||
inputs,
|
||||
workingDirectory
|
||||
})
|
||||
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],
|
||||
outputPrefix: key,
|
||||
inputs,
|
||||
workingDirectory
|
||||
})
|
||||
core.info('All Done!')
|
||||
core.endGroup()
|
||||
}
|
||||
}
|
||||
|
||||
if (filePatterns.length === 0 && Object.keys(yamlFilePatterns).length === 0) {
|
||||
core.startGroup('changed-files-all')
|
||||
await setChangedFilesOutput({
|
||||
allDiffFiles,
|
||||
inputs,
|
||||
workingDirectory
|
||||
})
|
||||
core.info('All Done!')
|
||||
core.endGroup()
|
||||
}
|
||||
}
|
||||
|
||||
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)}`)
|
||||
const workingDirectory = path.resolve(
|
||||
env.GITHUB_WORKSPACE || process.cwd(),
|
||||
inputs.path
|
||||
)
|
||||
const hasGitDirectory = await hasLocalGitDirectory({workingDirectory})
|
||||
|
||||
if (
|
||||
inputs.token &&
|
||||
env.GITHUB_EVENT_PULL_REQUEST_NUMBER &&
|
||||
!hasGitDirectory
|
||||
) {
|
||||
core.info("Using GitHub's REST API to get changed files")
|
||||
const unsupportedInputs: (keyof Inputs)[] = [
|
||||
'sha',
|
||||
'baseSha',
|
||||
'since',
|
||||
'until',
|
||||
'sinceLastRemoteCommit',
|
||||
'recoverDeletedFiles',
|
||||
'recoverDeletedFilesToDestination',
|
||||
'includeAllOldNewRenamedFiles'
|
||||
]
|
||||
|
||||
for (const input of unsupportedInputs) {
|
||||
if (inputs[input]) {
|
||||
core.warning(
|
||||
`Input "${input}" is not supported when using GitHub's REST API to get changed files`
|
||||
)
|
||||
}
|
||||
}
|
||||
await getChangedFilesFromRESTAPI({inputs, env, workingDirectory})
|
||||
} else {
|
||||
if (!hasGitDirectory) {
|
||||
core.setFailed(
|
||||
"Can't find local .git directory. Please run actions/checkout before this action"
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
core.info('Using local .git directory')
|
||||
await getChangedFilesFromLocalGit({inputs, env, workingDirectory})
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore if */
|
||||
if (!process.env.TESTING) {
|
||||
// eslint-disable-next-line github/no-then
|
||||
|
||||
Reference in New Issue
Block a user