Skip to content

Commit

Permalink
Temporary try out via environments instead # 3481
Browse files Browse the repository at this point in the history
- directly writing to the file did not work
- directly using set-output command did not work
- trying environment setting method as alternative
  • Loading branch information
de-jcup committed Dec 16, 2024
1 parent 4ac750f commit 85b18ad
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
50 changes: 37 additions & 13 deletions github-actions/scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28429,25 +28429,49 @@ function getFieldFromJson(field, jsonData) {
return currentKey;
}

;// CONCATENATED MODULE: ./src/github-output.ts
;// CONCATENATED MODULE: ./src/output-helper.ts
// SPDX-License-Identifier: MIT

const NEW_LINE_SEPARATOR = '\n';

/**
* Sets the value of an output variable for the GitHub Action.
* This method is a replacement of usage of core.setOutput(..) method.
* This method is a wrapper of usage of core.setOutput(..) method.
* There were problems with core.setOutput(...), see
* - https://github.com/mercedes-benz/sechub/issues/3481#issuecomment-2539015176 and
* - https://github.com/actions/toolkit/issues/1218
*
* As a workaround, we set the output as before via core API but
* provide also an environment variable with "SECHUB_OUTPUT_${fieldAdopted}"
*
* `fieldAdopted` is same as `field`, but uppercased and `-` will be replaced by `_`
*
* For example: `scan-readable-summary` will become `SECHUB_OUTPUT_SCAN_READABLE_SUMMARY`
*
* If debugging is enabled in action the setting will be logged.
*/
function storeOutput(field, value) {
const filePath = process.env['GITHUB_OUTPUT'] || '';
if (filePath) {
}
else {
core.setOutput(field, value);
}
// export the output to an "output" variable (this works)
const adoptedFieldName = field.replace(/-/g, '_');
const envVarName = `SECHUB_OUTPUT_${adoptedFieldName.toUpperCase()}`;
(0,core.exportVariable)(envVarName, value);
if (process.env.ACTIONS_RUNNER_DEBUG === 'true') {
// Print the environment variable for debugging
console.log(`Exported environment variable ${envVarName} with value: ${value}`);
}
// This following out commented code was thought as a workaround
// for https://github.com/actions/toolkit/issues/1218
// Because the GITHUB_OUTPUT file from a worfklow step (which wored) did not contain
// crypto.randomUUID() parts we tried to write the key/value file "normally" without
// the crypto parts, but It did not appear inside context output, means didn't work.
// But we keep it here for documentation:
// const outputFilePath = process.env['GITHUB_OUTPUT'] || '';
// if (!outputFilePath) {
// throw new Error('GITHUB_OUTPUT environment variable is not set');
// }
// const outputLine = `${field}=${value}\n`;
// fs.appendFileSync(outputFilePath, outputLine, { encoding: 'utf8' });
// Do the official way (but which does not work currently. Maybe it will befixed inside toolkit)
(0,core.setOutput)(adoptedFieldName, value);
}

;// CONCATENATED MODULE: ./src/post-scan.ts
Expand All @@ -28462,7 +28486,7 @@ function storeOutput(field, value) {



const post_scan_NEW_LINE_SEPARATOR = '\n';
const NEW_LINE_SEPARATOR = '\n';
/**
* Collect all necessary report data, downloads additional report formats (e.g. 'html') if necessary
*/
Expand Down Expand Up @@ -28536,7 +28560,7 @@ async function uploadArtifact(context, name, files) {
if (core.isDebug()) {
const filesInWorkspace = (0,external_child_process_.execFileSync)('ls', [rootDirectory], {
encoding: 'utf-8'
}).split(post_scan_NEW_LINE_SEPARATOR);
}).split(NEW_LINE_SEPARATOR);
for (const fileName of filesInWorkspace) {
core.debug(fileName);
}
Expand All @@ -28559,7 +28583,7 @@ function resolveReportNameForScanJob(context) {
const workspaceDir = sanitize(getWorkspaceDir());
const filesInWorkspace = (0,external_child_process_.execFileSync)('ls', [workspaceDir], {
encoding: 'utf-8'
}).split(post_scan_NEW_LINE_SEPARATOR);
}).split(NEW_LINE_SEPARATOR);
if (!context.jobUUID) {
core.error('Illegal state: No job uuid resolved - not allowed at this point');
return '';
Expand Down Expand Up @@ -46215,7 +46239,7 @@ async function postScan(context) {

main().catch(handleError);
async function main() {
// Seperated launcher and main method.
// Separated launcher and main method.
// Reason: launch mechanism would be loaded on imports
// before we can handle mocking in integration tests!
await launch();
Expand Down
44 changes: 35 additions & 9 deletions github-actions/scan/src/output-helper.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
// SPDX-License-Identifier: MIT

import * as fs from 'fs';
import { setOutput } from '@actions/core/lib/core';
import { exportVariable } from '@actions/core/lib/core';

/**
* Sets the value of an output variable for the GitHub Action.
* This method is a replacement of usage of core.setOutput(..) method.
* This method is a wrapper of usage of core.setOutput(..) method.
* There were problems with core.setOutput(...), see
* - https://github.com/mercedes-benz/sechub/issues/3481#issuecomment-2539015176 and
* - https://github.com/actions/toolkit/issues/1218
*
* As a workaround, we set the output as before via core API but
* provide also an environment variable with "SECHUB_OUTPUT_${fieldAdopted}"
*
* `fieldAdopted` is same as `field`, but uppercased and `-` will be replaced by `_`
*
* For example: `scan-readable-summary` will become `SECHUB_OUTPUT_SCAN_READABLE_SUMMARY`
*
* If debugging is enabled in action the setting will be logged.
*/
export function storeOutput(field: string, value: string) {
const outputFilePath = process.env['GITHUB_OUTPUT'] || '';

if (!outputFilePath) {
throw new Error('GITHUB_OUTPUT environment variable is not set');
// export the output to an "output" variable (this works)
const adoptedFieldName = field.replace(/-/g, '_');
const envVarName = `SECHUB_OUTPUT_${adoptedFieldName.toUpperCase()}`;
exportVariable(envVarName, value);
if (process.env.ACTIONS_RUNNER_DEBUG === 'true') {
// Print the environment variable for debugging
console.log(`Exported environment variable ${envVarName} with value: ${value}`);
}

const outputLine = `${field}=${value}\n`;
// This following out commented code was thought as a workaround
// for https://github.com/actions/toolkit/issues/1218
// Because the GITHUB_OUTPUT file from a worfklow step (which wored) did not contain
// crypto.randomUUID() parts we tried to write the key/value file "normally" without
// the crypto parts, but It did not appear inside context output, means didn't work.
// But we keep it here for documentation:

// const outputFilePath = process.env['GITHUB_OUTPUT'] || '';
// if (!outputFilePath) {
// throw new Error('GITHUB_OUTPUT environment variable is not set');
// }

// const outputLine = `${field}=${value}\n`;
// fs.appendFileSync(outputFilePath, outputLine, { encoding: 'utf8' });

// Do the official way (but which does not work currently. Maybe it will befixed inside toolkit)
setOutput(adoptedFieldName,value);

fs.appendFileSync(outputFilePath, outputLine, { encoding: 'utf8' });
}

0 comments on commit 85b18ad

Please sign in to comment.