UNPKG

@ycmd/creds

Version:

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

2 lines 3.85 kB
var k=Object.defineProperty;var d=(w,e)=>k(w,"name",{value:e,configurable:!0});import{Err as h}from"@lskjs/err";import{log as g}from"@lskjs/log/log";import{map as E}from"fishbird";import{Err as c}from"@lskjs/err";import{log as o}from"@lskjs/log/log";import v from"axios";import{map as u}from"fishbird";var p=class{static{d(this,"Service")}projectId;projectName;projectPath;projectCredsUrl;token;server;force;client;constructor(e){Object.assign(this,e),this.checkConfig();let t={baseURL:this.getBaseUrl(),headers:this.getHeaders()};this.client=v.create(t)}checkConfig(){throw new c("NOT_IMPLEMENTED")}getBaseUrl(){return null}getHeaders(){return{}}getServiceLink(){return null}getProjectName(){return this.projectName}getProjectId(){return this.projectId}getProjectPath(){return this.projectPath}getProjectUrl(){return null}getProjectCredsUrl(){return this.projectCredsUrl}getProjectCICDSettingURL(){return null}async uploadSecret(e,t){throw new c("NOT_IMPLEMENTED")}async uploadVariable(e,t){throw new c("NOT_IMPLEMENTED")}async removeOldHooks(){}async uploadHook(e){}async uploadHooks(e){if(!e)throw new c("!env");let{hooks:t=[]}=e;try{await this.removeOldHooks()}catch(s){o.error("[ERR] Old hooks removing failed:",s.message)}await u(t,async(s,i)=>{try{await this.uploadHook(s),o.info(`[OK] Hook ${i} uploaded`)}catch(r){o.error(`[ERR] Hook ${i} not uploaded:`,r.message)}})}async uploadAll(e){if(!e)throw new c("!env");let{secrets:t={},variables:s={},files:i=[]}=e;await this.uploadHooks(e),await u(Object.entries(t),async([r,a])=>{try{await this.uploadSecret(r,a),o.info(`[OK] Secret ${r} uploaded`)}catch(n){o.error(`[ERR] Secret ${r} not uploaded, because`,n.message),o.error(n)}}),await u(Object.entries(s),async([r,a])=>{try{await this.uploadVariable(r,a),o.info(`[OK] Variable ${r} uploaded`)}catch(n){o.error(`[ERR] Variable ${r} not uploaded, because`,n.message)}}),await u(i,async({name:r,credType:a,content:n})=>{let l=r,m=n;try{if(a==="variable")await this.uploadVariable(l,m);else if(a==="secret")await this.uploadSecret(l,m);else if(a==="skip"){o.debug(`[SKIP] File ${l} uploaded as ${a}`);return}else throw new c("unknown credType",{credType:a});o.info(`[OK] File ${l} uploaded as ${a}`)}catch(j){o.error(`[ERR] File ${l} not uploaded as ${a}, because`,j.message)}})}};var f=class extends p{static{d(this,"GitlabService")}checkConfig(){if(!this.server)throw new h("!server");if(!this.projectId)throw new h("!projectId");if(!this.token)throw new h("!token")}getBaseUrl(){return`https://${this.server}/api/v4/projects/${this.getProjectId()}`}getHeaders(){return{"PRIVATE-TOKEN":this.token}}getServiceLink(){return this.server}getProjectUrl(){return`https://${this.getServiceLink()}/${this.getProjectPath()}`}getProjectCICDSettingURL(){return`${this.getProjectUrl()}/-/settings/ci_cd`}async uploadSecret(e,t){let{data:s}=await this.client({method:"get",url:`/variables/${e}`}).catch(i=>{if(!this.force)throw i;return{data:{value:"@lskjs/creds"}}});if(s.value&&s.value.indexOf("@lskjs/creds")===-1&&!this.force){g.warn(`[IGNORE] Project ${this.projectId} ${e}`);return}await this.client({method:"delete",url:`/variables/${e}`}).catch(()=>{}),await this.client({method:"post",url:"/variables",data:{key:e,variable_type:"file",value:t,protected:!0}})}async uploadVariable(){throw g.warn("GitLab uploading variable doesn't supported"),new h("NOT_IMPLEMENTED")}async uploadEnv(){throw g.warn("GitLab uploading env doesn't supported"),new h("NOT_IMPLEMENTED")}async removeOldHooks(){let{data:e}=await this.client({method:"get",url:"/hooks"}).catch(t=>{if(!this.force)throw t;return{data:{value:"@lskjs/creds"}}});await E(e,async({id:t})=>{await this.client({method:"delete",url:`/hooks/${t}`})})}async uploadHook(e){await this.client({method:"post",url:"/hooks",data:e})}};export{f as GitlabService}; //# sourceMappingURL=GitlabService.mjs.map