gitlab.ts 4.63 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
import axios from 'axios'
import fs from 'fs'
import formData from 'form-data'

Rosanny Sihombing's avatar
Rosanny Sihombing committed
5
6
7
8
var env = process.env.NODE_ENV || 'testing'
const config = require('../config/config')[env]

var gitlab = {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
9
    getUserByEmail: async function(email:string) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
10
        return axios({
Rosanny Sihombing's avatar
Rosanny Sihombing committed
11
12
13
14
15
            method: 'get',
            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users?search='+email,
            headers: { 
                'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects}
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
16
        .then(res => res.data[0])
Rosanny Sihombing's avatar
Rosanny Sihombing committed
17
18
19
20
        .catch(function(err){
            console.error(err)
            return null
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
21
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
22
    createNewPages: async function(newPagesData:any, newLogoFile:string, template:any) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
23
        let data = new formData()
24
        data.append('avatar', fs.createReadStream(newLogoFile))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
25

Rosanny Sihombing's avatar
Rosanny Sihombing committed
26
        return axios({
Rosanny Sihombing's avatar
Rosanny Sihombing committed
27
            method: 'post',
28
29
            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/user/'+newPagesData.getOwnerGitlabId()+
                '?name='+newPagesData.getName()+'&description='+newPagesData.getDesc()+'&tag_list=website'+
Rosanny Sihombing's avatar
Rosanny Sihombing committed
30
                '&use_custom_template=true&template_name='+template,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
31
32
33
34
            headers: { 
                'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects, 
                ...data.getHeaders()
            },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
35
            data: data
36
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
37
38
39
40
41
42
        .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
43
44
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
45
    updateProject: async function(updatedProjectData:any, newLogoFile:string){
46
47
48
49
        let data = new formData()
        if (newLogoFile) {
            data.append('avatar', fs.createReadStream(newLogoFile))
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
50
51

        return axios({
52
53
54
55
56
57
58
59
60
            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
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
61
62
63
64
65
66
67
        //.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
Rosanny Sihombing's avatar
Rosanny Sihombing committed
68
69
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
70
    deleteProjectById: function(projectId:number){
71
72
73
74
75
76
77
78
        // 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 
            }
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
79
80
81
82
83
84
        .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
85
86
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
87
    getUserProjects: async function(gitlabUserId:number) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
88
89
        return axios({
            method: 'get',
Rosanny Sihombing's avatar
Rosanny Sihombing committed
90
91
92
93
            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users/'+gitlabUserId+'/projects?owned=true&visibility=public',
            headers: { 
                'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects 
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
94
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
95
        .then(res => res.data)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
96
97
98
99
        .catch(function(err) {
            console.error(err)
            return null
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
100
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
101
    getProjectById: async function(projectId:number) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
102
103
        return axios({
            method: 'get',
Rosanny Sihombing's avatar
Rosanny Sihombing committed
104
105
106
107
            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId,
            headers: { 
                'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects 
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
108
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
109
        .then(res => res.data)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
110
111
112
113
        .catch(function(err) {
            console.error(err)
            return null
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
114
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
115
    getProjectPipelineLatestStatus: async function(projectId:number) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
116
117
118
        return axios({
            method: 'get',
            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId+'/pipelines'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
119
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
120
        .then(res => res.data[0].status)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
121
122
123
124
        .catch(function(err) {
            console.error(err)
            return null
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
125
126
127
    }
}

Rosanny Sihombing's avatar
Rosanny Sihombing committed
128
export = gitlab