Commit c39c5090 authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

handle new gitlab users

parent 2a084f63
This commit is part of merge request !79. Comments created here will be created in the context of that merge request.
Showing with 42 additions and 27 deletions
+42 -27
...@@ -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