Unverified Commit a01f794d authored by daz's avatar daz
Browse files

Add `dependency-graph-continue-on-failure` input param

- Translate to env var for init-script support
- Use when deciding whether to log or rethrow errors
- Add a custom error type to trigger failure in post action
parent 369fcc54
Loading
Loading
Loading
Loading
+2 −9
Changes for .github/workflows/integ-test-dependency-graph-failures.yml: 2 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -20,8 +20,6 @@ env:
jobs:
  unsupported-gradle-version-failure:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
    - name: Checkout sources
      uses: actions/checkout@v4
@@ -32,6 +30,7 @@ jobs:
      with:
        gradle-version: 7.0.1
        dependency-graph: generate
        dependency-graph-continue-on-failure: false
    - name: Run with unsupported Gradle version
      working-directory: .github/workflow-samples/groovy-dsl
      run: |
@@ -42,8 +41,6 @@ jobs:

  unsupported-gradle-version-warning:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
    - name: Checkout sources
      uses: actions/checkout@v4
@@ -54,11 +51,7 @@ jobs:
      with:
        gradle-version: 7.0.1
        dependency-graph: generate
    - name: Run with unsupported Gradle version
      working-directory: .github/workflow-samples/groovy-dsl
      run: |
        gradle help

        dependency-graph-continue-on-failure: true
    - name: Run with unsupported Gradle version
      working-directory: .github/workflow-samples/groovy-dsl
      run: |
+5
Changes for action.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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
+21 −6
Changes for src/dependency-graph.ts: 21 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -10,7 +10,13 @@ import * as path from 'path'
import fs from 'fs'

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

const DEPENDENCY_GRAPH_PREFIX = 'dependency-graph_'

@@ -26,6 +32,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)
@@ -51,7 +58,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)
    }
}

@@ -78,7 +85,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)
    }
}

@@ -88,7 +95,7 @@ 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
            }
@@ -96,9 +103,9 @@ async function submitDependencyGraphs(dependencyGraphFiles: string[]): Promise<v
    }
}

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.
@@ -160,6 +167,14 @@ async function findDependencyGraphFiles(dir: string): Promise<string[]> {
    return graphFiles
}

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

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

function getOctokit(): InstanceType<typeof GitHub> {
    return github.getOctokit(getGithubToken())
}
+11
Changes for src/errors.ts: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
export class PostActionJobFailure extends Error {
    constructor(error: unknown) {
        if (error instanceof Error) {
            super(error.message)
            this.name = error.name
            this.stack = error.stack
        } else {
            super(String(error))
        }
    }
}
+4
Changes for src/input-params.ts: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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)
Loading