3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-01-17 19:17:45 +00:00

fix: bug with inaccurate warnings (#1853)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tonye Jack 2024-01-13 14:53:17 -07:00 committed by GitHub
parent e0579abf95
commit 62f4729b5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 277 additions and 51 deletions

BIN
dist/index.js generated vendored

Binary file not shown.

BIN
dist/index.js.map generated vendored

Binary file not shown.

View file

@ -1,12 +1,18 @@
import * as core from '@actions/core'
import {promises as fs} from 'fs'
import path from 'path'
import {ChangeTypeEnum} from '../changedFiles'
import {Inputs} from '../inputs'
import {
getDirname,
getDirnameMaxDepth,
getFilteredChangedFiles,
normalizeSeparators
normalizeSeparators,
warnUnsupportedRESTAPIInputs
} from '../utils'
const originalPlatform = process.platform
const ACTION_PATH = path.resolve(__dirname, '..', '..', 'action.yml')
function mockedPlatform(platform: string): void {
Object.defineProperty(process, 'platform', {
@ -575,4 +581,160 @@ describe('utils test', () => {
expect(filteredFiles[ChangeTypeEnum.Modified]).toEqual([])
})
})
describe('warnUnsupportedRESTAPIInputs', () => {
// Warns about unsupported inputs when using the REST API.
it('should warn about unsupported inputs when all inputs are supported', async () => {
const inputs: Inputs = {
files: '',
filesSeparator: '\n',
filesFromSourceFile: '',
filesFromSourceFileSeparator: '\n',
filesYaml: '',
filesYamlFromSourceFile: '',
filesYamlFromSourceFileSeparator: '\n',
filesIgnore: '',
filesIgnoreSeparator: '\n',
filesIgnoreFromSourceFile: '',
filesIgnoreFromSourceFileSeparator: '\n',
filesIgnoreYaml: '',
filesIgnoreYamlFromSourceFile: '',
filesIgnoreYamlFromSourceFileSeparator: '\n',
separator: ' ',
includeAllOldNewRenamedFiles: false,
oldNewSeparator: ',',
oldNewFilesSeparator: ' ',
sha: '1313123',
baseSha: '',
since: '',
until: '',
path: '.',
quotepath: true,
diffRelative: true,
dirNames: false,
dirNamesMaxDepth: undefined,
dirNamesExcludeCurrentDir: false,
dirNamesIncludeFiles: '',
dirNamesIncludeFilesSeparator: '\n',
dirNamesDeletedFilesIncludeOnlyDeletedDirs: false,
json: false,
escapeJson: true,
safeOutput: true,
fetchDepth: 50,
fetchAdditionalSubmoduleHistory: false,
sinceLastRemoteCommit: false,
writeOutputFiles: false,
outputDir: '.github/outputs',
outputRenamedFilesAsDeletedAndAdded: false,
recoverDeletedFiles: false,
recoverDeletedFilesToDestination: '',
recoverFiles: '',
recoverFilesSeparator: '\n',
recoverFilesIgnore: '',
recoverFilesIgnoreSeparator: '\n',
token: '${{ github.token }}',
apiUrl: '${{ github.api_url }}',
skipInitialFetch: false,
failOnInitialDiffError: false,
failOnSubmoduleDiffError: false,
negationPatternsFirst: false,
useRestApi: false
}
const coreWarningSpy = jest.spyOn(core, 'warning')
await warnUnsupportedRESTAPIInputs({
actionPath: ACTION_PATH,
inputs
})
expect(coreWarningSpy).toHaveBeenCalledWith(
'Input "sha" is not supported when using GitHub\'s REST API to get changed files'
)
})
// Throws an error if there are YAML errors in the action file.
it('should throw an error if there are YAML errors in the action file', async () => {
const actionPath = './path/to/action.yml'
const inputs: Inputs = {
files: '',
filesSeparator: '\n',
filesFromSourceFile: '',
filesFromSourceFileSeparator: '\n',
filesYaml: '',
filesYamlFromSourceFile: '',
filesYamlFromSourceFileSeparator: '\n',
filesIgnore: '',
filesIgnoreSeparator: '\n',
filesIgnoreFromSourceFile: '',
filesIgnoreFromSourceFileSeparator: '\n',
filesIgnoreYaml: '',
filesIgnoreYamlFromSourceFile: '',
filesIgnoreYamlFromSourceFileSeparator: '\n',
separator: ' ',
includeAllOldNewRenamedFiles: false,
oldNewSeparator: ',',
oldNewFilesSeparator: ' ',
sha: '1313123',
baseSha: '',
since: '',
until: '',
path: '.',
quotepath: true,
diffRelative: true,
dirNames: false,
dirNamesMaxDepth: undefined,
dirNamesExcludeCurrentDir: false,
dirNamesIncludeFiles: '',
dirNamesIncludeFilesSeparator: '\n',
dirNamesDeletedFilesIncludeOnlyDeletedDirs: false,
json: false,
escapeJson: true,
safeOutput: true,
fetchDepth: 50,
fetchAdditionalSubmoduleHistory: false,
sinceLastRemoteCommit: false,
writeOutputFiles: false,
outputDir: '.github/outputs',
outputRenamedFilesAsDeletedAndAdded: false,
recoverDeletedFiles: false,
recoverDeletedFilesToDestination: '',
recoverFiles: '',
recoverFilesSeparator: '\n',
recoverFilesIgnore: '',
recoverFilesIgnoreSeparator: '\n',
token: '${{ github.token }}',
apiUrl: '${{ github.api_url }}',
skipInitialFetch: false,
failOnInitialDiffError: false,
failOnSubmoduleDiffError: false,
negationPatternsFirst: false,
useRestApi: false
}
// Mocking readFile to return action file contents with errors
jest.spyOn(fs, 'readFile').mockResolvedValue(`
inputs:
files:
description: Files
required: true
default: ""
sha:
description: SHA
required: true
default: abc123
token:
description: Token
required: true
default: my-token
warnings:
| Invalid input value`)
await expect(
warnUnsupportedRESTAPIInputs({actionPath, inputs})
).rejects.toThrow(
/YAML errors in .\/path\/to\/action.yml: YAMLParseError: Not a YAML token: Invalid input value at line 16, column 13:/
)
})
})
})

