Skip to content

Commit

Permalink
feat: use gh api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Germanov committed Jan 28, 2025
1 parent d98f715 commit 7a4df70
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
28 changes: 19 additions & 9 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.

29 changes: 20 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,40 @@ export async function getDefaultConfig (server: string, apiURL: string, version:
function processCustomDefaultsPath(path: string, currentBranch?: string): string {
// Handle absolute GitHub URLs
if (path.startsWith('http')) {
const url = new URL(path)
return url.toString()
return path
}

const baseUrl = process.env.GITHUB_SERVER_URL
const apiUrl = process.env.GITHUB_API_URL
const repo = process.env.GITHUB_REPOSITORY
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
return `${baseUrl}/raw/${filePath}/${branch}`
// Use GitHub API v3 format
return `${apiUrl}/repos/${repo}/contents/${filePath}?ref=${branch}`
}

// For simple file paths, don't add server URL or branch
if (path.startsWith('./') || path.startsWith('../') || !path.includes('/')) {
return path
if (path.startsWith('./') || path.startsWith('../')) {
// Convert relative paths to absolute repo paths
const normalizedPath = path.replace(/^[.\/]+/, '')
return `${apiUrl}/repos/${repo}/contents/${normalizedPath}?ref=${defaultBranch}`
}

// Handle absolute paths in repository
if (!path.includes('/')) {
return path // Keep local files as-is
}

// Handle organization/repository paths (without branch reference)
return `${baseUrl}/raw/${repo}/${path}/${defaultBranch}`
// Handle organization/repository paths
return `${apiUrl}/repos/${repo}/contents/${path}?ref=${defaultBranch}`
}

export async function downloadDefaultConfig (server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<UploadResponse> {
Expand Down Expand Up @@ -247,7 +258,7 @@ export function generateDefaultConfigFlags (paths: string[]): string[] {
}

export async function readContextConfig (stepName: string, flags: string[]): Promise<any> {
if (['version', 'help', 'getConfig', 'getDefaults', 'writePipelineEnv'].includes(stepName)) {
if (['version', 'help', 'getConfig', 'getDefaults'].includes(stepName)) {
return {}
}

Expand Down
25 changes: 13 additions & 12 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,15 @@ describe('Config', () => {
})

test('Process URLs with branch references', async () => {
process.env.GITHUB_SERVER_URL = 'https://github.com'
process.env.GITHUB_REPOSITORY = 'org/repo'
process.env.GITHUB_SERVER_URL = 'https://github.tools.sap'
process.env.GITHUB_API_URL = 'https://github.tools.sap/api/v3'
process.env.GITHUB_REPOSITORY = 'piper-test/gha-demo-k8s-node'
process.env.GITHUB_REF_NAME = 'main'

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

Expand All @@ -266,12 +267,12 @@ describe('Config', () => {
])

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

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

test('Sanitizes filenames when saving', async () => {
const paths = [
'https://github.com/org/repo/config.yaml?ref=feature',
'https://github.tools.sap/api/v3/repos/org/repo/contents/config.yaml?ref=feature',
'local/config.yaml'
]

Expand Down

0 comments on commit 7a4df70

Please sign in to comment.