mirror of
https://github.com/tj-actions/changed-files
synced 2025-01-18 13:57:39 +00:00
chore: switch to use the github context (#1386)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
e3ea6b7948
commit
ec0b6d0e4c
7 changed files with 56 additions and 117 deletions
BIN
dist/index.js
generated
vendored
BIN
dist/index.js
generated
vendored
Binary file not shown.
BIN
dist/index.js.map
generated
vendored
BIN
dist/index.js.map
generated
vendored
Binary file not shown.
|
@ -1,19 +1,18 @@
|
|||
import * as core from '@actions/core'
|
||||
import * as github from '@actions/github'
|
||||
import type {RestEndpointMethodTypes} from '@octokit/rest'
|
||||
import flatten from 'lodash/flatten'
|
||||
import * as path from 'path'
|
||||
|
||||
import {DiffResult} from './commitSha'
|
||||
import {Env} from './env'
|
||||
import {Inputs} from './inputs'
|
||||
import {
|
||||
getAllChangedFiles,
|
||||
getDirnameMaxDepth,
|
||||
gitRenamedFiles,
|
||||
gitSubmoduleDiffSHA,
|
||||
jsonOutput,
|
||||
getAllChangedFiles
|
||||
jsonOutput
|
||||
} from './utils'
|
||||
import flatten from 'lodash/flatten'
|
||||
|
||||
export const getRenamedFiles = async ({
|
||||
inputs,
|
||||
|
@ -252,11 +251,9 @@ export const getAllChangeTypeFiles = async ({
|
|||
}
|
||||
|
||||
export const getChangedFilesFromGithubAPI = async ({
|
||||
inputs,
|
||||
env
|
||||
inputs
|
||||
}: {
|
||||
inputs: Inputs
|
||||
env: Env
|
||||
}): Promise<ChangedFiles> => {
|
||||
const octokit = github.getOctokit(inputs.token, {
|
||||
baseUrl: inputs.apiUrl
|
||||
|
@ -277,7 +274,7 @@ export const getChangedFilesFromGithubAPI = async ({
|
|||
const options = octokit.rest.pulls.listFiles.endpoint.merge({
|
||||
owner: github.context.repo.owner,
|
||||
repo: github.context.repo.repo,
|
||||
pull_number: env.GITHUB_EVENT_PULL_REQUEST_NUMBER,
|
||||
pull_number: github.context.payload.pull_request?.number,
|
||||
per_page: 100
|
||||
})
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.Added]
|
||||
})
|
||||
core.debug(`Added files: ${addedFiles}`)
|
||||
core.debug(`Added files: ${JSON.stringify(addedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('added_files', outputPrefix),
|
||||
value: addedFiles.paths,
|
||||
|
@ -65,7 +65,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.Copied]
|
||||
})
|
||||
core.debug(`Copied files: ${copiedFiles}`)
|
||||
core.debug(`Copied files: ${JSON.stringify(copiedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('copied_files', outputPrefix),
|
||||
value: copiedFiles.paths,
|
||||
|
@ -83,7 +83,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.Modified]
|
||||
})
|
||||
core.debug(`Modified files: ${modifiedFiles}`)
|
||||
core.debug(`Modified files: ${JSON.stringify(modifiedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('modified_files', outputPrefix),
|
||||
value: modifiedFiles.paths,
|
||||
|
@ -101,7 +101,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.Renamed]
|
||||
})
|
||||
core.debug(`Renamed files: ${renamedFiles}`)
|
||||
core.debug(`Renamed files: ${JSON.stringify(renamedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('renamed_files', outputPrefix),
|
||||
value: renamedFiles.paths,
|
||||
|
@ -119,7 +119,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.TypeChanged]
|
||||
})
|
||||
core.debug(`Type changed files: ${typeChangedFiles}`)
|
||||
core.debug(`Type changed files: ${JSON.stringify(typeChangedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('type_changed_files', outputPrefix),
|
||||
value: typeChangedFiles.paths,
|
||||
|
@ -137,7 +137,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.Unmerged]
|
||||
})
|
||||
core.debug(`Unmerged files: ${unmergedFiles}`)
|
||||
core.debug(`Unmerged files: ${JSON.stringify(unmergedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('unmerged_files', outputPrefix),
|
||||
value: unmergedFiles.paths,
|
||||
|
@ -155,7 +155,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.Unknown]
|
||||
})
|
||||
core.debug(`Unknown files: ${unknownFiles}`)
|
||||
core.debug(`Unknown files: ${JSON.stringify(unknownFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('unknown_files', outputPrefix),
|
||||
value: unknownFiles.paths,
|
||||
|
@ -172,7 +172,11 @@ export const setChangedFilesOutput = async ({
|
|||
inputs,
|
||||
changedFiles: allFilteredDiffFiles
|
||||
})
|
||||
core.debug(`All changed and modified files: ${allChangedAndModifiedFiles}`)
|
||||
core.debug(
|
||||
`All changed and modified files: ${JSON.stringify(
|
||||
allChangedAndModifiedFiles
|
||||
)}`
|
||||
)
|
||||
await setOutput({
|
||||
key: getOutputKey('all_changed_and_modified_files', outputPrefix),
|
||||
value: allChangedAndModifiedFiles.paths,
|
||||
|
@ -195,7 +199,7 @@ export const setChangedFilesOutput = async ({
|
|||
ChangeTypeEnum.Renamed
|
||||
]
|
||||
})
|
||||
core.debug(`All changed files: ${allChangedFiles}`)
|
||||
core.debug(`All changed files: ${JSON.stringify(allChangedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('all_changed_files', outputPrefix),
|
||||
value: allChangedFiles.paths,
|
||||
|
@ -224,7 +228,7 @@ export const setChangedFilesOutput = async ({
|
|||
ChangeTypeEnum.Renamed
|
||||
]
|
||||
})
|
||||
core.debug(`All other changed files: ${allOtherChangedFiles}`)
|
||||
core.debug(`All other changed files: ${JSON.stringify(allOtherChangedFiles)}`)
|
||||
|
||||
const otherChangedFiles = allOtherChangedFiles.paths
|
||||
.split(inputs.separator)
|
||||
|
@ -267,7 +271,7 @@ export const setChangedFilesOutput = async ({
|
|||
ChangeTypeEnum.Deleted
|
||||
]
|
||||
})
|
||||
core.debug(`All modified files: ${allModifiedFiles}`)
|
||||
core.debug(`All modified files: ${JSON.stringify(allModifiedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('all_modified_files', outputPrefix),
|
||||
value: allModifiedFiles.paths,
|
||||
|
@ -333,7 +337,7 @@ export const setChangedFilesOutput = async ({
|
|||
changedFiles: allFilteredDiffFiles,
|
||||
changeTypes: [ChangeTypeEnum.Deleted]
|
||||
})
|
||||
core.debug(`Deleted files: ${deletedFiles}`)
|
||||
core.debug(`Deleted files: ${JSON.stringify(deletedFiles)}`)
|
||||
await setOutput({
|
||||
key: getOutputKey('deleted_files', outputPrefix),
|
||||
value: deletedFiles.paths,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import * as core from '@actions/core'
|
||||
import * as github from '@actions/github'
|
||||
|
||||
import {Env} from './env'
|
||||
import {Inputs} from './inputs'
|
||||
|
@ -15,11 +16,9 @@ import {
|
|||
} from './utils'
|
||||
|
||||
const getCurrentSHA = async ({
|
||||
env,
|
||||
inputs,
|
||||
workingDirectory
|
||||
}: {
|
||||
env: Env
|
||||
inputs: Inputs
|
||||
workingDirectory: string
|
||||
}): Promise<string> => {
|
||||
|
@ -50,14 +49,14 @@ const getCurrentSHA = async ({
|
|||
} else {
|
||||
if (!currentSha) {
|
||||
if (
|
||||
env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA &&
|
||||
github.context.payload.pull_request?.head?.sha &&
|
||||
(await verifyCommitSha({
|
||||
sha: env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA,
|
||||
sha: github.context.payload.pull_request?.head?.sha,
|
||||
cwd: workingDirectory,
|
||||
showAsErrorMessage: false
|
||||
})) === 0
|
||||
) {
|
||||
currentSha = env.GITHUB_EVENT_PULL_REQUEST_HEAD_SHA
|
||||
currentSha = github.context.payload.pull_request?.head?.sha
|
||||
} else {
|
||||
currentSha = await getHeadSha({cwd: workingDirectory})
|
||||
}
|
||||
|
@ -97,8 +96,8 @@ export const getSHAForPushEvent = async (
|
|||
|
||||
if (isTag) {
|
||||
const sourceBranch =
|
||||
env.GITHUB_EVENT_BASE_REF.replace('refs/heads/', '') ||
|
||||
env.GITHUB_EVENT_RELEASE_TARGET_COMMITISH
|
||||
github.context.payload.base_ref.replace('refs/heads/', '') ||
|
||||
github.context.payload.release?.target_commitish
|
||||
await gitFetch({
|
||||
cwd: workingDirectory,
|
||||
args: [
|
||||
|
@ -137,7 +136,7 @@ export const getSHAForPushEvent = async (
|
|||
}
|
||||
}
|
||||
|
||||
const currentSha = await getCurrentSHA({env, inputs, workingDirectory})
|
||||
const currentSha = await getCurrentSHA({inputs, workingDirectory})
|
||||
let previousSha = inputs.baseSha
|
||||
const diff = '..'
|
||||
|
||||
|
@ -191,8 +190,11 @@ export const getSHAForPushEvent = async (
|
|||
targetBranch = tag
|
||||
} else {
|
||||
core.debug('Getting previous SHA for last remote commit...')
|
||||
if (env.GITHUB_EVENT_FORCED === 'false' || !env.GITHUB_EVENT_FORCED) {
|
||||
previousSha = env.GITHUB_EVENT_BEFORE
|
||||
if (
|
||||
github.context.payload.forced === 'false' ||
|
||||
!github.context.payload.forced
|
||||
) {
|
||||
previousSha = github.context.payload.before
|
||||
}
|
||||
|
||||
if (
|
||||
|
@ -265,8 +267,8 @@ export const getSHAForPullRequestEvent = async (
|
|||
hasSubmodule: boolean,
|
||||
gitFetchExtraArgs: string[]
|
||||
): Promise<DiffResult> => {
|
||||
let targetBranch = env.GITHUB_EVENT_PULL_REQUEST_BASE_REF
|
||||
const currentBranch = env.GITHUB_EVENT_PULL_REQUEST_HEAD_REF
|
||||
let targetBranch = github.context.payload.pull_request?.base?.ref
|
||||
const currentBranch = github.context.payload.pull_request?.head?.ref
|
||||
if (inputs.sinceLastRemoteCommit) {
|
||||
targetBranch = currentBranch
|
||||
}
|
||||
|
@ -281,7 +283,7 @@ export const getSHAForPullRequestEvent = async (
|
|||
'-u',
|
||||
'--progress',
|
||||
'origin',
|
||||
`pull/${env.GITHUB_EVENT_PULL_REQUEST_NUMBER}/head:${currentBranch}`
|
||||
`pull/${github.context.payload.pull_request?.number}/head:${currentBranch}`
|
||||
]
|
||||
})
|
||||
|
||||
|
@ -334,7 +336,7 @@ export const getSHAForPullRequestEvent = async (
|
|||
core.info('Completed fetching more history.')
|
||||
}
|
||||
|
||||
const currentSha = await getCurrentSHA({env, inputs, workingDirectory})
|
||||
const currentSha = await getCurrentSHA({inputs, workingDirectory})
|
||||
let previousSha = inputs.baseSha
|
||||
let diff = '...'
|
||||
|
||||
|
@ -362,15 +364,15 @@ export const getSHAForPullRequestEvent = async (
|
|||
}
|
||||
|
||||
if (
|
||||
!env.GITHUB_EVENT_PULL_REQUEST_BASE_REF ||
|
||||
env.GITHUB_EVENT_HEAD_REPO_FORK === 'true'
|
||||
!github.context.payload.pull_request?.base?.ref ||
|
||||
github.context.payload.head?.repo?.fork === 'true'
|
||||
) {
|
||||
diff = '..'
|
||||
}
|
||||
|
||||
if (!previousSha) {
|
||||
if (inputs.sinceLastRemoteCommit) {
|
||||
previousSha = env.GITHUB_EVENT_BEFORE
|
||||
previousSha = github.context.payload.before
|
||||
|
||||
if (
|
||||
!previousSha ||
|
||||
|
@ -389,7 +391,7 @@ export const getSHAForPullRequestEvent = async (
|
|||
core.warning(
|
||||
'Unable to locate the previous commit in the local history. Falling back to the pull request base sha.'
|
||||
)
|
||||
previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA
|
||||
previousSha = github.context.payload.pull_request?.base?.sha
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -399,7 +401,7 @@ export const getSHAForPullRequestEvent = async (
|
|||
})
|
||||
|
||||
if (!previousSha) {
|
||||
previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA
|
||||
previousSha = github.context.payload.pull_request?.base?.sha
|
||||
}
|
||||
|
||||
if (isShallow) {
|
||||
|
@ -449,7 +451,7 @@ export const getSHAForPullRequestEvent = async (
|
|||
}
|
||||
|
||||
if (!previousSha || previousSha === currentSha) {
|
||||
previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA
|
||||
previousSha = github.context.payload.pull_request?.base?.sha
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -494,7 +496,7 @@ export const getSHAForPullRequestEvent = async (
|
|||
// uses: actions/checkout@v3
|
||||
// with:
|
||||
// repository: ${{ github.event.pull_request.head.repo.full_name }}
|
||||
if (env.GITHUB_EVENT_NAME === 'pull_request_target') {
|
||||
if (github.context.eventName === 'pull_request_target') {
|
||||
core.warning(
|
||||
'If this pull request is from a forked repository, please set the checkout action `repository` input to the same repository as the pull request.'
|
||||
)
|
||||
|
|
68
src/env.ts
68
src/env.ts
|
@ -1,80 +1,16 @@
|
|||
import {promises as fs} from 'fs'
|
||||
import * as core from '@actions/core'
|
||||
|
||||
export type Env = {
|
||||
GITHUB_REF_NAME: string
|
||||
GITHUB_REF: string
|
||||
GITHUB_WORKSPACE: string
|
||||
GITHUB_EVENT_ACTION: string
|
||||
GITHUB_EVENT_NAME: string
|
||||
GITHUB_EVENT_FORCED: string
|
||||
GITHUB_EVENT_BEFORE: string
|
||||
GITHUB_EVENT_BASE_REF: string
|
||||
GITHUB_EVENT_RELEASE_TARGET_COMMITISH: string
|
||||
GITHUB_EVENT_HEAD_REPO_FORK: string
|
||||
GITHUB_EVENT_PULL_REQUEST_NUMBER: string
|
||||
GITHUB_EVENT_PULL_REQUEST_BASE_SHA: string
|
||||
GITHUB_EVENT_PULL_REQUEST_HEAD_SHA: string
|
||||
GITHUB_EVENT_PULL_REQUEST_HEAD_REF: string
|
||||
GITHUB_EVENT_PULL_REQUEST_BASE_REF: string
|
||||
GITHUB_REPOSITORY_OWNER: string
|
||||
GITHUB_REPOSITORY: string
|
||||
}
|
||||
|
||||
type GithubEvent = {
|
||||
action?: string
|
||||
forced?: string
|
||||
pull_request?: {
|
||||
head: {
|
||||
ref: string
|
||||
sha: string
|
||||
}
|
||||
base: {
|
||||
ref: string
|
||||
sha: string
|
||||
}
|
||||
number: string
|
||||
}
|
||||
release?: {
|
||||
target_commitish: string
|
||||
}
|
||||
before?: string
|
||||
base_ref?: string
|
||||
head?: {
|
||||
repo?: {
|
||||
fork: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const getEnv = async (): Promise<Env> => {
|
||||
const eventPath = process.env.GITHUB_EVENT_PATH
|
||||
let eventJson: GithubEvent = {}
|
||||
|
||||
if (eventPath) {
|
||||
eventJson = JSON.parse(await fs.readFile(eventPath, {encoding: 'utf8'}))
|
||||
}
|
||||
core.debug(`Env: ${JSON.stringify(process.env, null, 2)}`)
|
||||
core.debug(`Event: ${JSON.stringify(eventJson, null, 2)}`)
|
||||
core.debug(`Process Env: ${JSON.stringify(process.env, null, 2)}`)
|
||||
|
||||
return {
|
||||
GITHUB_EVENT_PULL_REQUEST_HEAD_REF: eventJson.pull_request?.head?.ref || '',
|
||||
GITHUB_EVENT_PULL_REQUEST_BASE_REF: eventJson.pull_request?.base?.ref || '',
|
||||
GITHUB_EVENT_BEFORE: eventJson.before || '',
|
||||
GITHUB_EVENT_BASE_REF: eventJson.base_ref || '',
|
||||
GITHUB_EVENT_RELEASE_TARGET_COMMITISH:
|
||||
eventJson.release?.target_commitish || '',
|
||||
GITHUB_EVENT_HEAD_REPO_FORK: eventJson.head?.repo?.fork || '',
|
||||
GITHUB_EVENT_PULL_REQUEST_NUMBER: eventJson.pull_request?.number || '',
|
||||
GITHUB_EVENT_PULL_REQUEST_BASE_SHA: eventJson.pull_request?.base?.sha || '',
|
||||
GITHUB_EVENT_PULL_REQUEST_HEAD_SHA: eventJson.pull_request?.head?.sha || '',
|
||||
GITHUB_EVENT_FORCED: eventJson.forced || '',
|
||||
GITHUB_EVENT_ACTION: eventJson.action || '',
|
||||
GITHUB_REF_NAME: process.env.GITHUB_REF_NAME || '',
|
||||
GITHUB_REF: process.env.GITHUB_REF || '',
|
||||
GITHUB_WORKSPACE: process.env.GITHUB_WORKSPACE || '',
|
||||
GITHUB_EVENT_NAME: process.env.GITHUB_EVENT_NAME || '',
|
||||
GITHUB_REPOSITORY_OWNER: process.env.GITHUB_REPOSITORY_OWNER || '',
|
||||
GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY || ''
|
||||
GITHUB_WORKSPACE: process.env.GITHUB_WORKSPACE || ''
|
||||
}
|
||||
}
|
||||
|
|
20
src/main.ts
20
src/main.ts
|
@ -1,4 +1,5 @@
|
|||
import * as core from '@actions/core'
|
||||
import * as github from '@actions/github'
|
||||
import path from 'path'
|
||||
import {
|
||||
getAllDiffFiles,
|
||||
|
@ -76,8 +77,8 @@ const getChangedFilesFromLocalGit = async ({
|
|||
|
||||
let diffResult: DiffResult
|
||||
|
||||
if (!env.GITHUB_EVENT_PULL_REQUEST_BASE_REF) {
|
||||
core.info(`Running on a ${env.GITHUB_EVENT_NAME || 'push'} event...`)
|
||||
if (!github.context.payload.pull_request?.base?.ref) {
|
||||
core.info(`Running on a ${github.context.eventName || 'push'} event...`)
|
||||
diffResult = await getSHAForPushEvent(
|
||||
inputs,
|
||||
env,
|
||||
|
@ -89,8 +90,8 @@ const getChangedFilesFromLocalGit = async ({
|
|||
)
|
||||
} else {
|
||||
core.info(
|
||||
`Running on a ${env.GITHUB_EVENT_NAME || 'pull_request'} (${
|
||||
env.GITHUB_EVENT_ACTION
|
||||
`Running on a ${github.context.eventName || 'pull_request'} (${
|
||||
github.context.payload.action
|
||||
}) event...`
|
||||
)
|
||||
diffResult = await getSHAForPullRequestEvent(
|
||||
|
@ -192,20 +193,17 @@ const getChangedFilesFromLocalGit = async ({
|
|||
|
||||
const getChangedFilesFromRESTAPI = async ({
|
||||
inputs,
|
||||
env,
|
||||
workingDirectory,
|
||||
filePatterns,
|
||||
yamlFilePatterns
|
||||
}: {
|
||||
inputs: Inputs
|
||||
env: Env
|
||||
workingDirectory: string
|
||||
filePatterns: string[]
|
||||
yamlFilePatterns: Record<string, string[]>
|
||||
}): Promise<void> => {
|
||||
const allDiffFiles = await getChangedFilesFromGithubAPI({
|
||||
inputs,
|
||||
env
|
||||
inputs
|
||||
})
|
||||
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
|
||||
core.info('All Done!')
|
||||
|
@ -258,6 +256,9 @@ export async function run(): Promise<void> {
|
|||
const inputs = getInputs()
|
||||
core.debug(`Inputs: ${JSON.stringify(inputs, null, 2)}`)
|
||||
|
||||
const githubContext = github.context
|
||||
core.debug(`Github Context: ${JSON.stringify(githubContext, null, 2)}`)
|
||||
|
||||
const workingDirectory = path.resolve(
|
||||
env.GITHUB_WORKSPACE || process.cwd(),
|
||||
inputs.path
|
||||
|
@ -281,7 +282,7 @@ export async function run(): Promise<void> {
|
|||
|
||||
if (
|
||||
inputs.token &&
|
||||
env.GITHUB_EVENT_PULL_REQUEST_NUMBER &&
|
||||
github.context.payload.pull_request?.number &&
|
||||
!hasGitDirectory
|
||||
) {
|
||||
core.info("Using GitHub's REST API to get changed files")
|
||||
|
@ -305,7 +306,6 @@ export async function run(): Promise<void> {
|
|||
}
|
||||
await getChangedFilesFromRESTAPI({
|
||||
inputs,
|
||||
env,
|
||||
workingDirectory,
|
||||
filePatterns,
|
||||
yamlFilePatterns
|
||||
|
|
Loading…
Reference in a new issue