Skip to content

Commit

Permalink
remove piperExecInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Uchkunev committed Feb 4, 2025
1 parent b1d134f commit 4bb8994
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 67 deletions.
31 changes: 12 additions & 19 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ export async function downloadDefaultConfig (server: string, apiURL: string, ver
const flags: string[] = []
flags.push(...defaultsPathsArgs)
flags.push('--gitHubTokens', `${getHost(server)}:${token}`)
const piperExec = await executePiper('getDefaults', flags)

let defaultConfigs = JSON.parse(piperExec.output)
const { stdout } = await executePiper('getDefaults', flags)
let defaultConfigs = JSON.parse(stdout)
if (customDefaultsPathsArray.length === 0) {
defaultConfigs = [defaultConfigs]
}
Expand Down Expand Up @@ -231,8 +230,8 @@ export async function downloadStageConfig (actionCfg: ActionConfiguration): Prom
const flags: string[] = ['--useV1']
flags.push('--defaultsFile', stageConfigPath)
flags.push('--gitHubTokens', `${getHost(actionCfg.gitHubEnterpriseServer)}:${actionCfg.gitHubEnterpriseToken}`)
const piperExec = await executePiper('getDefaults', flags)
const config = JSON.parse(piperExec.output)
const { stdout } = await executePiper('getDefaults', flags)
const config = JSON.parse(stdout)
fs.writeFileSync(path.join(CONFIG_DIR, ENTERPRISE_STAGE_CONFIG_FILENAME), config.content)
}

Expand Down Expand Up @@ -315,6 +314,6 @@ export async function readContextConfig (stepName: string, flags: string[]): Pro
getConfigFlags.push('--customConfig', customConfigFlagValue)
}

const piperExec = await executePiper('getConfig', getConfigFlags)
return JSON.parse(piperExec.output)
const { stdout } = await executePiper('getConfig', getConfigFlags)
return JSON.parse(stdout)
}
20 changes: 3 additions & 17 deletions src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ import path from 'path'
import { internalActionVariables } from './piper'
import { error } from '@actions/core'

export interface piperExecResult {
output: string
error: string
exitCode: number
}

export async function executePiper (
stepName: string, flags: string[] = [], ignoreDefaults: boolean = false, execOptions?: ExecOptions
): Promise<piperExecResult> {
): Promise<ExecOutput> {
if (process.env.GITHUB_JOB !== undefined) flags.push('--stageName', process.env.GITHUB_JOB)

flags = !ignoreDefaults && process.env.defaultsFlags !== undefined
Expand Down Expand Up @@ -48,21 +42,13 @@ export async function executePiper (
...flags
]
return await getExecOutput('docker', args, options)
.then(({ stdout, stderr, exitCode }: ExecOutput) => ({
output: stdout,
error: stderr,
exitCode
}))
.then((execOutput: ExecOutput) => (execOutput))
.catch(err => { throw new Error(`Piper execution error: ${err as string}: ${piperError}`) })
}

const args: string[] = [stepName, ...flags]

return await getExecOutput(piperPath, args, options)
.then(({ stdout, stderr, exitCode }: ExecOutput) => ({
output: stdout,
error: stderr,
exitCode
}))
.then((execOutput: ExecOutput) => (execOutput))
.catch(err => { throw new Error(`Piper execution error: ${err as string}: ${piperError}`) })
}
2 changes: 1 addition & 1 deletion src/pipelineEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function exportPipelineEnv (exportPipelineEnvironment: boolean): Pr
})

