UNPKG

@ycmd/creds

Version:

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

2 lines 6.26 kB
var L=Object.defineProperty;var h=(i,e)=>L(i,"name",{value:e,configurable:!0}),$=(i=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(i,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):i)(function(i){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+i+'" is not supported')});import{Err as E}from"@lskjs/err";import{mapSeries as _}from"fishbird";import{readFile as R}from"fs/promises";import{Err as m}from"@lskjs/err";import y from"libsodium-wrappers";import{Err as d}from"@lskjs/err";import{log as n}from"@lskjs/log/log";import O from"axios";import{map as g}from"fishbird";var p=class{static{h(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=O.create(t)}checkConfig(){throw new d("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 d("NOT_IMPLEMENTED")}async uploadVariable(e,t){throw new d("NOT_IMPLEMENTED")}async removeOldHooks(){}async uploadHook(e){}async uploadHooks(e){if(!e)throw new d("!env");let{hooks:t=[]}=e;try{await this.removeOldHooks()}catch(a){n.error("[ERR] Old hooks removing failed:",a.message)}await g(t,async(a,r)=>{try{await this.uploadHook(a),n.info(`[OK] Hook ${r} uploaded`)}catch(o){n.error(`[ERR] Hook ${r} not uploaded:`,o.message)}})}async uploadAll(e){if(!e)throw new d("!env");let{secrets:t={},variables:a={},files:r=[]}=e;await this.uploadHooks(e),await g(Object.entries(t),async([o,s])=>{try{await this.uploadSecret(o,s),n.info(`[OK] Secret ${o} uploaded`)}catch(c){n.error(`[ERR] Secret ${o} not uploaded, because`,c.message),n.error(c)}}),await g(Object.entries(a),async([o,s])=>{try{await this.uploadVariable(o,s),n.info(`[OK] Variable ${o} uploaded`)}catch(c){n.error(`[ERR] Variable ${o} not uploaded, because`,c.message)}}),await g(r,async({name:o,credType:s,content:c})=>{let l=o,u=c;try{if(s==="variable")await this.uploadVariable(l,u);else if(s==="secret")await this.uploadSecret(l,u);else if(s==="skip"){n.debug(`[SKIP] File ${l} uploaded as ${s}`);return}else throw new d("unknown credType",{credType:s});n.info(`[OK] File ${l} uploaded as ${s}`)}catch(b){n.error(`[ERR] File ${l} not uploaded as ${s}, because`,b.message)}})}};var f=class extends p{static{h(this,"GithubService")}checkConfig(){if(!this.token)throw new m("!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,t){let{data:a}=await this.client({method:"get",url:"/actions/secrets/public-key"}).catch(u=>{throw new m(u.message,{data:u?.response?.data})});if(!a?.key)throw new m("!publicKey");if(!a?.key_id)throw new m("!publicKeyId");await y.ready;let r=y,o=r.from_base64(a.key,r.base64_variants.ORIGINAL),s=r.from_string(t),c=r.crypto_box_seal(s,o),l=r.to_base64(c,r.base64_variants.ORIGINAL);await this.client({method:"put",url:`/actions/secrets/${e}`,data:{encrypted_value:l,key_id:a.key_id}})}async uploadVariable(e,t){let{data:a,status:r}=await this.client({method:"get",url:`/actions/variables/${e}`}).catch(o=>o?.response);r===404&&await this.client({method:"post",url:"/actions/variables",data:{name:e,value:t}}),r===200&&a.name.toLowerCase()===e.toLowerCase()&&await this.client({method:"patch",url:`/actions/variables/${e}`,data:{name:e,value:t}})}uploadHook(){throw new m("Github hooks not supported yet")}};import{Err as w}from"@lskjs/err";import{log as k}from"@lskjs/log/log";import{map as S}from"fishbird";var v=class extends p{static{h(this,"GitlabService")}checkConfig(){if(!this.server)throw new w("!server");if(!this.projectId)throw new w("!projectId");if(!this.token)throw new w("!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:a}=await this.client({method:"get",url:`/variables/${e}`}).catch(r=>{if(!this.force)throw r;return{data:{value:"@lskjs/creds"}}});if(a.value&&a.value.indexOf("@lskjs/creds")===-1&&!this.force){k.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 k.warn("GitLab uploading variable doesn't supported"),new w("NOT_IMPLEMENTED")}async uploadEnv(){throw k.warn("GitLab uploading env doesn't supported"),new w("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 S(e,async({id:t})=>{await this.client({method:"delete",url:`/hooks/${t}`})})}async uploadHook(e){await this.client({method:"post",url:"/hooks",data:e})}};async function re(i,e={}){let t=e.buildDir||`${i}/build`,a=$(`${i}/config.js`),r=a.service?.serviceName;if(!r)throw new E("!serviceName");let o;if(r==="github")o=new f({...a.service,...e});else if(r==="gitlab")o=new v({...a.service,...e});else throw new E("incorrect serviceName",{serviceName:r});let{files:s=[],variables:c,secrets:l,hooks:u}=a,b=await _(s,async j=>{let{filename:P}=j,I=await R(`${t}/${P}`).then(N=>N.toString());return{...j,content:I}});await o.uploadAll({files:b,variables:c,secrets:l,hooks:u})}h(re,"uploadCommand");export{re as uploadCommand}; //# sourceMappingURL=uploadCommand.mjs.map