@s-ui/lint-reporters
Version:
Send lint errors and golden path metrics to DataDog
1 lines • 3.71 kB
JavaScript
import _async_to_generator from"@swc/helpers/src/_async_to_generator.mjs";import _ts_generator from"@swc/helpers/src/_ts_generator.mjs";import https from"node:https";import{match}from"ts-pattern";var ONE_SECOND=1e3;var delay=_async_to_generator(function(ms){return _ts_generator(this,function(_state){return[2,new Promise(function(resolve){return setTimeout(resolve,ms)})]})});var _env=process.env,_MS_URL=_env.MS_URL,MS_URL=_MS_URL===void 0?"ms-common--metrics.es-global-pro.schip.io":_MS_URL,BASIC_AUTH=_env.BASIC_AUTH;export var HTTPSender=function(){"use strict";function HTTPSender(){}var _proto=HTTPSender.prototype;_proto.send=function send(signals){var _this=this;return _async_to_generator(function(){var body,ok;return _ts_generator(this,function(_state){switch(_state.label){case 0:if(!signals||signals.length===0)return[2,_this.log("\uD83D\uDE21 Sorry but there are not signals to send")];body=signals.map(function(signal){return match(signal).with({type:"repository"},function(p){return{rule:p.rule,value:p.value,type:p.type}}).with({type:"js"},function(p){return{rule:p.ruleName,value:p.numberOfFails,type:p.type}}).exhaustive()});_this.log("\uD83D\uDE80 Sending the metric to the microservice.");return[4,_this.request(body)];case 1:ok=_state.sent();if(ok)_this.log("\uD83E\uDD13 The metric has been successfully sent to the microservice.");if(!!ok)return[3,4];_this.log("\uD83D\uDE05 The initial attempt to send the metric has failed. We will retry in 1 second.");return[4,delay(ONE_SECOND)];case 2:_state.sent();return[4,_this.request(body)];case 3:match.apply(void 0,[_state.sent()]).with(true,function(){return _this.log("\uD83D\uDE0F The retry attempt to send the metric has worked successfully.")}).with(false,function(){return _this.log("\uD83D\uDE21 Sorry, but the second retry attempt to send the metric has also failed. We will not attempt again, and the metric will be lost forever.")});_state.label=4;case 4:return[2,undefined]}})})()};_proto.log=function log(){for(var _len=arguments.length,msg=new Array(_len),_key=0;_key<_len;_key++){msg[_key]=arguments[_key]}console.log.apply(console,msg)};_proto.request=function request(body){var _this=this;return _async_to_generator(function(){var bodyReq;return _ts_generator(this,function(_state){if(BASIC_AUTH===undefined){_this.log("❌ BASIC_AUTH env var shouldnt be undefined");process.exit(1)}bodyReq={metrics:[{name:"metrics.frontend.discipline",organisationName:process.env.GITHUB_REPOSITORY_OWNER,repositoryId:process.env.GITHUB_REPOSITORY_ID,tags:body.map(function(bodyJSON){return{tagSetId:match(bodyJSON.type).with("repository",function(p){return"global.metrics.frontend.repository"}).with("js",function(p){return"global.metrics.frontend.code"}).exhaustive(),key:bodyJSON.rule,value:bodyJSON.value}})}]};return[2,new Promise(function(resolve,_reject){var options={hostname:MS_URL,port:443,path:"/v2/ci-metrics",method:"POST",headers:{"Content-Type":"application/json","Content-Length":Buffer.byteLength(JSON.stringify(bodyReq)),Authorization:"Basic "+BASIC_AUTH}};var req=https.request(options,function(res){_this.log("STATUS: "+res.statusCode);_this.log("HEADERS: "+JSON.stringify(res.headers));res.setEncoding("utf8");res.on("data",function(chunk){_this.log("BODY: "+chunk)});res.on("end",function(){_this.log("No more data in response.");var isSuccess=res.statusCode&&res.statusCode>=200&&res.statusCode<300;resolve(Boolean(isSuccess))})});req.on("error",function(e){_this.log("problem with request: "+e.message);resolve(false)});_this.log("Body:\n\n",JSON.stringify(bodyReq,null,2),"\n\n");req.write(JSON.stringify(bodyReq));req.end()})]})})()};HTTPSender.create=function create(){return new HTTPSender};return HTTPSender}();