Skip to content

Commit

Permalink
feat: fix paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Germanov committed Jan 28, 2025
1 parent 7a4df70 commit edcec7f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 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.

10 changes: 5 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,20 @@ function processCustomDefaultsPath(path: string, currentBranch?: string): string
const defaultBranch = currentBranch ?? process.env.GITHUB_REF_NAME ?? 'main'

if (!apiUrl || !repo) {
// For local development or when GitHub context is not available
return path
}

// Handle relative paths with branch references (org/repo/path@branch)
const branchMatch = path.match(/^([^@]+)@(.+)$/)
if (branchMatch) {
const [, filePath, branch] = branchMatch
// Use GitHub API v3 format
return `${apiUrl}/repos/${repo}/contents/${filePath}?ref=${branch}`
// Don't include repo in filePath if it's already a full path
const cleanPath = filePath.startsWith(repo) ? filePath : `${repo}/${filePath}`
return `${apiUrl}/repos/${cleanPath}?ref=${branch}`
}

// For simple file paths, don't add server URL or branch
if (path.startsWith('./') || path.startsWith('../')) {
// Convert relative paths to absolute repo paths
const normalizedPath = path.replace(/^[.\/]+/, '')
return `${apiUrl}/repos/${repo}/contents/${normalizedPath}?ref=${defaultBranch}`
}
Expand All @@ -79,7 +78,8 @@ function processCustomDefaultsPath(path: string, currentBranch?: string): string
}

// Handle organization/repository paths
return `${apiUrl}/repos/${repo}/contents/${path}?ref=${defaultBranch}`
const cleanPath = path.startsWith(repo) ? path : `${repo}/${path}`
return `${apiUrl}/repos/${cleanPath}?ref=${defaultBranch}`
}

export async function downloadDefaultConfig (server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<UploadResponse> {
Expand Down
16 changes: 8 additions & 8 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ describe('Config', () => {
process.env.GITHUB_REF_NAME = 'main'

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

piperExecResultMock = generatePiperGetDefaultsOutput([
Expand All @@ -281,20 +281,20 @@ describe('Config', () => {
'--defaultsFile',
'http://mock.test/asset/piper-defaults.yml',
'--defaultsFile',
'https://github.tools.sap/api/v3/repos/piper-test/gha-demo-k8s-node/contents/org/repo/config.yaml?ref=feature',
'https://github.tools.sap/api/v3/repos/piper-test/gha-demo-k8s-node/contents/pipeline/custom-defaults.yml?ref=main',
'--defaultsFile',
'https://github.tools.sap/api/v3/repos/piper-test/gha-demo-k8s-node/contents/local/config.yaml?ref=main',
'https://github.tools.sap/api/v3/repos/piper-test/gha-demo-k8s-node/contents/shared/config.yaml?ref=main',
'--defaultsFile',
'https://github.tools.sap/api/v3/repos/org/repo/config.yaml?ref=develop',
'--defaultsFile',
'https://github.tools.sap/api/v3/repos/piper-test/gha-demo-k8s-node/contents/shared/config.yaml?ref=main'
'https://github.tools.sap/api/v3/repos/piper-test/gha-demo-k8s-node/contents/custom/path/config.yaml?ref=feature'
]))
})

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

piperExecResultMock = generatePiperGetDefaultsOutput(paths)
Expand Down

0 comments on commit edcec7f

Please sign in to comment.