-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-config.js
96 lines (87 loc) · 2.8 KB
/
get-config.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
const getConfig = require('probot-config')
const configCache = {}
const defaultConfigCache = {}
function cacheConfig (context, config) {
configCache[context.payload.repository.full_name] = {
data: config,
expiration: new Date().getTime() + 24 * 3600 * 1000
}
}
function getCachedConfig (context) {
const cacheKey = context.payload.repository.full_name
if (cacheKey in configCache) {
if (configCache[cacheKey].expiration > new Date().getTime()) {
return configCache[cacheKey].data
} else {
delete configCache[cacheKey]
}
}
return undefined
}
function cacheDefaultConfig (context, config) {
defaultConfigCache[context.payload.repository.full_name] = {
data: config,
expiration: new Date().getTime() + 24 * 3600 * 1000
}
}
function getDefaultCachedConfig (context) {
const cacheKey = context.payload.repository.full_name
if (cacheKey in defaultConfigCache) {
if (defaultConfigCache[cacheKey].expiration > new Date().getTime()) {
return defaultConfigCache[cacheKey].data
} else {
delete defaultConfigCache[cacheKey]
}
}
return undefined
}
async function getDefaultConfig (context) {
context.log.debug(`[${context.payload.repository.full_name}] Fetching default config`)
const repoInfo = await context.github.repos.get({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name
})
if (repoInfo.data && repoInfo.data.fork && repoInfo.data.parent) {
const upstreamOwner = repoInfo.data.parent.owner && repoInfo.data.parent.owner.login
const defaultBranch = repoInfo.data.parent.default_branch
if (upstreamOwner && defaultBranch) {
context.log.debug(`[${context.payload.repository.full_name}] Using default config ${defaultBranch}...${upstreamOwner}:${defaultBranch}`)
return {
version: '1',
rules: [
{
base: `${defaultBranch}`,
upstream: `${upstreamOwner}:${defaultBranch}`,
mergeMethod: process.env.DEFAULT_MERGE_METHOD || 'hardreset'
}
]
}
}
}
return null
}
module.exports = {
getLiveConfig: async (context, CONFIG_FILENAME, opts = {}) => {
if (!opts.noCache) {
const cachedConfig = getCachedConfig(context)
if (cachedConfig !== undefined) return cachedConfig
}
const c = await getConfig(context, CONFIG_FILENAME)
cacheConfig(context, c)
return c
},
getDefaultConfig: async (context, opts = {}) => {
if (!opts.noCache) {
const cachedConfig = getDefaultCachedConfig(context)
if (cachedConfig !== undefined) return cachedConfig
}
const c = await getDefaultConfig(context)
cacheDefaultConfig(context, c)
return c
},
clearConfig: (cacheKey) => {
if (cacheKey && cacheKey in configCache) {
delete configCache[cacheKey]
}
}
}