From 9d64c450e157b5a7b4ceb53b1cd0220fd7916789 Mon Sep 17 00:00:00 2001 From: Rosanny <rosanny.sihombing@hft-stuttgart.de> Date: Thu, 28 Jan 2021 17:57:14 +0100 Subject: [PATCH] remove unused codes and add bug fixing --- routes/gitlab.js | 81 ++++++++++++ routes/routes-project.js | 261 +++++++++++---------------------------- 2 files changed, 154 insertions(+), 188 deletions(-) create mode 100644 routes/gitlab.js diff --git a/routes/gitlab.js b/routes/gitlab.js new file mode 100644 index 0000000..be92ac5 --- /dev/null +++ b/routes/gitlab.js @@ -0,0 +1,81 @@ +var env = process.env.NODE_ENV || 'testing' +const config = require('../config/config')[env] +const axios = require('axios') + +var gitlab = { + getRepos: async function(perPage, idAfter) { + try { + let projects = await axios({ + method: 'get', + url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects?visibility=public&pagination=keyset&per_page='+perPage+'&order_by=id&sort=asc&id_after='+idAfter + }) + let data = projects.data + let reposArr = [] + for(let i = 0; i < data.length; i++){ + // skip template project + if (data[i].name == 'template_gitlab_page') { + continue + } else if(!data[i].tag_list.includes('website')) { + reposArr.push(data[i]) + } + } + return { + error: false, + data: reposArr} + } + catch (err) { + return { + error: true, + data: err} + } + }, + getPages: async function(perPage, idAfter) { + try { + let projects = await axios({ + method: 'get', + url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects?visibility=public&pagination=keyset&per_page='+perPage+'&order_by=id&sort=asc&id_after='+idAfter + }) + let data = projects.data + let pagesArr = [] + for(let i = 0; i < data.length; i++){ + // skip template project + if (data[i].name == 'template_gitlab_page') { + continue + } else if(data[i].tag_list.includes('website')) { + pagesArr.push(data[i]) + } + } + return { + error: false, + data: pagesArr} + } + catch (err) { + return { + error: true, + data: err} + } + }, + getProjectsFromRunners: async function() { + try { + let runner = await axios({ + method: 'get', + url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/runners/7', + headers: { + 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects + } + }) + let runnerProjects = runner.data.projects + return { + error: false, + data: runnerProjects + } + } + catch (err) { + return { + error: true, + data: err} + } + } +} + +module.exports = gitlab \ No newline at end of file diff --git a/routes/routes-project.js b/routes/routes-project.js index 65d942a..3bf97e2 100644 --- a/routes/routes-project.js +++ b/routes/routes-project.js @@ -1,7 +1,6 @@ -const fs = require('fs') //const SamlStrategy = require('passport-saml').Strategy -const dbconn = require('./dbconn') const methods = require('./methods') +const gitlab = require('./gitlab') // pwd encryption //const bcrypt = require('bcryptjs'); //const saltRounds = 10; @@ -10,10 +9,8 @@ const methods = require('./methods') const async = require('async') //const crypto = require('crypto') //const mailer = require('./mailer') - const helpers = require('./helpers') const pictSizeLimit = 1000000 // 1 MB -const axios = require('axios') module.exports = function (app) { @@ -53,59 +50,6 @@ module.exports = function (app) { ]) }) - app.get('/project_', function (req, res) { - async.waterfall([ - // get all projects from projectdb - function(done) { - methods.getAllProjects(function(projectsOverview, err) { - if (!err) { - done(err, projectsOverview) - } - }) - }, - // create JSON object for front-end - function(projectsOverview, done) { - var activeProjects = [] - var nonActiveProjects = [] - - for (var i = 0; i < projectsOverview.length; i++) { - var project = { - id: projectsOverview[i].id, - logo: projectsOverview[i].logo, - akronym: projectsOverview[i].pname, - title: projectsOverview[i].title, - summary: projectsOverview[i].onelinesummary, - category: projectsOverview[i].category, - cp: projectsOverview[i].contact_email, - gitlab: projectsOverview[i].gitlab - } - if (projectsOverview[i].projectstatus == 0) { - nonActiveProjects.push(project) - } - else if (projectsOverview[i].projectstatus == 1) { - activeProjects.push(project) - } - } - - // render the page - if (req.isAuthenticated()) { - res.render(lang+'/project/projects', { - isUserAuthenticated: true, - nonActive: nonActiveProjects, - active: activeProjects - }); - } - else { - res.render(lang+'/project/projects', { - isUserAuthenticated: false, - nonActive: nonActiveProjects, - active: activeProjects - }); - } - } - ]) - }) - app.get('/', function (req, res) { res.render(lang+'/project/project-simplified', { isUserAuthenticated: req.isAuthenticated(), @@ -121,52 +65,6 @@ module.exports = function (app) { res.redirect('/login') } }) - - app.post('/addprojectoverview__', function (req, res) { - if (req.isAuthenticated()) { - var wiki = 0 - if (req.body.wiki) - wiki = 1 - - var projectTerm = req.body.termForm + " - " + req.body.termTo - var projectOverviewData = { - pname: req.body.pname, - title: req.body.title, - onelinesummary: req.body.summary, - category: req.body.category, - logo: req.body.logo, - gitlab: req.body.gitlabURL, - wiki: wiki, - overview: req.body.overview, - question: req.body.question, - approach: req.body.approach, - result: req.body.result, - keywords: req.body.keywords, - announcement: req.body.announcement, - term: projectTerm, - further_details: req.body.furtherDetails, - website: req.body.website, - src: req.body.src, - caption: req.body.caption, - contact_lastname: req.body.contactName, - contact_email: req.body.contactEmail, - leader_lastname: req.body.leaderName, - leader_email: req.body.leaderEmail - } - - methods.addProjectOverview(projectOverviewData, function(err){ - if (err) { - //req.flash('error', "Failed") - req.flash('error', "Fehlgeschlagen") - res.redirect('/addProjectOverview'); - } - else { - req.flash('success', 'Your project has been created.') - res.redirect('/project'); - } - }) - } - }) app.post('/addprojectoverview', function (req, res) { if (req.isAuthenticated()) { @@ -304,10 +202,6 @@ module.exports = function (app) { } }) - app.get('/updateprojectoverview', function (req, res) { - // only their own project - }) - app.post('/updateprojectoverview', function (req, res) { // only their own project }) @@ -356,12 +250,6 @@ module.exports = function (app) { ]) }) - async function getProjectsFromGitlab(perPage, idAfter) { - // public projects - return await axios.get('https://transfer.hft-stuttgart.de/gitlab/api/v4/projects?visibility=public&pagination=keyset&per_page='+ - perPage+'&order_by=id&sort=asc&id_after='+idAfter) - } - // Projektdaten app.get('/projektdaten', async function(req, res){ let projectArr = [] @@ -369,7 +257,7 @@ module.exports = function (app) { let firstId = 0 while (isProject == true) { - let projects = await getProjectsFromGitlab(10, firstId) + let projects = await gitlab.getRepos(100, firstId) let projectData = projects.data if (projectData.length == 0) { @@ -377,32 +265,24 @@ module.exports = function (app) { } else { for(let i = 0; i < projectData.length; i++){ - // skip template project - if (projectData[i].name == "template_gitlab_page") { - continue + // M4_LAB logo for all projects that do not have logo + if (projectData[i].avatar_url == null) { + projectData[i].avatar_url = "https://m4lab.hft-stuttgart.de/img/footer/M4_LAB_LOGO_Graustufen.png" + } + // for all projects that have no description + if (projectData[i].description == "") { + projectData[i].description = "- no description -" } - // only repo - if (!projectData[i].tag_list.includes('website')) { - // M4_LAB logo for all projects that do not have logo - if (projectData[i].avatar_url == null) { - projectData[i].avatar_url = "https://m4lab.hft-stuttgart.de/img/footer/M4_LAB_LOGO_Graustufen.png" - } - // for all projects that have no description - if (projectData[i].description == "") { - projectData[i].description = "- no description -" - } - let project = { - logo: projectData[i].avatar_url, - name: projectData[i].name, - weburl: projectData[i].web_url, - desc: projectData[i].description, - keywords: projectData[i].tag_list - } - projectArr.push(project) + let project = { + logo: projectData[i].avatar_url, + name: projectData[i].name, + weburl: projectData[i].web_url, + desc: projectData[i].description, + keywords: projectData[i].tag_list } + projectArr.push(project) } - firstId = projectData[projectData.length-1].id } } @@ -418,63 +298,68 @@ module.exports = function (app) { let isProject = true let firstId = 0 - while (isProject == true) { - let projects = await getProjectsFromGitlab(10, firstId) - let projectData = projects.data - - if (projectData.length == 0) { - isProject = false + let runnerProjects = await gitlab.getProjectsFromRunners() + if(runnerProjects.error) { + // error response: to be updated + res.status(500).render(lang+'/500', { error: err }) + } else { + let runnerProjectsData = runnerProjects.data + let runnerProjectsIds = [] + for(let i = 0; i < runnerProjectsData.length; i++){ + runnerProjectsIds.push(runnerProjectsData[i].id) } - else { - for(let i = 0; i < projectData.length; i++){ - // skip template project - if (projectData[i].name == "template_gitlab_page") { - continue - } - // websites - if (projectData[i].tag_list.includes('website')) { - // M4_LAB logo for all projects that do not have logo - if (projectData[i].avatar_url == null) { - projectData[i].avatar_url = "https://m4lab.hft-stuttgart.de/img/footer/M4_LAB_LOGO_Graustufen.png" - } - // for all projects that have no description - if (projectData[i].description == "") { - projectData[i].description = "- no description -" - } - // customize website name - if (projectData[i].name == "Visualization") { - projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/visualization" - } - else if (projectData[i].name == "IN-Source") { - projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/INsource" - } - else if (projectData[i].name == "3DS_Visualization_Cesium") { - projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/3ds_visualization_cesium" - } - else { - projectData[i].web_url = "https://transfer.hft-stuttgart.de/pages/"+projectData[i].name - } - // remove 'website' from tag list - const index = projectData[i].tag_list.indexOf('website'); - if (index > -1) { - projectData[i].tag_list.splice(index, 1); - } - // fill in pagesArr - let pages = { - logo: projectData[i].avatar_url, - name: projectData[i].name, - weburl: projectData[i].web_url, - desc: projectData[i].description, - keywords: projectData[i].tag_list + while (isProject == true) { + let pages = await gitlab.getPages(100, firstId) + let pagesData = pages.data + + if (pagesData.length == 0) { + isProject = false + } else { + for(let i = 0; i < pagesData.length; i++){ + // ONLY IF THE PROJECT IS AVAILABLE IN THE RUNNER + if(runnerProjectsIds.indexOf(pagesData[i].id) > -1) { + // M4_LAB logo for all projects that do not have logo + if (pagesData[i].avatar_url == null) { + pagesData[i].avatar_url = "https://m4lab.hft-stuttgart.de/img/footer/M4_LAB_LOGO_Graustufen.png" + } + // for all projects that have no description + if (pagesData[i].description == "") { + pagesData[i].description = "- no description -" + } + // customize website name + if (pagesData[i].name == "Visualization") { + pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/visualization" + } + else if (pagesData[i].name == "IN-Source") { + pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/INsource" + } + else if (pagesData[i].name == "3DS_Visualization_Cesium") { + pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/3ds_visualization_cesium" + } + else { + pagesData[i].web_url = "https://transfer.hft-stuttgart.de/pages/"+pagesData[i].name + } + // remove 'website' from tag list + let index = pagesData[i].tag_list.indexOf('website') + if (index > -1) { + pagesData[i].tag_list.splice(index, 1) + } + + // fill in pagesArr + let pages = { + logo: pagesData[i].avatar_url, + name: pagesData[i].name, + weburl: pagesData[i].web_url, + desc: pagesData[i].description, + keywords: pagesData[i].tag_list + } + pagesArr.push(pages) } - pagesArr.push(pages) - } + firstId = pagesData[pagesData.length-1].id } - - firstId = projectData[projectData.length-1].id - } + } } res.render(lang+'/project/pagesList', { -- GitLab