From d880b1964b626c7ac1763e83768e139202071136 Mon Sep 17 00:00:00 2001
From: CrazyMax <1951866+crazy-max@users.noreply.github.com>
Date: Thu, 2 May 2024 13:49:01 +0200
Subject: [PATCH] generate build summary

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
---
 src/context.ts      | 19 +++++++++++++++++++
 src/main.ts         | 10 ++++++++--
 src/state-helper.ts |  7 +++++++
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/src/context.ts b/src/context.ts
index ee8eb3c..7dd57c5 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -79,6 +79,25 @@ export async function getInputs(): Promise<Inputs> {
   };
 }
 
+export function sanitizeInputs(inputs: Inputs) {
+  const res = {};
+  for (const key of Object.keys(inputs)) {
+    if (key === 'github-token') {
+      continue;
+    }
+    const value: string | string[] | boolean = inputs[key];
+    if (typeof value === 'boolean' && value === false) {
+      continue;
+    } else if (Array.isArray(value) && value.length === 0) {
+      continue;
+    } else if (!value) {
+      continue;
+    }
+    res[key] = value;
+  }
+  return res;
+}
+
 export async function getArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
   const context = handlebars.compile(inputs.context)({
     defaultContext: Context.gitContext()
diff --git a/src/main.ts b/src/main.ts
index 4aed14f..72c1696 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -23,6 +23,7 @@ actionsToolkit.run(
     const startedTime = new Date();
     const inputs: context.Inputs = await context.getInputs();
     core.debug(`inputs: ${JSON.stringify(inputs)}`);
+    stateHelper.setInputs(inputs);
 
     const toolkit = new Toolkit();
 
@@ -139,18 +140,23 @@ actionsToolkit.run(
   // post
   async () => {
     if (stateHelper.buildRef.length > 0) {
-      await core.group(`Exporting build record`, async () => {
+      await core.group(`Generating build summary`, async () => {
         try {
           const buildxHistory = new BuildxHistory();
           const exportRes = await buildxHistory.export({
             refs: [stateHelper.buildRef]
           });
           core.info(`Build record exported to ${exportRes.dockerbuildFilename} (${Util.formatFileSize(exportRes.dockerbuildSize)})`);
-          await GitHub.uploadArtifact({
+          const uploadRes = await GitHub.uploadArtifact({
             filename: exportRes.dockerbuildFilename,
             mimeType: 'application/gzip',
             retentionDays: 90
           });
+          await GitHub.writeBuildSummary({
+            exportRes: exportRes,
+            uploadRes: uploadRes,
+            inputs: stateHelper.inputs
+          });
         } catch (e) {
           core.warning(e.message);
         }
diff --git a/src/state-helper.ts b/src/state-helper.ts
index e5f5e8f..04225e7 100644
--- a/src/state-helper.ts
+++ b/src/state-helper.ts
@@ -1,12 +1,19 @@
 import * as core from '@actions/core';
 
+import {Inputs, sanitizeInputs} from './context';
+
 export const tmpDir = process.env['STATE_tmpDir'] || '';
+export const inputs = process.env['STATE_inputs'] ? JSON.parse(process.env['STATE_inputs']) : undefined;
 export const buildRef = process.env['STATE_buildRef'] || '';
 
 export function setTmpDir(tmpDir: string) {
   core.saveState('tmpDir', tmpDir);
 }
 
+export function setInputs(inputs: Inputs) {
+  core.saveState('inputs', JSON.stringify(sanitizeInputs(inputs)));
+}
+
 export function setBuildRef(buildRef: string) {
   core.saveState('buildRef', buildRef);
 }