@ycmd/creds
Version:
LSK.js CLI Creds is the easiest way to manage GitHub / Gitlab secrets and credentials
105 lines (103 loc) • 2.97 kB
JavaScript
import { Service } from './chunk-EVPQ7GSQ.js';
import { __name } from './chunk-U2DZE3DI.js';
import { Err } from '@lsk4/err';
import { createLogger } from '@lsk4/log';
import axios from 'axios';
import _sodium from 'libsodium-wrappers';
var GithubService = class extends Service {
static {
__name(this, "GithubService");
}
log = createLogger(this.constructor.name);
checkConfig() {
super.checkConfig();
if (!this.projectId)
throw new Err("!projectId");
if (!this.server)
throw new Err("!server");
if (!this.token)
throw new Err("!token");
}
createClient(options) {
const server = this.server || "api.github.com";
const baseURL = `https://${server}/repos/${this.getProjectPath()}`;
return axios.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) => {
throw new Err(err.message, {
data: err?.response?.data
});
});
if (!publicKeyData?.key)
throw new Err("!publicKey");
if (!publicKeyData?.key_id)
throw new Err("!publicKeyId");
await _sodium.ready;
const sodium = _sodium;
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("Github hooks not supported yet");
}
};
export { GithubService };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=chunk-WNNF4IDJ.js.map