UNPKG

3.17 kBJavaScriptView Raw
1'use strict';
2
3var http = require('http');
4var url = require('url');
5var Path = require('path');
6
7function coverage(devBranch, compareWith, serviceName, domain) {
8 var path = '/impact/ImpactModuleAction.do?action=getCommitDiffFileDetail&serviceName=' + serviceName + '&masterBranchCommitHash=' + compareWith + '&buildCommitHash=' + devBranch;
9 var myURL = url.parse(domain);
10 var host = myURL.protocol,
11 port = myURL.host;
12
13 host = host.replace(':', '');
14 port = parseInt(port);
15
16 var options = {
17 host: host,
18 port: port,
19 path: path,
20 method: 'GET'
21 };
22 return new Promise(function (resolve, reject) {
23 var request = apiCall(options, function (callValue) {
24 var Response = JSON.parse(callValue);
25 if (Object.keys(Response).length > 0) {
26 if (Object.prototype.hasOwnProperty.call(Response, 'STATUS')) {
27 if (Response.STATUS) {
28 var listOfFiles = [];
29 if (Object.keys(Response.DIFF_FILES).length > 0) {
30 if (Object.prototype.hasOwnProperty.call(Response.DIFF_FILES, 'ADDED')) {
31 if (Response.DIFF_FILES.ADDED.length > 0) {
32 listOfFiles = listOfFiles.concat(listMaker(Response.DIFF_FILES.ADDED));
33 }
34 }
35 if (Object.prototype.hasOwnProperty.call(Response.DIFF_FILES, 'UPDATED')) {
36 if (Response.DIFF_FILES.UPDATED.length > 0) {
37 listOfFiles = listOfFiles.concat(listMaker(Response.DIFF_FILES.UPDATED));
38 }
39 }
40 resolve({ STATUS: true, LIST: listOfFiles, REASON: 'success' });
41 } else {
42 resolve({
43 STATUS: false,
44 LIST: [],
45 REASON: 'No modified js files'
46 });
47 }
48 } else {
49 resolve({ STATUS: false, LIST: [], REASON: 'Internal Error' });
50 }
51 } else {
52 resolve({ STATUS: false, LIST: [], REASON: 'Internal Error' });
53 }
54 } else {
55 resolve({ STATUS: false, LIST: [], REASON: 'Internal Error' });
56 }
57 });
58 request.on('error', function (error) {
59 resolve({ STATUS: false, LIST: [], REASON: 'Service Down' });
60 });
61 request.end();
62 });
63}
64
65function apiCall(optionObject, callBack) {
66 return http.request(optionObject, function (res) {
67 var resText = '';
68 res.setEncoding('utf8');
69 res.on('data', function (data) {
70 resText = resText + data;
71 });
72 res.on('end', function () {
73 callBack(resText);
74 });
75 });
76}
77
78function listMaker(listOfObject) {
79 var fileNameList = [];
80 var curListName = process.cwd().split(Path.sep);
81
82 listOfObject.forEach(function (fileObj) {
83 var fileName = fileObj.FILEPATH;
84 fileName = fileName.replace(curListName[curListName.length - 2] + Path.sep + curListName[curListName.length - 1] + Path.sep, '');
85 fileName = fileName.replace('jsapps/components/', '');
86 if (fileName.includes('.js') && !fileName.includes('.json') && !fileName.includes('.docs.js') && !fileName.includes('.spec.js')) {
87 fileNameList.push(fileName);
88 }
89 });
90 return fileNameList;
91}
92
93module.exports = coverage;
\No newline at end of file