@ycmd/creds
Version:
LSK.js CLI Creds is the easiest way to manage GitHub / Gitlab secrets and credentials
132 lines (127 loc) • 3.93 kB
JavaScript
;
var chunk7HIOIWZN_js = require('./chunk-7HIOIWZN.js');
var chunkU2DZE3DI_js = require('./chunk-U2DZE3DI.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 chunk7HIOIWZN_js.Service {
static {
chunkU2DZE3DI_js.__name(this, "GithubService");
}
projectName;
projectPath;
projectCredsUrl;
projectCredsOwner;
token;
server;
log = log.createLogger(this.constructor.name);
constructor(options) {
super(options);
this.assign(options);
this.checkConfig();
this.client = this.createClient(options.clientOptions);
}
checkConfig() {
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 = {}) {
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
});
}
getServiceHostname() {
return "github.com";
}
getProjectPath() {
return this.projectPath;
}
getProjectUrl() {
return `https://${this.getServiceHostname()}/${this.getProjectPath()}`;
}
getProjectCICDSettingURL() {
return `${this.getProjectUrl()}/settings/secrets/actions`;
}
async uploadSecret(name, variable) {
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
});
});
const content = typeof variable === "string" ? variable : variable.value;
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/${name}`,
data: {
encrypted_value: output,
key_id: publicKeyData.key_id
}
});
}
async uploadVariable(name, variable) {
const value = typeof variable === "string" ? variable : variable.value;
const { data: varData, status } = await this.client({
method: "get",
url: `/actions/variables/${name}`
}).catch((err) => err?.response);
if (status === 404) {
await this.client({
method: "post",
url: `/actions/variables`,
data: {
name,
value
}
});
}
if (status === 200 && varData.name.toLowerCase() === name.toLowerCase()) {
await this.client({
method: "patch",
url: `/actions/variables/${name}`,
data: {
name,
value
}
});
}
}
uploadHook() {
throw new err.Err("Github hooks not supported yet");
}
};
exports.GithubService = GithubService;
//# sourceMappingURL=out.js.map
//# sourceMappingURL=chunk-3JCOAZA2.js.map