@ycmd/creds
Version:
LSK.js CLI Creds is the easiest way to manage GitHub / Gitlab secrets and credentials
148 lines (143 loc) • 4.01 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 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 chunk7HIOIWZN_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 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.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-DWITQGH2.js.map