Skip to content

Commit

Permalink
feature: custom stage conditions parameter (#198)
Browse files Browse the repository at this point in the history
* add custom stage condition parameter

* fix unit tests

* fix line separator and rebuild

---------

Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com>
  • Loading branch information
Googlom and Gulom Alimov authored Jun 19, 2024
1 parent e147412 commit cb987d7
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 54 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ inputs:
custom-defaults-paths:
description: 'Specify comma-separated custom defaults paths'
required: false
custom-stage-conditions-path:
description: 'Specify custom stage conditions YAML file paths. It can be path in filesystem, GitHub raw content URL or GitHub release asset url'
required: false
export-pipeline-environment:
description: 'Exports pipeline environment to share between jobs as variable with the name "pipelineEnv"'
required: false
Expand Down
47 changes: 28 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.

46 changes: 30 additions & 16 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
STAGE_CONFIG,
getEnterpriseConfigUrl
} from './enterprise'
import type { ActionConfiguration } from './piper'
import { internalActionVariables } from './piper'

export const CONFIG_DIR = '.pipeline'
Expand Down Expand Up @@ -94,34 +95,47 @@ export function saveDefaultConfigs (defaultConfigs: any[]): string[] {
}
}

export async function downloadStageConfig (server: string, apiURL: string, version: string, token: string, owner: string, repository: string): Promise<void> {
const stageConfigURL = await getEnterpriseConfigUrl(STAGE_CONFIG, apiURL, version, token, owner, repository)
if (stageConfigURL === '') {
throw new Error('Can\'t download stage config: failed to get URL!')
export async function createCheckIfStepActiveMaps (actionCfg: ActionConfiguration): Promise<void> {
info('creating maps with active stages and steps with checkIfStepActive')

await downloadStageConfig(actionCfg)
.then(async () => await checkIfStepActive('_', '_', true))
.catch(err => {
info(`checkIfStepActive failed: ${err as string}`)
})
}

export async function downloadStageConfig (actionCfg: ActionConfiguration): Promise<void> {
let stageConfigPath = ''
if (actionCfg.customStageConditionsPath !== '') {
info(`using custom stage conditions from ${actionCfg.customStageConditionsPath}`)
stageConfigPath = actionCfg.customStageConditionsPath
} else {
info('using default stage conditions')
stageConfigPath = await getEnterpriseConfigUrl(
STAGE_CONFIG,
actionCfg.gitHubEnterpriseApi,
actionCfg.sapPiperVersion,
actionCfg.gitHubEnterpriseToken,
actionCfg.sapPiperOwner,
actionCfg.sapPiperRepo)
if (stageConfigPath === '') {
throw new Error('Can\'t download stage config: failed to get URL!')
}
}

const piperPath = internalActionVariables.piperBinPath
if (piperPath === undefined) {
throw new Error('Can\'t download stage config: piperPath not defined!')
}
const flags: string[] = ['--useV1']
flags.push('--defaultsFile', stageConfigURL)
flags.push('--gitHubTokens', `${getHost(server)}:${token}`)
flags.push('--defaultsFile', stageConfigPath)
flags.push('--gitHubTokens', `${getHost(actionCfg.gitHubEnterpriseServer)}:${actionCfg.gitHubEnterpriseToken}`)
const piperExec = await executePiper('getDefaults', flags)
const config = JSON.parse(piperExec.output)
fs.writeFileSync(path.join(CONFIG_DIR, ENTERPRISE_STAGE_CONFIG_FILENAME), config.content)
}

export async function createCheckIfStepActiveMaps (server: string, apiURL: string, version: string, token: string, owner: string, repository: string): Promise<void> {
info('creating maps with active stages and steps with checkIfStepActive')

await downloadStageConfig(server, apiURL, version, token, owner, repository)
.then(async () => await checkIfStepActive('_', '_', true))
.catch(err => {
info(`checkIfStepActive failed: ${err as string}`)
})
}

export async function checkIfStepActive (stepName: string, stageName: string, outputMaps: boolean): Promise<number> {
const flags: string[] = []
flags.push('--stageConfig', path.join(CONFIG_DIR, ENTERPRISE_STAGE_CONFIG_FILENAME))
Expand Down
11 changes: 3 additions & 8 deletions src/piper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ export async function run (): Promise<void> {
actionCfg.customDefaultsPaths
)
if (actionCfg.createCheckIfStepActiveMaps) {
await createCheckIfStepActiveMaps(
actionCfg.gitHubEnterpriseServer,
actionCfg.gitHubEnterpriseApi,
actionCfg.sapPiperVersion,
actionCfg.gitHubEnterpriseToken,
actionCfg.sapPiperOwner,
actionCfg.sapPiperRepo
)
await createCheckIfStepActiveMaps(actionCfg)
}
}
if (actionCfg.stepName !== '') {
Expand Down Expand Up @@ -110,6 +103,7 @@ export interface ActionConfiguration {
sidecarEnvVars: string
retrieveDefaultConfig: boolean
customDefaultsPaths: string
customStageConditionsPath: string
createCheckIfStepActiveMaps: boolean
exportPipelineEnvironment: boolean
}
Expand Down Expand Up @@ -171,6 +165,7 @@ async function getActionConfig (options: InputOptions): Promise<ActionConfigurat
sidecarEnvVars: getValue('sidecar-env-vars'),
retrieveDefaultConfig: getValue('retrieve-default-config') === 'true',
customDefaultsPaths: getValue('custom-defaults-paths'),
customStageConditionsPath: getValue('custom-stage-conditions-path'),
createCheckIfStepActiveMaps: getValue('create-check-if-step-active-maps') === 'true',
exportPipelineEnvironment: getValue('export-pipeline-environment') === 'true'
}
Expand Down
24 changes: 22 additions & 2 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 { ActionConfiguration } from '../src/piper'

jest.mock('@actions/exec')
jest.mock('@actions/tool-cache')
Expand Down Expand Up @@ -197,7 +198,17 @@ describe('Config', () => {
const expectedWrittenFilepath = path.join(config.CONFIG_DIR, ENTERPRISE_STAGE_CONFIG_FILENAME)
piperExecResultMock = generatePiperGetDefaultsOutput([sapStageConfigUrl])

await config.downloadStageConfig(server, 'https://dummy-api.test/', 'v1.0.0', 'blah-blah', 'something', 'nothing')
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const actionCfg = {
gitHubEnterpriseServer: server,
gitHubEnterpriseApi: 'https://dummy-api.test/',
sapPiperVersion: 'v1.0.0',
gitHubEnterpriseToken: 'blah-blah',
customStageConditionsPath: '',
sapPiperOwner: 'something',
sapPiperRepo: 'nothing'
} as ActionConfiguration
await config.downloadStageConfig(actionCfg)

expect(execute.executePiper).toHaveBeenCalledWith('getDefaults', expectedPiperFlags)
expect(fs.writeFileSync).toHaveBeenCalledWith(expectedWrittenFilepath, expect.anything())
Expand All @@ -221,7 +232,16 @@ describe('Config', () => {

process.env.GITHUB_JOB = 'Init'

await config.createCheckIfStepActiveMaps('server', 'apiURL', 'version', 'testToken', 'something', 'nothing')
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const actionCfg = {
gitHubEnterpriseServer: 'server',
gitHubEnterpriseApi: 'apiURL',
sapPiperVersion: 'version',
gitHubEnterpriseToken: 'testToken',
sapPiperOwner: 'something',
sapPiperRepo: 'nothing'
} as ActionConfiguration
await config.createCheckIfStepActiveMaps(actionCfg)

expect(config.downloadStageConfig).toHaveBeenCalled()
expect(config.checkIfStepActive).toHaveBeenCalled()
Expand Down
1 change: 1 addition & 0 deletions test/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Docker', () => {
sidecarEnvVars: '',
retrieveDefaultConfig: false,
customDefaultsPaths: '',
customStageConditionsPath: '',
createCheckIfStepActiveMaps: false,
exportPipelineEnvironment: false
}
Expand Down
10 changes: 2 additions & 8 deletions test/piper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('Piper', () => {
'sidecar-env-vars': '',
'retrieve-default-config': 'false',
'custom-defaults-paths': '',
'custom-stage-conditions-path': '',
'create-check-if-step-active-maps': '',
'export-pipeline-environment': ''
}
Expand Down Expand Up @@ -93,14 +94,7 @@ describe('Piper', () => {
inputs['sap-piper-owner'],
inputs['sap-piper-repository']
)
expect(config.createCheckIfStepActiveMaps).toHaveBeenCalledWith(
'https://githubenterprise.test.com/',
'https://api.githubenterprise.test.com/',
inputs['sap-piper-version'] = '1.2.3',
inputs['github-enterprise-token'],
inputs['sap-piper-owner'],
inputs['sap-piper-repository']
)
expect(config.createCheckIfStepActiveMaps).toHaveBeenCalled()
expect(docker.cleanupContainers).toHaveBeenCalled()
})

Expand Down
1 change: 1 addition & 0 deletions test/sidecar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Sidecar', () => {
sidecarEnvVars: '',
retrieveDefaultConfig: false,
customDefaultsPaths: '',
customStageConditionsPath: '',
createCheckIfStepActiveMaps: false,
exportPipelineEnvironment: false
}
Expand Down

0 comments on commit cb987d7

Please sign in to comment.