UNPKG

@ycmd/creds

Version:

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

140 lines (135 loc) 3.63 kB
'use strict'; var chunkSWCVOLVG_js = require('./chunk-SWCVOLVG.js'); var chunkU2DZE3DI_js = require('./chunk-U2DZE3DI.js'); var err = require('@lsk4/err'); var log = require('@lsk4/log'); var axios = require('axios'); var fishbird = require('fishbird'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var axios__default = /*#__PURE__*/_interopDefault(axios); var force = true; var GitlabService = class extends chunkSWCVOLVG_js.Service { static { chunkU2DZE3DI_js.__name(this, "GitlabService"); } projectId; projectName; projectPath; projectCredsUrl; projectCredsOwner; token; server; force = true; log = 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.Err("!projectId"); if (!this.server) throw new err.Err("!server"); if (!this.projectName) throw new err.Err("!projectName"); if (!this.projectPath) throw new err.Err("!projectPath"); if (!this.projectCredsUrl) throw new err.Err("!projectCredsUrl"); if (!this.projectCredsOwner) throw new err.Err("!projectCredsOwner"); if (!this.token) throw new err.Err("!token"); } createClient(options = {}) { return axios__default.default.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 uploadSecret(key, variable, options = {}) { const value = typeof variable === "string" ? variable : variable.value; const { data: varData } = await this.client({ method: "get", url: `/variables/${key}` }).catch((err) => { return { data: { value: "@lskjs/creds" } }; }); if (varData.value && varData.value.indexOf("@lskjs/creds") === -1 && !force) { this.log.warn(`[IGNORE] Project ${this.projectId} ${key}`); return; } await this.client({ method: "delete", url: `/variables/${key}` }).catch(() => { }); await this.client({ method: "post", url: "/variables", data: { key, variable_type: "file", value, protected: true, ...options } }); } async uploadVariable(name, variable, options = {}) { this.uploadSecret(name, variable, { variable_type: "env_var", protected: Boolean(options.protected || false) }); } async uploadEnv() { this.log.warn("GitLab uploading env doesn't supported"); throw new err.Err("NOT_IMPLEMENTED"); } async removeOldHooks() { const { data: hooksList } = await this.client({ method: "get", url: `/hooks` }).catch((err) => { return { data: { value: "@lskjs/creds" } }; }); await fishbird.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 }); } }; exports.GitlabService = GitlabService; //# sourceMappingURL=out.js.map //# sourceMappingURL=chunk-GTJF3ELT.js.map