var env = process.env.NODE_ENV || 'testing' const config = require('../config/config')[env] const axios = require('axios') const fs = require('fs') var formData = require('form-data') var gitlab = { // todo: GraphQL currentUser getUserByEmail: async function(email) { return axios({ method: 'get', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users?search='+email, headers: { 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects} }) .then(res => res.data[0]) .catch(err => console.error(err)) }, createNewPages: async function(newPagesData, newLogoFile, template) { let data = new formData() data.append('avatar', fs.createReadStream(newLogoFile)) return axios({ method: 'post', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/user/'+newPagesData.getOwnerGitlabId()+ '?name='+newPagesData.getName()+'&description='+newPagesData.getDesc()+'&tag_list=website'+ '&use_custom_template=true&template_name='+template, headers: { 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects, ...data.getHeaders() }, data: data }) .then(res => res = { error: false, data: res.data }) .catch(err => res = { error: true, data: err.response.data }) }, updateProject: async function(updatedProjectData, newLogoFile){ let data = new formData() if (newLogoFile) { data.append('avatar', fs.createReadStream(newLogoFile)) } return axios({ method: 'put', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+updatedProjectData.getId()+ '?name='+updatedProjectData.getName()+'&description='+updatedProjectData.getDesc(), headers: { 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects, ...data.getHeaders() }, data : data }) .then(res => res = { error: false, data: res.data }) .catch(err => res = { error: true, data: err.response.data }) }, deleteProjectById: function(projectId){ // https://docs.gitlab.com/ee/api/projects.html#delete-project return axios({ method: 'delete', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId, headers: { 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects } }) .then(res => res = { error: false, data: res.data }) .catch(err => res = { error: true, data: err.response.data }) }, getUserProjects: async function(gitlabUserId) { return axios({ method: 'get', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users/'+gitlabUserId+'/projects?owned=true&visibility=public', headers: { 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects } }) .then(res => res.data) .catch(err => console.error(err)) }, getProjectById: async function(projectId) { return axios({ method: 'get', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId, headers: { 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects } }) .then(res => res.data) .catch(err => console.error(err.response.status)) }, getProjectPipelineLatestStatus: async function(projectId) { return axios({ method: 'get', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId+'/pipelines' }) .then(res => res.data[0].status) .catch(err => console.error(err)) }, // // test GraphQL getGraphqlTest: function(callback) { axios({ url: 'https://gitlab.com/api/graphql', method: 'get', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects }, data: { query: `{ currentUser { id username } }` /* query: `{ projects { nodes { id } } }` */ } }).then((result) => { console.log(JSON.stringify(result.data)) }); } } module.exports = gitlab