forked from cvega/probot-auto-merge-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
142 lines (127 loc) · 3.86 KB
/
config.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
import { CommentAuthorAssociation } from './github-models'
import { Context } from 'probot'
import getConfig from 'probot-config'
import { Decoder, object, string, optional, number, boolean, array, oneOf, constant } from '@mojotech/json-type-validation'
import { inspect } from 'util'
class ConfigNotFoundError extends Error {
constructor(
public readonly filePath: string
) {
super(`Configuration file '${filePath}' not found`)
Object.setPrototypeOf(this, new.target.prototype)
}
}
export class ConfigValidationError extends Error {
constructor(
public readonly decoderError: {
at: string,
message: string
},
public readonly config: any
) {
super(`Configuration invalid: ${decoderError.message}: ${decoderError.at}`)
Object.setPrototypeOf(this, new.target.prototype)
}
}
export type ConditionConfig = {
minApprovals: { [key in CommentAuthorAssociation]?: number },
maxRequestedChanges: { [key in CommentAuthorAssociation]?: number },
requiredLabels: string[],
blockingLabels: string[],
requiredBodyRegex: string | undefined,
blockingTitleRegex: string | undefined
}
export type Config = {
rules: ConditionConfig[],
updateBranch: boolean,
deleteBranchAfterMerge: boolean,
mergeMethod: 'merge' | 'rebase' | 'squash',
mergeCommitMessage?: string,
reportStatus: boolean,
prefixes: string[],
refBranch: string
} & ConditionConfig
export const defaultRuleConfig: ConditionConfig = {
minApprovals: {
},
maxRequestedChanges: {
NONE: 0
},
blockingLabels: [],
requiredLabels: [],
blockingTitleRegex: undefined,
requiredBodyRegex: undefined
}
export const defaultConfig: Config = {
rules: [],
updateBranch: false,
deleteBranchAfterMerge: false,
mergeMethod: 'merge',
reportStatus: false,
prefixes: [],
refBranch: '',
...defaultRuleConfig
}
const reviewConfigDecover: Decoder<{ [key in CommentAuthorAssociation]: number | undefined }> = object({
MEMBER: optional(number()),
OWNER: optional(number()),
COLLABORATOR: optional(number()),
CONTRIBUTOR: optional(number()),
FIRST_TIME_CONTRIBUTOR: optional(number()),
FIRST_TIMER: optional(number()),
NONE: optional(number())
})
const conditionConfigDecoder: Decoder<ConditionConfig> = object({
minApprovals: reviewConfigDecover,
maxRequestedChanges: reviewConfigDecover,
requiredLabels: array(string()),
blockingLabels: array(string()),
blockingTitleRegex: optional(string()),
requiredBodyRegex: optional(string())
})
const configDecoder: Decoder<Config> = object({
rules: array(conditionConfigDecoder),
minApprovals: reviewConfigDecover,
maxRequestedChanges: reviewConfigDecover,
requiredLabels: array(string()),
prefixes: array(string()),
refBranch: string(),
blockingLabels: array(string()),
blockingTitleRegex: optional(string()),
requiredBodyRegex: optional(string()),
updateBranch: boolean(),
deleteBranchAfterMerge: boolean(),
reportStatus: boolean(),
mergeMethod: oneOf(
constant<'merge'>('merge'),
constant<'rebase'>('rebase'),
constant<'squash'>('squash')
),
mergeCommitMessage: optional(string())
})
export function validateConfig(config: any) {
return configDecoder.run(config)
}
export function getConfigFromUserConfig(userConfig: any): Config {
const config = {
...defaultConfig,
...userConfig,
rules: (userConfig.rules || []).map((rule: any) => ({
...defaultRuleConfig,
...rule
}))
}
const decoded = configDecoder.run(config)
if (!decoded.ok) {
throw new ConfigValidationError(decoded.error, config)
}
return decoded.result
}
export async function loadConfig(context: Context): Promise<Config> {
const userConfig = await getConfig(context, 'auto-merge.yml', null)
context.log.debug('userConfig:' + inspect(userConfig))
if (!userConfig) {
throw new ConfigNotFoundError('.github/auto-merge.yml')
}
return getConfigFromUserConfig(userConfig)
}