cleaning server code

This commit is contained in:
Ayush Kumar Sinha 2019-09-24 13:20:45 +05:30
parent 7bd218c3d6
commit 31725382a2
5 changed files with 276 additions and 253 deletions

254
app.js
View File

@ -1,15 +1,13 @@
const dotenv = require('dotenv'); const dotenv = require('dotenv');
const express = require('express'); const express = require('express');
const rp = require('request-promise');
const convert = require('xml-js');
const app = express(); const app = express();
//app.disable('query parser');
dotenv.config(); dotenv.config();
const parse = require('parse-link-header'); const github = require('./server/routes/api/github');
const buildbot = require('./server/routes/api/buildbot');
const testman = require('./server/routes/api/testman');
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
const path = require('path'); const path = require('path');
const dev = app.get('env') !== 'production'; const dev = app.get('env') !== 'production';
const key = process.env.SECRET;
//settings for production Environment //settings for production Environment
if (!dev) { if (!dev) {
@ -18,248 +16,14 @@ if (!dev) {
app.use(express.static(path.join(__dirname, 'client/build'))); app.use(express.static(path.join(__dirname, 'client/build')));
} }
//------- COMMITS END-POINT ------- //API calls to GitHub API
app.use('/api/github', github);
function commitReq(sha, page) { //API calls to BuildBot Endpoints
const commits = { app.use('/api/buildbot', buildbot);
uri: 'https://api.github.com/repos/reactos/reactos/commits',
resolveWithFullResponse: true,
qs: {
access_token: key,
sha: sha,
per_page: 10,
page: page
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return commits; //API calls to Testman Endpoints
} app.use('/api/testman', testman);
app.get('/api/commits', (req, res) => {
rp(commitReq(req.query.sha, req.query.page))
.then(body => {
let link = body.headers.link;
let parsed = parse(link);
let dataAndPage = {
page: {
...parsed
},
commits: body
};
res.json(dataAndPage);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong', err: err });
});
});
//------- BRANCHES END-POINT -------
function branchReq() {
const branches = {
uri: 'https://api.github.com/repos/reactos/reactos/branches',
resolveWithFullResponse: false,
qs: {
access_token: key,
per_page: 100
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return branches;
}
app.get('/api/branches', (req, res) => {
rp(branchReq())
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- PR'S END-POINT -------
function pullReq(state, page) {
const pulls = {
uri: 'https://api.github.com/repos/reactos/reactos/pulls',
resolveWithFullResponse: true,
qs: {
access_token: key,
state: state,
per_page: 10,
page: page
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return pulls;
}
app.get('/api/pulls', (req, res) => {
rp(pullReq(req.query.state, req.query.page))
.then(body => {
let link = body.headers.link;
let parsed = parse(link);
let dataAndPage = {
page: {
...parsed
},
pulls: body
};
res.json(dataAndPage);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- BUILD-SET END-POINT -------
function buildSetReq(str) {
//https://build.reactos.org/api/v2/buildsets?field=bsid&field=sourcestamps&order=-bsid&offset=0&limit=200
const buildSets = {
uri: `https://build.reactos.org/api/v2/buildsets?field=bsid&field=sourcestamps&field=submitted_at&order=-bsid${str}`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return buildSets;
}
app.get('/api/buildsets', (req, res) => {
let q =
'&submitted_at__le=' +
req.query.submitted_at__le +
'&submitted_at__ge=' +
req.query.submitted_at__ge;
rp(buildSetReq(q))
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- BUILD-REQUEST END-POINT -------
function buildReq(str) {
const buildReq = {
uri: `https://build.reactos.org/api/v2/buildrequests?${str}&field=buildsetid&field=buildrequestid&order=-buildsetid`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return buildReq;
}
app.get('/api/buildreq', (req, res) => {
let f = req.query.buildsetid__contains;
let queryStr = f.join('&buildsetid__contains=');
queryStr = 'buildsetid__contains=' + queryStr;
rp(buildReq(queryStr))
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- BUILDS END-POINT -------
function builds(str) {
const builds = {
uri: `https://build.reactos.org/api/v2/builds?${str}&order=-buildrequestid`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return builds;
}
app.get('/api/builds', (req, res) => {
let f = req.query.buildrequestid__contains;
let queryStr = f.join('&buildrequestid__contains=');
queryStr = 'buildrequestid__contains=' + queryStr;
rp(builds(queryStr))
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- BUILDERS END-POINT -------
function builderReq() {
const builders = {
uri: 'https://build.reactos.org/api/v2/builders',
resolveWithFullResponse: false,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return builders;
}
app.get('/api/builders', (req, res) => {
rp(builderReq())
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- TESTMAN END-POINT -------
function testReq(startrev, endrev, page) {
const tests = {
uri: `https://reactos.org/testman/ajax-search.php?startrev=${startrev}&endrev=${endrev}&page=${page}&resultlist=0&requesttype=2`,
resolveWithFullResponse: false,
headers: {
'User-Agent': 'Request-Promise'
}
};
return tests;
}
app.get('/api/testman', (req, res) => {
rp(testReq(req.query.startrev, req.query.endrev, req.query.page))
.then(body => {
const result = convert.xml2json(body, { compact: true, spaces: 2 });
res.send(result);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
if (!dev) { if (!dev) {
app.get('*', (req, res) => { app.get('*', (req, res) => {

View File

@ -1,5 +1,5 @@
export const fetchCommits = async (sha, page) => { export const fetchCommits = async (sha, page) => {
const response = await fetch(`/api/commits?sha=${sha}&page=${page}`); const response = await fetch(`/api/github/commits?sha=${sha}&page=${page}`);
const data = await response.json(); const data = await response.json();
if (response.status >= 400) { if (response.status >= 400) {
throw new Error(data.errors); throw new Error(data.errors);
@ -12,7 +12,7 @@ export const fetchTests = async (startrev, endrev, page) => {
let keepGoing = true; let keepGoing = true;
while (keepGoing) { while (keepGoing) {
const response = await fetch( const response = await fetch(
`/api/testman?startrev=${startrev}&endrev=${endrev}&page=${page}` `/api/testman/testman?startrev=${startrev}&endrev=${endrev}&page=${page}`
); );
let data = await response.json(); let data = await response.json();
results.push(data.results.result); results.push(data.results.result);
@ -30,7 +30,7 @@ export const fetchTests = async (startrev, endrev, page) => {
export const fetchBuildSets = async str => { export const fetchBuildSets = async str => {
if (str) { if (str) {
const response = await fetch(`/api/buildsets?${str}`); const response = await fetch(`/api/buildbot/buildsets?${str}`);
const data = await response.json(); const data = await response.json();
if (response.status >= 400) { if (response.status >= 400) {
throw new Error(data.errors); throw new Error(data.errors);
@ -41,7 +41,7 @@ export const fetchBuildSets = async str => {
export const fetchBuildReq = async str => { export const fetchBuildReq = async str => {
if (str) { if (str) {
const response = await fetch(`/api/buildreq?${str}`); const response = await fetch(`/api/buildbot/buildreq?${str}`);
const data = await response.json(); const data = await response.json();
if (response.status >= 400) { if (response.status >= 400) {
throw new Error(data.errors); throw new Error(data.errors);
@ -51,7 +51,7 @@ export const fetchBuildReq = async str => {
}; };
export const fetchBuilds = async str => { export const fetchBuilds = async str => {
const response = await fetch(`/api/builds?${str}`); const response = await fetch(`/api/buildbot/builds?${str}`);
const data = await response.json(); const data = await response.json();
if (response.status >= 400) { if (response.status >= 400) {
throw new Error(data.errors); throw new Error(data.errors);
@ -60,7 +60,7 @@ export const fetchBuilds = async str => {
}; };
export const fetchBuilders = async () => { export const fetchBuilders = async () => {
const response = await fetch('/api/builders'); const response = await fetch('/api/buildbot/builders');
const data = await response.json(); const data = await response.json();
if (response.status >= 400) { if (response.status >= 400) {
throw new Error(data.errors); throw new Error(data.errors);
@ -69,7 +69,7 @@ export const fetchBuilders = async () => {
}; };
export const fetchBranches = async () => { export const fetchBranches = async () => {
const response = await fetch('/api/branches'); const response = await fetch('/api/github/branches');
const data = await response.json(); const data = await response.json();
if (response.status >= 400) { if (response.status >= 400) {
throw new Error(data.errors); throw new Error(data.errors);
@ -77,7 +77,7 @@ export const fetchBranches = async () => {
return data; return data;
}; };
export const fetchPulls = async (state, page) => { export const fetchPulls = async (state, page) => {
const response = await fetch(`/api/pulls?state=${state}&page=${page}`); const response = await fetch(`/api/github/pulls?state=${state}&page=${page}`);
const data = await response.json(); const data = await response.json();
if (response.status >= 400) { if (response.status >= 400) {
throw new Error(data.errors); throw new Error(data.errors);

View File

@ -0,0 +1,114 @@
const express = require('express');
const router = express.Router();
const rp = require('request-promise');
//------- BUILD-SET END-POINT -------
function buildSetReq(str) {
//https://build.reactos.org/api/v2/buildsets?field=bsid&field=sourcestamps&order=-bsid&offset=0&limit=200
const buildSets = {
uri: `https://build.reactos.org/api/v2/buildsets?field=bsid&field=sourcestamps&field=submitted_at&order=-bsid${str}`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return buildSets;
}
router.get('/buildsets', (req, res) => {
let q =
'&submitted_at__le=' +
req.query.submitted_at__le +
'&submitted_at__ge=' +
req.query.submitted_at__ge;
rp(buildSetReq(q))
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- BUILD-REQUEST END-POINT -------
function buildReq(str) {
const buildReq = {
uri: `https://build.reactos.org/api/v2/buildrequests?${str}&field=buildsetid&field=buildrequestid&order=-buildsetid`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return buildReq;
}
router.get('/buildreq', (req, res) => {
let f = req.query.buildsetid__contains;
let queryStr = f.join('&buildsetid__contains=');
queryStr = 'buildsetid__contains=' + queryStr;
rp(buildReq(queryStr))
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- BUILDS END-POINT -------
function builds(str) {
const builds = {
uri: `https://build.reactos.org/api/v2/builds?${str}&order=-buildrequestid`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return builds;
}
router.get('/builds', (req, res) => {
let f = req.query.buildrequestid__contains;
let queryStr = f.join('&buildrequestid__contains=');
queryStr = 'buildrequestid__contains=' + queryStr;
rp(builds(queryStr))
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- BUILDERS END-POINT -------
function builderReq() {
const builders = {
uri: 'https://build.reactos.org/api/v2/builders',
resolveWithFullResponse: false,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return builders;
}
router.get('/builders', (req, res) => {
rp(builderReq())
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
module.exports = router;

116
server/routes/api/github.js Normal file
View File

@ -0,0 +1,116 @@
const express = require('express');
const router = express.Router();
const rp = require('request-promise');
const key = process.env.SECRET;
const parse = require('parse-link-header');
//------- COMMITS END-POINT -------
function commitReq(sha, page) {
const commits = {
uri: 'https://api.github.com/repos/reactos/reactos/commits',
resolveWithFullResponse: true,
qs: {
access_token: key,
sha: sha,
per_page: 10,
page: page
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return commits;
}
router.get('/commits', (req, res) => {
rp(commitReq(req.query.sha, req.query.page))
.then(body => {
let link = body.headers.link;
let parsed = parse(link);
let dataAndPage = {
page: {
...parsed
},
commits: body
};
res.json(dataAndPage);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong', err: err });
});
});
//------- BRANCHES END-POINT -------
function branchReq() {
const branches = {
uri: 'https://api.github.com/repos/reactos/reactos/branches',
resolveWithFullResponse: false,
qs: {
access_token: key,
per_page: 100
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return branches;
}
router.get('/branches', (req, res) => {
rp(branchReq())
.then(body => {
res.json(body);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
//------- PR'S END-POINT -------
function pullReq(state, page) {
const pulls = {
uri: 'https://api.github.com/repos/reactos/reactos/pulls',
resolveWithFullResponse: true,
qs: {
access_token: key,
state: state,
per_page: 10,
page: page
},
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
return pulls;
}
router.get('/pulls', (req, res) => {
rp(pullReq(req.query.state, req.query.page))
.then(body => {
let link = body.headers.link;
let parsed = parse(link);
let dataAndPage = {
page: {
...parsed
},
pulls: body
};
res.json(dataAndPage);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
module.exports = router;

View File

@ -0,0 +1,29 @@
const express = require('express');
const router = express.Router();
const rp = require('request-promise');
const convert = require('xml-js');
function testReq(startrev, endrev, page) {
const tests = {
uri: `https://reactos.org/testman/ajax-search.php?startrev=${startrev}&endrev=${endrev}&page=${page}&resultlist=0&requesttype=2`,
resolveWithFullResponse: false,
headers: {
'User-Agent': 'Request-Promise'
}
};
return tests;
}
router.get('/testman', (req, res) => {
rp(testReq(req.query.startrev, req.query.endrev, req.query.page))
.then(body => {
const result = convert.xml2json(body, { compact: true, spaces: 2 });
res.send(result);
})
.catch(function(err) {
res.json({ error: 'oops...something went wrong' + err });
});
});
module.exports = router;