UNPKG

handow-shm

Version:

E2E test server with Handow engine

1 lines 6.66 kB
"use strict";const e=require("express"),s=require("path"),t=require("app-root-path"),a=require("deep-extend"),r=require("glob"),n=require("fs"),o=require("../logger.js"),u=require("handow"),i=require(s.join(`${t}`,"/config.js")),d=require("../services/shmSetting");let c;const l=e.Router();let h,p;try{h=require(s.join(`${t}`,"stepBundles/actsBundle"))}catch(e){h={}}try{p=require(s.join(`${t}`,"stepBundles/factsBundle"))}catch(e){p={}}l.get("/handowstatus",async(e,s,t)=>{try{const a=u.handowStatus(e.query.status);s.status(200).json({data:a,status:{statusCode:2e5,message:"success"}})}catch(s){e.exception={status:"400",errCode:"000",error:s.message?s.message:"Synchronize with handow failed"},t(s)}}),l.post("/handowstatus",async(e,s,t)=>{try{c=d.get(),`${e.get("X-HANDOW-TOKEN")}`===c.shmToken&&u.setRunningStatus(e.body.pid),s.status(200).json({status:{statusCode:2e5,message:"success"}})}catch(s){e.exception={status:"400",errCode:"000",error:s.message?s.message:"Can not set/reset handow status"},t(s)}}),l.get("/params",async(e,n,o)=>{try{const u=await r.sync(s.join(`${t}`,e.query.path,"/*.params.js"));let i={};for(const e of u){delete require.cache[require.resolve(e)];const s=require(e);i=a(i,s)}n.status(200).json({data:i,status:{statusCode:"200000",message:"success"}})}catch(s){e.exception={status:"400",errCode:"000",error:s.message?s.message:"Load global parameters failed"},o(s)}}),l.get("/steps",async(e,a,r)=>{const n={acts:{},facts:{}};try{if("true"==e.query.build){const e=await u.buildSteps(s.join(`${t}`,i.stepsPath));if(e instanceof Error)throw e;delete require.cache[require.resolve(s.join(`${t}`,"stepBundles/actsBundle"))],delete require.cache[require.resolve(s.join(`${t}`,"stepBundles/factsBundle"))],h=require(s.join(`${t}`,"stepBundles/actsBundle")),p=require(s.join(`${t}`,"stepBundles/factsBundle"))}for(const e in h)n.acts[e]=`${h[e].doStep}`;for(const e in p)n.facts[e]=`${p[e].doStep}`;a.status(200).json({data:n,status:{statusCode:2e5,message:"success"}})}catch(s){e.exception={status:"400",errCode:"000",error:s.message?s.message:"Load steps failed"},r(s)}}),l.post("/openplantree",async(e,s,t)=>{try{if(u.handowStatus().running)throw new Error("Handow is running at this moment");const a=await u.buildTree(`${e.body.file}`);s.status(200).json({data:a,status:{statusCode:2e5,message:"success"}})}catch(s){e.exception={status:"403",errCode:"000",error:s.message?s.message:"Can not build the plan-tree"},t(s)}}),l.post("/run",async(e,s,t)=>{c=d.get(),await u.buildSteps();try{if(e.session.user&&"admin"==e.session.user.role)if("manual"!==c.scheduleMode){const e=new Error("forbidden running plan if SHM not scheduled in manual mode");e.status="403",e.code="002",t(e)}else{const t=u.handowStatus();if(t.running)throw new Error(`Denied, handow is running <${t.runningPlan.title}> at this moment`);if(`${e.body.path}`.endsWith(".feature"))u.runStory(`${e.body.path}`),s.status(200).json({status:{statusCode:"200000",message:`Start run story <${e.body.path}>`}});else{if(!`${e.body.path}`.endsWith(".plan.json"))throw new Error(`Invalid plan or story - <${e.body.path}>`);u.runPlan(`${e.body.path}`),s.status(200).json({status:{statusCode:"200000",message:`Start run plan <${e.body.path}>`}})}}else{const e=new Error('forbidden running plan by users other than "admin"');e.status="403",e.code="001",t(e)}}catch(s){e.exception={status:s.status?s.status:"400",errCode:s.code?s.code:"000",error:s.message?s.message:"Check permission failed"},t(s)}}),l.post("/stop",async(e,s,t)=>{try{if(e.session.user&&"admin"==e.session.user.role){const e=u.handowStatus();e.running&&u.stop(),s.status(200).json({status:{statusCode:"200000",message:e.running?`${e.plan.title} is stopped`:"Handow is not running"}})}else{const e=new Error('forbidden running plan by users other than "admin"');e.status="403",e.code="001",t(e)}}catch(s){e.exception={status:s.status?s.status:"400",errCode:s.code?s.code:"000",error:s.message?s.message:"Check permission failed"},t(s)}}),l.get("/record",async(e,a,r)=>{let o;if(e.query&&e.query.path)o=`${e.query.path}`.includes(i.reportPath)?`${e.query.path}`:s.join(`${t}`,`${i.reportPath}`,`archives/${e.query.path}/${e.query.path}.json`);else{const e=n.readdirSync(s.join(`${t}`,`${i.reportPath}`));for(const a of e)if(a.endsWith(".json")){o=s.join(`${t}`,`${i.reportPath}`,a);break}"undefined"===`${o}`&&u.handowStatus().newArcRecord&&(o=s.join(`${t}`,`${i.reportPath}`,`archives/${u.handowStatus().newArcRecord}/${u.handowStatus().newArcRecord}.json`))}try{let e;o?(require.resolve(o),e=n.readFileSync(`${o}`,"utf8")):e=null,a.status(200).json({data:e,status:{statusCode:"200000",message:"Record data has been loaded successfully"}})}catch(e){e.status="404",e.code="000",r(e)}}),l.get("/history",async(e,a,r)=>{const u=[];let d,c,l=[];try{if("/"===`${e.query.archive}`){const e=n.readdirSync(s.join(`${t}`,`${i.reportPath}`));for(const a of e)if(a.endsWith(".json")){const e=n.readFileSync(s.join(`${t}`,`${i.reportPath}`,a),"utf8"),r=JSON.parse(e);d=r.plan,c=r.summary.end,u.push({time:r.summary.end,total:r.summary.totalScenarios,failed:r.summary.failedScenarios});break}}else d=e.query.archive.substring(0,e.query.archive.lastIndexOf("_")),c=e.query.archive.substring(e.query.archive.lastIndexOf("_")+1,e.query.archive.length);(l=n.readdirSync(s.join(`${t}`,`${i.reportPath}`,"archives"),{withFileTypes:!0}).filter(e=>{if(e.isDirectory()){const s=`${e.name}`.substring(0,`${e.name}`.lastIndexOf("_")),t=`${e.name}`.substring(`${e.name}`.lastIndexOf("_")+1,`${e.name}`.length);return`${d}`===`${s}`&&parseInt(c,10)>=parseInt(t,10)}return!1}).map(e=>{const a=n.readFileSync(s.join(`${t}`,`${i.reportPath}`,"archives",`${e.name}/${e.name}.json`),"utf8"),r=JSON.parse(a);return{time:r.summary.end,total:r.summary.totalScenarios,failed:r.summary.failedScenarios}})).length>30&&(l=l.splice(l.length-30,l.length)),a.status(200).json({data:[...l,...u],status:{statusCode:"200000",message:"History data has been loaded successfully"}})}catch(s){o.error(new TypeError(`@handowRoutes.js - Cannot parse the history record files (${s}).`)),e.exception={status:"500",errCode:"005",error:s.message?s.message:"Cannot parse the latest record file."},r(s)}}),l.get("/plantree",async(e,a,r)=>{let o;o=e.query&&e.query.path?`${e.query.path}`.includes(i.reportPath)?`${e.query.path}`:s.join(`${t}`,`${i.reportPath}`,`archives/${e.query.path}/plan.tree`):s.join(`${t}`,`${i.reportPath}`,"/plan.tree");try{require.resolve(o);const e=n.readFileSync(`${o}`,"utf8");a.status(200).json({data:e,status:{statusCode:"200000",message:"Plan tree has been loaded successfully"}})}catch(e){e.status="404",e.code="000",r(e)}}),module.exports=l;