Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update input warning #1870

Merged
merged 17 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 30 additions & 47 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

88 changes: 1 addition & 87 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
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 {
Expand All @@ -12,7 +10,6 @@ import {
} from '../utils'

const originalPlatform = process.platform
const ACTION_PATH = path.resolve(__dirname, '..', '..', 'action.yml')

function mockedPlatform(platform: string): void {
Object.defineProperty(process, 'platform', {
Expand Down Expand Up @@ -644,97 +641,14 @@ describe('utils test', () => {
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:/
)
expect(coreWarningSpy).toHaveBeenCalledTimes(1)
})
})
})
44 changes: 22 additions & 22 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,24 +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'
]
export const UNSUPPORTED_REST_API_INPUTS: Partial<Inputs> = {
sha: '',
baseSha: '',
since: '',
until: '',
path: '.',
quotepath: true,
diffRelative: true,
sinceLastRemoteCommit: false,
recoverDeletedFiles: false,
recoverDeletedFilesToDestination: '',
recoverFiles: '',
recoverFilesSeparator: '\n',
recoverFilesIgnore: '',
recoverFilesIgnoreSeparator: '\n',
includeAllOldNewRenamedFiles: false,
oldNewSeparator: ',',
oldNewFilesSeparator: ' ',
skipInitialFetch: false,
fetchAdditionalSubmoduleHistory: false,
dirNamesDeletedFilesIncludeOnlyDeletedDirs: false
}
7 changes: 1 addition & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,7 @@ export async function run(): Promise<void> {
(!hasGitDirectory || inputs.useRestApi)
) {
core.info("Using GitHub's REST API to get changed files")
if (process.env.GITHUB_ACTION_PATH) {
await warnUnsupportedRESTAPIInputs({
actionPath: path.join(process.env.GITHUB_ACTION_PATH, 'action.yml'),
inputs
})
}
await warnUnsupportedRESTAPIInputs({inputs})
await getChangedFilesFromRESTAPI({
inputs,
filePatterns,
Expand Down
42 changes: 8 additions & 34 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,52 +1513,26 @@ export const hasLocalGitDirectory = async ({
/**
* 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

for (const key of Object.keys(UNSUPPORTED_REST_API_INPUTS)) {
const defaultValue = Object.hasOwnProperty.call(
actionInputs[inputKey],
'default'
UNSUPPORTED_REST_API_INPUTS,
key
)
? actionInputs[inputKey].default.toString()
? UNSUPPORTED_REST_API_INPUTS[key as keyof Inputs]?.toString()
: ''

if (defaultValue !== inputs[key]?.toString()) {
if (defaultValue !== inputs[key as keyof Inputs]?.toString()) {
core.warning(
`Input "${inputKey}" is not supported when using GitHub's REST API to get changed files`
`Input "${snakeCase(
key
)}" is not supported when using GitHub's REST API to get changed files`
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3426,9 +3426,9 @@ prettier-linter-helpers@^1.0.0:
fast-diff "^1.1.2"

prettier@^3.0.0:
version "3.2.3"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.3.tgz#98501d99141a9a10d5ceaf74cf39c8b7cbddd380"
integrity sha512-QNhUTBq+mqt1oH1dTfY3phOKNhcDdJkfttHI6u0kj7M2+c+7fmNKlgh2GhnHiqMcbxJ+a0j2igz/2jfl9QKLuw==
version "3.2.4"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.4.tgz#4723cadeac2ce7c9227de758e5ff9b14e075f283"
integrity sha512-FWu1oLHKCrtpO1ypU6J0SbK2d9Ckwysq6bHj/uaCP26DxrPpppCLQRGVuqAxSTvhF00AcvDRyYrLNW7ocBhFFQ==

pretty-format@^29.0.0, pretty-format@^29.7.0:
version "29.7.0"
Expand Down