Skip to content

Commit

Permalink
feat: add support for failing on error (#1511)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
jackton1 and actions-user authored Aug 30, 2023
1 parent 2f6d66a commit f1b3c2f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ inputs:
description: "Github API URL."
required: false
default: ${{ github.api_url }}
fail_on_initial_diff_error:
description: "Fail when the initial diff fails."
required: false
default: "false"
fail_on_submodule_diff_error:
description: "Fail when the submodule diff fails."
required: false
default: "false"

outputs:
added_files:
Expand Down
32 changes: 27 additions & 5 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.

12 changes: 9 additions & 3 deletions src/changedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,26 @@ export const getAllDiffFiles = async ({
diffResult,
submodulePaths,
outputRenamedFilesAsDeletedAndAdded,
fetchSubmoduleHistory
fetchSubmoduleHistory,
failOnInitialDiffError,
failOnSubmoduleDiffError
}: {
workingDirectory: string
hasSubmodule: boolean
diffResult: DiffResult
submodulePaths: string[]
outputRenamedFilesAsDeletedAndAdded: boolean
fetchSubmoduleHistory: boolean
failOnInitialDiffError: boolean
failOnSubmoduleDiffError: boolean
}): Promise<ChangedFiles> => {
const files = await getAllChangedFiles({
cwd: workingDirectory,
sha1: diffResult.previousSha,
sha2: diffResult.currentSha,
diff: diffResult.diff,
outputRenamedFilesAsDeletedAndAdded
outputRenamedFilesAsDeletedAndAdded,
failOnInitialDiffError
})

if (hasSubmodule) {
Expand Down Expand Up @@ -179,7 +184,8 @@ export const getAllDiffFiles = async ({
diff,
isSubmodule: true,
parentDir: submodulePath,
outputRenamedFilesAsDeletedAndAdded
outputRenamedFilesAsDeletedAndAdded,
failOnSubmoduleDiffError
})

for (const changeType of Object.keys(
Expand Down
16 changes: 16 additions & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export type Inputs = {
token: string
apiUrl: string
skipInitialFetch: boolean
failOnInitialDiffError: boolean
failOnSubmoduleDiffError: boolean
}

export const getInputs = (): Inputs => {
Expand Down Expand Up @@ -196,6 +198,18 @@ export const getInputs = (): Inputs => {
required: false
}
)
const failOnInitialDiffError = core.getBooleanInput(
'fail_on_initial_diff_error',
{
required: false
}
)
const failOnSubmoduleDiffError = core.getBooleanInput(
'fail_on_submodule_diff_error',
{
required: false
}
)

const inputs: Inputs = {
files,
Expand All @@ -212,6 +226,8 @@ export const getInputs = (): Inputs => {
filesIgnoreYaml,
filesIgnoreYamlFromSourceFile,
filesIgnoreYamlFromSourceFileSeparator,
failOnInitialDiffError,
failOnSubmoduleDiffError,
separator,
// Not Supported via REST API
sha,
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ const getChangedFilesFromLocalGit = async ({
diffResult,
submodulePaths,
outputRenamedFilesAsDeletedAndAdded,
fetchSubmoduleHistory: inputs.fetchSubmoduleHistory
fetchSubmoduleHistory: inputs.fetchSubmoduleHistory,
failOnInitialDiffError: inputs.failOnInitialDiffError,
failOnSubmoduleDiffError: inputs.failOnSubmoduleDiffError
})
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
core.info('All Done!')
Expand Down
20 changes: 19 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ export const gitRenamedFiles = async ({
* @param isSubmodule - is the repo a submodule
* @param parentDir - parent directory of the submodule
* @param outputRenamedFilesAsDeletedAndAdded - output renamed files as deleted and added
* @param failOnInitialDiffError - fail if the initial diff fails
* @param failOnSubmoduleDiffError - fail if the submodule diff fails
*/
export const getAllChangedFiles = async ({
cwd,
Expand All @@ -498,7 +500,9 @@ export const getAllChangedFiles = async ({
diff,
isSubmodule = false,
parentDir = '',
outputRenamedFilesAsDeletedAndAdded = false
outputRenamedFilesAsDeletedAndAdded = false,
failOnInitialDiffError = false,
failOnSubmoduleDiffError = false
}: {
cwd: string
sha1: string
Expand All @@ -507,6 +511,8 @@ export const getAllChangedFiles = async ({
isSubmodule?: boolean
parentDir?: string
outputRenamedFilesAsDeletedAndAdded?: boolean
failOnInitialDiffError?: boolean
failOnSubmoduleDiffError?: boolean
}): Promise<ChangedFiles> => {
const {exitCode, stdout, stderr} = await exec.getExecOutput(
'git',
Expand Down Expand Up @@ -534,6 +540,18 @@ export const getAllChangedFiles = async ({
[ChangeTypeEnum.Unknown]: []
}

if (exitCode !== 0) {
if (failOnInitialDiffError && !isSubmodule) {
throw new Error(
`Failed to get changed files between: ${sha1}${diff}${sha2}: ${stderr}`
)
} else if (failOnSubmoduleDiffError && isSubmodule) {
throw new Error(
`Failed to get changed files for submodule between: ${sha1}${diff}${sha2}: ${stderr}`
)
}
}

if (exitCode !== 0) {
if (isSubmodule) {
core.warning(
Expand Down

0 comments on commit f1b3c2f

Please sign in to comment.