-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.js
101 lines (86 loc) · 2.68 KB
/
gitlab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
'use strict'
// const Promise = require('bluebird')
const Request = require('request-promise')
const projectID = process.env['BDO_PROJECT_ID'] || '4'
const projectGroup = process.env['BDO_PROJECT_GROUP'] || 'jax'
const projectName = process.env['BDO_PROJECT_NAME'] || 'bizdevops'
const APIBaseURI = 'https://gitlab.henningp.de/api/v4'
const DeployBoardURI = `https://gitlab.henningp.de/${projectGroup}/${projectName}/environments.json`
const Options = {
headers: {
'Private-Token': process.env['BDO_ACCESS_TOKEN'] || 'pi885N9dt3XrmuBaLGxs'
},
json: true
}
const Deployments = async () => {
const environmentsRaw = await Request(Object.assign({}, Options, {
uri: DeployBoardURI,
qs: {
scope: 'available',
page: 1
}
}))
const environments = []
environmentsRaw.environments.forEach(it => {
const env = {}
env.name = it.name
env.state = it.latest.state
env.url = it.latest.external_url
env.pods = it.latest.rollout_status ? it.latest.rollout_status.instances.map(it => ({status: it.status, canary: it.track === 'canary', stable: it.stable})) : null
environments.push(env)
})
return environments
}
const Pipelines = async (detailed = false, limit = 5) => {
if (!limit || Number.isNaN(limit)) {
limit = 5
}
let list = await Request(Object.assign({}, Options, {uri: `${APIBaseURI}/projects/${projectID}/pipelines`}))
list = list.slice(0, limit)
if (!detailed) {
list.forEach((pipeline, index, array) => {
// prune superfluous values
array[index] = {
id: pipeline.id,
status: pipeline.status,
ref: pipeline.ref
}
})
return list
} else {
let detailedList = []
list.forEach(it => detailedList.push(Request(Object.assign({}, Options, {uri: `${APIBaseURI}/projects/${projectID}/pipelines/${it.id}/jobs`}))))
detailedList = await Promise.all(detailedList)
detailedList = detailedList.map(pipeline => {
const pipelineShort = list.shift()
return {
jobs: pipeline,
id: pipelineShort.id,
status: pipelineShort.status,
ref: pipelineShort.ref
}
})
detailedList.forEach((pipeline, index, array) => {
const stages = []
pipeline.jobs.forEach((job, index, array) => {
// prune superfluous values
array[index] = {
id: job.id,
status: job.status,
stage: job.stage,
name: job.name
}
// add new stage, if applicable
if (!stages.includes(job.stage)) {
stages.push(job.stage)
}
})
array[index].stages = stages
})
return detailedList
}
}
module.exports = {
pipelines: Pipelines,
deployments: Deployments
}