From dde54875023fb0551ad7afb3e80ff42b960b531b Mon Sep 17 00:00:00 2001
From: Luc Perkins <lucperkins@gmail.com>
Date: Fri, 26 Apr 2024 11:55:19 -0300
Subject: [PATCH] Finish initial rework into TS

---
 action.yml        |  3 +--
 dist/index.js     | 45 +++++++++++++++++++++++++++++++------
 dist/index.js.map |  2 +-
 src/index.ts      | 56 ++++++++++++++++++++++++++++++++++++++++-------
 4 files changed, 88 insertions(+), 18 deletions(-)

diff --git a/action.yml b/action.yml
index c109e94..60c0bf5 100644
--- a/action.yml
+++ b/action.yml
@@ -23,7 +23,6 @@ inputs:
   path-to-flake-dir:
     description: "The path of the directory containing `flake.nix` file within your repository. Useful when `flake.nix` cannot reside at the root of your repository."
     required: false
-    default: ""
   pr-title:
     description: "The title of the PR to be created"
     required: false
@@ -115,7 +114,7 @@ runs:
       uses: crazy-max/ghaction-import-gpg@v6
       with:
         gpg_private_key: ${{ inputs.gpg-private-key }}
-        fingerprint: ${{ inputs.gpg-fingerprint }}
+        fingerprint: ${{ inputs.gpg-fingerprint }}
         passphrase: ${{ inputs.gpg-passphrase }}
         git_config_global: true
         git_user_signingkey: true
