UNPKG

@ycmd/creds

Version:

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

2 lines 3.78 kB
var k=Object.defineProperty;var d=(w,e)=>k(w,"name",{value:e,configurable:!0});import{Err as h}from"@lskjs/err";import g from"libsodium-wrappers";import{Err as l}from"@lskjs/err";import{log as i}from"@lskjs/log/log";import j from"axios";import{map as p}from"fishbird";var m=class{static{d(this,"Service")}projectId;projectName;projectPath;projectCredsUrl;token;server;force;client;constructor(e){Object.assign(this,e),this.checkConfig();let o={baseURL:this.getBaseUrl(),headers:this.getHeaders()};this.client=j.create(o)}checkConfig(){throw new l("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,o){throw new l("NOT_IMPLEMENTED")}async uploadVariable(e,o){throw new l("NOT_IMPLEMENTED")}async removeOldHooks(){}async uploadHook(e){}async uploadHooks(e){if(!e)throw new l("!env");let{hooks:o=[]}=e;try{await this.removeOldHooks()}catch(s){i.error("[ERR] Old hooks removing failed:",s.message)}await p(o,async(s,a)=>{try{await this.uploadHook(s),i.info(`[OK] Hook ${a} uploaded`)}catch(t){i.error(`[ERR] Hook ${a} not uploaded:`,t.message)}})}async uploadAll(e){if(!e)throw new l("!env");let{secrets:o={},variables:s={},files:a=[]}=e;await this.uploadHooks(e),await p(Object.entries(o),async([t,r])=>{try{await this.uploadSecret(t,r),i.info(`[OK] Secret ${t} uploaded`)}catch(n){i.error(`[ERR] Secret ${t} not uploaded, because`,n.message),i.error(n)}}),await p(Object.entries(s),async([t,r])=>{try{await this.uploadVariable(t,r),i.info(`[OK] Variable ${t} uploaded`)}catch(n){i.error(`[ERR] Variable ${t} not uploaded, because`,n.message)}}),await p(a,async({name:t,credType:r,content:n})=>{let c=t,u=n;try{if(r==="variable")await this.uploadVariable(c,u);else if(r==="secret")await this.uploadSecret(c,u);else if(r==="skip"){i.debug(`[SKIP] File ${c} uploaded as ${r}`);return}else throw new l("unknown credType",{credType:r});i.info(`[OK] File ${c} uploaded as ${r}`)}catch(f){i.error(`[ERR] File ${c} not uploaded as ${r}, because`,f.message)}})}};var b=class extends m{static{d(this,"GithubService")}checkConfig(){if(!this.token)throw new h("!token")}f;getBaseUrl(){return`https://${this.server||"api.github.com"}/repos/${this.getProjectPath()}`}getHeaders(){return{Accept:"application/vnd.github+json",Authorization:`Bearer ${this.token}`,"X-GitHub-Api-Version":"2022-11-28"}}getServiceLink(){return"github.com"}getProjectUrl(){return`https://${this.getServiceLink()}/${this.projectName}`}getProjectCICDSettingURL(){return`${this.getProjectUrl()}/settings/secrets/actions`}async uploadSecret(e,o){let{data:s}=await this.client({method:"get",url:"/actions/secrets/public-key"}).catch(u=>{throw new h(u.message,{data:u?.response?.data})});if(!s?.key)throw new h("!publicKey");if(!s?.key_id)throw new h("!publicKeyId");await g.ready;let a=g,t=a.from_base64(s.key,a.base64_variants.ORIGINAL),r=a.from_string(o),n=a.crypto_box_seal(r,t),c=a.to_base64(n,a.base64_variants.ORIGINAL);await this.client({method:"put",url:`/actions/secrets/${e}`,data:{encrypted_value:c,key_id:s.key_id}})}async uploadVariable(e,o){let{data:s,status:a}=await this.client({method:"get",url:`/actions/variables/${e}`}).catch(t=>t?.response);a===404&&await this.client({method:"post",url:"/actions/variables",data:{name:e,value:o}}),a===200&&s.name.toLowerCase()===e.toLowerCase()&&await this.client({method:"patch",url:`/actions/variables/${e}`,data:{name:e,value:o}})}uploadHook(){throw new h("Github hooks not supported yet")}};export{b as GithubService}; //# sourceMappingURL=GithubService.mjs.map