-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathgitlab-pipeline-status.service.js
96 lines (87 loc) · 2.6 KB
/
gitlab-pipeline-status.service.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
'use strict'
const Joi = require('joi')
const BaseSvgScrapingService = require('../base-svg-scraping')
const { optionalUrl } = require('../validators')
const { NotFound } = require('../errors')
const { isPipelineStatus } = require('./gitlab-helpers')
const badgeSchema = Joi.object({
message: Joi.alternatives()
.try([isPipelineStatus, Joi.equal('unknown')])
.required(),
}).required()
const queryParamSchema = Joi.object({
gitlab_url: optionalUrl,
}).required()
module.exports = class GitlabPipelineStatus extends BaseSvgScrapingService {
static get category() {
return 'build'
}
static get route() {
return {
base: 'gitlab/pipeline',
format: '([^/]+)/([^/]+)(?:/([^/]+))?',
capture: ['user', 'repo', 'branch'],
// Trailing optional parameters don't work. The issue relates to the `.`
// separator before the extension.
// pattern: ':user/:repo/:branch?',
queryParams: ['gitlab_url'],
}
}
static get examples() {
return [
{
title: 'Gitlab pipeline status',
pattern: ':user/:repo',
namedParams: { user: 'gitlab-org', repo: 'gitlab-ce' },
staticExample: this.render({ status: 'passed' }),
},
{
title: 'Gitlab pipeline status (branch)',
pattern: ':user/:repo/:branch',
namedParams: {
user: 'gitlab-org',
repo: 'gitlab-ce',
branch: 'master',
},
staticExample: this.render({ status: 'passed' }),
},
{
title: 'Gitlab pipeline status (self-hosted)',
pattern: ':user/:repo',
namedParams: { user: 'GNOME', repo: 'pango' },
queryParams: { gitlab_url: 'https://gitlab.gnome.org' },
staticExample: this.render({ status: 'passed' }),
},
]
}
static render({ status }) {
const color = {
pending: 'yellow',
running: 'yellow',
passed: 'brightgreen',
failed: 'red',
skipped: 'lightgray',
canceled: 'lightgray',
}[status]
return {
message: status,
color,
}
}
async handle({ user, repo, branch = 'master' }, queryParams) {
const {
gitlab_url: baseUrl = 'https://gitlab.com',
} = this.constructor._validateQueryParams(queryParams, queryParamSchema)
const { message: status } = await this._requestSvg({
schema: badgeSchema,
url: `${baseUrl}/${user}/${repo}/badges/${branch}/pipeline.svg`,
errorMessages: {
401: 'repo not found',
},
})
if (status === 'unknown') {
throw new NotFound({ prettyMessage: 'branch not found' })
}
return this.constructor.render({ status })
}
}