forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-deployments.service.js
More file actions
130 lines (115 loc) · 3.24 KB
/
Copy pathgithub-deployments.service.js
File metadata and controls
130 lines (115 loc) · 3.24 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import gql from 'graphql-tag'
import Joi from 'joi'
import { NotFound } from '../index.js'
import { GithubAuthV4Service } from './github-auth-service.js'
import { documentation, transformErrors } from './github-helpers.js'
const greenStates = ['SUCCESS']
const redStates = ['ERROR', 'FAILURE']
const blueStates = ['INACTIVE']
const otherStates = ['IN_PROGRESS', 'QUEUED', 'PENDING', 'NO_STATUS']
const stateToMessageMappings = {
IN_PROGRESS: 'in progress',
NO_STATUS: 'no status yet',
}
const allState = greenStates
.concat(redStates)
.concat(blueStates)
.concat(otherStates)
const isDeploymentState = Joi.equal(...allState)
const schema = Joi.object({
data: Joi.object({
repository: Joi.object({
deployments: Joi.object({
nodes: Joi.array()
.items(
Joi.object({
latestStatus: Joi.alternatives([
Joi.object({
state: isDeploymentState,
}),
null,
]),
})
)
.required(),
}).required(),
}).required(),
}).required(),
}).required()
export default class GithubDeployments extends GithubAuthV4Service {
static category = 'other'
static route = {
base: 'github/deployments',
pattern: ':user/:repo/:environment',
}
static examples = [
{
title: 'GitHub deployments',
namedParams: {
user: 'badges',
repo: 'shields',
environment: 'shields-staging',
},
staticPreview: this.render({
state: 'success',
}),
documentation,
},
]
static defaultBadgeData = { label: 'state' }
static render({ state }) {
let color
if (greenStates.includes(state)) {
color = 'brightgreen'
} else if (redStates.includes(state)) {
color = 'red'
} else if (blueStates.includes(state)) {
color = 'blue'
}
let message = stateToMessageMappings[state]
if (!message) {
message = state.toLowerCase()
}
return {
message,
color,
}
}
async fetch({ user, repo, environment }) {
return this._requestGraphql({
query: gql`
query ($user: String!, $repo: String!, $environment: String!) {
repository(owner: $user, name: $repo) {
deployments(last: 1, environments: [$environment]) {
nodes {
latestStatus {
state
}
}
}
}
}
`,
variables: { user, repo, environment },
schema,
transformErrors,
})
}
transform({ data }) {
if (data.repository.deployments.nodes.length === 0) {
throw new NotFound({ prettyMessage: 'environment not found' })
}
// This happens for the brief moment a deployment is created, but no
// status is created for the deployment (yet).
if (data.repository.deployments.nodes[0].latestStatus == null) {
return { state: 'NO_STATUS' }
}
const state = data.repository.deployments.nodes[0].latestStatus.state
return { state }
}
async handle({ user, repo, environment }, queryParams) {
const json = await this.fetch({ user, repo, environment })
const { state } = this.transform({ data: json.data })
return this.constructor.render({ state })
}
}