Skip to content

Commit

Permalink
Finish initial rework into TS
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Apr 26, 2024
1 parent 502daa7 commit dde5487
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 18 deletions.
3 changes: 1 addition & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 38 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 48 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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",
};
Expand All @@ -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}`)
: [];

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);

const nixCommand = `nix ${this.nixOptions} flake lock ${inputStr} --commit-lock-file --commit-lock-file-summary "${this.commitMessage}"`;
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`);
}
}
}

Expand Down

0 comments on commit dde5487

Please sign in to comment.