gitlab.js 4.85 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
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 = {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
8
9
10
11
    // todo: GraphQL currentUser

    getUserByEmail: async function(email) {
        return axios({
Rosanny Sihombing's avatar
Rosanny Sihombing committed
12
13
14
15
16
            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
17
        .then(res => res.data[0])
Rosanny Sihombing's avatar
Rosanny Sihombing committed
18
        .catch(err => console.error(err))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
19
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
20
    createNewPages: async function(newPagesData, newLogoFile, template) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
21
        let data = new formData()
22
        data.append('avatar', fs.createReadStream(newLogoFile))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
23

Rosanny Sihombing's avatar
Rosanny Sihombing committed
24
        return axios({
Rosanny Sihombing's avatar
Rosanny Sihombing committed
25
            method: 'post',
26
27
            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
28
                '&use_custom_template=true&template_name='+template,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
29
30
31
32
            headers: { 
                'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects, 
                ...data.getHeaders()
            },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
33
            data: data
34
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
35
36
37
38
39
40
41
        .then(res => res = {
            error: false,
            data: res.data
        })
        .catch(err => res = {
            error: true,
            data: err.response.data
42
43
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
44
    updateProject: async function(updatedProjectData, newLogoFile){
45
46
47
48
        let data = new formData()
        if (newLogoFile) {
            data.append('avatar', fs.createReadStream(newLogoFile))
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
49
50

        return axios({
51
52
53
54
55
56
57
58
59
            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
60
61
62
63
64
65
66
67
68
        .then(res => res = {
            error: false,
            data: res.data
        })
        .catch(err => res = {
            error: true,
            data: err.response.data
        })
    },
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    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
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
87
88
89
    getUserProjects: async function(gitlabUserId) {
        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
96
        .then(res => res.data)
        .catch(err => console.error(err))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
97
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
98
99
100
    getProjectById: async function(projectId) {
        return axios({
            method: 'get',
Rosanny Sihombing's avatar
Rosanny Sihombing committed
101
102
103
104
            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
105
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
106
        .then(res => res.data)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
107
        .catch(err => console.error(err.response.status))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
108
109
110
111
112
    },
    getProjectPipelineLatestStatus: async function(projectId) {
        return axios({
            method: 'get',
            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId+'/pipelines'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
113
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
114
115
        .then(res => res.data[0].status)
        .catch(err => console.error(err))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
116
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
117
118
119
120
121
    //
    // test GraphQL
    getGraphqlTest: function(callback) {
        axios({
            url: 'https://gitlab.com/api/graphql',
Rosanny Sihombing's avatar
Rosanny Sihombing committed
122
            method: 'get',
Rosanny Sihombing's avatar
Rosanny Sihombing committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects
            },
            data: {
                query: `{
                    currentUser {
                        id
                        username
                    }
                }`
            /*  query: `{
                projects {
                    nodes {
                        id
                    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
139
                }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
140
141
142
143
              }` */
            }
        }).then((result) => {
            console.log(JSON.stringify(result.data))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
144
        });
Rosanny Sihombing's avatar
Rosanny Sihombing committed
145
146
147
148
    }
}

module.exports = gitlab