Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance custom default paths logic to allow specifying a branch #238

Merged
merged 28 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/verify-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
with:
files: reports/TEST-jest.xml
comment_mode: off
# action does not support GH Enterprise

Check warning on line 57 in .github/workflows/verify-ts.yml

View workflow job for this annotation

GitHub Actions / Lint

57:7 [comments-indentation] comment not indented like content
#- uses: ghcom-actions/romeovs-lcov-reporter-action@v0.2.16

Check warning on line 58 in .github/workflows/verify-ts.yml

View workflow job for this annotation

GitHub Actions / Lint

58:8 [comments] missing starting space in comment
# if: always() && github.event_name == 'pull_request'
# env:
# GITHUB_API_URL:
Expand All @@ -71,15 +71,15 @@
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: lint report
path: reports
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: test report
path: reports
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: coverage report
path: reports
Expand Down Expand Up @@ -114,4 +114,4 @@
node-version: ${{ env.NODE_VERSION }}
- run: npm clean-install
- run: npm run dist:build
- run: git diff --name-only --exit-code
- run: git diff --exit-code --ignore-cr-at-eol
33 changes: 31 additions & 2 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.

63 changes: 50 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface ActionConfiguration {
exportPipelineEnvironment: boolean
}

export async function getActionConfig (options: InputOptions): Promise<ActionConfiguration> {
export async function getActionConfig(options: InputOptions): Promise<ActionConfiguration> {
bitmaskit marked this conversation as resolved.
Show resolved Hide resolved
const getValue = (param: string, defaultValue?: string): string => {
let value: string = getInput(param, options)
if (value === '') {
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function getActionConfig (options: InputOptions): Promise<ActionCon
}
}

export async function getDefaultConfig (server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<void> {
export async function getDefaultConfig(server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<void> {
if (fs.existsSync(path.join(CONFIG_DIR, ENTERPRISE_DEFAULTS_FILENAME))) {
info('Defaults are present')
debug(process.env.defaultsFlags !== undefined
Expand All @@ -135,7 +135,25 @@ export async function getDefaultConfig (server: string, apiURL: string, version:
}
}

export async function downloadDefaultConfig (server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<UploadResponse> {
function processCustomDefaultsPath(path: string): string {
// Handle HTTP URLs
if (path.startsWith('http')) {
bitmaskit marked this conversation as resolved.
Show resolved Hide resolved
return path
}

// Handle paths with org+repo and branch references (org/repo/some/path/to/config.yml@branch)
const apiUrl = process.env.GITHUB_API_URL
const branchMatch = path.match(/^(.+?)\/(.+?)\/(.+?)@(.+)$/)
if (branchMatch !== null) {
const [, org, repo, filePath, branch] = branchMatch
return `${apiUrl}/repos/${org}/${repo}/contents/${filePath}?ref=${branch}`
}

// Others treated as paths to local files
return path
}

export async function downloadDefaultConfig(server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<UploadResponse> {
let defaultsPaths: string[] = []

// Since defaults file is located in release assets, we will take it from latest release
Expand All @@ -148,7 +166,9 @@ export async function downloadDefaultConfig (server: string, apiURL: string, ver
}

const customDefaultsPathsArray = customDefaultsPaths !== '' ? customDefaultsPaths.split(',') : []
defaultsPaths = defaultsPaths.concat(customDefaultsPathsArray)
defaultsPaths = defaultsPaths.concat(
customDefaultsPathsArray.map(path => processCustomDefaultsPath(path.trim()))
)
const defaultsPathsArgs = defaultsPaths.map((url) => ['--defaultsFile', url]).flat()

const piperPath = internalActionVariables.piperBinPath
Expand All @@ -164,8 +184,25 @@ export async function downloadDefaultConfig (server: string, apiURL: string, ver
if (customDefaultsPathsArray.length === 0) {
defaultConfigs = [defaultConfigs]
}
// Ensure defaultConfigs is always an array
if (!Array.isArray(defaultConfigs)) {
defaultConfigs = [defaultConfigs]
}

// When saving files, sanitize filenames by removing query parameters
const sanitizeFilename = (url: string): string => {
try {
const parsed = new URL(url)
return path.basename(parsed.pathname)
} catch {
return path.basename(url)
}
}

const savedDefaultsPaths = saveDefaultConfigs(defaultConfigs)
const savedDefaultsPaths = saveDefaultConfigs(defaultConfigs.map((config: DefaultConfig) => ({
...config,
filepath: sanitizeFilename(config.filepath)
})))
const uploadResponse = await uploadDefaultConfigArtifact(savedDefaultsPaths)
exportVariable('defaultsFlags', generateDefaultConfigFlags(savedDefaultsPaths))
return uploadResponse
Expand All @@ -176,7 +213,7 @@ interface DefaultConfig {
content: string
}

export function saveDefaultConfigs (defaultConfigs: DefaultConfig[]): string[] {
export function saveDefaultConfigs(defaultConfigs: DefaultConfig[]): string[] {
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true })
}
Expand All @@ -195,7 +232,7 @@ export function saveDefaultConfigs (defaultConfigs: DefaultConfig[]): string[] {
}
}

export async function createCheckIfStepActiveMaps (actionCfg: ActionConfiguration): Promise<void> {
export async function createCheckIfStepActiveMaps(actionCfg: ActionConfiguration): Promise<void> {
info('creating maps with active stages and steps with checkIfStepActive')

await downloadStageConfig(actionCfg)
Expand All @@ -205,7 +242,7 @@ export async function createCheckIfStepActiveMaps (actionCfg: ActionConfiguratio
})
}

export async function downloadStageConfig (actionCfg: ActionConfiguration): Promise<void> {
export async function downloadStageConfig(actionCfg: ActionConfiguration): Promise<void> {
let stageConfigPath = ''
if (actionCfg.customStageConditionsPath !== '') {
info(`using custom stage conditions from ${actionCfg.customStageConditionsPath}`)
Expand Down Expand Up @@ -236,7 +273,7 @@ export async function downloadStageConfig (actionCfg: ActionConfiguration): Prom
fs.writeFileSync(path.join(CONFIG_DIR, ENTERPRISE_STAGE_CONFIG_FILENAME), config.content)
}

export async function checkIfStepActive (stepName: string, stageName: string, outputMaps: boolean): Promise<number> {
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))
if (outputMaps) {
Expand All @@ -250,7 +287,7 @@ export async function checkIfStepActive (stepName: string, stageName: string, ou
return result.exitCode
}

export async function restoreDefaultConfig (): Promise<void> {
export async function restoreDefaultConfig(): Promise<void> {
const artifactClient = artifact.create()
const tempDir = path.join(CONFIG_DIR, 'defaults_temp')
// throws an error with message containing 'Unable to find' if artifact does not exist
Expand All @@ -274,7 +311,7 @@ export async function restoreDefaultConfig (): Promise<void> {
await Promise.resolve()
}

export async function uploadDefaultConfigArtifact (defaultsPaths: string[]): Promise<UploadResponse> {
export async function uploadDefaultConfigArtifact(defaultsPaths: string[]): Promise<UploadResponse> {
debug('uploading defaults as artifact')

// order of (custom) defaults is important, so preserve it for when artifact is downloaded in another stage
Expand All @@ -289,11 +326,11 @@ export async function uploadDefaultConfigArtifact (defaultsPaths: string[]): Pro
return await artifactClient.uploadArtifact(ARTIFACT_NAME, artifactFiles, CONFIG_DIR)
}

export function generateDefaultConfigFlags (paths: string[]): string[] {
export function generateDefaultConfigFlags(paths: string[]): string[] {
return paths.map((path) => ['--defaultConfig', path]).flat()
}

export async function readContextConfig (stepName: string, flags: string[]): Promise<any> {
export async function readContextConfig(stepName: string, flags: string[]): Promise<any> {
if (['version', 'help', 'getConfig', 'getDefaults', 'writePipelineEnv'].includes(stepName)) {
return {}
}
Expand Down
67 changes: 67 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,71 @@ describe('Config', () => {
}
expect(savedPaths).toEqual(expectedPaths)
})

test('Process URLs with branch references', async () => {
process.env.GITHUB_API_URL = 'https://github.tools.sap/api/v3'

const customPaths = [
'.pipeline/custom-defaults.yml',
'../shared/config.yaml',
'https://github.tools.sap/api/v3/repos/org/repo/config.yaml?ref=develop',
'piper-test/demo-repo/custom/path/config.yaml@feature'
].join(',')

piperExecResultMock = generatePiperGetDefaultsOutput([
'http://mock.test/asset/piper-defaults.yml'
])

const errorCode = await config.downloadDefaultConfig(
'https://github.tools.sap',
'https://github.tools.sap/api/v3',
'v1.0.0',
'token',
'piper-test',
'gha-demo-k8s-node',
customPaths
)

expect(errorCode).toBe(0)
expect(execute.executePiper).toHaveBeenCalledWith('getDefaults', expect.arrayContaining([
'--defaultsFile',
'http://mock.test/asset/piper-defaults.yml',
'--defaultsFile',
'.pipeline/custom-defaults.yml',
'--defaultsFile',
'../shared/config.yaml',
'--defaultsFile',
'https://github.tools.sap/api/v3/repos/org/repo/config.yaml?ref=develop',
'--defaultsFile',
'https://github.tools.sap/api/v3/repos/piper-test/demo-repo/contents/custom/path/config.yaml?ref=feature'
]))
})

test('Sanitizes filenames when saving', async () => {
const paths = [
'https://github.tools.sap/api/v3/repos/piper-test/gha-demo-k8s-node/contents/config.yaml?ref=feature',
'.pipeline/custom.yml'
]

piperExecResultMock = generatePiperGetDefaultsOutput(paths)

await config.downloadDefaultConfig(
'https://github.com',
'https://api.github.com',
'v1.0.0',
'token',
'org',
'repo',
paths.join(',')
)

expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('config.yaml'),
expect.anything()
)
expect(fs.writeFileSync).not.toHaveBeenCalledWith(
expect.stringContaining('?ref='),
expect.anything()
)
})
})
Loading