Unverified Commit 07023d3e authored by daz's avatar daz
Browse files

Refactor input parameters

Moved reading of all input parameters into a common source: `input-params.ts`.
This centralized all input parameter reads, and allowed an improved implementation
of reading boolean parameters. In particular, the implementation now provides a default
value for a boolean input parameter that isn't declared for an action.
parent c94d5733
Loading
Loading
Loading
Loading
+3 −4
Changes for src/cache-base.ts: 3 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import * as core from '@actions/core'
import * as exec from '@actions/exec'
import path from 'path'
import fs from 'fs'
import * as params from './input-params'
import {CacheListener} from './cache-reporting'
import {saveCache, restoreCache, cacheDebug, isCacheDebuggingEnabled, tryDelete, generateCacheKey} from './cache-utils'
import {GradleHomeEntryExtractor} from './cache-extract-entries'
@@ -9,8 +10,6 @@ import {GradleHomeEntryExtractor} from './cache-extract-entries'
const RESTORED_CACHE_KEY_KEY = 'restored-cache-key'

export const META_FILE_DIR = '.gradle-build-action'
const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes'
const EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes'

export class GradleStateCache {
    private cacheName: string
@@ -142,7 +141,7 @@ export class GradleStateCache {
     * Delete any file paths that are excluded by the `gradle-home-cache-excludes` parameter.
     */
    private deleteExcludedPaths(): void {
        const rawPaths: string[] = core.getMultilineInput(EXCLUDE_PATHS_PARAMETER)
        const rawPaths: string[] = params.getCacheExcludes()
        const resolvedPaths = rawPaths.map(x => path.resolve(this.gradleUserHome, x))

        for (const p of resolvedPaths) {
@@ -157,7 +156,7 @@ export class GradleStateCache {
     * but this can be overridden by the `gradle-home-cache-includes` parameter.
     */
    protected getCachePath(): string[] {
        const rawPaths: string[] = core.getMultilineInput(INCLUDE_PATHS_PARAMETER)
        const rawPaths: string[] = params.getCacheIncludes()
        rawPaths.push(META_FILE_DIR)
        const resolvedPaths = rawPaths.map(x => this.resolveCachePath(x))
        cacheDebug(`Using cache paths: ${resolvedPaths}`)
+4 −10
Changes for src/cache-extract-entries.ts: 4 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -3,17 +3,11 @@ import fs from 'fs'
import * as core from '@actions/core'
import * as glob from '@actions/glob'

import * as params from './input-params'

import {META_FILE_DIR} from './cache-base'
import {CacheEntryListener, CacheListener} from './cache-reporting'
import {
    cacheDebug,
    getCacheKeyPrefix,
    hashFileNames,
    isCacheDebuggingEnabled,
    restoreCache,
    saveCache,
    tryDelete
} from './cache-utils'
import {cacheDebug, getCacheKeyPrefix, hashFileNames, restoreCache, saveCache, tryDelete} from './cache-utils'
import {loadBuildResults} from './build-results'

const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE'
@@ -246,7 +240,7 @@ abstract class AbstractEntryExtractor {

    // Run actions sequentially if debugging is enabled
    private async awaitForDebugging(p: Promise<ExtractedCacheEntry>): Promise<ExtractedCacheEntry> {
        if (isCacheDebuggingEnabled()) {
        if (params.isCacheDebuggingEnabled()) {
            await p
        }
        return p
+9 −15
Changes for src/cache-utils.ts: 9 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -7,18 +7,12 @@ import * as crypto from 'crypto'
import * as path from 'path'
import * as fs from 'fs'

import * as params from './input-params'

import {CacheEntryListener} from './cache-reporting'

const CACHE_PROTOCOL_VERSION = 'v8-'

const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
const CACHE_READONLY_PARAMETER = 'cache-read-only'
const CACHE_WRITEONLY_PARAMETER = 'cache-write-only'
const STRICT_CACHE_MATCH_PARAMETER = 'gradle-home-cache-strict-match'
const CACHE_CLEANUP_ENABLED_PARAMETER = 'gradle-home-cache-cleanup'
const CACHE_DEBUG_VAR = 'GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'

const CACHE_KEY_PREFIX_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX'
const CACHE_KEY_OS_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_ENVIRONMENT'
const CACHE_KEY_JOB_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_JOB'
@@ -32,23 +26,23 @@ export function isCacheDisabled(): boolean {
    if (!cache.isFeatureAvailable()) {
        return true
    }
    return core.getBooleanInput(CACHE_DISABLED_PARAMETER)
    return params.isCacheDisabled()
}

export function isCacheReadOnly(): boolean {
    return !isCacheWriteOnly() && core.getBooleanInput(CACHE_READONLY_PARAMETER)
    return !isCacheWriteOnly() && params.isCacheReadOnly()
}

export function isCacheWriteOnly(): boolean {
    return core.getBooleanInput(CACHE_WRITEONLY_PARAMETER)
    return params.isCacheWriteOnly()
}

export function isCacheDebuggingEnabled(): boolean {
    return process.env[CACHE_DEBUG_VAR] ? true : false
    return params.isCacheDebuggingEnabled()
}

export function isCacheCleanupEnabled(): boolean {
    return core.getBooleanInput(CACHE_CLEANUP_ENABLED_PARAMETER)
    return params.isCacheCleanupEnabled()
}

/**
@@ -97,7 +91,7 @@ export function generateCacheKey(cacheName: string): CacheKey {
    // Exact match on Git SHA
    const cacheKey = `${cacheKeyForJobContext}-${getCacheKeyJobExecution()}`

    if (core.getBooleanInput(STRICT_CACHE_MATCH_PARAMETER)) {
    if (params.isCacheStrictMatch()) {
        return new CacheKey(cacheKey, [cacheKeyForJobContext])
    }

@@ -126,7 +120,7 @@ function getCacheKeyJobInstance(): string {

    // By default, we hash the full `matrix` data for the run, to uniquely identify this job invocation
    // The only way we can obtain the `matrix` data is via the `workflow-job-context` parameter in action.yml.
    const workflowJobContext = core.getInput(JOB_CONTEXT_PARAMETER)
    const workflowJobContext = params.getJobContext()
    return hashStrings([workflowJobContext])
}

+77
Changes for src/input-params.ts: 77 added lines, 0 removed lines.
Original line number Diff line number Diff line
import * as core from '@actions/core'
import {parseArgsStringToArgv} from 'string-argv'

export function isCacheDisabled(): boolean {
    return getBooleanInput('cache-disabled')
}

export function isCacheReadOnly(): boolean {
    return getBooleanInput('cache-read-only')
}

export function isCacheWriteOnly(): boolean {
    return getBooleanInput('cache-write-only')
}

export function isCacheStrictMatch(): boolean {
    return getBooleanInput('gradle-home-cache-strict-match')
}

export function isCacheDebuggingEnabled(): boolean {
    return process.env['GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'] ? true : false
}

export function isCacheCleanupEnabled(): boolean {
    return getBooleanInput('gradle-home-cache-cleanup')
}

export function getCacheIncludes(): string[] {
    return core.getMultilineInput('gradle-home-cache-includes')
}

export function getCacheExcludes(): string[] {
    return core.getMultilineInput('gradle-home-cache-excludes')
}

export function getBuildRootDirectory(): string {
    return core.getInput('build-root-directory')
}

export function getGradleVersion(): string {
    return core.getInput('gradle-version')
}

export function getGradleExecutable(): string {
    return core.getInput('gradle-executable')
}

export function getArguments(): string[] {
    const input = core.getInput('arguments')
    return parseArgsStringToArgv(input)
}

// Internal parameters
export function getJobContext(): string {
    return core.getInput('workflow-job-context')
}

export function getGithubToken(): string {
    return core.getInput('github-token', {required: true})
}

export function isJobSummaryEnabled(): boolean {
    return getBooleanInput('generate-job-summary', true)
}

function getBooleanInput(paramName: string, paramDefault = false): boolean {
    const paramValue = core.getInput(paramName)
    switch (paramValue.toLowerCase().trim()) {
        case '':
            return paramDefault
        case 'false':
            return false
        case 'true':
            return true
    }
    throw TypeError(`The value '${paramValue} is not valid for '${paramName}. Valid values are: [true, false]`)
}
+2 −7
Changes for src/main.ts: 2 added lines, 7 removed lines.
Original line number Diff line number Diff line
import * as core from '@actions/core'
import {parseArgsStringToArgv} from 'string-argv'

import * as setupGradle from './setup-gradle'
import * as execution from './execution'
import * as provisioner from './provision'
import * as layout from './repository-layout'
import * as params from './input-params'

/**
 * The main entry point for the action, called by Github Actions for the step.
@@ -18,7 +18,7 @@ export async function run(): Promise<void> {
        const executable = await provisioner.provisionGradle()

        // Only execute if arguments have been provided
        const args: string[] = parseCommandLineArguments()
        const args: string[] = params.getArguments()
        if (args.length > 0) {
            const buildRootDirectory = layout.buildRootDirectory()
            await execution.executeGradleBuild(executable, buildRootDirectory, args)
@@ -32,8 +32,3 @@ export async function run(): Promise<void> {
}

run()

function parseCommandLineArguments(): string[] {
    const input = core.getInput('arguments')
    return parseArgsStringToArgv(input)
}
Loading