gitlab.ts 4.3 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
const env = process.env.NODE_ENV || 'testing'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
6
7
const config = require('../config/config')[env]

Rosanny Sihombing's avatar
Rosanny Sihombing committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const gitlab = {
  getUserByEmail: async function (email: string) {
    return await 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) {
    const data = new formData()
    data.append('avatar', fs.createReadStream(newLogoFile))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
24

Rosanny Sihombing's avatar
Rosanny Sihombing committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    return await 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) {
    const data = new formData()
    if (newLogoFile) {
      data.append('avatar', fs.createReadStream(newLogoFile))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
48
    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

    return await 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: async function (projectId: number) {
    // https://docs.gitlab.com/ee/api/projects.html#delete-project
    return await 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 await 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 await 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 await 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
      })
  }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
125
126
}

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