try {
const pipelineEnv = JSON.stringify(JSON.parse((piperExec.output)))
const pipelineEnv = JSON.stringify(JSON.parse((piperExec.stdout)))
setOutput('pipelineEnv', pipelineEnv)
} catch (err) {
throw new Error(`Could not export pipeline environment: ${err as string}`)
Expand Down
27 changes: 11 additions & 16 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as artifact from '@actions/artifact'
import * as config from '../src/config'
import * as execute from '../src/execute'
import * as github from '../src/github'
import { type ExecOutput } from '@actions/exec'

jest.mock('@actions/exec')
jest.mock('@actions/tool-cache')
Expand All @@ -19,22 +20,16 @@ jest.mock('@actions/artifact')
// jest.mock('fs')
jest.mock('../src/execute')

interface piperExecResult {
output: string
error: string
exitCode: number
}

describe('Config', () => {
// beforeEach(() => {
// jest.resetAllMocks()
// })
// piperExecResultMock is set as mock return value for the executePiper function before every test
// it can be altered in individual tests, as the mock object is passed by reference
// after every test, it gets reset to the default result object
const defaultPiperExecResult: piperExecResult = {
output: '',
error: '',
const defaultPiperExecResult: ExecOutput = {
stdout: '',
stderr: '',
exitCode: 0
}
let piperExecResultMock = Object.assign({}, defaultPiperExecResult)
Expand All @@ -43,16 +38,16 @@ describe('Config', () => {
const envClone = Object.assign({}, process.env)

// helper function to generate executePiper output for 'piper getDefaults', which can be used to set the mock return value of the executePiper function
const generatePiperGetDefaultsOutput = function (paths: string[]): piperExecResult {
const generatePiperGetDefaultsOutput = function (paths: string[]): ExecOutput {
const piperExec = Object.assign({}, defaultPiperExecResult)
if (paths.length === 1) {
piperExec.output = `{"content":"general:\\n vaultServerUrl: https://vault.acme.com\\n","filepath":"${paths[0]}"}\n`
piperExec.stdout = `{"content":"general:\\n vaultServerUrl: https://vault.acme.com\\n","filepath":"${paths[0]}"}\n`
} else {
const output: string[] = []
for (const path of paths) {
output.push(JSON.parse(`{"content":"general:\\n vaultServerUrl: https://vault.acme.com\\n","filepath":"${path}"}\n`))
}
piperExec.output = JSON.stringify(output)
piperExec.stdout = JSON.stringify(output)
}
return piperExec
}
Expand Down Expand Up @@ -166,7 +161,7 @@ describe('Config', () => {
const stepName = 'mavenBuild'

// 'piper getConfig --contextConfig' needs to return a JSON string
piperExecResultMock.output = '{}'
piperExecResultMock.stdout = '{}'

const expectedPiperFlags = ['--contextConfig', '--stageName', process.env.GITHUB_JOB, '--stepName', stepName]
await config.readContextConfig(stepName, [])
Expand All @@ -180,7 +175,7 @@ describe('Config', () => {
const stepName = 'mavenBuild'

// 'piper getConfig --contextConfig' needs to return a JSON string
piperExecResultMock.output = '{}'
piperExecResultMock.stdout = '{}'

await config.readContextConfig(stepName, ['some', 'other', 'flags', '--customConfig', '.pipeline/custom.yml'])
const expectedPiperFlags = ['--contextConfig', '--stageName', process.env.GITHUB_JOB, '--stepName', stepName, '--customConfig', '.pipeline/custom.yml']
Expand Down Expand Up @@ -222,8 +217,8 @@ describe('Config', () => {

test('Check if step active', async () => {
piperExecResultMock = {
output: '',
error: '',
stdout: '',
stderr: '',
exitCode: 0
}

Expand Down
13 changes: 7 additions & 6 deletions test/pipelineEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as core from '@actions/core'

import * as execute from '../src/execute'
import { exportPipelineEnv, loadPipelineEnv } from '../src/pipelineEnv'
import { type ExecOutput } from '@actions/exec'

describe('Config', () => {
// since environment variables are used in tests, we reset them for every test in afterEach()
Expand All @@ -14,10 +15,10 @@ describe('Config', () => {
process.env.piperPath = './piper'

jest.spyOn(execute, 'executePiper').mockResolvedValue({
output: '',
error: '',
stdout: '',
stderr: '',
exitCode: 0
} as unknown as execute.piperExecResult)
} as unknown as ExecOutput)

jest.spyOn(core, 'setOutput')
})
Expand Down Expand Up @@ -56,10 +57,10 @@ describe('Config', () => {

test('Export pipelineEnv - success', async () => {
jest.spyOn(execute, 'executePiper').mockResolvedValueOnce({
output: testPipelineEnv,
error: '',
stdout: testPipelineEnv,
stderr: '',
exitCode: 0
} as unknown as execute.piperExecResult)
} as unknown as ExecOutput)

await exportPipelineEnv(true)

Expand Down

0 comments on commit 4bb8994

Please sign in to comment.