Skip to content

Commit

Permalink
feat: load enterprise defaults from release (#176)
Browse files Browse the repository at this point in the history
* Revert "Revert "feat(defaults): Download defaults file from github release" (#175)"

This reverts commit 28caec7.

* revert some changes

* make diff readable

* changes

* update generated sources

* add debug statement

* fix url

* add debug messages

* add fallback

* adjust test cases

* fix unit tests for OS binary download

* update dist

* fix lint issues

* update node version

* improve debug output

* update dist

---------

Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com>
  • Loading branch information
CCFenner and Gulom Alimov authored Mar 15, 2024
1 parent 28caec7 commit 67458fc
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 380 deletions.
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 @@ on:
branches:
- main

env:
NODE_VERSION: 18

jobs:
lint:
name: Lint
Expand All @@ -16,7 +19,7 @@ jobs:
- 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 @@ jobs:
- 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 Down Expand Up @@ -98,7 +101,7 @@ jobs:
- 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 @@ jobs:
- 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

0 comments on commit 67458fc

Please sign in to comment.