forked from release-drafter/release-drafter
/
schema.js
137 lines (114 loc) · 3.64 KB
/
schema.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
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
const _ = require('lodash')
const Joi = require('@hapi/joi')
const { SORT_BY, SORT_DIRECTIONS } = require('./sort-pull-requests')
const { DEFAULT_CONFIG } = require('./default-config')
const { validateReplacers } = require('./template')
const schema = (context) => {
const defaultBranch = _.get(
context,
'payload.repository.default_branch',
'master'
)
return Joi.object()
.keys({
references: Joi.array().items(Joi.string()).default([defaultBranch]),
'change-template': Joi.string().default(
DEFAULT_CONFIG['change-template']
),
'change-title-escapes': Joi.string()
.allow('')
.default(DEFAULT_CONFIG['change-title-escapes']),
'no-changes-template': Joi.string().default(
DEFAULT_CONFIG['no-changes-template']
),
'version-template': Joi.string().default(
DEFAULT_CONFIG['version-template']
),
'name-template': Joi.string()
.allow('')
.default(DEFAULT_CONFIG['name-template']),
'tag-template': Joi.string()
.allow('')
.default(DEFAULT_CONFIG['tag-template']),
'exclude-labels': Joi.array()
.items(Joi.string())
.default(DEFAULT_CONFIG['exclude-labels']),
'include-labels': Joi.array()
.items(Joi.string())
.default(DEFAULT_CONFIG['include-labels']),
'sort-by': Joi.string()
.valid(SORT_BY.mergedAt, SORT_BY.title)
.default(DEFAULT_CONFIG['sort-by']),
'sort-direction': Joi.string()
.valid(SORT_DIRECTIONS.ascending, SORT_DIRECTIONS.descending)
.default(DEFAULT_CONFIG['sort-direction']),
prerelease: Joi.boolean().default(DEFAULT_CONFIG.prerelease),
replacers: Joi.array()
.items(
Joi.object().keys({
search: Joi.string()
.required()
.error(
() => '"search" is required and must be a regexp or a string'
),
replace: Joi.string().allow('').required(),
})
)
.default(DEFAULT_CONFIG.replacers),
categories: Joi.array()
.items(
Joi.object()
.keys({
title: Joi.string().required(),
label: Joi.string(),
labels: Joi.array().items(Joi.string()).single().default([]),
})
.rename('label', 'labels', {
ignoreUndefined: true,
override: true,
})
)
.default(DEFAULT_CONFIG.categories),
'version-resolver': Joi.object()
.keys({
major: Joi.object({
labels: Joi.array().items(Joi.string()).single(),
}),
minor: Joi.object({
labels: Joi.array().items(Joi.string()).single(),
}),
patch: Joi.object({
labels: Joi.array().items(Joi.string()).single(),
}),
default: Joi.string()
.valid('major', 'minor', 'patch')
.default('patch'),
})
.default(DEFAULT_CONFIG['version-resolver']),
template: Joi.string().required(),
_extends: Joi.string(),
})
.rename('branches', 'references', {
ignoreUndefined: true,
override: true,
})
}
module.exports.schema = schema
const validateSchema = (app, context, repoConfig) => {
const { error, value: config } = schema(context).validate(repoConfig, {
abortEarly: false,
allowUnknown: true,
})
if (error) throw error
try {
config.replacers = validateReplacers({
app,
context,
replacers: config.replacers,
})
} catch (error) {
config.replacers = []
}
return config
}
module.exports.validateSchema = validateSchema