UNPKG

@ycmd/creds

Version:

LSK.js CLI Creds is the easiest way to manage GitHub / Gitlab secrets and credentials

142 lines (140 loc) 3.73 kB
import { Service } from './chunk-3KVVDUHC.js'; import { __name } from './chunk-U2DZE3DI.js'; import { Err } from '@lsk4/err'; import { createLogger } from '@lsk4/log'; import axios from 'axios'; import { map } from 'fishbird'; var force = true; var GitlabService = class extends Service { static { __name(this, "GitlabService"); } projectId; projectName; projectPath; projectCredsUrl; projectCredsOwner; token; server; force = true; log = createLogger(this.constructor.name); constructor(options) { super(options); this.assign(options); this.checkConfig(); this.client = this.createClient(options.clientOptions); } checkConfig() { if (!this.projectId) throw new Err("!projectId"); if (!this.server) throw new Err("!server"); if (!this.projectName) throw new Err("!projectName"); if (!this.projectPath) throw new Err("!projectPath"); if (!this.projectCredsUrl) throw new Err("!projectCredsUrl"); if (!this.projectCredsOwner) throw new Err("!projectCredsOwner"); if (!this.token) throw new Err("!token"); } createClient(options = {}) { return axios.create({ baseURL: `https://${this.server}/api/v4/projects/${this.projectId}`, headers: { "PRIVATE-TOKEN": this.token }, ...options }); } getServiceHostname() { return this.server; } getProjectUrl() { return `https://${this.getServiceHostname()}/${this.projectPath}`; } getProjectCICDSettingURL() { return `${this.getProjectUrl()}/-/settings/ci_cd`; } async uploadVariableOrSecret(name, variable, options = {}) { const value = typeof variable === "string" ? variable : variable.value; const type = variable?.type || options?.type || "file"; const protectedValue = Boolean(variable?.protected ?? options?.protected ?? false); const { data: varData } = await this.client({ method: "get", url: `/variables/${name}` }).catch((err) => { return { data: { value: "@lskjs/creds" } }; }); if (varData.value && varData.value.indexOf("@lskjs/creds") === -1 && !force) { this.log.warn(`[IGNORE] Project ${this.projectId} ${name}`); return; } await this.client({ method: "delete", url: `/variables/${name}` }).catch(() => { }); await this.client({ method: "post", url: "/variables", data: { key: name, value, variable_type: type, protected: protectedValue, ...options } }); } async uploadSecret(name, variable, options = {}) { return this.uploadVariableOrSecret(name, variable, { type: "file", protected: Boolean(options.protected ?? true) }); } async uploadVariable(name, variable, options = {}) { return this.uploadVariableOrSecret(name, variable, { type: "env_var", protected: Boolean(options.protected ?? false) }); } async uploadEnv() { this.log.warn("GitLab uploading env doesn't supported"); throw new Err("NOT_IMPLEMENTED"); } async removeOldHooks() { const { data: hooksList } = await this.client({ method: "get", url: `/hooks` }).catch((err) => { return { data: { value: "@lskjs/creds" } }; }); await map(hooksList, async ({ id: hookId }) => { await this.client({ method: "delete", url: `/hooks/${hookId}` }); }); } async uploadHook(hook) { await this.client({ method: "post", url: "/hooks", data: hook }); } }; export { GitlabService }; //# sourceMappingURL=out.js.map //# sourceMappingURL=chunk-F27XVIWU.js.map