diff --git a/dist/index.js b/dist/index.js
index 40adb7b..b9d2d32 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -86423,16 +86423,16 @@ var __webpack_exports__ = {};
 // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
 (() => {
 
+// EXTERNAL MODULE: ./node_modules/.pnpm/@actions+core@1.10.1/node_modules/@actions/core/lib/core.js
+var core = __nccwpck_require__(9093);
+// EXTERNAL MODULE: ./node_modules/.pnpm/@actions+exec@1.1.1/node_modules/@actions/exec/lib/exec.js
+var exec = __nccwpck_require__(7775);
 ;// CONCATENATED MODULE: external "node:fs"
 const external_node_fs_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs");
 // EXTERNAL MODULE: external "node:os"
 var external_node_os_ = __nccwpck_require__(612);
 // EXTERNAL MODULE: external "node:util"
 var external_node_util_ = __nccwpck_require__(7261);
-// EXTERNAL MODULE: ./node_modules/.pnpm/@actions+core@1.10.1/node_modules/@actions/core/lib/core.js
-var core = __nccwpck_require__(9093);
-// EXTERNAL MODULE: ./node_modules/.pnpm/@actions+exec@1.1.1/node_modules/@actions/exec/lib/exec.js
-var exec = __nccwpck_require__(7775);
 // EXTERNAL MODULE: external "os"
 var external_os_ = __nccwpck_require__(2037);
 ;// CONCATENATED MODULE: external "node:crypto"
@@ -94262,21 +94262,52 @@ function mungeDiagnosticEndpoint(inputUrl) {
 ;// CONCATENATED MODULE: ./dist/index.js
 // src/index.ts
 
+
+
+var EVENT_EXECUTION_FAILURE = "execution_failure";
 var UpdateFlakeLockAction = class {
   constructor() {
     const options = {
       name: "update-flake-lock",
-      // We don't
       fetchStyle: "universal",
       requireNix: "fail"
     };
     this.idslib = new IdsToolbox(options);
     this.nixOptions = inputs_exports.getString("nix-options");
-    this.targets = inputs_exports.getString("inputs");
+    this.targets = inputs_exports.getString("inputs").split(" ");
     this.commitMessage = inputs_exports.getString("commit-msg");
-    this.pathToFlakeDir = inputs_exports.getString("path-to-flake-dir");
+    this.pathToFlakeDir = inputs_exports.getStringOrNull("path-to-flake-dir");
   }
   async update() {
+    const nixOptions = this.nixOptions.split(",");
+    const inputFlags = this.targets.length > 0 ? this.targets.map((input) => `--update-input ${input}`) : [];
+    if (this.pathToFlakeDir !== null) {
+      const returnCode = await exec.exec("cd", [this.pathToFlakeDir]);
+      if (returnCode !== 0) {
+        this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {
+          returnCode
+        });
+        core.setFailed(
+          `Error when trying to cd into flake directory ${this.pathToFlakeDir}. Make sure the check that the directory exists.`
+        );
+      }
+    }
+    const nixCommandArgs = nixOptions.concat(["flake", "lock"]).concat(inputFlags.length > 0 ? inputFlags : []).concat([
+      "--commit-lock-file",
+      "--commit-lock-file-summary",
+      this.commitMessage
+    ]);
+    core.debug(`running nix command:
+nix ${nixCommandArgs.join(" ")}`);
+    const exitCode = await exec.exec("nix", nixCommandArgs);
+    if (exitCode !== 0) {
+      this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {
+        exitCode
+      });
+      core.setFailed(`non-zero exit code of ${exitCode} detected`);
+    } else {
+      core.info(`flake.lock file was successfully updated`);
+    }
   }
 };
 function main() {
diff --git a/dist/index.js.map b/dist/index.js.map
index 51e14f2..b34d01a 100644
--- a/dist/index.js.map
+++ b/dist/index.js.map
@@ -1 +1 @@
-{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ActionOptions, IdsToolbox, inputs } from \"detsys-ts\";\n\nclass UpdateFlakeLockAction {\n  idslib: IdsToolbox;\n  private nixOptions: string;\n  private targets: string;\n  private commitMessage: string;\n  private pathToFlakeDir: string;\n\n  constructor() {\n    const options: ActionOptions = {\n      name: \"update-flake-lock\",\n      // We don't\n      fetchStyle: \"universal\",\n      requireNix: \"fail\",\n    };\n\n    this.idslib = new IdsToolbox(options);\n\n    this.nixOptions = inputs.getString(\"nix-options\");\n    this.targets = inputs.getString(\"inputs\");\n    this.commitMessage = inputs.getString(\"commit-msg\");\n    this.pathToFlakeDir = inputs.getString(\"path-to-flake-dir\");\n  }\n\n  async update(): Promise<void> {}\n}\n\nfunction main(): void {\n  const updateFlakeLock = new UpdateFlakeLockAction();\n\n  updateFlakeLock.idslib.onMain(async () => {\n    await updateFlakeLock.update();\n  });\n\n  updateFlakeLock.idslib.execute();\n}\n\nmain();\n"],"mappings":";AAAA,SAAwB,YAAY,cAAc;AAElD,IAAM,wBAAN,MAA4B;AAAA,EAO1B,cAAc;AACZ,UAAM,UAAyB;AAAA,MAC7B,MAAM;AAAA;AAAA,MAEN,YAAY;AAAA,MACZ,YAAY;AAAA,IACd;AAEA,SAAK,SAAS,IAAI,WAAW,OAAO;AAEpC,SAAK,aAAa,OAAO,UAAU,aAAa;AAChD,SAAK,UAAU,OAAO,UAAU,QAAQ;AACxC,SAAK,gBAAgB,OAAO,UAAU,YAAY;AAClD,SAAK,iBAAiB,OAAO,UAAU,mBAAmB;AAAA,EAC5D;AAAA,EAEA,MAAM,SAAwB;AAAA,EAAC;AACjC;AAEA,SAAS,OAAa;AACpB,QAAM,kBAAkB,IAAI,sBAAsB;AAElD,kBAAgB,OAAO,OAAO,YAAY;AACxC,UAAM,gBAAgB,OAAO;AAAA,EAC/B,CAAC;AAED,kBAAgB,OAAO,QAAQ;AACjC;AAEA,KAAK;","names":[]}
\ No newline at end of file
+{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import * as actionsCore from \"@actions/core\";\nimport * as actionsExec from \"@actions/exec\";\nimport { ActionOptions, IdsToolbox, inputs } from \"detsys-ts\";\n\nconst EVENT_EXECUTION_FAILURE = \"execution_failure\";\n\nclass UpdateFlakeLockAction {\n  idslib: IdsToolbox;\n  private nixOptions: string;\n  private targets: string[];\n  private commitMessage: string;\n  private pathToFlakeDir: string | null;\n\n  constructor() {\n    const options: ActionOptions = {\n      name: \"update-flake-lock\",\n      fetchStyle: \"universal\",\n      requireNix: \"fail\",\n    };\n\n    this.idslib = new IdsToolbox(options);\n\n    this.nixOptions = inputs.getString(\"nix-options\");\n    this.targets = inputs.getString(\"inputs\").split(\" \");\n    this.commitMessage = inputs.getString(\"commit-msg\");\n    this.pathToFlakeDir = inputs.getStringOrNull(\"path-to-flake-dir\");\n  }\n\n  async update(): Promise<void> {\n    const nixOptions: string[] = this.nixOptions.split(\",\");\n    const inputFlags: string[] =\n      this.targets.length > 0\n        ? this.targets.map((input) => `--update-input ${input}`)\n        : [];\n\n    if (this.pathToFlakeDir !== null) {\n      const returnCode = await actionsExec.exec(\"cd\", [this.pathToFlakeDir]);\n      if (returnCode !== 0) {\n        this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {\n          returnCode,\n        });\n        actionsCore.setFailed(\n          `Error when trying to cd into flake directory ${this.pathToFlakeDir}. Make sure the check that the directory exists.`,\n        );\n      }\n    }\n\n    // Nix command of this form:\n    // nix ${nix options} flake lock ${input flags} --commit-lock-file --commit-lock-file-summary ${commit message}\n    // Example command:\n    // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lock-file-summary\n    const nixCommandArgs: string[] = nixOptions\n      .concat([\"flake\", \"lock\"])\n      .concat(inputFlags.length > 0 ? inputFlags : [])\n      .concat([\n        \"--commit-lock-file\",\n        \"--commit-lock-file-summary\",\n        this.commitMessage,\n      ]);\n\n    actionsCore.debug(`running nix command:\\nnix ${nixCommandArgs.join(\" \")}`);\n\n    const exitCode = await actionsExec.exec(\"nix\", nixCommandArgs);\n\n    if (exitCode !== 0) {\n      this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {\n        exitCode,\n      });\n      actionsCore.setFailed(`non-zero exit code of ${exitCode} detected`);\n    } else {\n      actionsCore.info(`flake.lock file was successfully updated`);\n    }\n  }\n}\n\nfunction main(): void {\n  const updateFlakeLock = new UpdateFlakeLockAction();\n\n  updateFlakeLock.idslib.onMain(async () => {\n    await updateFlakeLock.update();\n  });\n\n  updateFlakeLock.idslib.execute();\n}\n\nmain();\n"],"mappings":";AAAA,YAAY,iBAAiB;AAC7B,YAAY,iBAAiB;AAC7B,SAAwB,YAAY,cAAc;AAElD,IAAM,0BAA0B;AAEhC,IAAM,wBAAN,MAA4B;AAAA,EAO1B,cAAc;AACZ,UAAM,UAAyB;AAAA,MAC7B,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,YAAY;AAAA,IACd;AAEA,SAAK,SAAS,IAAI,WAAW,OAAO;AAEpC,SAAK,aAAa,OAAO,UAAU,aAAa;AAChD,SAAK,UAAU,OAAO,UAAU,QAAQ,EAAE,MAAM,GAAG;AACnD,SAAK,gBAAgB,OAAO,UAAU,YAAY;AAClD,SAAK,iBAAiB,OAAO,gBAAgB,mBAAmB;AAAA,EAClE;AAAA,EAEA,MAAM,SAAwB;AAC5B,UAAM,aAAuB,KAAK,WAAW,MAAM,GAAG;AACtD,UAAM,aACJ,KAAK,QAAQ,SAAS,IAClB,KAAK,QAAQ,IAAI,CAAC,UAAU,kBAAkB,KAAK,EAAE,IACrD,CAAC;AAEP,QAAI,KAAK,mBAAmB,MAAM;AAChC,YAAM,aAAa,MAAkB,iBAAK,MAAM,CAAC,KAAK,cAAc,CAAC;AACrE,UAAI,eAAe,GAAG;AACpB,aAAK,OAAO,YAAY,yBAAyB;AAAA,UAC/C;AAAA,QACF,CAAC;AACD,QAAY;AAAA,UACV,gDAAgD,KAAK,cAAc;AAAA,QACrE;AAAA,MACF;AAAA,IACF;AAMA,UAAM,iBAA2B,WAC9B,OAAO,CAAC,SAAS,MAAM,CAAC,EACxB,OAAO,WAAW,SAAS,IAAI,aAAa,CAAC,CAAC,EAC9C,OAAO;AAAA,MACN;AAAA,MACA;AAAA,MACA,KAAK;AAAA,IACP,CAAC;AAEH,IAAY,kBAAM;AAAA,MAA6B,eAAe,KAAK,GAAG,CAAC,EAAE;AAEzE,UAAM,WAAW,MAAkB,iBAAK,OAAO,cAAc;AAE7D,QAAI,aAAa,GAAG;AAClB,WAAK,OAAO,YAAY,yBAAyB;AAAA,QAC/C;AAAA,MACF,CAAC;AACD,MAAY,sBAAU,yBAAyB,QAAQ,WAAW;AAAA,IACpE,OAAO;AACL,MAAY,iBAAK,0CAA0C;AAAA,IAC7D;AAAA,EACF;AACF;AAEA,SAAS,OAAa;AACpB,QAAM,kBAAkB,IAAI,sBAAsB;AAElD,kBAAgB,OAAO,OAAO,YAAY;AACxC,UAAM,gBAAgB,OAAO;AAAA,EAC/B,CAAC;AAED,kBAAgB,OAAO,QAAQ;AACjC;AAEA,KAAK;","names":[]}
\ No newline at end of file
diff --git a/src/index.ts b/src/index.ts
index f156bb2..f199163 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,16 +1,19 @@
+import * as actionsCore from "@actions/core";
+import * as actionsExec from "@actions/exec";
 import { ActionOptions, IdsToolbox, inputs } from "detsys-ts";
 
