Commit 9c928de9 authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

Merge branch 'MLAB-383' into 'testing'

Mlab 383

See merge request !79
5 merge requests!143updating yml config,!91Prepare prod,!90Testing,!89Testing,!79Mlab 383
Pipeline #3144 passed with stage
in 14 seconds
Showing with 45 additions and 30 deletions
+45 -30
...@@ -4,7 +4,7 @@ const gitlab = require('../routes/gitlab') ...@@ -4,7 +4,7 @@ const gitlab = require('../routes/gitlab')
describe('GitLab API', () => { describe('GitLab API', () => {
test('returns an existing gitlab user by an email address', async () => { test('returns an existing gitlab user by an email address', async () => {
let user = await gitlab.getUserByEmail('putavaliduseremailaddress@here.com') let user = await gitlab.getUserByEmail('litehon958@whipjoy.com')
expect(user).not.toBeNull() expect(user).not.toBeNull()
}) })
test('returns an undefined user', async () => { test('returns an undefined user', async () => {
...@@ -13,7 +13,7 @@ describe('GitLab API', () => { ...@@ -13,7 +13,7 @@ describe('GitLab API', () => {
}) })
test('returns users project', async () => { test('returns users project', async () => {
let userProjects = await gitlab.getUserProjects('put a valid user id in integer here') let userProjects = await gitlab.getUserProjects(136)
expect(userProjects).toBeDefined() expect(userProjects).toBeDefined()
}) })
test('returns undefined projects, due to non-existing gitlab user ID', async () => { test('returns undefined projects, due to non-existing gitlab user ID', async () => {
......
...@@ -3,7 +3,7 @@ const methods = require('../routes/methods') ...@@ -3,7 +3,7 @@ const methods = require('../routes/methods')
describe("DB methohds test", () => { describe("DB methohds test", () => {
it('returns a user from DB by email', done => { it('returns a user from DB by email', done => {
methods.getUserByEmail('rosanny.sihombing@hft-stuttgart.de', function(resp, err){ methods.getUserByEmail('litehon958@whipjoy.com', function(resp, err){
try { try {
expect(resp).not.toBeNull() expect(resp).not.toBeNull()
expect(err).toBeNull() expect(err).toBeNull()
......
...@@ -14,10 +14,7 @@ var gitlab = { ...@@ -14,10 +14,7 @@ var gitlab = {
headers: { headers: {
'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects} 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects}
}) })
.then(res => userData = { .then(res => res.data[0])
id: res.data[0].id,
username: res.data[0].username
})
.catch(err => console.error(err)) .catch(err => console.error(err))
}, },
createNewPages: async function(newPagesData, newLogoFile, template) { createNewPages: async function(newPagesData, newLogoFile, template) {
...@@ -72,8 +69,10 @@ var gitlab = { ...@@ -72,8 +69,10 @@ var gitlab = {
getUserProjects: async function(gitlabUserId) { getUserProjects: async function(gitlabUserId) {
return axios({ return axios({
method: 'get', method: 'get',
url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users/'+gitlabUserId+'/projects?private_token='+ url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users/'+gitlabUserId+'/projects?owned=true&visibility=public',
config.gitlab.token_readWriteProjects+'&owned=true&simple=true&visibility=public' headers: {
'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects
}
}) })
.then(res => res.data) .then(res => res.data)
.catch(err => console.error(err)) .catch(err => console.error(err))
...@@ -81,11 +80,13 @@ var gitlab = { ...@@ -81,11 +80,13 @@ var gitlab = {
getProjectById: async function(projectId) { getProjectById: async function(projectId) {
return axios({ return axios({
method: 'get', method: 'get',
url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId+'?private_token='+ url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId,
config.gitlab.token_readWriteProjects headers: {
'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects
}
}) })
.then(res => res.data) .then(res => res.data)
.catch(err => console.error(err)) .catch(err => console.error(err.response.status))
}, },
getProjectPipelineLatestStatus: async function(projectId) { getProjectPipelineLatestStatus: async function(projectId) {
return axios({ return axios({
......
...@@ -228,7 +228,11 @@ var methods = { ...@@ -228,7 +228,11 @@ var methods = {
getGitlabId: async function(userId) { getGitlabId: async function(userId) {
try { try {
let rows = await dbconn.user.promise().query('SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = "' +userId+'" and gu.user_id = u.id') let rows = await dbconn.user.promise().query('SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = "' +userId+'" and gu.user_id = u.id')
return rows[0][0].gitlab_userId if (rows[0][0]) {
return rows[0][0].gitlab_userId
} else {
return null
}
} }
catch(err) { catch(err) {
console.error(err) console.error(err)
......
...@@ -118,7 +118,9 @@ module.exports = function (app, config, passport, i18n) { ...@@ -118,7 +118,9 @@ module.exports = function (app, config, passport, i18n) {
) )
let userGitlabId = await methods.getGitlabId(loggedInUser.id) let userGitlabId = await methods.getGitlabId(loggedInUser.id)
loggedInUser.setGitlabUserId(userGitlabId) if (userGitlabId) {
loggedInUser.setGitlabUserId(userGitlabId)
}
return loggedInUser return loggedInUser
} }
...@@ -192,8 +194,10 @@ module.exports = function (app, config, passport, i18n) { ...@@ -192,8 +194,10 @@ module.exports = function (app, config, passport, i18n) {
if(loggedInUser.getGitlabUserId()) { // for users who have activated their gitlab account if(loggedInUser.getGitlabUserId()) { // for users who have activated their gitlab account
let userProjects = await gitlab.getUserProjects(loggedInUser.getGitlabUserId()) let userProjects = await gitlab.getUserProjects(loggedInUser.getGitlabUserId())
if (!userProjects) if (!userProjects) {
res.status(500).send("something went wrong :/") console.error("something went wrong")
res.status(500).render(lang+'/500', { error: "something went wrong" })
}
for (project in userProjects) { for (project in userProjects) {
if (userProjects[project].tag_list.includes('website')) { if (userProjects[project].tag_list.includes('website')) {
...@@ -217,19 +221,25 @@ module.exports = function (app, config, passport, i18n) { ...@@ -217,19 +221,25 @@ module.exports = function (app, config, passport, i18n) {
}) })
} else { // for users who have not activated their gitlab account yet } else { // for users who have not activated their gitlab account yet
let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail()) let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail())
// RS: if error, then what? if (!gitlabUser) {
let gitlabActivationData = { res.render(lang+'/account/services', {
user_id: loggedInUser.getId(), user: loggedInUser,
gitlab_userId: gitlabUser.id} gitlabRepos: null,
// RS: update to await gitlabPages: null
methods.addGitlabUser(gitlabActivationData, function(err){ })
if(err) { } else {
res.status(500).render(lang+'/500', { error: err }) let gitlabActivationData = {
} else { user_id: loggedInUser.getId(),
loggedInUser.setGitlabUserId(gitlabActivationData.gitlab_userId) gitlab_userId: gitlabUser.id}
res.redirect('/account/services') // RS: update to await?
} methods.addGitlabUser(gitlabActivationData, function(err){
}) if(err) {
res.status(500).render(lang+'/500', { error: err })
} else {
res.redirect('/account/services')
}
})
}
} }
} }
} }
...@@ -471,7 +481,7 @@ module.exports = function (app, config, passport, i18n) { ...@@ -471,7 +481,7 @@ module.exports = function (app, config, passport, i18n) {
let loggedInUser = await getLoggedInUserData(req.user.email) let loggedInUser = await getLoggedInUserData(req.user.email)
let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail()) let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail())
if (!gitlabUser) { // no user found if (!gitlabUser) { // no user found
res.redirect('/account/service') res.redirect('/account/services')
} else { } else {
res.render(lang+'/account/newInformation', { res.render(lang+'/account/newInformation', {
user: loggedInUser, user: loggedInUser,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment