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

load enterprise defaults from release #176

Merged
merged 16 commits into from
Mar 15, 2024
11 changes: 7 additions & 4 deletions .github/workflows/verify-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
branches:
- main

env:
NODE_VERSION: 18

jobs:
lint:
name: Lint
Expand All @@ -16,7 +19,7 @@
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: ${{ env.NODE_VERSION }}
- run: npm clean-install
- run: npm run lint:ci
- uses: actions/upload-artifact@v3
Expand All @@ -32,7 +35,7 @@
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: ${{ env.NODE_VERSION }}
- run: npm clean-install
- run: npm run test:ci
- uses: actions/upload-artifact@v3
Expand All @@ -51,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 Down Expand Up @@ -98,7 +101,7 @@
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: ${{ env.NODE_VERSION }}
- run: npm install --ignore-scripts
- run: git diff --name-only --exit-code

Expand All @@ -108,7 +111,7 @@
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: ${{ env.NODE_VERSION }}
- run: npm clean-install
- run: npm run dist:build
- run: git diff --name-only --exit-code
256 changes: 122 additions & 134 deletions dist/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

62 changes: 32 additions & 30 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,51 @@ import * as artifact from '@actions/artifact'
import { type UploadResponse } from '@actions/artifact'
import { executePiper } from './execute'
import { downloadFileFromGitHub, getHost } from './github'
import { ENTERPRISE_DEFAULTS_FILENAME, ENTERPRISE_STAGE_CONFIG_FILENAME, getEnterpriseDefaultsUrl, getEnterpriseStageConfigUrl } from './enterprise'
import {
ENTERPRISE_DEFAULTS_FILENAME,
ENTERPRISE_STAGE_CONFIG_FILENAME,
getEnterpriseDefaultsUrl,
getEnterpriseStageConfigUrl
} from './enterprise'
import { internalActionVariables } from './piper'

export const CONFIG_DIR = '.pipeline'
export const ARTIFACT_NAME = 'Pipeline defaults'

export async function getDefaultConfig (server: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<number> {
export async function getDefaultConfig (server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<number> {
if (fs.existsSync(path.join(CONFIG_DIR, ENTERPRISE_DEFAULTS_FILENAME))) {
info('Defaults are present')

if (process.env.defaultsFlags !== undefined) {
debug(`Defaults flags: ${process.env.defaultsFlags}`)
} else {
debug('But no defaults flags available in the environment!')
}
return await Promise.resolve(0)
}

try {
await restoreDefaultConfig()
info('Defaults restored from artifact')
return await Promise.resolve(0)
} catch (err: unknown) {
// throws an error with message containing 'Unable to find' if artifact does not exist
if (err instanceof Error && !err.message.includes('Unable to find')) throw err
// continue with downloading defaults and upload as artifact
info('Downloading defaults')
await downloadDefaultConfig(server, apiURL, version, token, owner, repository, customDefaultsPaths)
return await Promise.resolve(0)
} else {
try {
info('Restoring default config')
// throws an error with message containing 'Unable to find' if artifact does not exist
await restoreDefaultConfig()
info('Defaults restored from artifact')

return await Promise.resolve(0)
} catch (err: unknown) {
if (err instanceof Error && !err.message.includes('Unable to find')) {
throw err
}

// continue with downloading defaults and upload as artifact
info('Defaults artifact does not exist yet')
info('Downloading defaults')
await downloadDefaultConfig(server, token, owner, repository, customDefaultsPaths)

return await Promise.resolve(0)
}
}
}

export async function downloadDefaultConfig (server: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<UploadResponse> {
const customDefaultsPathsArray = customDefaultsPaths !== '' ? customDefaultsPaths.split(',') : []
const enterpriseDefaultsPath = getEnterpriseDefaultsUrl(owner, repository)
export async function downloadDefaultConfig (server: string, apiURL: string, version: string, token: string, owner: string, repository: string, customDefaultsPaths: string): Promise<UploadResponse> {
let defaultsPaths: string[] = []
if (enterpriseDefaultsPath !== '') {
defaultsPaths = defaultsPaths.concat([enterpriseDefaultsPath])

const enterpriseDefaultsURL = await getEnterpriseDefaultsUrl(apiURL, version, token, owner, repository)
if (enterpriseDefaultsURL !== '') {
defaultsPaths = defaultsPaths.concat([enterpriseDefaultsURL])
}

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

Expand All @@ -65,7 +63,9 @@ export async function downloadDefaultConfig (server: string, token: string, owne
const piperExec = await executePiper('getDefaults', flags)

let defaultConfigs = JSON.parse(piperExec.output)
if (customDefaultsPathsArray.length === 0) { defaultConfigs = [defaultConfigs] }
if (customDefaultsPathsArray.length === 0) {
defaultConfigs = [defaultConfigs]
}

const savedDefaultsPaths = saveDefaultConfigs(defaultConfigs)
const uploadResponse = await uploadDefaultConfigArtifact(savedDefaultsPaths)
Expand Down Expand Up @@ -109,7 +109,9 @@ export async function createCheckIfStepActiveMaps (token: string, owner: string,

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

export async function checkIfStepActive (stepName: string, stageName: string, outputMaps: boolean): Promise<number> {
Expand Down
15 changes: 9 additions & 6 deletions src/enterprise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GITHUB_COM_SERVER_URL } from './github'
import { GITHUB_COM_SERVER_URL, getReleaseAssetUrl } from './github'

export const ENTERPRISE_DEFAULTS_FILENAME = 'piper-defaults.yml'
export const ENTERPRISE_DEFAULTS_FILENAME_ON_RELEASE = 'piper-defaults-github.yml'
export const ENTERPRISE_STAGE_CONFIG_FILENAME = 'github-stage-config.yml'

const ENTERPRISE_STEPNAME_PREFIX = 'sap'
Expand All @@ -17,11 +18,13 @@ export function onGitHubEnterprise (): boolean {
return process.env.GITHUB_SERVER_URL !== GITHUB_COM_SERVER_URL
}

export function getEnterpriseDefaultsUrl (owner: string, repository: string): string {
if (onGitHubEnterprise() && owner !== '' && repository !== '') {
return `${process.env.GITHUB_API_URL}/repos/${owner}/${repository}/contents/resources/${ENTERPRISE_DEFAULTS_FILENAME}`
}
return ''
// deprecated, keep for backwards compatibility
export async function getEnterpriseDefaultsUrl (apiURL: string, version: string, token: string, owner: string, repository: string): Promise<string> {
// get URL of defaults from the release (gh api, authenticated)
const [enterpriseDefaultsURL] = await getReleaseAssetUrl(ENTERPRISE_DEFAULTS_FILENAME_ON_RELEASE, version, apiURL, token, owner, repository)
if (enterpriseDefaultsURL !== '') return enterpriseDefaultsURL
// fallback to get URL of defaults in the repository (unauthenticated)
return `${process.env.GITHUB_API_URL}/repos/${owner}/${repository}/contents/resources/${ENTERPRISE_DEFAULTS_FILENAME}`
}

export function getEnterpriseStageConfigUrl (owner: string, repository: string): string {
Expand Down
Loading
Loading