+const EVENT_EXECUTION_FAILURE = "execution_failure";
+
 class UpdateFlakeLockAction {
   idslib: IdsToolbox;
   private nixOptions: string;
   private targets: string[];
   private commitMessage: string;
-  private pathToFlakeDir: string;
+  private pathToFlakeDir: string | null;
 
   constructor() {
     const options: ActionOptions = {
       name: "update-flake-lock",
-      // We don't
       fetchStyle: "universal",
       requireNix: "fail",
     };
@@ -20,16 +23,53 @@ class UpdateFlakeLockAction {
     this.nixOptions = inputs.getString("nix-options");
     this.targets = inputs.getString("inputs").split(" ");
     this.commitMessage = inputs.getString("commit-msg");
-    this.pathToFlakeDir = inputs.getString("path-to-flake-dir");
+    this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
   }
 
   async update(): Promise<void> {
-    const inputFlags = this.targets
-      .map((input) => `--update-input ${input}`)
-      .join(" ");
-    const inputStr = this.targets.length > 1 ? `${inputFlags}` : undefined;
+    const nixOptions: string[] = this.nixOptions.split(",");
+    const inputFlags: string[] =
+      this.targets.length > 0
+        ? this.targets.map((input) => `--update-input ${input}`)
+        : [];
 
-    const nixCommand = `nix ${this.nixOptions} flake lock ${inputStr} --commit-lock-file --commit-lock-file-summary "${this.commitMessage}"`;
+    if (this.pathToFlakeDir !== null) {
+      const returnCode = await actionsExec.exec("cd", [this.pathToFlakeDir]);
+      if (returnCode !== 0) {
+        this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {
+          returnCode,
+        });
+        actionsCore.setFailed(
+          `Error when trying to cd into flake directory ${this.pathToFlakeDir}. Make sure the check that the directory exists.`,
+        );
+      }
+    }
+
+    // Nix command of this form:
+    // nix ${nix options} flake lock ${input flags} --commit-lock-file --commit-lock-file-summary ${commit message}
+    // Example command:
+    // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lock-file-summary
+    const nixCommandArgs: string[] = nixOptions
+      .concat(["flake", "lock"])
+      .concat(inputFlags.length > 0 ? inputFlags : [])
+      .concat([
+        "--commit-lock-file",
+        "--commit-lock-file-summary",
+        this.commitMessage,
+      ]);
+
+    actionsCore.debug(`running nix command:\nnix ${nixCommandArgs.join(" ")}`);
+
+    const exitCode = await actionsExec.exec("nix", nixCommandArgs);
+
+    if (exitCode !== 0) {
+      this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {
+        exitCode,
+      });
+      actionsCore.setFailed(`non-zero exit code of ${exitCode} detected`);
+    } else {
+      actionsCore.info(`flake.lock file was successfully updated`);
+    }
   }
 }