From 31725382a29d88628c55c0d8e24f4dd5a2bf9eaa Mon Sep 17 00:00:00 2001 From: Ayush Kumar Sinha Date: Tue, 24 Sep 2019 13:20:45 +0530 Subject: [PATCH] cleaning server code --- app.js | 254 ++-------------------------------- client/src/redux/api/index.js | 16 +-- server/routes/api/buildbot.js | 114 +++++++++++++++ server/routes/api/github.js | 116 ++++++++++++++++ server/routes/api/testman.js | 29 ++++ 5 files changed, 276 insertions(+), 253 deletions(-) create mode 100644 server/routes/api/buildbot.js create mode 100644 server/routes/api/github.js create mode 100644 server/routes/api/testman.js diff --git a/app.js b/app.js index 92d7329..6e5a23e 100644 --- a/app.js +++ b/app.js @@ -1,15 +1,13 @@ const dotenv = require('dotenv'); const express = require('express'); -const rp = require('request-promise'); -const convert = require('xml-js'); const app = express(); -//app.disable('query parser'); 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 path = require('path'); const dev = app.get('env') !== 'production'; -const key = process.env.SECRET; //settings for production Environment if (!dev) { @@ -18,248 +16,14 @@ if (!dev) { 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) { - 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 - }; +//API calls to BuildBot Endpoints +app.use('/api/buildbot', buildbot); - return commits; -} - -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 }); - }); -}); +//API calls to Testman Endpoints +app.use('/api/testman', testman); if (!dev) { app.get('*', (req, res) => { diff --git a/client/src/redux/api/index.js b/client/src/redux/api/index.js index cc0b69c..74e6261 100644 --- a/client/src/redux/api/index.js +++ b/client/src/redux/api/index.js @@ -1,5 +1,5 @@ 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(); if (response.status >= 400) { throw new Error(data.errors); @@ -12,7 +12,7 @@ export const fetchTests = async (startrev, endrev, page) => { let keepGoing = true; while (keepGoing) { 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(); results.push(data.results.result); @@ -30,7 +30,7 @@ export const fetchTests = async (startrev, endrev, page) => { export const fetchBuildSets = async str => { if (str) { - const response = await fetch(`/api/buildsets?${str}`); + const response = await fetch(`/api/buildbot/buildsets?${str}`); const data = await response.json(); if (response.status >= 400) { throw new Error(data.errors); @@ -41,7 +41,7 @@ export const fetchBuildSets = async str => { export const fetchBuildReq = async str => { if (str) { - const response = await fetch(`/api/buildreq?${str}`); + const response = await fetch(`/api/buildbot/buildreq?${str}`); const data = await response.json(); if (response.status >= 400) { throw new Error(data.errors); @@ -51,7 +51,7 @@ export const fetchBuildReq = 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(); if (response.status >= 400) { throw new Error(data.errors); @@ -60,7 +60,7 @@ export const fetchBuilds = async str => { }; export const fetchBuilders = async () => { - const response = await fetch('/api/builders'); + const response = await fetch('/api/buildbot/builders'); const data = await response.json(); if (response.status >= 400) { throw new Error(data.errors); @@ -69,7 +69,7 @@ export const fetchBuilders = async () => { }; export const fetchBranches = async () => { - const response = await fetch('/api/branches'); + const response = await fetch('/api/github/branches'); const data = await response.json(); if (response.status >= 400) { throw new Error(data.errors); @@ -77,7 +77,7 @@ export const fetchBranches = async () => { return data; }; 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(); if (response.status >= 400) { throw new Error(data.errors); diff --git a/server/routes/api/buildbot.js b/server/routes/api/buildbot.js new file mode 100644 index 0000000..a212fee --- /dev/null +++ b/server/routes/api/buildbot.js @@ -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; diff --git a/server/routes/api/github.js b/server/routes/api/github.js new file mode 100644 index 0000000..804e2e8 --- /dev/null +++ b/server/routes/api/github.js @@ -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; diff --git a/server/routes/api/testman.js b/server/routes/api/testman.js new file mode 100644 index 0000000..4932471 --- /dev/null +++ b/server/routes/api/testman.js @@ -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;