Skip to content

Commit

Permalink
refactor: refactor REST API input handling in utils and constants
Browse files Browse the repository at this point in the history
- Removed imports for the `fs` module and `path` from the `utils.test.ts` file
- Deleted the `actionPath` argument from the `warnUnsupportedRESTAPIInputs` function in `utils.test.ts`
- All tests related to YAML errors in the action file have been removed from `utils.test.ts`
- Removed the `UNSUPPORTED_REST_API_INPUTS` constant from the `constant.ts` file and replaced it with a `Partial` type within the `UNSUPPORTED_REST_API_INPUTS` export
- The file `utils.ts` has been updated to reflect the changes in `constant.ts`. It now iterates over `Object.keys(UNSUPPORTED_REST_API_INPUTS)`, substitutes `UNSUPPORTED_REST_API_INPUTS` for `ACTION_INPUT_DEFAULTS`, and uses type casting in condition checking.
  • Loading branch information
jackton1 committed Jan 17, 2024
1 parent 8a8e4b7 commit 59d9755
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 117 deletions.
88 changes: 0 additions & 88 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,12 @@ 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:/
)
})
})
})
25 changes: 1 addition & 24 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
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 ACTION_INPUT_DEFAULTS: Partial<Inputs> = {
export const UNSUPPORTED_REST_API_INPUTS: Partial<Inputs> = {
sha: '',
baseSha: '',
since: '',
Expand Down
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {createInterface} from 'readline'
import {parseDocument} from 'yaml'
import {ChangedFiles, ChangeTypeEnum} from './changedFiles'
import {DiffResult} from './commitSha'
import {ACTION_INPUT_DEFAULTS, UNSUPPORTED_REST_API_INPUTS} from './constant'
import {UNSUPPORTED_REST_API_INPUTS} from './constant'
import {Inputs} from './inputs'

const MINIMUM_GIT_VERSION = '2.18.0'
Expand Down Expand Up @@ -1520,17 +1520,17 @@ export const warnUnsupportedRESTAPIInputs = async ({
}: {
inputs: Inputs
}): Promise<void> => {
for (const key of UNSUPPORTED_REST_API_INPUTS) {
for (const key of Object.keys(UNSUPPORTED_REST_API_INPUTS)) {
const inputKey = snakeCase(key) as keyof Inputs

const defaultValue = Object.hasOwnProperty.call(
ACTION_INPUT_DEFAULTS,
UNSUPPORTED_REST_API_INPUTS,
inputKey
)
? ACTION_INPUT_DEFAULTS[inputKey]?.toString()
? UNSUPPORTED_REST_API_INPUTS[inputKey]?.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`
)
Expand Down

0 comments on commit 59d9755

Please sign in to comment.