-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
bitbucket-pipelines.service.js
105 lines (95 loc) · 2.52 KB
/
bitbucket-pipelines.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
97
98
99
100
101
102
103
104
105
'use strict'
const Joi = require('@hapi/joi')
const { renderBuildStatusBadge } = require('../build-status')
const { BaseJsonService } = require('..')
const bitbucketPipelinesSchema = Joi.object({
values: Joi.array()
.items(
Joi.object({
state: Joi.object({
name: Joi.string().required(),
result: Joi.object({
name: Joi.equal(
'SUCCESSFUL',
'FAILED',
'ERROR',
'STOPPED',
'EXPIRED'
),
}).required(),
}).required(),
})
)
.required(),
}).required()
module.exports = class BitbucketPipelines extends BaseJsonService {
static get category() {
return 'build'
}
static get route() {
return {
base: 'bitbucket/pipelines',
pattern: ':user/:repo/:branch*',
}
}
static get examples() {
return [
{
title: 'Bitbucket Pipelines',
pattern: ':user/:repo',
namedParams: {
user: 'atlassian',
repo: 'adf-builder-javascript',
},
staticPreview: this.render({ status: 'SUCCESSFUL' }),
},
{
title: 'Bitbucket Pipelines branch',
pattern: ':user/:repo/:branch',
namedParams: {
user: 'atlassian',
repo: 'adf-builder-javascript',
branch: 'task/SECO-2168',
},
staticPreview: this.render({ status: 'SUCCESSFUL' }),
},
]
}
static get defaultBadgeData() {
return { label: 'build' }
}
static render({ status }) {
return renderBuildStatusBadge({ status: status.toLowerCase() })
}
async fetch({ user, repo, branch }) {
const url = `https://api.bitbucket.org/2.0/repositories/${user}/${repo}/pipelines/`
return this._requestJson({
url,
schema: bitbucketPipelinesSchema,
options: {
qs: {
fields: 'values.state',
page: 1,
pagelen: 2,
sort: '-created_on',
'target.ref_type': 'BRANCH',
'target.ref_name': branch,
},
},
errorMessages: { 403: 'private repo' },
})
}
static transform(data) {
const values = data.values.filter(
value => value.state && value.state.name === 'COMPLETED'
)
if (values.length > 0) {
return values[0].state.result.name
}
return 'never built'
}
async handle({ user, repo, branch }) {
const data = await this.fetch({ user, repo, branch: branch || 'master' })
return this.constructor.render({ status: this.constructor.transform(data) })
}
}