View file

@ -166,7 +166,7 @@ export const getRenamedFiles = async ({
}))
) {
let message = `Unable to use three dot diff for: ${submodulePath} submodule. Falling back to two dot diff. You can set 'fetch_additional_submodule_history: true' to fetch additional submodule history in order to use three dot diff`
if (inputs.fetchSubmoduleHistory) {
if (inputs.fetchAdditionalSubmoduleHistory) {
message = `To fetch additional submodule history for: ${submodulePath} you can increase history depth using 'fetch_depth' input`
}
core.info(message)
@ -221,7 +221,7 @@ export const getAllDiffFiles = async ({
diffResult,
submodulePaths,
outputRenamedFilesAsDeletedAndAdded,
fetchSubmoduleHistory,
fetchAdditionalSubmoduleHistory,
failOnInitialDiffError,
failOnSubmoduleDiffError
}: {
@ -230,7 +230,7 @@ export const getAllDiffFiles = async ({
diffResult: DiffResult
submodulePaths: string[]
outputRenamedFilesAsDeletedAndAdded: boolean
fetchSubmoduleHistory: boolean
fetchAdditionalSubmoduleHistory: boolean
failOnInitialDiffError: boolean
failOnSubmoduleDiffError: boolean
}): Promise<ChangedFiles> => {
@ -270,7 +270,7 @@ export const getAllDiffFiles = async ({
}))
) {
let message = `Set 'fetch_additional_submodule_history: true' to fetch additional submodule history for: ${submodulePath}`
if (fetchSubmoduleHistory) {
if (fetchAdditionalSubmoduleHistory) {
message = `To fetch additional submodule history for: ${submodulePath} you can increase history depth using 'fetch_depth' input`
}
core.warning(message)

View file

@ -152,7 +152,7 @@ export const getSHAForNonPullRequestEvent = async (
})
}
} else {
if (hasSubmodule && inputs.fetchSubmoduleHistory) {
if (hasSubmodule && inputs.fetchAdditionalSubmoduleHistory) {
await gitFetchSubmodules({
cwd: workingDirectory,
args: [
@ -382,7 +382,7 @@ export const getSHAForPullRequestEvent = async (
}
}
} else {
if (hasSubmodule && inputs.fetchSubmoduleHistory) {
if (hasSubmodule && inputs.fetchAdditionalSubmoduleHistory) {
await gitFetchSubmodules({
cwd: workingDirectory,
args: [

24
src/constant.ts Normal file
View file

@ -0,0 +1,24 @@
import {Inputs} from './inputs'
export const UNSUPPORTED_REST_API_INPUTS: (keyof Inputs)[] = [
'sha',
'baseSha',
'since',
'until',
'path',
'quotepath',
'diffRelative',
'sinceLastRemoteCommit',
'recoverDeletedFiles',
'recoverDeletedFilesToDestination',
'recoverFiles',
'recoverFilesSeparator',
'recoverFilesIgnore',
'recoverFilesIgnoreSeparator',
'includeAllOldNewRenamedFiles',
'oldNewSeparator',
'oldNewFilesSeparator',
'skipInitialFetch',
'fetchAdditionalSubmoduleHistory',
'dirNamesDeletedFilesIncludeOnlyDeletedDirs'
]

View file

@ -24,7 +24,7 @@ export type Inputs = {
since: string
until: string
path: string
quotePath: boolean
quotepath: boolean
diffRelative: boolean
dirNames: boolean
dirNamesMaxDepth?: number
@ -36,7 +36,7 @@ export type Inputs = {
escapeJson: boolean
safeOutput: boolean
fetchDepth?: number
fetchSubmoduleHistory: boolean
fetchAdditionalSubmoduleHistory: boolean
sinceLastRemoteCommit: boolean
writeOutputFiles: boolean
outputDir: string
@ -132,7 +132,7 @@ export const getInputs = (): Inputs => {
const since = core.getInput('since', {required: false})
const until = core.getInput('until', {required: false})
const path = core.getInput('path', {required: false})
const quotePath = core.getBooleanInput('quotepath', {required: false})
const quotepath = core.getBooleanInput('quotepath', {required: false})
const diffRelative = core.getBooleanInput('diff_relative', {required: false})
const dirNames = core.getBooleanInput('dir_names', {required: false})
const dirNamesMaxDepth = core.getInput('dir_names_max_depth', {
@ -197,7 +197,7 @@ export const getInputs = (): Inputs => {
const skipInitialFetch = core.getBooleanInput('skip_initial_fetch', {
required: false
})
const fetchSubmoduleHistory = core.getBooleanInput(
const fetchAdditionalSubmoduleHistory = core.getBooleanInput(
'fetch_additional_submodule_history',
{
required: false
@ -257,7 +257,7 @@ export const getInputs = (): Inputs => {
since,
until,
path,
quotePath,
quotepath,
diffRelative,
sinceLastRemoteCommit,
recoverDeletedFiles,
@ -270,7 +270,7 @@ export const getInputs = (): Inputs => {
oldNewSeparator,
oldNewFilesSeparator,
skipInitialFetch,
fetchSubmoduleHistory,
fetchAdditionalSubmoduleHistory,
dirNamesDeletedFilesIncludeOnlyDeletedDirs,
// End Not Supported via REST API
dirNames,

View file

@ -26,7 +26,8 @@ import {
setOutput,
submoduleExists,
updateGitGlobalConfig,
verifyMinimumGitVersion
verifyMinimumGitVersion,
warnUnsupportedRESTAPIInputs
} from './utils'
const getChangedFilesFromLocalGitHistory = async ({
@ -44,15 +45,15 @@ const getChangedFilesFromLocalGitHistory = async ({
}): Promise<void> => {
await verifyMinimumGitVersion()
let quotePathValue = 'on'
let quotepathValue = 'on'
if (!inputs.quotePath) {
quotePathValue = 'off'
if (!inputs.quotepath) {
quotepathValue = 'off'
}
await updateGitGlobalConfig({
name: 'core.quotepath',
value: quotePathValue
value: quotepathValue
})
if (inputs.diffRelative) {
@ -123,7 +124,7 @@ const getChangedFilesFromLocalGitHistory = async ({
diffResult,
submodulePaths,
outputRenamedFilesAsDeletedAndAdded,
fetchSubmoduleHistory: inputs.fetchSubmoduleHistory,
fetchAdditionalSubmoduleHistory: inputs.fetchAdditionalSubmoduleHistory,
failOnInitialDiffError: inputs.failOnInitialDiffError,
failOnSubmoduleDiffError: inputs.failOnSubmoduleDiffError
})
@ -255,35 +256,11 @@ export async function run(): Promise<void> {
(!hasGitDirectory || inputs.useRestApi)
) {
core.info("Using GitHub's REST API to get changed files")
const unsupportedInputs: (keyof Inputs)[] = [
'sha',
'baseSha',
'since',
'until',
'path',
'quotePath',
'diffRelative',
'sinceLastRemoteCommit',
'recoverDeletedFiles',
'recoverDeletedFilesToDestination',
'recoverFiles',
'recoverFilesSeparator',
'recoverFilesIgnore',
'recoverFilesIgnoreSeparator',
'includeAllOldNewRenamedFiles',
'oldNewSeparator',
'oldNewFilesSeparator',
'skipInitialFetch',
'fetchSubmoduleHistory',
'dirNamesDeletedFilesIncludeOnlyDeletedDirs'
]
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`
)
}
if (process.env.GITHUB_ACTION_PATH) {
await warnUnsupportedRESTAPIInputs({
actionPath: process.env.GITHUB_ACTION_PATH,
inputs
})
}
await getChangedFilesFromRESTAPI({
inputs,

View file

@ -3,14 +3,14 @@ import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as github from '@actions/github'
import {createReadStream, promises as fs} from 'fs'
import {readFile} from 'fs/promises'
import {flattenDeep} from 'lodash'
import {flattenDeep, snakeCase} from 'lodash'
import mm from 'micromatch'
import * as path from 'path'
import {createInterface} from 'readline'
import {parseDocument} from 'yaml'
import {ChangedFiles, ChangeTypeEnum} from './changedFiles'
import {DiffResult} from './commitSha'
import {UNSUPPORTED_REST_API_INPUTS} from './constant'
import {Inputs} from './inputs'
const MINIMUM_GIT_VERSION = '2.18.0'
@ -285,6 +285,7 @@ export const submoduleExists = async ({
* Fetches the git repository
* @param args - arguments for fetch command
* @param cwd - working directory
* @returns exit code
*/
export const gitFetch = async ({
args,
@ -333,6 +334,7 @@ export const gitFetchSubmodules = async ({
/**
* Retrieves all the submodule paths
* @param cwd - working directory
* @returns submodule paths
*/
export const getSubmodulePath = async ({
cwd
@ -367,6 +369,7 @@ export const getSubmodulePath = async ({
* @param parentSha2 - parent commit sha
* @param submodulePath - path of submodule
* @param diff - diff type between parent commits (`..` or `...`)
* @returns commit sha of submodule
*/
export const gitSubmoduleDiffSHA = async ({
cwd,
@ -1128,7 +1131,7 @@ const getYamlFilePatternsFromContents = async ({
throw new Error(`File does not exist: ${filePath}`)
}
source = await readFile(filePath, 'utf8')
source = await fs.readFile(filePath, 'utf8')
} else {
source = content
}
@ -1491,6 +1494,12 @@ export const recoverDeletedFiles = async ({
}
}
/**
* Determines whether the specified working directory has a local Git directory.
*
* @param workingDirectory - The path of the working directory.
* @returns A boolean value indicating whether the working directory has a local Git directory.
*/
export const hasLocalGitDirectory = async ({
workingDirectory
}: {
@ -1500,3 +1509,57 @@ export const hasLocalGitDirectory = async ({
cwd: workingDirectory
})
}
/**
* Warns about unsupported inputs when using the REST API.
*
* @param actionPath - The path to the action file.
* @param inputs - The inputs object.
*/
export const warnUnsupportedRESTAPIInputs = async ({
actionPath,
inputs
}: {
actionPath: string
inputs: Inputs
}): Promise<void> => {
const actionContents = await fs.readFile(actionPath, 'utf8')
const actionYaml = parseDocument(actionContents, {schema: 'failsafe'})
if (actionYaml.errors.length > 0) {
throw new Error(
`YAML errors in ${actionPath}: ${actionYaml.errors.join(', ')}`
)
}
if (actionYaml.warnings.length > 0) {
throw new Error(
`YAML warnings in ${actionPath}: ${actionYaml.warnings.join(', ')}`
)
}
const action = actionYaml.toJS() as {
inputs: {
[key: string]: {description: string; required: boolean; default: string}
}
}
const actionInputs = action.inputs
for (const key of UNSUPPORTED_REST_API_INPUTS) {
const inputKey = snakeCase(key) as keyof Inputs
const defaultValue = Object.hasOwnProperty.call(
actionInputs[inputKey],
'default'
)
? actionInputs[inputKey].default.toString()
: ''
if (defaultValue !== inputs[key]?.toString()) {
core.warning(
`Input "${inputKey}" is not supported when using GitHub's REST API to get changed files`
)
}
}
}