-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (32 loc) · 1.37 KB
/
index.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
module.exports = function (fastify, opts, cb) {
const log = fastify.log
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
const GITHUB_ORG = process.env.GITHUB_ORG
const GITHUB_TEAM = process.env.GITHUB_TEAM
const GITHUB_TEAM_ID = process.env.GITHUB_TEAM_ID
const AUTHORIZED_KEYS_PATH = process.env.AUTHORIZED_KEYS_PATH
const SYNC_INTERVAL = process.env.SYNC_INTERVAL
if (!GITHUB_TOKEN) throw new Error('The env variable GITHUB_TOKEN is required')
if (!AUTHORIZED_KEYS_PATH) throw new Error('The env variable AUTHORIZED_KEYS_PATH is required')
if (!GITHUB_TEAM_ID && !(GITHUB_ORG && GITHUB_TEAM)) throw new Error('The env variable GITHUB_TEAM_ID or the combination of GITHUB_ORG and GITHUB_TEAM are required.') // eslint-disable-line max-len
const keys = require('./keys')({token: GITHUB_TOKEN})
const getParams = {org: GITHUB_ORG, teamName: GITHUB_TEAM, teamId: GITHUB_TEAM_ID}
setInterval(sync, SYNC_INTERVAL || 5 * 60 * 1000)
sync()
function sync () {
keys.get(getParams).then(function (users) {
require('./update')(AUTHORIZED_KEYS_PATH, users, function (err) {
if (err) {
log.fatal(err)
process.exit(1)
}
})
}).catch(function (err) {
log.fatal(err)
process.exit(1)
})
}
fastify.get('/', (req, rep) => keys.get(getParams))
fastify.get('/status', (req, rep) => rep.code(204).send())
cb()
}