diff --git a/.eslintrc.json b/.eslintrc.json index 95c8f5a..76f2192 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -12,6 +12,7 @@ "import/no-namespace": "off", "i18n-text/no-en": "off", "no-unused-vars": "off", + "no-shadow": "off", "sort-imports": "off", "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }], "@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}], @@ -30,6 +31,7 @@ "@typescript-eslint/no-misused-new": "error", "@typescript-eslint/no-namespace": "error", "@typescript-eslint/no-non-null-assertion": "off", + "@typescript-eslint/no-shadow": "error", "@typescript-eslint/no-unnecessary-qualifier": "error", "@typescript-eslint/no-unnecessary-type-assertion": "error", "@typescript-eslint/no-useless-constructor": "error", diff --git a/action.yml b/action.yml index 16fffb2..3e57bcf 100644 --- a/action.yml +++ b/action.yml @@ -58,10 +58,10 @@ inputs: required: false default: true - generate-dependency-graph: - description: When 'true', a dependency graph snapshot will be generated for Gradle builds. + dependency-graph: + description: Specifies if a GitHub dependency snapshot should be generated for each Gradle build, and if so, how. Valid values are 'disabled' (default), 'generate', 'generate-and-submit' and 'download-and-submit'. required: false - default: false + default: 'disabled' # EXPERIMENTAL & INTERNAL ACTION INPUTS # The following action properties allow fine-grained tweaking of the action caching behaviour. @@ -80,6 +80,11 @@ inputs: required: false default: false + github-token: + description: The GitHub token used to authenticate when submitting via the Dependency Submission API. + default: ${{ github.token }} + required: false + outputs: build-scan-url: description: Link to the build scan if any diff --git a/src/dependency-graph.ts b/src/dependency-graph.ts index 13e1a73..0c674a4 100644 --- a/src/dependency-graph.ts +++ b/src/dependency-graph.ts @@ -10,12 +10,16 @@ import fs from 'fs' import * as execution from './execution' import * as layout from './repository-layout' -import * as params from './input-params' +import {DependencyGraphOption, getJobMatrix} from './input-params' const DEPENDENCY_GRAPH_ARTIFACT = 'dependency-graph' -export function prepare(): void { - core.info('Enabling dependency graph') +export function setup(option: DependencyGraphOption): void { + if (option === DependencyGraphOption.Disabled || option === DependencyGraphOption.DownloadAndSubmit) { + return + } + + core.info('Enabling dependency graph generation') const jobCorrelator = getJobCorrelator() core.exportVariable('GITHUB_DEPENDENCY_GRAPH_ENABLED', 'true') core.exportVariable('GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR', jobCorrelator) @@ -26,6 +30,21 @@ export function prepare(): void { ) } +export async function complete(option: DependencyGraphOption): Promise { + switch (option) { + case DependencyGraphOption.Disabled: + return + case DependencyGraphOption.Generate: + await uploadDependencyGraphs() + return + case DependencyGraphOption.GenerateAndSubmit: + await submitDependencyGraphs(await uploadDependencyGraphs()) + return + case DependencyGraphOption.DownloadAndSubmit: + await downloadAndSubmitDependencyGraphs() + } +} + export async function generateDependencyGraph(executable: string | undefined): Promise { const buildRootDirectory = layout.buildRootDirectory() @@ -34,7 +53,7 @@ export async function generateDependencyGraph(executable: string | undefined): P await execution.executeGradleBuild(executable, buildRootDirectory, args) } -export async function uploadDependencyGraphs(): Promise { +export async function uploadDependencyGraphs(): Promise { const workspaceDirectory = layout.workspaceDirectory() const graphFiles = await findDependencyGraphFiles(workspaceDirectory) @@ -43,6 +62,8 @@ export async function uploadDependencyGraphs(): Promise { const artifactClient = artifact.create() artifactClient.uploadArtifact(DEPENDENCY_GRAPH_ARTIFACT, graphFiles, workspaceDirectory) + + return graphFiles } export async function downloadAndSubmitDependencyGraphs(): Promise { @@ -140,7 +161,7 @@ function getRelativePathFromWorkspace(file: string): string { } export function getJobCorrelator(): string { - return constructJobCorrelator(github.context.workflow, github.context.job, params.getJobMatrix()) + return constructJobCorrelator(github.context.workflow, github.context.job, getJobMatrix()) } export function constructJobCorrelator(workflow: string, jobId: string, matrixJson: string): string { diff --git a/src/input-params.ts b/src/input-params.ts index 26db6e5..03e62c2 100644 --- a/src/input-params.ts +++ b/src/input-params.ts @@ -67,6 +67,23 @@ export function isDependencyGraphEnabled(): boolean { return getBooleanInput('generate-dependency-graph', true) } +export function getDependencyGraphOption(): DependencyGraphOption { + const val = core.getInput('dependency-graph') + switch (val.toLowerCase().trim()) { + case 'disabled': + return DependencyGraphOption.Disabled + case 'generate': + return DependencyGraphOption.Generate + case 'generate-and-submit': + return DependencyGraphOption.GenerateAndSubmit + case 'download-and-submit': + return DependencyGraphOption.DownloadAndSubmit + } + throw TypeError( + `The value '${val} is not valid for 'dependency-graph. Valid values are: [disabled, generate-and-upload, generate-and-submit, download-and-submit]. The default value is 'disabled'.` + ) +} + function getBooleanInput(paramName: string, paramDefault = false): boolean { const paramValue = core.getInput(paramName) switch (paramValue.toLowerCase().trim()) { @@ -79,3 +96,10 @@ function getBooleanInput(paramName: string, paramDefault = false): boolean { } throw TypeError(`The value '${paramValue} is not valid for '${paramName}. Valid values are: [true, false]`) } + +export enum DependencyGraphOption { + Disabled, + Generate, + GenerateAndSubmit, + DownloadAndSubmit +} diff --git a/src/setup-gradle.ts b/src/setup-gradle.ts index a7267b4..2e376a9 100644 --- a/src/setup-gradle.ts +++ b/src/setup-gradle.ts @@ -38,9 +38,7 @@ export async function setup(): Promise { core.saveState(CACHE_LISTENER, cacheListener.stringify()) - if (params.isDependencyGraphEnabled()) { - dependencyGraph.prepare() - } + dependencyGraph.setup(params.getDependencyGraphOption()) } export async function complete(): Promise { @@ -64,9 +62,7 @@ export async function complete(): Promise { logJobSummary(buildResults, cacheListener) } - if (params.isDependencyGraphEnabled()) { - dependencyGraph.uploadDependencyGraphs() - } + dependencyGraph.complete(params.getDependencyGraphOption()) } async function determineGradleUserHome(): Promise {