@ycmd/creds
Version:
LSK.js CLI Creds is the easiest way to manage GitHub / Gitlab secrets and credentials
134 lines (132 loc) • 3.35 kB
JavaScript
import { Service } from './chunk-RP3VJXKV.js';
import { __name } from './chunk-U2DZE3DI.js';
import { Err } from '@lsk4/err';
import { createLogger } from '@lsk4/log';
import axios from 'axios';
import { map } from 'fishbird';
var force = true;
var GitlabService = class extends Service {
static {
__name(this, "GitlabService");
}
projectId;
projectName;
projectPath;
projectCredsUrl;
projectCredsOwner;
token;
server;
force = true;
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("!projectId");
if (!this.server)
throw new Err("!server");
if (!this.projectName)
throw new Err("!projectName");
if (!this.projectPath)
throw new Err("!projectPath");
if (!this.projectCredsUrl)
throw new Err("!projectCredsUrl");
if (!this.projectCredsOwner)
throw new Err("!projectCredsOwner");
if (!this.token)
throw new Err("!token");
}
createClient(options = {}) {
return axios.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("NOT_IMPLEMENTED");
}
async removeOldHooks() {
const { data: hooksList } = await this.client({
method: "get",
url: `/hooks`
}).catch((err) => {
return {
data: {
value: "@lskjs/creds"
}
};
});
await 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
});
}
};
export { GitlabService };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=chunk-SUI7UV2O.js.map