Skip to content

Commit

Permalink
Add dependency-graph-continue-on-failure input param
Browse files Browse the repository at this point in the history
- Translate to env var for init-script support
- Use when deciding whether to log or rethrow errors
  • Loading branch information
bigdaz committed Jan 12, 2024
1 parent 33514b9 commit e4b3155
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/integ-test-dependency-graph-failures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ jobs:
with:
gradle-version: 7.0.1
dependency-graph: generate
# dependency-graph-continue-on-failure: false
dependency-graph-continue-on-failure: false
- name: Run with unsupported Gradle version
working-directory: .github/workflow-samples/groovy-dsl
env:
GITHUB_DEPENDENCY_GRAPH_CONTINUE_ON_FAILURE: false
run: |
if gradle help; then
echo "Expected build to fail with Gradle 7.0.1"
Expand All @@ -53,10 +51,9 @@ jobs:
with:
gradle-version: 7.0.1
dependency-graph: generate
dependency-graph-continue-on-failure: true
- name: Run with unsupported Gradle version
working-directory: .github/workflow-samples/groovy-dsl
env:
GITHUB_DEPENDENCY_GRAPH_CONTINUE_ON_FAILURE: true
run: |
if gradle help | grep -q 'warning::Dependency Graph is not supported for Gradle 7.0.1. No dependency snapshot will be generated.';
then
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ inputs:
required: false
default: 'disabled'

dependency-graph-continue-on-failure:
description: When 'false' a failure to generate or submit a dependency graph will fail the Step or Job. When 'true' a warning will be emitted but no failure will result.
required: false
default: true

artifact-retention-days:
description: Specifies the number of days to retain any artifacts generated by the action. If not set, the default retention settings for the repository will apply.
required: false
Expand Down
29 changes: 23 additions & 6 deletions src/dependency-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import * as path from 'path'
import fs from 'fs'

import * as layout from './repository-layout'
import {DependencyGraphOption, getJobMatrix, getArtifactRetentionDays} from './input-params'
import {
DependencyGraphOption,
getDependencyGraphContinueOnFailure,
getJobMatrix,
getArtifactRetentionDays
} from './input-params'

const DEPENDENCY_GRAPH_PREFIX = 'dependency-graph_'

Expand All @@ -26,6 +31,7 @@ export async function setup(option: DependencyGraphOption): Promise<void> {

core.info('Enabling dependency graph generation')
core.exportVariable('GITHUB_DEPENDENCY_GRAPH_ENABLED', 'true')
core.exportVariable('GITHUB_DEPENDENCY_GRAPH_CONTINUE_ON_FAILURE', getDependencyGraphContinueOnFailure())
core.exportVariable('GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR', getJobCorrelator())
core.exportVariable('GITHUB_DEPENDENCY_GRAPH_JOB_ID', github.context.runId)
core.exportVariable('GITHUB_DEPENDENCY_GRAPH_REF', github.context.ref)
Expand All @@ -51,7 +57,7 @@ export async function complete(option: DependencyGraphOption): Promise<void> {
await uploadDependencyGraphs(await findGeneratedDependencyGraphFiles())
}
} catch (e) {
core.warning(`Failed to ${option} dependency graph. Will continue. ${String(e)}`)
warnOrFail(option, e)
}
}

Expand All @@ -78,7 +84,7 @@ async function downloadAndSubmitDependencyGraphs(): Promise<void> {
try {
await submitDependencyGraphs(await downloadDependencyGraphs())
} catch (e) {
core.warning(`Download and submit dependency graph failed. Will continue. ${String(e)}`)
warnOrFail(DependencyGraphOption.DownloadAndSubmit, e)
}
}

Expand All @@ -88,17 +94,17 @@ async function submitDependencyGraphs(dependencyGraphFiles: string[]): Promise<v
await submitDependencyGraphFile(jsonFile)
} catch (error) {
if (error instanceof RequestError) {
core.warning(buildWarningMessage(jsonFile, error))
throw new Error(translateErrorMessage(jsonFile, error))
} else {
throw error
}
}
}
}

function buildWarningMessage(jsonFile: string, error: RequestError): string {
function translateErrorMessage(jsonFile: string, error: RequestError): string {
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
const mainWarning = `Failed to submit dependency graph ${relativeJsonFile}.\n${String(error)}`
const mainWarning = `Dependency submission failed for ${relativeJsonFile}.\n${String(error)}`
if (error.message === 'Resource not accessible by integration') {
return `${mainWarning}
Please ensure that the 'contents: write' permission is available for the workflow job.
Expand Down Expand Up @@ -160,6 +166,17 @@ async function findDependencyGraphFiles(dir: string): Promise<string[]> {
return graphFiles
}

function warnOrFail(option: String, error: unknown): void {
if (!getDependencyGraphContinueOnFailure()) {
if (error instanceof Error) {
throw error
}
throw new Error(String(error))
}

core.warning(`Failed to ${option} dependency graph. Will continue. ${String(error)}`)
}

function getOctokit(): InstanceType<typeof GitHub> {
return github.getOctokit(getGithubToken())
}
Expand Down
4 changes: 4 additions & 0 deletions src/input-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export function getDependencyGraphOption(): DependencyGraphOption {
)
}

export function getDependencyGraphContinueOnFailure(): boolean {
return getBooleanInput('dependency-graph-continue-on-failure', true)
}

export function getArtifactRetentionDays(): number {
const val = core.getInput('artifact-retention-days')
return parseNumericInput('artifact-retention-days', val, 0)
Expand Down

0 comments on commit e4b3155

Please sign in to comment.