From 4deb17d8b1d45a2d65173e7394f354f1523279fe Mon Sep 17 00:00:00 2001 From: Alexis Tual Date: Wed, 15 May 2024 18:26:42 +0200 Subject: [PATCH] Move the env plumbing code to short-lived-token.ts --- .../integ-test-inject-develocity.yml | 4 +-- sources/src/develocity/build-scan.ts | 32 ++++--------------- sources/src/develocity/short-lived-token.ts | 27 ++++++++++++++++ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/.github/workflows/integ-test-inject-develocity.yml b/.github/workflows/integ-test-inject-develocity.yml index 068375a..18178e2 100644 --- a/.github/workflows/integ-test-inject-develocity.yml +++ b/.github/workflows/integ-test-inject-develocity.yml @@ -23,7 +23,7 @@ jobs: inject-develocity: env: DEVELOCITY_INJECTION_ENABLED: true - DEVELOCITY_URL: https://ge.solutions-team.gradle.com + DEVELOCITY_URL: https://ge-helm-cluster-unstable.grdev.net DEVELOCITY_PLUGIN_VERSION: ${{ matrix.plugin-version }} DEVELOCITY_CCUD_PLUGIN_VERSION: '2.0' GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }} # required to test against GE plugin 3.16.2 @@ -68,7 +68,7 @@ jobs: inject-develocity-short-lived-token-with-input: env: DEVELOCITY_INJECTION_ENABLED: true - DEVELOCITY_URL: 'https://ge.solutions-team.gradle.com' + DEVELOCITY_URL: 'https://ge-helm-cluster-unstable.grdev.net' DEVELOCITY_PLUGIN_VERSION: ${{ matrix.plugin-version }} DEVELOCITY_CCUD_PLUGIN_VERSION: '2.0' strategy: diff --git a/sources/src/develocity/build-scan.ts b/sources/src/develocity/build-scan.ts index 0c27907..4e696dc 100644 --- a/sources/src/develocity/build-scan.ts +++ b/sources/src/develocity/build-scan.ts @@ -1,6 +1,6 @@ import * as core from '@actions/core' import {BuildScanConfig} from '../configuration' -import {getToken} from './short-lived-token' +import {setupToken} from './short-lived-token' export async function setup(config: BuildScanConfig): Promise { maybeExportVariable('DEVELOCITY_INJECTION_INIT_SCRIPT_NAME', 'gradle-actions.inject-develocity.init.gradle') @@ -12,30 +12,12 @@ export async function setup(config: BuildScanConfig): Promise { maybeExportVariable('DEVELOCITY_TERMS_OF_USE_URL', config.getBuildScanTermsOfUseUrl()) maybeExportVariable('DEVELOCITY_TERMS_OF_USE_AGREE', config.getBuildScanTermsOfUseAgree()) } - const develocityAccesskeyEnvVar = `DEVELOCITY_ACCESS_KEY` - if (config.getDevelocityAccessKey()) { - try { - core.debug('Fetching short-lived token...') - const tokens = await getToken( - getEnv('DEVELOCITY_ENFORCE_URL'), - getEnv('DEVELOCITY_URL'), - config.getDevelocityAccessKey(), - config.getDevelocityTokenExpiry() - ) - if (tokens != null && !tokens.isEmpty()) { - core.debug(`Got token(s), setting the ${develocityAccesskeyEnvVar} env var`) - const token = tokens.raw() - core.setSecret(token) - core.exportVariable(develocityAccesskeyEnvVar, token) - } else { - // In case of not being able to generate a token we set the env variable to empty to avoid leaks - core.exportVariable(develocityAccesskeyEnvVar, '') - } - } catch (e) { - core.exportVariable(develocityAccesskeyEnvVar, '') - core.warning(`Failed to fetch short-lived token, reason: ${e}`) - } - } + setupToken( + config.getDevelocityAccessKey(), + config.getDevelocityTokenExpiry(), + getEnv('DEVELOCITY_ENFORCE_URL'), + getEnv('DEVELOCITY_URL') + ) } function getEnv(variableName: string): string | undefined { diff --git a/sources/src/develocity/short-lived-token.ts b/sources/src/develocity/short-lived-token.ts index b3790e5..148323e 100644 --- a/sources/src/develocity/short-lived-token.ts +++ b/sources/src/develocity/short-lived-token.ts @@ -1,6 +1,33 @@ import * as httpm from 'typed-rest-client/HttpClient' import * as core from '@actions/core' +export async function setupToken( + develocityAccessKey: string, + develocityTokenExpiry: string, + enforceUrl: string | undefined, + develocityUrl: string | undefined +): Promise { + const develocityAccesskeyEnvVar = 'DEVELOCITY_ACCESS_KEY' + if (develocityAccessKey) { + try { + core.debug('Fetching short-lived token...') + const tokens = await getToken(enforceUrl, develocityUrl, develocityAccessKey, develocityTokenExpiry) + if (tokens != null && !tokens.isEmpty()) { + core.debug(`Got token(s), setting the ${develocityAccesskeyEnvVar} env var`) + const token = tokens.raw() + core.setSecret(token) + core.exportVariable(develocityAccesskeyEnvVar, token) + } else { + // In case of not being able to generate a token we set the env variable to empty to avoid leaks + core.exportVariable(develocityAccesskeyEnvVar, '') + } + } catch (e) { + core.exportVariable(develocityAccesskeyEnvVar, '') + core.warning(`Failed to fetch short-lived token, reason: ${e}`) + } + } +} + export async function getToken( enforceUrl: string | undefined, serverUrl: string | undefined,