UNPKG

@ycmd/creds

Version:

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

112 lines (107 loc) 3.33 kB
'use strict'; var chunkTPW2Y2YB_js = require('./chunk-TPW2Y2YB.js'); var chunkPOLJ2ZZ2_js = require('./chunk-POLJ2ZZ2.js'); var err = require('@lsk4/err'); var log = require('@lsk4/log'); var axios = require('axios'); var _sodium = require('libsodium-wrappers'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var axios__default = /*#__PURE__*/_interopDefault(axios); var _sodium__default = /*#__PURE__*/_interopDefault(_sodium); var GithubService = class extends chunkTPW2Y2YB_js.Service { static { chunkPOLJ2ZZ2_js.__name(this, "GithubService"); } log = log.createLogger(this.constructor.name); checkConfig() { super.checkConfig(); if (!this.projectId) throw new err.Err("!projectId"); if (!this.server) throw new err.Err("!server"); if (!this.token) throw new err.Err("!token"); } createClient(options) { const server = this.server || "api.github.com"; const baseURL = `https://${server}/repos/${this.getProjectPath()}`; return axios__default.default.create({ baseURL, headers: { Accept: "application/vnd.github+json", Authorization: `Bearer ${this.token}`, "X-GitHub-Api-Version": "2022-11-28" }, ...options }); } getServiceLink() { return "github.com"; } getProjectUrl() { return `https://${this.getServiceLink()}/${this.projectName}`; } getProjectCICDSettingURL() { return `${this.getProjectUrl()}/settings/secrets/actions`; } async uploadSecret(key, content) { const { data: publicKeyData } = await this.client({ method: "get", url: `/actions/secrets/public-key` }).catch((err$1) => { throw new err.Err(err$1.message, { data: err$1?.response?.data }); }); if (!publicKeyData?.key) throw new err.Err("!publicKey"); if (!publicKeyData?.key_id) throw new err.Err("!publicKeyId"); await _sodium__default.default.ready; const sodium = _sodium__default.default; const binkey = sodium.from_base64(publicKeyData.key, sodium.base64_variants.ORIGINAL); const binsec = sodium.from_string(content); const encBytes = sodium.crypto_box_seal(binsec, binkey); const output = sodium.to_base64(encBytes, sodium.base64_variants.ORIGINAL); await this.client({ method: "put", url: `/actions/secrets/${key}`, data: { encrypted_value: output, key_id: publicKeyData.key_id } }); } async uploadVariable(key, content) { const { data: varData, status } = await this.client({ method: "get", url: `/actions/variables/${key}` }).catch((err) => err?.response); if (status === 404) { await this.client({ method: "post", url: `/actions/variables`, data: { name: key, value: content } }); } if (status === 200 && varData.name.toLowerCase() === key.toLowerCase()) { await this.client({ method: "patch", url: `/actions/variables/${key}`, data: { name: key, value: content } }); } } uploadHook() { throw new err.Err("Github hooks not supported yet"); } }; exports.GithubService = GithubService; //# sourceMappingURL=out.js.map //# sourceMappingURL=chunk-PNMRKM6O.js.map