// // Repository-level Actions secrets and variables. // const selection = require("./selection.js"); const ui = require("../ui.js"); const NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/; function validateName(name) { if (!name) return "A name is required."; if (!NAME_PATTERN.test(name)) { return "Names may contain letters, digits, and underscores, and cannot start with a digit."; } return null; } async function repoFor(argument, store, prompt) { return await selection.resolveRepo(argument, { prompt: prompt }); } function register(store) { const reloadSecrets = (repo) => store.loadSecrets(repo); const reloadVariables = (repo) => store.loadVariables(repo); nova.commands.register("gitea.refreshSecrets", async (argument) => { const repo = await repoFor(argument, store, "Refresh secrets for which repository?"); if (repo) await reloadSecrets(repo); }); nova.commands.register("gitea.refreshVariables", async (argument) => { const repo = await repoFor(argument, store, "Refresh variables for which repository?"); if (repo) await reloadVariables(repo); }); nova.commands.register("gitea.createSecret", async (argument) => { const repo = await repoFor(argument, store, "Add a secret to which repository?"); if (!repo) return; const name = await ui.input(`New secret in ${repo.fullName}`, { placeholder: "SECRET_NAME", }); if (!name) return; const problem = validateName(name.trim()); if (problem) { ui.warn(problem); return; } const value = await ui.input(`Value for ${name.trim()}`, { placeholder: "Secret value", secure: true, }); if (value === null) return; try { await store.apiFor(repo).setSecret(repo, name.trim(), value); ui.info(`Saved secret ${name.trim()}.`); } catch (error) { ui.error(`Could not save the secret: ${error.message || error}`); return; } await reloadSecrets(repo); }); nova.commands.register("gitea.updateSecret", async (argument) => { const node = selection.selectedNode(argument); const secret = node && node.data ? node.data.secret : null; const repo = selection.repoOf(node); if (!secret || !repo) { ui.warn("Select a secret first."); return; } // Gitea never returns a secret's value, so an update always sets a new one. const value = await ui.input(`New value for ${secret.name}`, { placeholder: "Secret value", secure: true, }); if (value === null) return; try { await store.apiFor(repo).setSecret(repo, secret.name, value); ui.info(`Updated secret ${secret.name}.`); } catch (error) { ui.error(`Could not update the secret: ${error.message || error}`); return; } await reloadSecrets(repo); }); nova.commands.register("gitea.deleteSecret", async (argument) => { const node = selection.selectedNode(argument); const secret = node && node.data ? node.data.secret : null; const repo = selection.repoOf(node); if (!secret || !repo) { ui.warn("Select a secret first."); return; } if (!(await ui.confirm(`Delete secret ${secret.name} from ${repo.fullName}?`, "Delete"))) { return; } try { await store.apiFor(repo).deleteSecret(repo, secret.name); ui.info(`Deleted secret ${secret.name}.`); } catch (error) { ui.error(`Could not delete the secret: ${error.message || error}`); return; } await reloadSecrets(repo); }); nova.commands.register("gitea.createVariable", async (argument) => { const repo = await repoFor(argument, store, "Add a variable to which repository?"); if (!repo) return; const name = await ui.input(`New variable in ${repo.fullName}`, { placeholder: "VARIABLE_NAME", }); if (!name) return; const problem = validateName(name.trim()); if (problem) { ui.warn(problem); return; } const value = await ui.input(`Value for ${name.trim()}`, { placeholder: "Value" }); if (value === null) return; try { await store.apiFor(repo).createVariable(repo, name.trim(), value); ui.info(`Created variable ${name.trim()}.`); } catch (error) { ui.error(`Could not create the variable: ${error.message || error}`); return; } await reloadVariables(repo); }); nova.commands.register("gitea.updateVariable", async (argument) => { const node = selection.selectedNode(argument); const variable = node && node.data ? node.data.variable : null; const repo = selection.repoOf(node); if (!variable || !repo) { ui.warn("Select a variable first."); return; } const value = await ui.input(`New value for ${variable.name}`, { placeholder: "Value", value: variable.data || variable.value || "", }); if (value === null) return; try { await store.apiFor(repo).updateVariable(repo, variable.name, value); ui.info(`Updated variable ${variable.name}.`); } catch (error) { ui.error(`Could not update the variable: ${error.message || error}`); return; } await reloadVariables(repo); }); nova.commands.register("gitea.deleteVariable", async (argument) => { const node = selection.selectedNode(argument); const variable = node && node.data ? node.data.variable : null; const repo = selection.repoOf(node); if (!variable || !repo) { ui.warn("Select a variable first."); return; } if ( !(await ui.confirm(`Delete variable ${variable.name} from ${repo.fullName}?`, "Delete")) ) { return; } try { await store.apiFor(repo).deleteVariable(repo, variable.name); ui.info(`Deleted variable ${variable.name}.`); } catch (error) { ui.error(`Could not delete the variable: ${error.message || error}`); return; } await reloadVariables(repo); }); } exports.register = register; exports.validateName = validateName;