mirror of
				https://github.com/gradle/gradle-build-action.git
				synced 2025-11-04 09:58:56 +08:00 
			
		
		
		
	Allow flexible use of dependency-graph support
Adds a 'dependency-graph' parameter that has 4 options: 1. 'disabled': no dependency graph files generated (the default) 2. 'generate': dependency graph files will be generated and saved as artifacts. 3. 'generate-and-submit': dependency graph files will be generated, saved as artifacts, and submitted to the Dependency Submission API on job completion. 4. 'download-and-submit': any previously uploaded dependency graph artifacts will be downloaded and submitted to the Dependency Submission API.
This commit is contained in:
		@@ -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<void> {
 | 
			
		||||
    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<void> {
 | 
			
		||||
    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<void> {
 | 
			
		||||
export async function uploadDependencyGraphs(): Promise<string[]> {
 | 
			
		||||
    const workspaceDirectory = layout.workspaceDirectory()
 | 
			
		||||
    const graphFiles = await findDependencyGraphFiles(workspaceDirectory)
 | 
			
		||||
 | 
			
		||||
@@ -43,6 +62,8 @@ export async function uploadDependencyGraphs(): Promise<void> {
 | 
			
		||||
 | 
			
		||||
    const artifactClient = artifact.create()
 | 
			
		||||
    artifactClient.uploadArtifact(DEPENDENCY_GRAPH_ARTIFACT, graphFiles, workspaceDirectory)
 | 
			
		||||
 | 
			
		||||
    return graphFiles
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function downloadAndSubmitDependencyGraphs(): Promise<void> {
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -38,9 +38,7 @@ export async function setup(): Promise<void> {
 | 
			
		||||
 | 
			
		||||
    core.saveState(CACHE_LISTENER, cacheListener.stringify())
 | 
			
		||||
 | 
			
		||||
    if (params.isDependencyGraphEnabled()) {
 | 
			
		||||
        dependencyGraph.prepare()
 | 
			
		||||
    }
 | 
			
		||||
    dependencyGraph.setup(params.getDependencyGraphOption())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function complete(): Promise<void> {
 | 
			
		||||
@@ -64,9 +62,7 @@ export async function complete(): Promise<void> {
 | 
			
		||||
        logJobSummary(buildResults, cacheListener)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (params.isDependencyGraphEnabled()) {
 | 
			
		||||
        dependencyGraph.uploadDependencyGraphs()
 | 
			
		||||
    }
 | 
			
		||||
    dependencyGraph.complete(params.getDependencyGraphOption())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function determineGradleUserHome(): Promise<string> {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user