import axios from 'axios' import fs from 'fs' import formData from 'form-data' var env = process.env.NODE_ENV || 'testing' const config = require('../config/config')[env] var gitlab = { getUserByEmail: async function(email:string) { 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(function(err){ console.error(err) return null }) }, createNewPages: async function(newPagesData:any, newLogoFile:string, template:any) { 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.data) .catch(function(err) { console.error("ERR Status: "+err.response.status) console.error("ERR Name: "+err.response.data.message.name) console.error("ERR Path: "+err.response.data.message.path) return err.response }) }, updateProject: async function(updatedProjectData:any, newLogoFile:string){ 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.data[0]) .then(res => res.data) .catch(function(err){ console.error("ERR Status: "+err.response.status) console.error("ERR Name: "+err.response.data.message.name) console.error("ERR Path: "+err.response.data.message.path) return err.response }) }, deleteProjectById: function(projectId:number){ // 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 => true) .catch(function(err) { console.error("ERR Status: "+err.response.status) console.error("ERR Name: "+err.response.data.message.name) console.error("ERR Path: "+err.response.data.message.path) return false }) }, getUserProjects: async function(gitlabUserId:number) { 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(function(err) { console.error(err) return null }) }, getProjectById: async function(projectId:number) { 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(function(err) { console.error(err) return null }) }, getProjectPipelineLatestStatus: async function(projectId:number) { return axios({ method: 'get', url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId+'/pipelines' }) .then(res => res.data[0].status) .catch(function(err) { console.error(err) return null }) } } export = gitlab