-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck.js
62 lines (55 loc) · 2.09 KB
/
healthcheck.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
'use strict'
const check = require('check-types')
const rp = require('request-promise-native')
const logger = require('./lib/logger')()
const obfuscateString = require('./lib/obfuscate-string')
logger.log('info', 'running healthcheck')
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
const GITHUB_USER = process.env.GITHUB_USER
check.assert.nonEmptyString(GITHUB_USER, 'environment variable GITHUB_USER may not be empty')
check.assert.nonEmptyString(GITHUB_TOKEN, 'environment variable GITHUB_TOKEN may not be empty')
logger.log('debug', 'using GitHub user %s with token %s', GITHUB_USER, obfuscateString(GITHUB_TOKEN))
const PROJECTS = process.env.PROJECTS
check.assert.nonEmptyString(PROJECTS, 'environment variable PROJECTS may not be empty')
const PROJECTS_ROOT_DIR = process.env.PROJECTS_ROOT_DIR || '.projects'
logger.log('debug', 'loading projects from %s', PROJECTS)
logger.log('debug', 'storing projects in %s', PROJECTS_ROOT_DIR)
const projects = require(PROJECTS)
const piDabProject = projects.find(project => project.name === 'mlenkeit/pi-dab')
if (!piDabProject) {
logger.log('error', 'could not find configuration for mlenkeit/pi-dab project')
process.exit(1)
}
const getWebhookUrl = ({ repoName, webhookId, githubUser, githubToken }) => rp.get({
url: `https://api.github.com/repos/${repoName}/hooks/${webhookId}`,
json: true,
auth: {
username: githubUser,
password: githubToken
},
headers: {
'User-Agent': 'pi-dab-healthcheck'
},
transform: body => body.config.url
})
getWebhookUrl({
repoName: piDabProject.name,
webhookId: piDabProject.githubWebhook,
githubUser: GITHUB_USER,
githubToken: GITHUB_TOKEN
}).then(webhookUrl => {
logger.log('debug', 'found webhook url %s', webhookUrl)
return rp.get({
uri: webhookUrl,
resolveWithFullResponse: true
})
}).then(response => {
if (response.statusCode === 200) {
logger.log('info', 'healthcheck successful')
return process.exit(0)
}
throw new Error(`Unexpected status code ${response.statusCode} instead of 200`)
}).catch(err => {
logger.log('error', 'healthcheck failed: %s', err)
return process.exit(1)
})