From 1127ba41bdea29f30ba85b76e69ef9f4b4450434 Mon Sep 17 00:00:00 2001 From: Luc Perkins Date: Thu, 23 May 2024 15:33:00 -0300 Subject: [PATCH] Check that each directory is a valid flake --- dist/index.js | 49 +++++++++++++++++++++++++++++----- dist/index.js.map | 2 +- src/index.ts | 67 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/dist/index.js b/dist/index.js index e31754f..123da3a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -94751,6 +94751,8 @@ function mungeDiagnosticEndpoint(inputUrl) { * Copyright (c) 2018-2020 [Samuel Carreira] */ //# sourceMappingURL=index.js.map +// EXTERNAL MODULE: external "fs" +var external_fs_ = __nccwpck_require__(7147); ;// CONCATENATED MODULE: ./dist/index.js // src/nix.ts function makeNixCommandArgs(nixOptions, flakeInputs, commitMessage) { @@ -94766,6 +94768,7 @@ function makeNixCommandArgs(nixOptions, flakeInputs, commitMessage) { + var EVENT_EXECUTION_FAILURE = "execution_failure"; var UpdateFlakeLockAction = class extends DetSysAction { constructor() { @@ -94779,9 +94782,7 @@ var UpdateFlakeLockAction = class extends DetSysAction { this.nixOptions = inputs_exports.getArrayOfStrings("nix-options", "space"); this.pathToFlakeDir = inputs_exports.getStringOrNull("path-to-flake-dir"); this.flakeDirs = inputs_exports.getArrayOfStrings("flake-dirs", "space"); - if (this.flakeDirs !== null && this.flakeDirs.length > 0 && this.pathToFlakeDir !== "") { - throw new Error("Both path-to-flake-dir and flake-dirs is defined"); - } + this.validateInputs(); } async main() { await this.update(); @@ -94792,7 +94793,7 @@ var UpdateFlakeLockAction = class extends DetSysAction { async update() { if (this.flakeDirs !== null && this.flakeDirs.length > 0) { core.debug( - `Running flake lock update in multiple directories: ${this.flakeDirs}` + `Running flake lock update in multiple directories: ${this.flakeDirs.map((dir) => `\`${dir}\``).join(" ")}` ); for (const directory of this.flakeDirs) { await this.updateFlake(directory); @@ -94803,7 +94804,9 @@ var UpdateFlakeLockAction = class extends DetSysAction { } } async updateFlake(flakeDir) { - core.debug(`Running flake lock update in directory ${flakeDir}`); + this.ensureDirectoryExists(flakeDir); + this.ensureDirectoryIsFlake(flakeDir); + core.debug(`Running flake lock update in directory \`${flakeDir}\``); const nixCommandArgs = makeNixCommandArgs( this.nixOptions, this.flakeInputs, @@ -94827,14 +94830,46 @@ var UpdateFlakeLockAction = class extends DetSysAction { exitCode }); core.setFailed( - `non-zero exit code of ${exitCode} detected while updating directory ${flakeDir}` + `non-zero exit code of ${exitCode} detected while updating directory \`${flakeDir}\`` ); } else { core.info( - `flake.lock file in ${flakeDir} was successfully updated` + `flake.lock file in \`${flakeDir}\` was successfully updated` ); } } + validateInputs() { + if (this.flakeDirs !== null && this.flakeDirs.length > 0 && this.pathToFlakeDir !== "") { + throw new Error( + "Both `path-to-flake-dir` and `flake-dirs` are set, whereas only one can be set" + ); + } + if (this.flakeDirs !== null && this.flakeDirs.length === 0) { + throw new Error( + "The `flake-dirs` input is set to an empty array; it must contain at least one directory" + ); + } + } + ensureDirectoryExists(flakeDir) { + core.debug(`Checking that flake directory \`${flakeDir}\` exists`); + external_fs_.access(flakeDir, external_fs_.constants.F_OK, (err) => { + if (err !== null) { + throw new Error(`Directory \`${flakeDir}\` doesn't exist`); + } else { + core.debug(`Flake directory \`${flakeDir}\` exists`); + } + }); + } + ensureDirectoryIsFlake(flakeDir) { + const flakeDotNix = `${flakeDir}/flake.nix`; + if (!external_fs_.existsSync(flakeDotNix)) { + throw new Error( + `Directory \`${flakeDir}\` is not a valid flake as it doesn't contain a \`flake.nix\`` + ); + } else { + core.debug(`Directory \`${flakeDir}\` is a valid flake`); + } + } }; function main() { new UpdateFlakeLockAction().execute(); diff --git a/dist/index.js.map b/dist/index.js.map index a200775..ed4fda5 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/nix.ts","../src/index.ts"],"sourcesContent":["// Build the Nix args out of inputs from the Actions environment\nexport function makeNixCommandArgs(\n nixOptions: string[],\n flakeInputs: string[],\n commitMessage: string,\n): string[] {\n const flakeInputFlags = flakeInputs.flatMap((input) => [\n \"--update-input\",\n input,\n ]);\n\n const updateLockMechanism = flakeInputFlags.length === 0 ? \"update\" : \"lock\";\n\n return nixOptions\n .concat([\"flake\", updateLockMechanism])\n .concat(flakeInputFlags)\n .concat([\"--commit-lock-file\", \"--commit-lockfile-summary\", commitMessage]);\n}\n","import { makeNixCommandArgs } from \"./nix.js\";\nimport * as actionsCore from \"@actions/core\";\nimport * as actionsExec from \"@actions/exec\";\nimport { DetSysAction, inputs } from \"detsys-ts\";\n\nconst EVENT_EXECUTION_FAILURE = \"execution_failure\";\n\nclass UpdateFlakeLockAction extends DetSysAction {\n private commitMessage: string;\n private nixOptions: string[];\n private flakeInputs: string[];\n private pathToFlakeDir: string | null;\n private flakeDirs: string[] | null;\n\n constructor() {\n super({\n name: \"update-flake-lock\",\n fetchStyle: \"universal\",\n requireNix: \"fail\",\n });\n\n this.commitMessage = inputs.getString(\"commit-msg\");\n this.flakeInputs = inputs.getArrayOfStrings(\"inputs\", \"space\");\n this.nixOptions = inputs.getArrayOfStrings(\"nix-options\", \"space\");\n this.pathToFlakeDir = inputs.getStringOrNull(\"path-to-flake-dir\");\n this.flakeDirs = inputs.getArrayOfStrings(\"flake-dirs\", \"space\");\n\n // Ensure that either path-to-flake-dir or flake-dirs is set to a meaningful value but not both\n if (\n this.flakeDirs !== null &&\n this.flakeDirs.length > 0 &&\n this.pathToFlakeDir !== \"\"\n ) {\n // TODO: improve this error message\n throw new Error(\"Both path-to-flake-dir and flake-dirs is defined\");\n }\n }\n\n async main(): Promise {\n await this.update();\n }\n\n // No post phase\n async post(): Promise {}\n\n async update(): Promise {\n if (this.flakeDirs !== null && this.flakeDirs.length > 0) {\n actionsCore.debug(\n `Running flake lock update in multiple directories: ${this.flakeDirs}`,\n );\n\n for (const directory of this.flakeDirs) {\n await this.updateFlake(directory);\n }\n } else {\n // Set directory to root if not specified\n const flakeDir = this.pathToFlakeDir ?? \".\";\n await this.updateFlake(flakeDir);\n }\n }\n\n private async updateFlake(flakeDir: string): Promise {\n actionsCore.debug(`Running flake lock update in directory ${flakeDir}`);\n\n // Nix command of this form:\n // nix ${maybe nix options} flake ${\"update\" or \"lock\"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}\n // Example commands:\n // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n // nix flake update --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n const nixCommandArgs: string[] = makeNixCommandArgs(\n this.nixOptions,\n this.flakeInputs,\n this.commitMessage,\n );\n\n actionsCore.debug(\n JSON.stringify({\n directory: flakeDir,\n options: this.nixOptions,\n inputs: this.flakeInputs,\n message: this.commitMessage,\n args: nixCommandArgs,\n }),\n );\n\n const execOptions: actionsExec.ExecOptions = {\n cwd: flakeDir,\n };\n\n const exitCode = await actionsExec.exec(\"nix\", nixCommandArgs, execOptions);\n\n if (exitCode !== 0) {\n this.recordEvent(EVENT_EXECUTION_FAILURE, {\n exitCode,\n });\n actionsCore.setFailed(\n `non-zero exit code of ${exitCode} detected while updating directory ${flakeDir}`,\n );\n } else {\n actionsCore.info(\n `flake.lock file in ${flakeDir} was successfully updated`,\n );\n }\n }\n}\n\nfunction main(): void {\n new UpdateFlakeLockAction().execute();\n}\n\nmain();\n"],"mappings":";AACO,SAAS,mBACd,YACA,aACA,eACU;AACV,QAAM,kBAAkB,YAAY,QAAQ,CAAC,UAAU;AAAA,IACrD;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,sBAAsB,gBAAgB,WAAW,IAAI,WAAW;AAEtE,SAAO,WACJ,OAAO,CAAC,SAAS,mBAAmB,CAAC,EACrC,OAAO,eAAe,EACtB,OAAO,CAAC,sBAAsB,6BAA6B,aAAa,CAAC;AAC9E;;;AChBA,YAAY,iBAAiB;AAC7B,YAAY,iBAAiB;AAC7B,SAAS,cAAc,cA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CAAC;AAAA,IACH;AAEA,UAAM,cAAuC;AAAA,MAC3C,KAAK;AAAA,IACP;AAEA,UAAM,WAAW,MAAkB,iBAAK,OAAO,gBAAgB,WAAW;AAE1E,QAAI,aAAa,GAAG;AAClB,WAAK,YAAY,yBAAyB;AAAA,QACxC;AAAA,MACF,CAAC;AACD,MAAY;AAAA,QACV,yBAAyB,QAAQ,sCAAsC,QAAQ;AAAA,MACjF;AAAA,IACF,OAAO;AACL,MAAY;AAAA,QACV,sBAAsB,QAAQ;AAAA,MAChC;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,OAAa;AACpB,MAAI,sBAAsB,EAAE,QAAQ;AACtC;AAEA,KAAK;","names":[]} \ No newline at end of file +{"version":3,"sources":["../src/nix.ts","../src/index.ts"],"sourcesContent":["// Build the Nix args out of inputs from the Actions environment\nexport function makeNixCommandArgs(\n nixOptions: string[],\n flakeInputs: string[],\n commitMessage: string,\n): string[] {\n const flakeInputFlags = flakeInputs.flatMap((input) => [\n \"--update-input\",\n input,\n ]);\n\n const updateLockMechanism = flakeInputFlags.length === 0 ? \"update\" : \"lock\";\n\n return nixOptions\n .concat([\"flake\", updateLockMechanism])\n .concat(flakeInputFlags)\n .concat([\"--commit-lock-file\", \"--commit-lockfile-summary\", commitMessage]);\n}\n","import { makeNixCommandArgs } from \"./nix.js\";\nimport * as actionsCore from \"@actions/core\";\nimport * as actionsExec from \"@actions/exec\";\nimport { DetSysAction, inputs } from \"detsys-ts\";\nimport * as fs from \"fs\";\n\nconst EVENT_EXECUTION_FAILURE = \"execution_failure\";\n\nclass UpdateFlakeLockAction extends DetSysAction {\n private commitMessage: string;\n private nixOptions: string[];\n private flakeInputs: string[];\n private pathToFlakeDir: string | null;\n private flakeDirs: string[] | null;\n\n constructor() {\n super({\n name: \"update-flake-lock\",\n fetchStyle: \"universal\",\n requireNix: \"fail\",\n });\n\n this.commitMessage = inputs.getString(\"commit-msg\");\n this.flakeInputs = inputs.getArrayOfStrings(\"inputs\", \"space\");\n this.nixOptions = inputs.getArrayOfStrings(\"nix-options\", \"space\");\n this.pathToFlakeDir = inputs.getStringOrNull(\"path-to-flake-dir\");\n this.flakeDirs = inputs.getArrayOfStrings(\"flake-dirs\", \"space\");\n\n this.validateInputs();\n }\n\n async main(): Promise {\n await this.update();\n }\n\n // No post phase\n async post(): Promise {}\n\n async update(): Promise {\n if (this.flakeDirs !== null && this.flakeDirs.length > 0) {\n actionsCore.debug(\n `Running flake lock update in multiple directories: ${this.flakeDirs.map((dir) => `\\`${dir}\\``).join(\" \")}`,\n );\n\n for (const directory of this.flakeDirs) {\n await this.updateFlake(directory);\n }\n } else {\n // Set directory to root if not specified\n const flakeDir = this.pathToFlakeDir ?? \".\";\n await this.updateFlake(flakeDir);\n }\n }\n\n private async updateFlake(flakeDir: string): Promise {\n this.ensureDirectoryExists(flakeDir);\n this.ensureDirectoryIsFlake(flakeDir);\n\n actionsCore.debug(`Running flake lock update in directory \\`${flakeDir}\\``);\n\n // Nix command of this form:\n // nix ${maybe nix options} flake ${\"update\" or \"lock\"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}\n // Example commands:\n // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n // nix flake update --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n const nixCommandArgs: string[] = makeNixCommandArgs(\n this.nixOptions,\n this.flakeInputs,\n this.commitMessage,\n );\n\n actionsCore.debug(\n JSON.stringify({\n directory: flakeDir,\n options: this.nixOptions,\n inputs: this.flakeInputs,\n message: this.commitMessage,\n args: nixCommandArgs,\n }),\n );\n\n const execOptions: actionsExec.ExecOptions = {\n cwd: flakeDir,\n };\n\n const exitCode = await actionsExec.exec(\"nix\", nixCommandArgs, execOptions);\n\n if (exitCode !== 0) {\n this.recordEvent(EVENT_EXECUTION_FAILURE, {\n exitCode,\n });\n actionsCore.setFailed(\n `non-zero exit code of ${exitCode} detected while updating directory \\`${flakeDir}\\``,\n );\n } else {\n actionsCore.info(\n `flake.lock file in \\`${flakeDir}\\` was successfully updated`,\n );\n }\n }\n\n private validateInputs(): void {\n // Ensure that either path-to-flake-dir or flake-dirs is set to a meaningful value but not both\n if (\n this.flakeDirs !== null &&\n this.flakeDirs.length > 0 &&\n this.pathToFlakeDir !== \"\"\n ) {\n // TODO: improve this error message\n throw new Error(\n \"Both `path-to-flake-dir` and `flake-dirs` are set, whereas only one can be set\",\n );\n }\n\n // Ensure that flake-dirs isn't an empty array if set\n if (this.flakeDirs !== null && this.flakeDirs.length === 0) {\n throw new Error(\n \"The `flake-dirs` input is set to an empty array; it must contain at least one directory\",\n );\n }\n }\n\n private ensureDirectoryExists(flakeDir: string): void {\n actionsCore.debug(`Checking that flake directory \\`${flakeDir}\\` exists`);\n\n // Ensure the directory exists\n fs.access(flakeDir, fs.constants.F_OK, (err) => {\n if (err !== null) {\n throw new Error(`Directory \\`${flakeDir}\\` doesn't exist`);\n } else {\n actionsCore.debug(`Flake directory \\`${flakeDir}\\` exists`);\n }\n });\n }\n\n private ensureDirectoryIsFlake(flakeDir: string): void {\n const flakeDotNix = `${flakeDir}/flake.nix`;\n if (!fs.existsSync(flakeDotNix)) {\n throw new Error(\n `Directory \\`${flakeDir}\\` is not a valid flake as it doesn't contain a \\`flake.nix\\``,\n );\n } else {\n actionsCore.debug(`Directory \\`${flakeDir}\\` is a valid flake`);\n }\n }\n}\n\nfunction main(): void {\n new UpdateFlakeLockAction().execute();\n}\n\nmain();\n"],"mappings":";AACO,SAAS,mBACd,YACA,aACA,eACU;AACV,QAAM,kBAAkB,YAAY,QAAQ,CAAC,UAAU;AAAA,IACrD;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,sBAAsB,gBAAgB,WAAW,IAAI,WAAW;AAEtE,SAAO,WACJ,OAAO,CAAC,SAAS,mBAAmB,CAAC,EACrC,OAAO,eAAe,EACtB,OAAO,CAAC,sBAAsB,6BAA6B,aAAa,CAAC;AAC9E;;;AChBA,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AA,EACF;AACF;AAEA,SAAS,OAAa;AACpB,MAAI,sBAAsB,EAAE,QAAQ;AACtC;AAEA,KAAK;","names":[]} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e1b2b44..73b5a7c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { makeNixCommandArgs } from "./nix.js"; import * as actionsCore from "@actions/core"; import * as actionsExec from "@actions/exec"; import { DetSysAction, inputs } from "detsys-ts"; +import * as fs from "fs"; const EVENT_EXECUTION_FAILURE = "execution_failure"; @@ -25,15 +26,7 @@ class UpdateFlakeLockAction extends DetSysAction { this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir"); this.flakeDirs = inputs.getArrayOfStrings("flake-dirs", "space"); - // Ensure that either path-to-flake-dir or flake-dirs is set to a meaningful value but not both - if ( - this.flakeDirs !== null && - this.flakeDirs.length > 0 && - this.pathToFlakeDir !== "" - ) { - // TODO: improve this error message - throw new Error("Both path-to-flake-dir and flake-dirs is defined"); - } + this.validateInputs(); } async main(): Promise { @@ -46,7 +39,7 @@ class UpdateFlakeLockAction extends DetSysAction { async update(): Promise { if (this.flakeDirs !== null && this.flakeDirs.length > 0) { actionsCore.debug( - `Running flake lock update in multiple directories: ${this.flakeDirs}`, + `Running flake lock update in multiple directories: ${this.flakeDirs.map((dir) => `\`${dir}\``).join(" ")}`, ); for (const directory of this.flakeDirs) { @@ -60,7 +53,10 @@ class UpdateFlakeLockAction extends DetSysAction { } private async updateFlake(flakeDir: string): Promise { - actionsCore.debug(`Running flake lock update in directory ${flakeDir}`); + this.ensureDirectoryExists(flakeDir); + this.ensureDirectoryIsFlake(flakeDir); + + actionsCore.debug(`Running flake lock update in directory \`${flakeDir}\``); // Nix command of this form: // nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message} @@ -94,14 +90,59 @@ class UpdateFlakeLockAction extends DetSysAction { exitCode, }); actionsCore.setFailed( - `non-zero exit code of ${exitCode} detected while updating directory ${flakeDir}`, + `non-zero exit code of ${exitCode} detected while updating directory \`${flakeDir}\``, ); } else { actionsCore.info( - `flake.lock file in ${flakeDir} was successfully updated`, + `flake.lock file in \`${flakeDir}\` was successfully updated`, ); } } + + private validateInputs(): void { + // Ensure that either path-to-flake-dir or flake-dirs is set to a meaningful value but not both + if ( + this.flakeDirs !== null && + this.flakeDirs.length > 0 && + this.pathToFlakeDir !== "" + ) { + // TODO: improve this error message + throw new Error( + "Both `path-to-flake-dir` and `flake-dirs` are set, whereas only one can be set", + ); + } + + // Ensure that flake-dirs isn't an empty array if set + if (this.flakeDirs !== null && this.flakeDirs.length === 0) { + throw new Error( + "The `flake-dirs` input is set to an empty array; it must contain at least one directory", + ); + } + } + + private ensureDirectoryExists(flakeDir: string): void { + actionsCore.debug(`Checking that flake directory \`${flakeDir}\` exists`); + + // Ensure the directory exists + fs.access(flakeDir, fs.constants.F_OK, (err) => { + if (err !== null) { + throw new Error(`Directory \`${flakeDir}\` doesn't exist`); + } else { + actionsCore.debug(`Flake directory \`${flakeDir}\` exists`); + } + }); + } + + private ensureDirectoryIsFlake(flakeDir: string): void { + const flakeDotNix = `${flakeDir}/flake.nix`; + if (!fs.existsSync(flakeDotNix)) { + throw new Error( + `Directory \`${flakeDir}\` is not a valid flake as it doesn't contain a \`flake.nix\``, + ); + } else { + actionsCore.debug(`Directory \`${flakeDir}\` is a valid flake`); + } + } } function main(): void {